仅在多边形之一OSMNX中获取所有节点

时间:2020-07-18 01:04:31

标签: python networkx geopandas osmnx

我有一个由两个多边形构成的网络,现在我想确定哪些节点仅位于较大的多边形中。我该怎么办?

代码如下:

import osmnx as ox
import igraph as ig
import matplotlib.pyplot as plt
import pandas as pd
import networkx as nx

city = ['Portugal, Lisbon', 'Portugal, Amadora']
G = ox.graph_from_place(city, network_type='drive', simplify=True)
G_nx = nx.relabel.convert_node_labels_to_integers(G)
nodes, edges = ox.graph_to_gdfs(G_nx, nodes=True, edges=True)

以下是多边形: enter image description here

较小的多边形是Amadora,另一个是里斯本

1 个答案:

答案 0 :(得分:2)

您正在寻找within空间运算。这种操作是空间分析的基础,因此,我鼓励您仔细阅读所用工具的文档,以了解其基本概念和用法。如果使用OSMnx,它将包括networkx(用于网络分析)和geopandas(用于空间分析)。例如,在geopandas文档中详细介绍了within方法并给出了用法示例。

import osmnx as ox
ox.config(use_cache=True, log_console=True)

cities = ox.geocode_to_gdf(['Portugal, Lisbon', 'Portugal, Amadora'])
whole_polygon = cities.unary_union #unary union of both geometries
one_polygon = cities['geometry'].iloc[0] #geometry of just lisbon

G = ox.graph_from_polygon(whole_polygon, network_type='drive', simplify=True)
print(len(G)) #12811

# which nodes are within one_polygon?
nodes = ox.graph_to_gdfs(G, edges=False)
nodes_in_polygon = nodes[nodes.within(one_polygon)]
print(len(nodes_in_polygon)) #9734