itinero / routing

The routing core of itinero.
Apache License 2.0
220 stars 70 forks source link

RouterBaseExtension - ArgumentOutOfRangeException #363

Open Oyekunle86 opened 1 year ago

Oyekunle86 commented 1 year ago

Hello,

Please I get this error when I calculate route for multiple points/locations e.g.

        var locations = new[]
        {
            new Coordinate(6.43513833145211f, 3.45599909557983f),
            new Coordinate(6.43200924856121f, 3.46967836163486f)
        };

var route = router.Calculate(bus, locations); var routeGeoJson = route.ToGeoJson();

I get this error:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.Collections.Generic.List1.get_Item(Int32 index) at Itinero.RouterBaseExtensions.TryCalculate(RouterBase router, IProfileInstance profile, IMassResolvingAlgorithm resolvingAlgorithm, Single turnPenalty, Tuple2[] preferredTurns, CancellationToken cancellationToken)

Please help out

image

bjtrounson commented 9 months ago

Hi,

Don't know if you have figured this out already but I'll put this here for anyone else having issues with GPS coordinates.

Those GPS coordinates seem to show they are in the middle of the ocean, you might have your GPS coordinates reversed. I also recommend parsing the GPS points into router points. I find this gives better results when routing as it will snap the GPS point to the closest edge.

Here is a code snippet I like to use to convert, you could adapt it to process an array of coordinates if needed. This method will try different search distances incase the GPS point is pretty far away from the road / edge.

private static RouterPoint? BuildRouterPoints(RouterBase router, Coordinate coordinate, IProfileInstance profile)
{
        var routerPoint = router.TryResolve(profile, coordinate, 200);
        if (!routerPoint.IsError) return routerPoint.Value;
        Console.WriteLine($"{DateTime.Now} - [WARNING]: {routerPoint.ErrorMessage}");
        routerPoint = router.TryResolve(profile, coordinate, 2000);
        if (!routerPoint.IsError) return routerPoint.Value;
        Console.WriteLine($"{DateTime.Now} - [ERROR]: {routerPoint.ErrorMessage}");
        return null;
}

Another thing to be keep in mind is if you are getting errors with points not routing, make sure you're not on an island. I found this out when doing some routing in New Zealand the North Island roads only get added if you load island data were the South Island gets loaded without island data.

Here is how you can load island data

routerDb.AddIslandData(profile);

Hope this helps anyone that stumbles into the same issue