Logo
Section 1 / 18

空间特定属性

了解 GeoSeries 中可用的空间属性。

导入

空间特定属性

了解 GeoSeries 中可用的空间属性。

1import pandas as pd 2 3# 打开一个示例GPS追踪(.txt)文件 4df = pd.read_csv('go_track_trackspoints.csv') 5 6# 将时间戳列转换为datetime数据类型 7df['timestamp'] = pd.to_datetime(df['time']) 8 9# 使用.dt访问器提取时间值 10df['hour'] = df['timestamp'].dt.hour 11df['minute'] = df['timestamp'].dt.minute 12df['second'] = df['timestamp'].dt.second 13 14print(df.head().to_html())
1import geopandas as gpd 2 3countries = gpd.read_file('countries.geojson') 4 5print(countries.crs)
1import geopandas as gpd 2 3# 打开数据集 4gdf = gpd.read_file('countries.geojson') 5 6# 投影到 ESRI:54034 7gdf = gdf.to_crs('ESRI:54034') 8 9# 计算面积(单位:平方千米) 10gdf['area'] = gdf.area / 1e6 11 12# 获取面积最大的6个国家 13biggest = gdf.sort_values(by='area', ascending=False).head(6) 14 15# 创建字典以水平放置图例 16legend_kwds = {'loc': 'upper center', 'bbox_to_anchor': (0.7, 0.45), 'ncol': 2} 17 18# 重新投影回 WGS84(便于可视化) 19biggest = biggest.to_crs(4326) 20ax = biggest.plot(figsize=(10, 8), column='sovereignt', legend=True, legend_kwds=legend_kwds) 21ax.set_ylabel('纬度') 22ax.set_xlabel('经度') 23 24# 保存图像 25ax.figure.savefig('./output/biggest_countries.png', dpi=300) 26 27# 输出面积信息 28print(biggest[['name', 'area']].to_html())

结论

裁剪与合并