Get all nodes inside only one of the Polygons, OSMNX

允我心安 提交于 2020-12-15 04:38:05

问题


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?

Here is the code:

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)

Here are the Polygons:

The smaller Polygon is Amadora and the other Lisbon


回答1:


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


来源:https://stackoverflow.com/questions/62963574/get-all-nodes-inside-only-one-of-the-polygons-osmnx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!