当您尝试查找包含您的点的多边形时,首先需要从多线串几何图形中生成多边形。由于您没有提供数据,我正在使用 OSMnx 从 OSM 下载示例。
import osmnx as ox
import geopandas as gpd
import shapely
point = (40.742623, -73.977857)
streets_graph = ox.graph_from_point(point, distance=500, network_type='drive')
streets_graph = ox.project_graph(streets_graph)
我重新投影了它,因为它比使用学位更方便,特别是如果你想测量任何东西。
然后,您必须将 OSMnx 图转换为 geopandas GeoDataFrame。
streets = ox.save_load.graph_to_gdfs(streets_graph, nodes=False, edges=True,
node_geometry=False, fill_edge_geometry=True)
为了获得一些我可以使用的点,我将仅使用此地理数据框中心的点。
point = streets.unary_union.centroid
这就是它的样子。
接下来,您需要获取由街道定义的街区的多边形,使用shapely.ops.polygonize
正如我在上面的评论中建议的那样,并将它们存储为 GeoSeries。
polygons = shapely.ops.polygonize(streets.geometry)
polygons = gpd.GeoSeries(polygons)
接下来您唯一要做的就是找到哪个多边形包含您的点。
target = polygons.loc[polygons.contains(point)]
再次绘制:
ax = target.plot()
gpd.GeoSeries([point]).plot(ax=ax, color='r')
如果您想知道哪些街道形成了该多边形的边界,只需将其与原始网络相交即可。我正在过滤MultiLineString
排除仅与多边形相交于一点的街道。
target_streets = streets.loc[streets.intersection(target.iloc[0]).type == 'MultiLineString']
这就是结果的样子。
ax = target_streets2.plot()
gpd.GeoSeries([point]).plot(ax=ax, color='r')
希望能帮助到你。