python 为什么LINESTRING和MULTIPOLYGON几何之间的GeoPandas叠加交集为空?

hrirmatl  于 2023-01-19  发布在  Python
关注(0)|答案(1)|浏览(156)

我想按健康区域分割不列颠哥伦比亚省的道路网络。具体来说,我想为每个健康区域保存该区域内道路的形状文件。道路具有LINESTRING几何图形,而健康区域具有MULTIPOLYGON几何图形。
我想geopandas overlayhow=intersection可能会帮助我完成这一任务,但是它告诉我交集是空的,这没有意义,因为将道路和卫生当局一起绘制(未显示)会显示出明显的重叠,所以我怀疑我误解了geopandas处理不同几何体交集的方式。
下面是一些最基本的代码,但是除非您碰巧在本地存储了这些文件,否则它不会运行。

import geopandas as gpd
import matplotlib.pyplot as plt

health_regions_df = gpd.read_file(
    '../data/canada.geojson'
    )

bc_roads_df = gpd.read_file(
    '../data/geobase_nrn_rrn/bc/nrn_rrn_bc_shp_en.zip',
    layer='NRN_bc_14_0_ROADSEG'
    )

bc_roads_df.to_crs(
    'EPSG:4326', 
    inplace=True
)

bc_hrs = health_regions_df[health_regions_df['province'] == 'British Columbia']

intersection = gpd.overlay(bc_hrs, bc_roads_df, how='intersection')

print(intersection)

上面给出了以下打印输出:

Empty GeoDataFrame
Columns: [fid, hr_uid, province, engname, frename, count, NID, ROADSEGID, ADRANGENID, DATASETNAM, SPECVERS, ACCURACY, ACQTECH, PROVIDER, CREDATE, REVDATE, METACOVER, ROADCLASS, RTNUMBER1, RTNUMBER2, RTNUMBER3, RTNUMBER4, RTNUMBER5, RTENAME1FR, RTENAME2FR, RTENAME3FR, RTENAME4FR, RTENAME1EN, RTENAME2EN, RTENAME3EN, RTENAME4EN, EXITNBR, NBRLANES, PAVSTATUS, PAVSURF, UNPAVSURF, STRUCTID, STRUCTTYPE, STRUNAMEEN, STRUNAMEFR, L_ADDDIRFG, L_HNUMF, L_HNUML, L_STNAME_C, L_PLACENAM, R_ADDDIRFG, R_HNUMF, R_HNUML, R_STNAME_C, R_PLACENAM, CLOSING, ROADJURIS, SPEED, TRAFFICDIR, geometry]
Index: []

为什么LINESTRING和MULTIPOLYGON几何之间的GeoPandas叠加交集为空?

rslzwgfq

rslzwgfq1#

overlay方法应应用于地理数据框或系列:
intersection = bc_hrs.overlay(bc_roads_df, how='intersection')
下面是一些示例代码:

from shapely.geometry import Polygon
from shapely.geometry import LineString
import geopandas
from matplotlib import pyplot as plt

polys1 = geopandas.GeoSeries([Polygon([(3,3), (3,7), (7,7), (7,3), (3,3)])])
line1= geopandas.GeoSeries([LineString([(0,0), (10,10)])])

df1 = geopandas.GeoDataFrame({'geometry': polys1})
df2 = geopandas.GeoDataFrame({'geometry': line1})

# Perform the intersect operation
intersect = df1.overlay(df2, how='intersection', keep_geom_type=False)
# Plot the process
fig, ax = plt.subplots(2,2, figsize=(8, 8))
df1.plot(ax=ax[0,0], color='blue', alpha=0.7)
df2.plot(ax=ax[0,0], color='magenta')

df1.plot(ax=ax[0,1], color='blue', alpha=0.7)
df2.plot(ax=ax[0,1], color='magenta', alpha=0.7)
intersect.plot(ax=ax[0,1], color='black')

intersect.plot(ax=ax[1,0], color='black', legend=True)

ax[0,0].set_title('Original')
ax[0,1].set_title('Intersection with polygon & Line')
ax[1,0].set_title('Line')

ax[1,1].set_visible(False)

custom_xlim = (0, 10)
custom_ylim = (0, 10)

plt.setp(ax, xlim=custom_xlim, ylim=custom_ylim)
plt.show()

Image of plots

相关问题