Feraman
Feraman

Reputation: 39

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)

Upvotes: 0

Views: 1328

Answers (2)

gboeing
gboeing

Reputation: 6412

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

Upvotes: 1

Deniz
Deniz

Reputation: 321

You can create a list op tuples with a simple lambda function. I didn't test against the performance with respect to other possible solutions but for a 5000 row x 9 col. geodataframe it takes about 120 ms on a moderate desktop PC.

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

Upvotes: 0

Related Questions