使用OSMnx提取约束多边形 [英] Extract constrained polygon using OSMnx

查看:246
本文介绍了使用OSMnx提取约束多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OSMnx软件包来解决以下任务: -在地图上有一个由纬度和经度定义的点X -我们需要检测包含该点X并受相邻道路约束的多边形 -因此,X点基本上位于多边形内部,相邻的道路将成为该多边形的边界.

I'm playing around with OSMnx package in order to solve the following task: - there is a point X on the map defined by latitude and longitude - we need to detect a polygon that contains that point X and is constrained by the neighboring roads - so basically the point X is inside the polygon and the neighboring roads will be borders of that polygon.

到目前为止,我仅设法在地图上绘制图形的可视化并找到最接近点X的边缘/节点.

So far I've managed only to plot the visualization of the graph on the map and find the closest edge/node to point X.

在所附图像中,我用红色突出显示了要提取的区域.

In the attached image I've highlighted the area I want to extract in red.

推荐答案

在尝试查找包含点的多边形时,首先需要从多线串几何图形中生成多边形.由于您未提供数据,因此我正在使用OSMnx从OSM下载示例.

As you are trying to find a polygon containing your point, you first need to generate polygons out of multilinestring geometry. As you did not provide your data I am downloading a sample from OSM using OSMnx.

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)

我重新投影了它,因为它比使用度数更方便,特别是如果您想测量任何东西.

I have reprojected it as it is way more convenient than working with degrees, especially if you want to measure anything.

然后,您必须将OSMnx图形转换为geopandas GeoDataFrame.

You then have to convert OSMnx graph to geopandas GeoDataFrame.

streets = ox.save_load.graph_to_gdfs(streets_graph, nodes=False, edges=True,
                                     node_geometry=False, fill_edge_geometry=True)

为了说明我可以使用的一点,我将只使用此地理数据框中心的一个.

To get some point I can work with, I'll just use the one in the centre of this geodataframe.

point = streets.unary_union.centroid

这就是它的样子.

接下来,您需要使用我在上面的注释中建议的shapely.ops.polygonize来获取街道定义的街区的多边形,并将其存储为GeoSeries.

Next you need to get polygons of your blocks defined by streets, using shapely.ops.polygonize as I suggested in the comment above and store them as GeoSeries.

polygons = shapely.ops.polygonize(streets.geometry)
polygons = gpd.GeoSeries(polygons)

接下来唯一要做的就是找到哪个多边形包含您的点.

The only thing you have to do next is to find which polygon contains your point.

target = polygons.loc[polygons.contains(point)]

再次绘制:

ax = target.plot()
gpd.GeoSeries([point]).plot(ax=ax, color='r')

如果您想知道哪些街道构成了该多边形的边界,只需将其与原始网络相交即可.我正在过滤MultiLineString以排除仅在一个点与多边形相交的街道.

If you want to know which streets are forming the boundary of this polygon, just intersect it with the original network. I am filtering for MultiLineString to exclude streets which intersects the polygon only in one point.

target_streets = streets.loc[streets.intersection(target.iloc[0]).type == 'MultiLineString']

这就是结果的样子.

ax = target_streets2.plot()
gpd.GeoSeries([point]).plot(ax=ax, color='r')

希望有帮助.

这篇关于使用OSMnx提取约束多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆