routing icon indicating copy to clipboard operation
routing copied to clipboard

Calculate distance AND time between a source and multiple targets coordinates

Open 03July opened this issue 6 years ago • 5 comments

Is there a way to calculate the distance AND the time between a source and multiple targets coordinates ? I could find the CalculateWeight method in 'Matrix Calculations' but it indicates: 'A profile, like Vehicle.Car.Fastest(). Fastest will given you seconds, shortest will give you distance in meters.'

And the CalculteWeight only accept one profile which means that I have make calculation twice with Fastest and Shortest which is not optimal.

I'm looking for something like Distance Matrix API : 'https://developers.google.com/maps/documentation/distance-matrix/?hl=en'

http://maps.googleapis.com/maps/api/distancematrix/json?origins=48.630197,%202.474570&destinations=48.630707,%202.426762|48.701217,%202.308776

Thanks in advance.

03July avatar Sep 09 '17 21:09 03July

This is already possible but takes writing some extra code it's not something that works out-of-the-box.

xivk avatar Nov 03 '17 13:11 xivk

I'm trying to do the same thing at the moment, but I want the duration and distance of the 'fastest' route, not the distance of the shortest which I'm assuming the workaround mentioned in the original post would do.

@xivk - What does the extra code that you mentioned entail? Thanks.

jamiewinder avatar Sep 07 '18 15:09 jamiewinder

This is possible but stretches the current features of Itinero. You need to build a contracted graph that stored both time and distance. It is supported but I don't have a sample anywhere at the moment.

You should use one of these overloads:

https://github.com/itinero/routing/blob/develop/src/Itinero/RouterDbExtensions.cs#L141

Where instead of using the default 'weightHandler' you use this one:

https://github.com/itinero/routing/blob/develop/src/Itinero/Algorithms/Weights/WeightHandler.cs#L225

This one handles both time and distance. You can create an instance of this weight handler using this method:

https://github.com/itinero/routing/blob/develop/src/Itinero/Algorithms/Weights/Extensions.cs#L110

And then call the contraction method. After that you can use one of the overloads on the router class to use the same weight handler to calculate these matrices containing both time and distance:

https://github.com/itinero/routing/blob/develop/src/Itinero/RouterBaseExtensions.cs#L1254

xivk avatar Sep 11 '18 13:09 xivk

Thanks for the hints. I'll give it a go!

jamiewinder avatar Sep 11 '18 14:09 jamiewinder

Hello, I'm looking into this myself right now, but have come across the issue that the weights returned by the CalculateWeights with a custom weight handler appear to be faulty. The time and distance are always the exact same, and based on comparisons to calculating an actual route between one of the points on the matrix, they seem to always be the time (i.e. what the standard CalculateWeights would output anyway).

Currently, I'm initializing it as such:

var routerDb = RouterDb.Deserialize(fileStream); // from an uncontracted routerdb file
var profile = Itinero.Osm.Vehicles.Vehicle.Car.Fastest();
var router = new Router(routerDb);
var customWeightHandler = profile.AugmentedWeightHandler(router);
routerDb.AddContracted(profile, customWeightHandler);

I've also tried using the profile.AugmentedWeightHandlerCached method, as well as router.GetAugmentedWeightHandler, but all have yielded the same results.

Am I doing something wrong, or is this functionality simply bugged?

tompeeters368 avatar Sep 15 '20 02:09 tompeeters368