4

After two hard days getting through the unit tests and snippets of graphhopper on GitHub and the JavaDocs, thinking that I might be able to put all together in a very basic java app, I have to come to the conclusion, that I am not able :(

All I want to do is:

  1. Build a graph
  2. Init graphhopper and configure it correctly
  3. Load the graph
  4. Route on it

Thats my code so far:

package javaapplication1;

import com.graphhopper.*;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.storage.GraphBuilder;
import com.graphhopper.storage.GraphStorage;

public class JavaApplication1 {

    protected static String location = "./tmp/graphstorage";
    protected static String defaultGraph = "./tmp/graphstorage/default";  

    private static final EncodingManager encodingManager = new EncodingManager("CAR");

    public static GraphStorage createGraph() {

        GraphStorage graph = new GraphBuilder(encodingManager).setLocation(location).create();
        graph.setNode(0, 42, 10);
        graph.setNode(1, 42.1, 10.1);
        graph.setNode(2, 42.1, 10.2);
        graph.setNode(3, 42, 10.4);
        graph.setNode(4, 41.9, 10.2);

        graph.edge(0, 1, 10, true);
        graph.edge(1, 2, 10, false);
        graph.edge(2, 3, 10, true);
        graph.edge(0, 4, 40, true);
        graph.edge(4, 3, 40, true);

        return graph;
    }

    public static void main(String[] args) {

        double fromLat = 42;
        double fromLon = 10.4;
        double toLat = 42;
        double toLon = 10;

        GraphStorage gs = createGraph();

        GraphHopperAPI instance = new GraphHopper()
            .setEncodingManager(encodingManager)
            .setGraphHopperLocation(location)
            .disableCHShortcuts();

        GraphHopper hopper = (GraphHopper) instance;
        //hopper.setGraph(createGraph()); // protected because only for testing?

        hopper.load(location);

        GHResponse ph = hopper.route(new GHRequest(fromLat, fromLon, toLat, toLon));
        if(ph.isFound()) {
            System.out.println(ph.getDistance());
            System.out.println(ph.getPoints().getSize());
        } else {
            System.out.println("No Route found!");
        }   
    }
}

Java says: "Exception in thread "main" java.lang.IllegalStateException: Call load or importOrLoad before routing". But I am calling .load() on hopper, which unfortunately returns 'false', but I am not able to figure out why.

My goal for this thread is to provide a very basic working code example of the GH Components and how to wire them up to solve the use case "Route on a graph, not loaded from OSM".

Jürgen Zornig
  • 1,174
  • 20
  • 48

1 Answers1

3

If you don't want to use OSM as data source you have to options:

  1. Using the low level API via copy and pasting the code under GraphHopper.route. No need to reuse the GraphHopper class. But this is probably harder.
  2. Do MyGraphHopper extends GraphHopper and overload the necessary methods. Be sure you set the graph before the 'postProcessing' method is called. Which e.g. creates the shortcuts and builds the locationIndex.

BTW: in master GraphHopper.setGraph is public?

Karussell
  • 17,085
  • 16
  • 97
  • 197
  • ad 1. uhm...ok,didn't think about that but sounds obvious. At least analyzing this code segment gave me more understanding how graphhopper works. – Jürgen Zornig Dec 01 '13 at 22:26
  • ad 2. According to 1. I think I'll make a MyGraphHopper class which only implements GraphHopperAPI interface (as a tradeoff) cherry picking some code segments from GraphHopper.java. But then I think I need to know where 'postProcessing' is defined? And where is it called? – Jürgen Zornig Dec 01 '13 at 22:32
  • ad BTW: First (as I saw it in the unit test, and it showed up in NBs code completion) I thought that .setGraph() is public... Hooray! That's what I was looking for.... not! Why isn't there a function like this public? – Jürgen Zornig Dec 01 '13 at 22:33
  • It is public: https://github.com/graphhopper/graphhopper/blob/master/core/src/main/java/com/graphhopper/GraphHopper.java#L287 – Karussell Dec 02 '13 at 08:52
  • See GraphHopper.postProcessing and it is called in process (which is nothing you need) and in load – Karussell Dec 02 '13 at 08:54
  • Ok, did my own SimpleHopper implementation. Constructor just takes a GraphStorage and initializes with that (no need to call load() implementation). EncodingManger and Location is just taken from the GraphStorage itself. LocationIndex is also build directly from the GraphStorage Object. So I just have to 1. Build a GraphStorage 2. Create a SimpleHopper and 3. Call route on it. SimpleHopper doesn't support ContractionHierachies, only simple routing to gain some understanding of this great Java library and do some first steps. Btw: I'll try to contribute an INTREST Reader. – Jürgen Zornig Dec 07 '13 at 12:44
  • @JürgenZornig good that you got it working! Your contribution is always welcome - would be really nice to open up GraphHopper to other data provider! What is 'INTREST' - where can I find more info? Also have a look into https://github.com/graphhopper/graphhopper/blob/master/CONTRIBUTING.md – Karussell Dec 07 '13 at 13:06
  • INTREST is a data exchange format (looks like a simple CSV) built by bavarian ministries to exchange intermodal transport network data (http://www.intrest.org/ - german). It's used by many governmental and public state agencies in the transport sector in some middle european countries for exchanging traffic model data, i.e. from VISUM (http://vision-traffic.ptvgroup.com/en-us/products/ptv-visum/). In Austria we use it to export data from our national graph integration platform (http://www.gip.gv.at/gipat-en.html) to different router software. Next step then would be a direct GIP reader. – Jürgen Zornig Dec 07 '13 at 15:14
  • Thanks for the insights :) ! And again: would be a very valueable contribution! – Karussell Dec 07 '13 at 17:27
  • Having same problem. I'm using osm data downloaded from geofabrik pakistan-latest.osm.pbf coverted to pakistan-latest.osm-gh. Any idea? – Muhammad Babar Sep 10 '14 at 06:30