[英]Truncating osmnx by bbox (or polygon): How to create dummy nodes at boundaries?
I am trying to truncate an osmnx graph by bbox.我正在尝试通过 bbox 截断 osmnx 图。 It works as per the documentation.
它按照文档工作。 The reproducible self-explanatory code is given below:
可重现的自解释代码如下:
import numpy as np
import osmnx as ox
import geopandas as gpd
import networkx as nx
import matplotlib.pyplot as plt
N, S, E, W = 1.3235381983186159, 1.319982801681384, \
103.85361309942331 , 103.84833190057668,
graph = ox.graph_from_bbox(N, S, E, W, \
network_type='drive')
nodes= ox.graph_to_gdfs(graph, nodes=True, edges=False)
edges= ox.graph_to_gdfs(graph, edges=True, nodes=False)
fig, ax = ox.plot.plot_graph(
graph,
ax=None,
figsize=(10, 10),
bgcolor="white",
node_color="red",
node_size=5,
node_alpha=None,
node_edgecolor="none",
node_zorder=1,
edge_color="black",
edge_linewidth=0.1,
edge_alpha=None,
show=False,
close=False,
save=False,
bbox=None,
)
W_ = W + (E-W) * 0.8
S_ = S + (N-S)*0.7
width = (E - W)*0.07
height = (N - S)*0.1
rect = plt.Rectangle((W_, S_), width, height, facecolor="green", alpha=0.3, edgecolor=None)
ax.add_patch(rect)
plt.show()
g_truncated = ox.truncate.truncate_graph_bbox(graph, S_ + height, S_, W_+width, W_, truncate_by_edge=False)
ox.plot_graph(g_truncated)
The bbox and the extracted graphs are shown below: bbox和提取的图如下所示:
If I want to extract the subgraph such that I introduce dummy nodes at the boundaries, how can I do that?如果我想提取子图以便在边界处引入虚拟节点,我该怎么做? To be specific, I am trying to get a subgraph as it is visible in the picture.
具体来说,我正在尝试获取图片中可见的子图。 (ie a subgraph with 6 nodes in black as shown below:
(即黑色有6个节点的子图如下图:
Given the wide popularity of osmnx, does there exist a simple/straightforward way to acheive this?鉴于 osmnx 的广泛流行,是否存在一种简单/直接的方法来实现这一目标?
- LOOP over edges in graph
- LOOP over sides in box
- IF box side intersects edge
- remove edge
- Add vertex at intersection point
- Add edges between intersection vertex and original edge end points
You will need to google 'calculate intersection point of two line segments'您需要在谷歌上搜索“计算两条线段的交点”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.