转换 osmnx 投影地图的经纬度坐标
Posted
技术标签:
【中文标题】转换 osmnx 投影地图的经纬度坐标【英文标题】:transform lat long coordinates for a osmnx projected map 【发布时间】:2020-08-04 10:39:24 【问题描述】:我想从 osmnx 获取最接近 lat long 对的节点。
下载 osmnx 图形对象后,我想对其进行投影,以便按照文档中的说明从道路网络中移除死胡同。
我知道我需要投影图表和经纬度值,但我实际上不明白以哪种方式。
例如,我在项目中将 lat long 转换为 x y z,但我怎么知道这与该图所做的投影兼容。欧几里得方法似乎没有帮助。
rebuild_graph=True 标志是否也会撤消投影并返回未投影的图形对象?如果是这样,那么经纬度数据并没有靠得那么近,无法解释返回的同一节点。
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)
【问题讨论】:
【参考方案1】:我知道我需要投影图表和经纬度值,但我实际上不明白以哪种方式。
只需将您的点投影到与您的图表相同的 CRS。根据the docs,除非您指定不同的 CRS,否则 OSMnx 会将您的图形投影到本地 UTM 区域投影。只需使用投影图的 CRS 属性将您的 lat/lng 点投影到相同的位置,然后进行欧几里得最近节点搜索。
这是一个使用 cmets 的最小可重现示例:
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]
rebuild_graph=True 标志也会撤消投影并返回未投影的图形对象吗?
没有。
【讨论】:
首先感谢您的回复和您的开源工作。在路线中连接这些节点有什么想法吗?(通常我正在尝试从 gps crumbs 进行地图匹配)。当我得到这些节点时,其中一些节点最终会出现在停车场或其他随机道路上,这就是我想丢弃死胡同的原因。当我试图在每一对之间找到一条路径时,没有一条路径,或者整个路径与我正在搜索的路径完全不同。我在节点上的迭代中尝试的是检查 i、i+1 和 i+2 节点之间是否存在路径,如果不存在则丢弃 i+1 节点并再次尝试。即使这样,所有节点都被丢弃了。以上是关于转换 osmnx 投影地图的经纬度坐标的主要内容,如果未能解决你的问题,请参考以下文章
火星坐标百度坐标WGS-84坐标相互转换及墨卡托投影坐标转经纬度JavaScript版