【问题标题】:Get all nodes inside only one of the Polygons, OSMNX仅在一个多边形 OSMNX 中获取所有节点
【发布时间】:2020-11-07 19:50:35
【问题描述】:

我有一个由两个多边形组成的网络,我现在想知道哪些节点只在更大的多边形中。我该怎么做?

代码如下:

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)

这是多边形:

较小的多边形是阿马多拉和另一个里斯本

【问题讨论】:

  • 简单地获取Portugal, Amadora中的节点不是更容易吗?我的意思是将城市值设置为“葡萄牙,阿马多拉”?
  • 但我需要所有节点。在这两个城市。

标签: python networkx geopandas osmnx


【解决方案1】:

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

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

【讨论】:

  • 我测试了您的解决方案,然后将仅使用里斯本网络的节点长度与我从您的方法中获得的节点长度进行了比较,它们是不同的。从里斯本的网络我得到 9719 个节点,从这个方法我得到 9735 个。我使用的是官方名称:“Município de Lisboa”和“Município da Amadora”
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 2017-06-03
  • 1970-01-01
  • 2023-01-14
相关资源
最近更新 更多