我正在尝试使用 geopandas 将路段数据聚合到固定网格。
道路数据如下所示:
|Variable | geometry |
|---------|----------|
| 59 |LINESTRING (440735.351 4319767.843, 440733.320...|
| 48 |LINESTRING (440733.320 4319607.463, 440732.508...|
| 64 |LINESTRING (440858.641 4329887.089, 440853.551...|
| 64 |LINESTRING (440920.578 4330030.844, 440890.661...|
| 68 |LINESTRING (218573.705 4257347.137, 218586.697...|
我想要聚合道路数据的固定网格显示为:
|geometry|
|--------|
| POLYGON ((238749.978 4498509.611, 238898.825 4..|
| POLYGON ((240086.217 4498360.636, 240234.824 4... |
| POLYGON ((241421.845 4498211.925, 241570.857 4..|
| POLYGON ((242758.152 4498063.434, 242906.923 4... |
| POLYGON ((244094.479 4497914.762, 244243.009 4... |
我目前正在尝试按道路长度对变量进行加权,应用空间连接,将加权乘以网格单元内的道路长度,然后使用以下代码对所有道路的网格进行求和:
gdf_road['weighted_variable'] = gdf_road['Variable']/ gdf_road.geometry.legnth
gdf_joined = gpd.sjoin(gdf_road, gdf_grid, how="inner", op='intersects')
gdf_grid['agg_variable']=(gdf_joined['Weighted_Variable'] * gdf_joined.geometry.length).groupby(gdf_joined['index_right']).sum()
此代码的结果是保持原始道路数据空间结构的数据,但变量的大小似乎错误地高。我很好奇,在将道路数据聚合到网格的逻辑中,我是否可能忽略了一些东西——也许是在重复计算重叠道路时,而不是应用另一种类型的加权方案……等等。任何建议都会很棒。谢谢!
gdf.overlay 似乎可以解决问题
gdf_road['weighted_variable'] = gdf_road['Variable']/ gdf_road.geometry.legnth
gdf_join = gpd.overlay(gdf_road, gdf_grid, how="intersection")
gdf_join['new_length'] = gdf_join.to_crs('EPSG:26916').geometry.length
gdf_join['new_variable'] = gdf_join['weighted_variable'] * gdf_join['new_length']
# Spatial join & aggregate data
joined_data = gpd.sjoin(gdf_join, gdf_grid, how="inner", op="within")
grouped_data = joined_data.groupby('index_right')
agg_data = grouped_data[['new_variable']].sum().reset_index()