Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Example Code in Readme #25

Closed
Maik1 opened this issue May 7, 2015 · 4 comments
Closed

Example Code in Readme #25

Maik1 opened this issue May 7, 2015 · 4 comments

Comments

@Maik1
Copy link

Maik1 commented May 7, 2015

The sample code in the Readme is not working:

Graph graph = hopper.getGraph();
LocationIndexMatch locationIndex = new LocationIndexMatch(graph, new RAMDirectory());
locationIndex.prepareIndex();
MapMatching mapMatching = new MapMatching(graph, locationIndex, encoder);

I get the following error:

The constructor LocationIndexMatch(Graph, RAMDirectory) is undefined    Routing.java    line 145    Java Problem

That is because LocationIndexMatch expects a GraphStorage and not a Graph.
But if I change the code to, like it is done in the test files:

GraphStorage graph = hopper.getGraph();
LocationIndexMatch locationIndex = new LocationIndexMatch(graph, (LocationIndexTree) hopper.getLocationIndex());         
MapMatching mapMatching = new MapMatching(graph, locationIndex, encoder);

I get the following exception:

05-07 11:26:24.235: E/AndroidRuntime(18902): Caused by: java.lang.IllegalArgumentException: Call LevelGraph.getBaseGraph() instead of using the LevelGraph itself
karussell added a commit that referenced this issue May 8, 2015
@karussell
Copy link
Member

Thanks! This is indeed wrong - I've updated it to your code.

And you seem to try to use the map matching with hopper.setCHEnabled(true) where you would need to call graph.getBaseGraph before passing to the constructors. Currently a graph with enabled CH is not 'officially' supported and therefor not recommended.

@karims
Copy link

karims commented May 27, 2015

I have the same problem and it is not resolved.

When I use:

GraphStorage graph = hopper.getGraph();
        //LocationIndexMatch locationIndex = new LocationIndexMatch(graph, new LocationIndexTree(hopper.getGraph(), new RAMDirectory()));
        LocationIndexMatch locationIndex = new LocationIndexMatch(graph, new RAMDirectory());
        locationIndex.prepareIndex();
        MapMatching mapMatching = new MapMatching(graph, locationIndex, encoder);

I get compilation error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The constructor LocationIndexMatch(GraphStorage, RAMDirectory) is undefined

When I tried with:

LocationIndexMatch locationIndex = new LocationIndexMatch(graph, new LocationIndexTree(hopper.getGraph(), new RAMDirectory()));

I get this error:

Exception in thread "main" java.lang.IllegalThreadStateException: already created
    at com.graphhopper.storage.RAMDataAccess.create(RAMDataAccess.java:86)
    at com.graphhopper.storage.RAMDataAccess.create(RAMDataAccess.java:33)
    at com.graphhopper.storage.index.LocationIndexTree.prepareIndex(LocationIndexTree.java:319)
....

This is my full code block copied form README.md

    private void start( String[] args )
    {
        GraphHopper hopper = new GraphHopper().                
//                disableCHShortcuts().
                setEncodingManager(new EncodingManager("CAR")).
                setGraphHopperLocation("graphhopper-out-folder2/").
                setOSMFile("/Users/karim/Documents/workspace-other/mapmatching/src/main/java/com/eta/resources/india-latest.osm");
        CarFlagEncoder encoder = new CarFlagEncoder();
        hopper.setCHEnable(false);
        hopper.importOrLoad();

     // create MapMatching object, can and should be shared accross threads
        GraphStorage graph = hopper.getGraph();
        LocationIndexMatch locationIndex = new LocationIndexMatch(graph, new LocationIndexTree(hopper.getGraph(), new RAMDirectory()));
        //LocationIndexMatch locationIndex = new LocationIndexMatch(graph, new RAMDirectory());
        locationIndex.prepareIndex();
        MapMatching mapMatching = new MapMatching(graph, locationIndex, encoder);

        // do the actual matching, get the GPX entries from a file or via stream
        List<GPXEntry> inputGPXEntries = getList();
        MatchResult mr = mapMatching.doWork(inputGPXEntries);

        // return GraphHopper edges with all associated GPX entries
        List<EdgeMatch> matches = mr.getEdgeMatches();
        // now do something with the edges like storing the edgeIds or doing fetchWayGeometry etc
        for (Iterator iterator = matches.iterator(); iterator.hasNext();) {
            EdgeMatch edgeMatch = (EdgeMatch) iterator.next();
            System.out.println(edgeMatch.getEdgeState());

        }
}

@karussell
Copy link
Member

The readme is updated to reflect this change. For future reference look also into the unit tests.

@karims
Copy link

karims commented May 27, 2015

Thanks a lot :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants