leo_bouts
leo_bouts

Reputation: 337

transform lat long coordinates for a osmnx projected map

I want to get the closest node to a lat long pair from osmnx.

After i download the osmnx graph object i want to project it so i can remove dead ends from the road network as instructed in the docs.

I am aware that i project the graph and the lat, long values need to projected to but i dont actually understand in which way.

For example i have a conversion of lat long to x y z in my project but how can i know that this is compatible with the projection that this graph does. The euclidean method does not seem to help.

Also does the flag rebuild_graph=True undo the projection and return an unprojected graph obejct? If so the lat long data are not so close together to explain the same node being returned.

long = [20.83099222975307, 20.83099222975307, 20.831071057075036, 20.831507821410554, 20.831536549206216, 20.831590645309163, 20.831760926274246, 20.831829734056857, 20.832064826897437, 20.83211892318422, 20.832291232174775, 20.832453521327647, 20.832467537620413, 20.832744680362744, 20.83275203624094, 20.832893649116148, 20.832933033903316, 20.83298713048916, 20.833011166881615, 20.833109340046658, 20.83317212468842, 20.83317212468842, 20.83319216485246, 20.833193496936566, 20.833256281615377, 20.833335746832073, 20.833345766925337, 20.83338451522313, 20.83367104311693, 20.83367104311693, 20.83378456515314, 20.833896118665407, 20.833897450746417, 20.83400095279438, 20.83400095279438, 20.834029044642254, 20.83411517063695, 20.83413121469866, 20.834177955710697, 20.8341879758658, 20.83424074081229, 20.83424074081229, 20.83424074081229, 20.83424809681322, 20.834270801294185, 20.834296169936398, 20.834302193862516, 20.834302193862516, 20.83430485802092, 20.83430485802092, 20.83441438425335, 20.834493213575563, 20.834498541888962, 20.83452391055964, 20.834525938270623, 20.83458669580759, 20.83459405183661, 20.83459405183661, 20.834608068255694, 20.834616756364248, 20.83467288126638, 20.834695585806365, 20.834695585806365, 20.834742326972062, 20.83503887769283, 20.835118343769174, 20.83514840444336, 20.83576757270048, 20.836452193850544, 20.83755761485359, 20.83759706037195, 20.837852203156956, 20.83795570768881, 20.838343145143025, 20.838343145143025, 20.839993748127704, 20.84092763462079, 20.84092763462079, 20.84092763462079, 20.84092763462079, 20.84127702640877, 20.84127702640877, 20.84127702640877, 20.841985137270154, 20.843422866598996, 20.84377429407861, 20.84377429407861, 20.84377429407861, 20.84377429407861, 20.84377429407861, 20.84377429407861, 20.84377429407861, 20.84377429407861, 20.84377429407861, 20.84402151726155, 20.84402151726155, 20.84548742851209, 20.84548742851209, 20.85188606916644, 20.85501616058255, 20.856737430326934, 20.859542430698603, 20.8613760173422, 20.86163714950174, 20.863028413707386, 20.863180155858238, 20.863345567971184, 20.863412802449744, 20.863424615681698, 20.863451670631083, 20.863495295022673, 20.86349756245783, 20.86346547505983, 20.86346547505983, 20.86346547505983, 20.863419542480333, 20.863411941442653, 20.86332960137235, 20.862958153765447, 20.86259449784883, 20.862368831055996, 20.862368831055996, 20.86213427281035, 20.86212634419993, 20.86188638794884, 20.861807035358805, 20.861375561408494, 20.860967669982454, 20.858716012450735, 20.858716012450735, 20.858706051455755, 20.85862909256079, 20.85859787704128, 20.85611899992582, 20.85611899992582, 20.853380676295366, 20.84616157108611, 20.845523814987697, 20.844864025170992, 20.842355069052047, 20.841927997139937, 20.841920696524085, 20.83342199581793, 20.829528649520768, 20.824360163540142, 20.82338364296999, 20.82231856837287]

lat = [39.62319155463754, 39.62319155463754, 39.62320295662305, 39.6232970230751, 39.62329987357564, 39.623311275578985, 39.623345481600296, 39.6233625846173, 39.6234110431884, 39.62341959470448, 39.623456651286325, 39.6234908573795, 39.623496558396674, 39.623553568594225, 39.623553568594225, 39.623584924222904, 39.62359347576046, 39.62360202729907, 39.62361057883875, 39.62362768192127, 39.62364193449326, 39.62364193449326, 39.62364763552287, 39.62364763552287, 39.62365903758352, 39.62367614067801, 39.62368184171045, 39.623687542743355, 39.62374740361722, 39.62374740361722, 39.62377590881346, 39.62379586245781, 39.62379586245781, 39.623824367674004, 39.623824367674004, 39.623827218196276, 39.62384432133233, 39.623850022378626, 39.623858573948944, 39.62386427499641, 39.62387282656849, 39.62387282656849, 39.62387282656849, 39.62387567709275, 39.62388137814163, 39.62388422866624, 39.62388422866624, 39.62388422866624, 39.62388422866624, 39.62388422866624, 39.623912733918836, 39.623929837076034, 39.623929837076034, 39.62393553812936, 39.62393553812936, 39.623946940237445, 39.62395264129219, 39.62395264129219, 39.62395549181974, 39.62395549181974, 39.62396974445926, 39.62397544551588, 39.62397544551588, 39.6239839971017, 39.62404955929475, 39.62406666248575, 39.62407236355037, 39.62421203978002, 39.624368819557304, 39.62462821960523, 39.62463962182745, 39.624696632966725, 39.62472228799472, 39.624816356512106, 39.624816356512106, 39.62522398823179, 39.62546628794432, 39.62546628794432, 39.62546628794432, 39.62546628794432, 39.62556035747312, 39.62556035747312, 39.62556035747312, 39.62574564631297, 39.626144731653206, 39.62624450334792, 39.62624450334792, 39.62624450334792, 39.62624450334792, 39.62624450334792, 39.62624450334792, 39.62624450334792, 39.62624450334792, 39.62624450334792, 39.62631576893222, 39.62631576893222, 39.626754766550185, 39.626754766550185, 39.62903531847423, 39.63050061275056, 39.631469890610106, 39.63349401463708, 39.63539845582614, 39.63574057656538, 39.638423432026336, 39.63893378506902, 39.63967223552374, 39.640079954790295, 39.640191151371056, 39.64042494989323, 39.64123469722554, 39.64159395436001, 39.64219557167739, 39.64219557167739, 39.64219557167739, 39.642680290858735, 39.64276297858772, 39.643316133524635, 39.64490719146398, 39.64600213432235, 39.64658953268899, 39.64658953268899, 39.64713986675438, 39.64715697565563, 39.64768735369431, 39.64785559273931, 39.648711051843534, 39.64946101303195, 39.652951433740164, 39.652951433740164, 39.65296284063269, 39.65307120620556, 39.65311398213635, 39.656285178192974, 39.656285178192974, 39.65940518392453, 39.66665246841233, 39.66725430386289, 39.66786755390862, 39.67016657695176, 39.67055165652977, 39.670560213878105, 39.678005508599924, 39.68132905057261, 39.6857112353726, 39.686538632203565, 39.68744878016951]

bounding_box = (20.814945, 20.876184, 39.612716999999996, 39.685287)


# Defining the map boundaries
west, east, north, south = bounding_box[0], bounding_box[1], bounding_box[2], bounding_box[3]

# Downloading the map as a graph object
g = ox.graph_from_bbox(north,
               south,
               east,
               west,
               network_type='all', clean_periphery=True)

G_proj = ox.project_graph(g)

g = ox.consolidate_intersections(G_proj,
                         rebuild_graph=True,
                         tolerance=20,
                         dead_ends=False)
route = []

for i in range(len(lat)):

    point = (lat[i], long[i])
    node = ox.get_nearest_node(g, point)
    print(node)
    route.append(node)

print(route)

Upvotes: 4

Views: 3029

Answers (1)

gboeing
gboeing

Reputation: 6442

I am aware that i project the graph and the lat, long values need to projected to but i dont actually understand in which way.

Just project your points to the same CRS as your graph. Per the docs, OSMnx projects your graph to the local UTM zone projection unless you specify a different CRS. Just use the projected graph's CRS attribute to project your lat/lng points to the same, and then do a euclidean nearest-node search.

Here's a minimal reproducible example with comments:

import geopandas as gpd
import osmnx as ox
from shapely.geometry import Point
ox.config(use_cache=True, log_console=True)

# create graph, project it, then consolidate intersections
bbox = (39.612717, 39.685287, 20.876184, 20.814945)
G = ox.graph_from_bbox(*bbox, network_type='all')
Gp = ox.project_graph(G)
Gc = ox.consolidate_intersections(Gp, rebuild_graph=True, tolerance=20, dead_ends=False)

# turn lat/lng lists into list of shapely points
lats = [39.6654413, 39.6456468, 39.6175496]
lngs = [20.8586396, 20.8496936, 20.8437053]
points_list = [Point((lng, lat)) for lat, lng in zip(lats, lngs)]

# then turn list into GeoSeries with original CRS set
# i'm just guessing that your original CRS is 4326
points = gpd.GeoSeries(points_list, crs='epsg:4326')

# then project your points to the same CRS as your projected graph
points_proj = points.to_crs(Gp.graph['crs'])

# find nearest node in projected graph to each projected point
method = 'euclidean'
nearest_nodes = [ox.get_nearest_node(Gc, (pt.y, pt.x), method) for pt in points_proj]
print(nearest_nodes) # prints [1334, 777, 37]

Also does the flag rebuild_graph=True undo the projection and return an unprojected graph obejct?

No.

Upvotes: 3

Related Questions