【问题标题】:Getting list of coordinates (lat,long) from OSMNX Geoseries从 OSMNX Geoseries 获取坐标列表(纬度、经度)
【发布时间】:2021-04-15 23:00:28
【问题描述】:

我想计算目的地列表和起点之间的最短路径。但首先我需要找到离我的目的地最近的节点。我正在从 OSMNX 函数获取一组兴趣点 (geometries_from_place) 的目的地列表。

import osmnx as ox
import geopandas as gpd
import networkx as nx
print(ox.__version__)
ox.config(use_cache=True, log_console=True)
Kinshasa = [ "Kisenso, Mont Amba, 31, Democratic Republic of the Congo",
"N'djili, Tshangu, Democratic Republic of the Congo",
"Kinshasa, Democratic Republic of the Congo"]
G_Kinshasa = ox.graph.graph_from_place(Kinshasa, simplify=True, network_type='drive')
tags2 = {'amenity' : ['hospital','university','social_facility'],
        'landuse' : ['retail', 'commercial'],
         'shop' : ['water','bakery']}
POIS = ox.geometries_from_place(Kinshasa, tags2, which_result=1)
Nearest_Nodes = ox.get_nearest_nodes(G_Kinshasa, POIS['geometry'][x],POIS[geometry][y])

如何从作为 GeoSeries 的 POIS['geometry'] 对象获取 lats 和 longs 的 tules 列表,以将其传递给上面最后一行代码中的 get_nearest_nodes? 这是 POIS['geometry'] 的输出示例:

Out[10]: 
0                             POINT (15.34802 -4.39344)
1                             POINT (15.34074 -4.41001)
2                             POINT (15.34012 -4.40466)
3                             POINT (15.34169 -4.40443)
4                             POINT (15.35278 -4.40812)

【问题讨论】:

    标签: python geopandas osmnx


    【解决方案1】:

    这是一个最小的可重现解决方案(OSM 无法以当前形式对您的地点查询进行地理编码,因此我选择了一个仅用于演示目的的解决方案)。请注意,我指定了 balltree 方法来查找最近的节点,因为您正在使用未投影图和未投影点。

    import osmnx as ox
    ox.config(use_cache=True, log_console=True)
    
    place = 'Berkeley, CA, USA'
    G = ox.graph_from_place(place, network_type='drive')
    
    tags = {'amenity' : ['hospital','university','social_facility'],
            'landuse' : ['retail', 'commercial'],
            'shop' : ['water','bakery']}
    gdf = ox.geometries_from_place(place, tags)
    
    centroids = gdf.centroid
    X = centroids.x
    Y = centroids.y
    
    nn = ox.get_nearest_nodes(G, X, Y, method='balltree')
    

    【讨论】:

      【解决方案2】:

      您可以使用简单的 lambda 函数创建列表操作元组。 我没有针对其他可能的解决方案进行性能测试,而是针对 5000 行 x 9 列进行测试。 geodataframe 在中型台式电脑上大约需要 120 毫秒。

      pointlist = list(POIS.geometry.apply(lambda x: ( x.x, x.y )))
      

      【讨论】:

      • 不幸的是,这不起作用。我收到“多边形”对象没有属性“x”的错误
      • 我不知道您使用的是 osmnx 的 geometries_from_place 方法,它会返回多边形/多多边形而不是点。您应该使用返回点的函数或定义从多边形计算 x,y 的函数,例如获取质心。
      • 我正在从函数 (geometries_from_place) 中获取兴趣点,因此我确实将 POINTS 作为我的几何类型。只是我无法访问这些点内的 x 和 y。当我在 Python 中插入 type(POIS.geometry[1]) 时,它返回 shapely.geometry.point.Point
      猜你喜欢
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多