When using offline on a mobile (Android) device: How can I calculate the shortest distances between any point and the nearest (small) OSM path?

The offline file with graph/routing data could be used by a library to do the distance calculating. The size could be for a country like the Netherlands.

What kind of paths? I mean especially the smallest paths, like footpaths, hiking trails, etc. I don't need any further routing facilities.

How will I use the data? About 6 years I do geocaching with my onw geocaching app. I use OsmDroid, OsmDroid-with-Mapsforge i.c.m. OsmBonuspack. I work with downloaded mapsforge-v5 downloaded maps.

I am building a lot of AI stuff in my app so that I can guesstimate locations when I can't figure them out. For the last piece of AI I need to determine the shortest distance between any point to the nearest (foot)path.

If offline is too difficult, is there any API that can be called?

An example: I have a series coordinates (or points) in the woods. Which point is nearest to a footpath?

UPDATE: a lot of useful info is found at this location. So, now I only need to use the unpacked and pre-processed stuff to use the detailed API. Anyone experience with a simple main() method that does the trick?

  • You will certainly have to define what a "small" path is by using one or many of different OSM tags. In addition, do you already have downloaded the data locally? And how are you in coding for mobile devices? For the moment, what I would suggest is using a naive application such as OsmAnd. It doesn't do what you want but all "small" paths are displayed along your actual GPS position (if GPS is enable of course). I used it to hike abroad (Europe) without losing myself where data availability was high of course. It has an offline mode (max ~5 regions can be downloaded). – s.k Apr 11 '20 at 12:01
  • Thanks. I updated my question with your suggestions – tm1701 Apr 13 '20 at 9:05

The following solution works:

  • Get the graphhopper project in your IDE.
  • Get the raw Openstreetmap data (.pbf) via this very nice provider.
  • Run this command in your (git) bash shell: ./graphhopper.sh -a import -i gelderland-latest.osm.pbf. Of course, you take your own OSM area.
  • Make a new (maven) project with this pom.xml file:
  • Copy generated graphhopper-web-1.0-SNAPSHOT.jar from the graphhopper project to the libs folder of your new project so it will be included in your path. It may be even better to use a Gradle/Maven dependency of the graphhopper core or web libraries.

  • Copy the generated graphhopper data to a folder within your small project.

  • Create this simple demo:

    public static void main(String[] args) {
        private static String mapsFolder = "graphhopper-data/yourlocation-latest.osm-gh";
        GraphHopper graphHopper = new GraphHopper().forMobile();
        System.out.println("Found graph " + graphHopper.getGraphHopperStorage().toString() + ", nodes:" + graphHopper.getGraphHopperStorage().getNodes());
        QueryResult queryResult = graphHopper.getLocationIndex().findClosest( 52.11111, 6.111111, EdgeFilter.ALL_EDGES);
        double distance = queryResult.getQueryDistance();
        System.out.println( "Shortest distance is: " + distance);

I hope you enjoy this wonderful 'graphhopper' software!

UPDATE: Using the JAR on Android may give a lot of compiler errors due to library version mismatchers. A better solution for getting this example run at Android, is using this dependency in your build.gradle:

implementation 'com.graphhopper:graphhopper-core:1.0-pre33'

Consequence is that you have to set the default routing profile. You can achieve via this change:

graphHopper = new GraphHopper().forMobile();
// Next line: 
graphHopper.setProfiles( Collections.singletonList(new ProfileConfig("my_car").setVehicle("car").setWeighting("fastest")));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.