GraphHopper.com | Forum | GitHub | Maps | Blog

IndexOutOfBoundsException while using offline routing


#1

So, I’m trying to figure out a way of making the offline routing a possibility in my Android app, and so far I have achieved the following:

  1. Loaded the gh folder generated by the graphhopper.sh

val mapsFolder = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
“/graphhopper/maps/”
)
val graphHopperMobile = GraphHopper().forMobile()
graphHopperMobile.setElevation(true)
graphHopperMobile.setEnableInstructions(true)
graphHopperMobile.load(File(mapsFolder, “/spain-latest.osm-gh”).absolutePath)

  1. Requested a GHResponse (the getGHRequest method is working perfectly fine while using a GraphHopperWeb object with the same gh folder, and is set with a ‘hike’ vehicle and has hints with elevation set to true):

val ghResponse = graphHopperWeb.route(getGHRequest(request))

So when I call the route method, I catch the following exception:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

I debugged the code, and observed that it seems to be raised in ViaRoutingTemplate.java at line 95, after calling to the ghRequest.getFavoredHeading(0)

Any ideas about the reason of the exception? The favoredHeadings is indeed empty. But why¿? Should I add anything else?

With the GraphHopperWeb is working perfectly fine, so I suppose I should add something else in the configuration of the graphHopperMobile object.


#2

OK, I’ve just figured it out. Even though I was getting a proper GHResponse with my previous setting, it seems that the GraphHopper forMobile doesn’t behave the same way.

I changed the way I built the GHRequest, from this:

val ghRequest = GHRequest(GHPointStart, GHPointEnd)

for (i in 0 until request.points.size()) {
    ghRequest.points.add(
        GHPoint(latitude, longitude)
    )
}

ghRequest.vehicle = "hike"
ghRequest.hints.put("elevation", true)

To this:

val GHPointStart = GHPoint(startLatitude, startLongitude)
val GHPointEnd = GHPoint(endLatitude, endLongitude)
val ghRequest = GHRequest(GHPointStart, GHPointEnd)

And the second way worked very well. Perhaps, the GraphHopperWeb behaves different than GraphHopper().forMobile() . Otherwise, I cannot understand why was this happening.


#3

indeed, you should not do getPoints().add … and instead use addPoint … have created an issue: https://github.com/graphhopper/graphhopper/issues/1445


#4

Yes @karussell that’s what I did, since I don’t want to be restricted to a given start and end, and prefer to add points, since I’ll add multipoint routing capabilities soon.

Thank you!