Search code examples
androidopenstreetmaposmdroidgraphhopper

Graphhopper on Android offline routing pbf file error


I'm trying to create an application that combines Osmdroid with Graphhoper to achieve offline routing on a city area. I exported osm file from Open Street Maps and converted that file to pbf. The problem is that an application fails to load the pbf file because Graphhoper trying to parse the pbf file using java.xml.stream which is not available on Android devices. Here is the function that I'm using to load the pbf file.

    public void setRouting(){
    File dir = new File(Environment.getExternalStorageDirectory(),"osmdroid");
    File osmFile=new File(dir,"offline-map.osm.pbf");

    if(!osmFile.exists()){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("File doesn't exist").setTitle("Error");
        AlertDialog dialog = builder.create();
        dialog.show();
        return;
    }


    GraphHopper hopper = new GraphHopper().forMobile();
    hopper.setEncodingManager(encodingManager);
    hopper.setGraphHopperLocation(getApplicationContext().getFilesDir().getPath());
    hopper.setOSMFile(osmFile.getAbsolutePath());
    hopper.setCHShortcuts("fastest");
    hopper.importOrLoad();
}

I know that it is possible to convert osm to ghz (which works in my case): https://github.com/graphhopper/graphhopper/blob/0.3/docs/android/index.md but I can't execute this:

./graphhopper.sh import <your-osm-file>    

on windows OS.

I'm not using Maven to include Graphhopper. I inserted jar into libs folder.

How can I import a osm file into my project to achieve offline routing?

I finally managed to resolve this.

This is how I did it:

  1. Download Apache maven
  2. Scroll down to Install instruction - Windows section and follow maven installation steps
  3. Set Enviroment User Variable MAVEN_HOME ie: C:\Program Files\Apache Software Foundation\apache-maven-3.2.1. (important use same value as for M2_HOME)
  4. Clone Graphopper git to your local machine git or download repository as zip and extract it somewhere on your local disk
  5. Download and install Cygwin
  6. Run Cygwin
  7. $ cd /cygdrive/
  8. You are now positioned on root type "ls" to see all drives on computer
  9. Position your self into Graphhopper git root. ie for c:\git\graphopper: $ cd c $ cd git $ cd graphhoper

see linux navigation tips for how to navigate 10. Copy your filename.osm file to Graphhoper git root 11. $ ./graphhopper.sh import filename.osm 12. You shud have folder filename-gh in your Graphopper git root folder which have all you need. 13. Copy that folder to your mobile device 14. You can now use something like this: ` File dir = new File(Environment.getExternalStorageDirectory(),"map-parent-folder-name"); File ghDir=new File(dir.getAbsolutePath(),"filename-gh");

hopper = new GraphHopper().forMobile();
hopper.setCHShortcuts("fastest");
hopper.load(ghDir.getAbsolutePath());

to init graphopper

Two more tips. If you don't have Graphopper jar (graphhopper-0.3-SNAPSHOT.jar) you can create one with: $ ./graphhopper.sh build filename.osm new jar is located in core/target/ folder

You will need trove-3.0.3.jar which you may download here. Just add trove-3.0.3.jar from the zip into the libs folder of your project

I hope that I didn't forget anything :)


Solution

  • You cannot import the xml/pbf on Android (yet). As you are on windows have a look at the docs how to execute the import via cygwin.