根据地理参考多边形裁剪networkx图 [英] Clipping a networkx graph according to georeferenced polygon
问题描述
我正在运行一个循环,该循环为GeoDataFrames(城市)列表的每一行(邻域)计算一个networkx.classes.multidigraph.MultiDiGraph
.然后,它为每一行计算一些统计信息,并将文件写出到磁盘.问题是循环的计算时间非常长,因为图形是针对每一行计算的.
I am running a loop that computes a networkx.classes.multidigraph.MultiDiGraph
for each row (neighbourhood) of a list of GeoDataFrames (cities). It then computes some statistics for each row and writes the file out to disk. The problem is that the loop is extremely long to compute because the graph is computed for each row.
我想加快循环速度的方法是为整个GeoDataFrame计算图形,然后将图形裁剪到每一行(每行都有一个多边形).您可以使用geopandas.clip
为GeoSeries做到这一点.但是,看来networkx图不存在与geopandas.clip
等效的内容.
The way I want to quicken the loop is by computing the graph for the whole GeoDataFrame and then clipping the graph into each row (each row has a polygon). You can do this for GeoSeries with geopandas.clip
. It seems, however, that no equivalent to geopandas.clip
exists for networkx graphs.
-
有人知道剪裁networkx图的方法吗?
Does anyone know of a way to clip a networkx graph?
或者,还有哪些其他方法可以加快循环速度.
Alternatively, what other methods exist to speed up my loop.
注意:如果可以将networkx图转换为pandas对象,则可以进行裁剪.不幸的是,我认为当图形转换为pandas对象时,不可能保留osmnx所作用的属性.如果我错了,请这样说.
Note: clipping would work if I could convert the networkx graph to a pandas object. Unfortunately, I think it is not possible to keep the properties which osmnx acts on when the graph is converted to a pandas object. If I'm wrong, please say so.
这是我的初始代码:
import osmnx as ox
import pandas as pd
import geopandas as gpd
import os
path="C:/folder/"
files=[os.path.join(path, f) for f in os.listdir(path)]
for i in range(0,2):
city=gpd.read_file(files[i])
circ=[]
for i in range(0,181):
graph_for_row=ox.graph_from_polygon(city.geometry[i])
#above is the long command
stat = ox.basic_stats(graph_for_row)
circ.append(stat['circuity_avg'])
circ=pd.Series(circ)
merged.append(pd.concat([city, circ], axis=1))
for i in (range(0,len(merged))):
with open(geofiles[i], 'w') as f:
f.write(merged[i].to_json())
这是我想要的新循环:
clipped_graph=[]
for i in range(0,2):
city=gpd.read_file(files[i])
whole_city=city.unary_union
graph=ox.graph_from_polygon(whole_city)
clipped_graph.append(gpd.clip(graph, city.geometry))#this line
#does not work since 'graph' is a networkx object, not
#a GeoDataFrame or GeoSeries
circ=[]
for i in range(0,181)
stat = ox.basic_stats(clipped_graph[i])
circ.append(stat['circuity_avg'])
circ=pd.Series(circ)
merged.append(pd.concat([city, circ], axis=1))
for i in (range(0,len(merged))):
with open(geofiles[i], 'w') as f:
f.write(merged[i].to_json())
推荐答案
您可以使用各个多边形(在空间上)与图节点相交,然后使用这些节点来生成
You can use your individual polygons to (spatially) intersect the graph nodes, then use those nodes to induce a subgraph. MWE:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
# load a shapefile of polygons as geodataframe using geopandas
# here i just get 3 cities from OSM to make example reproducible without a shapefile
places = ['Cudahy, CA, USA', 'Bell, CA, USA', 'Maywood, CA, USA']
gdf = ox.gdf_from_places(places)
# get a graph of the union of their boundaries, then extract nodes as geodataframe
G = ox.graph_from_polygon(gdf.unary_union, network_type='drive')
nodes = ox.graph_to_gdfs(G, edges=False)
# for each city polygon, find intersecting nodes then induce subgraph
for polygon in gdf['geometry']:
intersecting_nodes = nodes[nodes.intersects(polygon)].index
G_sub = G.subgraph(intersecting_nodes)
fig, ax = ox.plot_graph(G_sub)
这篇关于根据地理参考多边形裁剪networkx图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!