Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get distance time and class through OpenstreetmapX?

I've been trying to query some edge information from an exported network from Openstreet map via this package OpenstreetmapX.jl . However, it appears that some functionality are not directly available. For instance:

Suppose you have a set of links in the form of Vector{Tuple{Int64, Int64}} and you wish to identify their distance, travel time, and class type. I know for the distance you can call something like the following command:


Links = [(301972446, 60649731)
 (60649731, 301972446)
 (2505989518, 2489651510)
 (2489651510, 60649731)
 (60649731, 301972503)
 (267224410, 2895172669)
 (2895172669, 267224410)
 (582794232, 582794289)]

# for example to know the distance of the first edge in Link set

distance(map.nodes[links[1][1]],map.nodes[links[1][2]] )

But this is not efficient. Is there anything that directly return the distance, time, and class of each edges? Like:

distance( (582794232, 582794289))
distance( links[1])
time(links[1])
class(links[1])
like image 486
Hemfri Avatar asked Dec 27 '25 21:12

Hemfri


1 Answers

You will need to access the MapData struct (type ?MapData for details).

Edges

Suppose you have a map:

using OpenStreetMapX
m = get_map_data(joinpath(dirname(pathof(OpenStreetMapX)),"..","test/data/reno_east3.osm");use_cache=false);

All edges are just a Vector

julia> links = m.e[1:2]   # sample 2 edges on map
2-element Vector{Tuple{Int64, Int64}}:
 (2441017888, 2975020216)
 (139988738, 385046328)

Edge classes

Edge classes are also a vector so for the above example you could do m.class[1:2]. Or you could search the id on the edge list:

 m.class[findfirst(==((140044645, 140044632)), m.e)]

This yields class number - for translation to class name use OpenStreetMapX.ROAD_CLASSES

Edge lenghts

The distances are available in w matrix, so you can do:

julia> m.w[m.v[140044645], m.v[140044632]]
41.239883456012905

You can also recalculate it using distance function:

julia> distance(m.nodes, [(140044645, 140044632)])
1-element Vector{Float64}:
 40.99997118352562

You might notice that this value is slightly lower as the distance function just calculates a stright line distance. However the get_map_data function when parsing the map, by default compresses irrelevant edges (the parameter only_intersections=true), and the distances in w are calculated before compression. In other words w includes curvature of the road and distance is just a straight line.

Travel times

You can get the travel times for edges at once:

julia> OpenStreetMapX.network_travel_times(m)
4220-element Vector{Float64}:
  1.5857447369115734
  2.3649894625077863
 ...

Routing

Finally, you can use A-star algorithm to find a distance between any two nodes:

julia> shortest_route(m, 140044645, 140044632)
([140044645, 140044632], 41.239883456012905, 1.4846358044164647)

The returned tuple contains:

  • the shortest route (nodes)
  • the distance in meters
  • the driving time in seconds
like image 58
Przemyslaw Szufel Avatar answered Dec 30 '25 22:12

Przemyslaw Szufel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!