仅在多边形之一OSMNX中获取所有节点 [英] Get all nodes inside only one of the Polygons, OSMNX
问题描述
我有一个由两个多边形构成的网络,现在我想确定哪些节点仅位于较大的多边形中.我该怎么办?
I have a network formed with two Polygons and I want to now which nodes are only in the bigger Polygon. How can I do this?
这是代码:
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)
以下是多边形:
较小的多边形是Amadora,另一个是里斯本
The smaller Polygon is Amadora and the other Lisbon
推荐答案
您正在寻找within
空间运算.这样的操作是空间分析的基础,因此,我鼓励您仔细阅读所用工具的文档,以了解其基本概念和用法.如果使用OSMnx,它将包括networkx(用于网络分析)和geopandas(用于空间分析).例如,在geopandas文档中详细描述了within
方法并给出了用法示例.
You are looking for a within
spatial operation. Such an operation is elementary in spatial analysis, and as such, I would encourage you to carefully read the documentation of the tools you are using to understand their underlying concepts and usage. If working with OSMnx, this would include networkx (for network analysis) and geopandas (for spatial analysis). For example, the within
method is described in detail and given usage examples in the geopandas documentation.
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
这篇关于仅在多边形之一OSMNX中获取所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!