Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

python - Getting list of coordinates (lat,long) from OSMNX Geoseries

I would like to calculate the shortest path between a list of destinations and an origin. But first I need to find the nodes closest to my destinations. I am getting the list of destinations from OSMNX function for a set of Points of Interests (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])

How can I get a list of tules of lats and longs from the POIS['geometry'] object which is a GeoSeries to pass it to get_nearest_nodes in the last line of code above? Here is a sample of output of 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is a minimal reproducible solution (OSM cannot geocode your place queries in their current form, so I picked one that works just for demonstration purposes). Note that I specify the balltree method to find nearest nodes, as you are working with an unprojected graph and unprojected points.

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...