只获取其中一个多边形 OSMNX 内的所有节点

Get all nodes inside only one of the Polygons, 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)

这是多边形:

较小的 Polygon 是 Amadora,另一个是 Lisbon

您正在寻找 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