计算两点之间的路径和到达时间在Python中。

huangapple go评论72阅读模式
英文:

calculate route and arrival time between two point in python

问题

Sure, here's the translation of the non-code part:

我有两个简单的坐标点(纬度、经度)和一个名为速度的参数。我需要找到路线和到达时间。

例如:

点1:(1,1)
点2:(5,5)
速度:x km/h
一个可能的路线是:(1,1) -> (2,2) -> (3,3) -> (4,4) -> (5,5)

现在我还需要每个路线上的到达时间,类似这样:

[(1,1),0:00] -> [(2,2),1:00] -> [(3,3),2:00] -> [(4,4),3:00] -> [(5,5),4:00]

第一个元素显示点,第二个元素显示到达时间(或到达该点所用的时间,或任何显示到达该点所需时间的其他值)。

一个重要的事情是,我希望在现实世界地图上运行这个,所以纬度和经度将来自世界地图。


至于你提到的代码和问题,如果你需要进一步的帮助或翻译,请告诉我。

英文:

i have two simple point(lat,lon) and one parameter named speed. i need to find the route and arrival time.

for example:

point1: (1,1)   
point2: (5,5)   
speed: x km/h
one possible route is : (1,1) -> (2,2) -> (3,3) -> (4,4) -> (5,5)

now i also need the arrival time to each point in route. something like this:

[(1,1),0:00] -> [(2,2),1:00] -> [(3,3),2:00] -> [(4,4),3:00] -> [(5,5),4:00]   

first element shows point and second shows arrival time (or time spent to reach that point or any other value that shows time to reach that point)

one important thing is that i want to run this on real-world map so then the lat and lng will be used from the world map.


so ... i did this:

import osmnx as ox
import networkx as nx
ox.settings.log_console=True
ox.settings.use_cache=True


start_latlng = (35.73509, 51.4171)
end_latlng = (35.73674, 51.40611)
mode      = 'walk'
optimizer = 'time'

graph = ox.graph_from_place('tehran', network_type = mode)
orig_node = ox.distance.nearest_nodes(graph, start_latlng[1],start_latlng[0])
dest_node = ox.distance.nearest_nodes(graph, end_latlng[1],end_latlng[0])

shortest_route = nx.shortest_path(graph,
                                orig_node,
                                dest_node,
                                weight=optimizer)


print(shortest_route)

the result was this:

[1649103291, 717069600, 1922773706, 423733650, 4444063663, 3376783364, 2178480086, 2178480071, 2179951182, 1880160160, 3429927627, 3856787096, 3856787099, 436153557, 1649103461, 29004286, 423746569]

1- the problem with this is that i don't know what are these numbers mean? i know i can plot them but i need lat, lng not this!

2- second i don't know the arrival time to each point. i mean how much does it take to travel from starting point to other point.

3- it is so slow ! i need too faster approach.

so how can i get results like this?

[(1,1),0:00] -> [(2,2),1:00] -> [(3,3),2:00] -> [(4,4),3:00] -> [(5,5),4:00]   

答案1

得分: 1

Your shortest_route 必须是从 orig_nodedest_node 的最短路径上你必须访问的节点 ID 序列(来自你的图形)。你可能在图形对象中有节点 ID 与(纬度,经度)元组之间的对应关系。

一旦你有了你的(纬度,经度)序列,你就可以通过首先计算每一对点之间的距离,然后根据你的平均速度推断出每个点的到达时间。你可以使用haversine formula计算两点之间的大圆距离(这假设地球是一个完美的球体),甚至更好的是计算地球上的测地距离(这假设地球是一个椭圆体,更接近实际情况)。

GeoPy包提供了轻松计算这两种距离的方法。

英文:

Your shortest_route must be the sequence of the nodes ID (from your graph) you must visit to go from orig_node to dest_node using the shortest path. You probably have in the graph object a correspondance between the node ID and a (lat, lng) tuple.

Once you have your (lat, lng) sequence, you will be able to determine the arrival time at each point by first computing the distance between each couple of points and then deducing the travel time using your mean speed. You can compute the great-circle distance between two points using the haversine formula (this assume that Earth is a perfect sphere) or even better, compute the geodesic distance (this assume that Earth is an ellipsoid, so closer to reality).

The GeoPy package offers methods to compute both easily.

huangapple
  • 本文由 发表于 2023年5月14日 19:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定