OSMNX 如何确保我们不在多边形之外绘制
OSMNX How to make sure we do not plot outside of polygon
我有下面的代码,它工作正常,除了我想确保水没有被绘制在 gdf 多边形之外作为道路网络。
ox.geometries_from_polygon 将正确的多边形作为参数,但是如果一个点在多边形内,它看起来像 OSM returns 所有元素。
任何 hints/examples 将不胜感激。
谢谢,
阿提拉
import matplotlib.pyplot as plt
import osmnx as ox
import networkx as nx
from descartes import PolygonPatch
from shapely.geometry import Polygon, MultiPolygon
ox.config(log_console=True)
water_color = '#aad3df'
place = 'Budapest, Hungary'
gdf = ox.geocode_to_gdf(place)
gdf_poly = gdf["geometry"].unary_union
fp = ox.geometries_from_polygon(gdf_poly, tags={'water':['river','lake'],"natural":["water"]})
G = ox.graph_from_place(place, network_type='drive', retain_all=True)
fig, ax = ox.plot_graph(G, show=False, close=False,save=False,
bgcolor=None,edge_color='w' , edge_linewidth=0.3, node_size=0)
for geometry in gdf['geometry'].tolist():
if isinstance(geometry, (Polygon, MultiPolygon)):
if isinstance(geometry, Polygon):
geometry = MultiPolygon([geometry])
for polygon in geometry:
patch = PolygonPatch(polygon, fc='#175C7D', ec='#175C7C', lw=1, alpha=1, zorder=-1)
ax.add_patch(patch)
fig, ax = ox.plot_footprints(fp,ax=ax, color=water_color, save=False, show=False)
margin = 0.02
west, south, east, north = gdf.unary_union.bounds
margin_ns = (north - south) * margin
margin_ew = (east - west) * margin
ax.set_ylim((south - margin_ns, north + margin_ns))
ax.set_xlim((west - margin_ew, east + margin_ew))
plt.show()
您可以将水体几何图形与地点边界相交,例如:
import matplotlib.pyplot as plt
import osmnx as ox
import networkx as nx
from descartes import PolygonPatch
from shapely.geometry import Polygon, MultiPolygon
ox.config(log_console=True)
water_color = '#aad3df'
place = 'Budapest, Hungary'
gdf = ox.geocode_to_gdf(place)
place_poly = gdf.unary_union
fp = ox.geometries_from_polygon(place_poly, tags={'water':['river','lake'],"natural":["water"]})
water_poly = fp[fp.geometry.geom_type.str.contains('Polygon')].unary_union.intersection(place_poly)
G = ox.graph_from_place(place, network_type='drive', retain_all=True)
fig, ax = ox.plot_graph(G, show=False, close=False,save=False,
bgcolor=None,edge_color='w' , edge_linewidth=0.3, node_size=0)
ax.add_patch(PolygonPatch(place_poly, fc='#175C7D', ec='#175C7C', lw=1, alpha=1, zorder=-1))
ax.add_patch(PolygonPatch(water_poly, fc=water_color, ec=water_color))
margin = 0.02
west, south, east, north = gdf.unary_union.bounds
margin_ns = (north - south) * margin
margin_ew = (east - west) * margin
ax.set_ylim((south - margin_ns, north + margin_ns))
ax.set_xlim((west - margin_ew, east + margin_ew))
plt.show()
我有下面的代码,它工作正常,除了我想确保水没有被绘制在 gdf 多边形之外作为道路网络。
ox.geometries_from_polygon 将正确的多边形作为参数,但是如果一个点在多边形内,它看起来像 OSM returns 所有元素。
任何 hints/examples 将不胜感激。
谢谢,
阿提拉
import matplotlib.pyplot as plt
import osmnx as ox
import networkx as nx
from descartes import PolygonPatch
from shapely.geometry import Polygon, MultiPolygon
ox.config(log_console=True)
water_color = '#aad3df'
place = 'Budapest, Hungary'
gdf = ox.geocode_to_gdf(place)
gdf_poly = gdf["geometry"].unary_union
fp = ox.geometries_from_polygon(gdf_poly, tags={'water':['river','lake'],"natural":["water"]})
G = ox.graph_from_place(place, network_type='drive', retain_all=True)
fig, ax = ox.plot_graph(G, show=False, close=False,save=False,
bgcolor=None,edge_color='w' , edge_linewidth=0.3, node_size=0)
for geometry in gdf['geometry'].tolist():
if isinstance(geometry, (Polygon, MultiPolygon)):
if isinstance(geometry, Polygon):
geometry = MultiPolygon([geometry])
for polygon in geometry:
patch = PolygonPatch(polygon, fc='#175C7D', ec='#175C7C', lw=1, alpha=1, zorder=-1)
ax.add_patch(patch)
fig, ax = ox.plot_footprints(fp,ax=ax, color=water_color, save=False, show=False)
margin = 0.02
west, south, east, north = gdf.unary_union.bounds
margin_ns = (north - south) * margin
margin_ew = (east - west) * margin
ax.set_ylim((south - margin_ns, north + margin_ns))
ax.set_xlim((west - margin_ew, east + margin_ew))
plt.show()
您可以将水体几何图形与地点边界相交,例如:
import matplotlib.pyplot as plt
import osmnx as ox
import networkx as nx
from descartes import PolygonPatch
from shapely.geometry import Polygon, MultiPolygon
ox.config(log_console=True)
water_color = '#aad3df'
place = 'Budapest, Hungary'
gdf = ox.geocode_to_gdf(place)
place_poly = gdf.unary_union
fp = ox.geometries_from_polygon(place_poly, tags={'water':['river','lake'],"natural":["water"]})
water_poly = fp[fp.geometry.geom_type.str.contains('Polygon')].unary_union.intersection(place_poly)
G = ox.graph_from_place(place, network_type='drive', retain_all=True)
fig, ax = ox.plot_graph(G, show=False, close=False,save=False,
bgcolor=None,edge_color='w' , edge_linewidth=0.3, node_size=0)
ax.add_patch(PolygonPatch(place_poly, fc='#175C7D', ec='#175C7C', lw=1, alpha=1, zorder=-1))
ax.add_patch(PolygonPatch(water_poly, fc=water_color, ec=water_color))
margin = 0.02
west, south, east, north = gdf.unary_union.bounds
margin_ns = (north - south) * margin
margin_ew = (east - west) * margin
ax.set_ylim((south - margin_ns, north + margin_ns))
ax.set_xlim((west - margin_ew, east + margin_ew))
plt.show()