GraphHopper.com | Forum | GitHub | Maps | Blog

Problems with GraphHopper Map Matching Java Demo


#1

Hello,

My intention was to evaluate if Graph Hopper Map Matching can be used for our project, where we have to match GPS tracks with road system/OSM. As a first step I started with the demo code as described in “github.com/graphhopper/map-matching#java-usage”. (sorry there are only 2 links per topic allewed for new members…)

I created a maven project in eclipse and ended up in following compile errors:
(see also the maven/dependency specified)

Please refer to the following screenshots for more detailed information:
source code
maven/dependency

I am wondering if this is caused by a version mismatch, but have no idea how this can be resolved?!

Do you have any hint for these problems?

Many thanks in advance!

The source code, Java demo/snippet :

import java.util.List;

import com.graphhopper.GraphHopper;
import com.graphhopper.matching.EdgeMatch;
import com.graphhopper.matching.GPXFile;
import com.graphhopper.matching.LocationIndexMatch;
import com.graphhopper.matching.MapMatching;
import com.graphhopper.routing.util.CarFlagEncoder;
import com.graphhopper.storage.GraphStorage;

public class MapMatcher {
	// import OpenStreetMap data
	public MapMatcher() {
		super();
		// TODO Auto-generated constructor stub
		


	GraphHopper hopper = new GraphHopper();
	hopper.setOSMFile("./map-data/leipzig_germany.osm.pbf");
	hopper.setGraphHopperLocation("./target/mapmatchingtest");
	CarFlagEncoder encoder = new CarFlagEncoder();
	hopper.setEncodingManager(new EncodingManager(encoder));
	hopper.setCHEnable(false);
	hopper.importOrLoad();

	// create MapMatching object, can and should be shared accross threads

	GraphStorage graph = hopper.getGraph();
	LocationIndexMatch locationIndex = new LocationIndexMatch(graph,
	                (LocationIndexTree) hopper.getLocationIndex());
	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 = new GPXFile("nice.gpx").read();
	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
	matches.get(0).edgeState,
	
	}

later I tried to fix compile errors by changing the demo source code as listed below:

	public MapMatcher() {
		super();
		// TODO Auto-generated constructor stub
		
		// import OpenStreetMap data
		GraphHopper hopper = new GraphHopper();
		hopper.setOSMFile("./map-data/leipzig_germany.osm.pbf");
		hopper.setGraphHopperLocation("./target/mapmatchingtest");
		CarFlagEncoder encoder = new CarFlagEncoder();
		hopper.setEncodingManager(new EncodingManager(encoder));
		hopper.setCHEnable(false);
		hopper.importOrLoad();

		// create MapMatching object, can and should be shared accross threads

		GraphHopperStorage graph = hopper.getGraphHopperStorage();
		LocationIndexMatch locationIndex = new LocationIndexMatch(graph,
		                (LocationIndexTree) hopper.getLocationIndex());
		locationIndex.prepareIndex();
		
		
		
		MapMatching mapMatching = new MapMatching(graph, locationIndex, encoder);

		// do the actual matching, get the GPX entries from a file or via stream
		GPXFile gpxFile = new GPXFile();
		gpxFile.doImport("nice.gpx");
		List<GPXEntry> inputGPXEntries = (new GPXFile()).getEntries();

		
		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
		matches.get(0).getEdgeState();
		
	}

but this resulted in the following exception/stacktrace:

2015-10-06 17:07:25,329 [main] INFO com.graphhopper.GraphHopper - version 0.5.0|2015-08-12T12:33:51+0000 (4,12,3,2,2,1)
2015-10-06 17:07:25,335 [main] INFO com.graphhopper.GraphHopper - graph car|RAM_STORE|2D|NoExt|4,12,3,2,2, details:edges:74 567(3MB), nodes:53 723(1MB), name:(1MB), geo:232 915(1MB), bounds:16.243235943687765,16.818523224112166,47.96737744586268,48.397507855240434
Exception in thread “main” java.lang.IllegalThreadStateException: already created
at com.graphhopper.storage.RAMDataAccess.create(RAMDataAccess.java:87)
at com.graphhopper.storage.RAMDataAccess.create(RAMDataAccess.java:34)
at com.graphhopper.storage.index.LocationIndexTree.prepareIndex(LocationIndexTree.java:320)
at vbtmm.MapMatcher.(MapMatcher.java:39)
at vbtmm.main.main(main.java:17)


#2

What happens if you use the original code version, remove the created files on disc and switch to the 0.5 branch?


#3

even after deletion of graphhopper dircetory from the local maven repository, and a subsequent "Run As -> Maven clean"
the same GH versions are listed in maven dependencies:

C:\Users\werner.m2\repository\com\graphhopper\map-matching\0.5.0.0\map-matching-0.5.0.0.jar
C:\Users\werner.m2\repository\com\graphhopper\graphhopper\0.5.0\graphhopper-0.5.0.jar

Sorry for asking again and maybe it is caused by misconfiguration in my environment, but In the demo code you provided, there are e.g. following instructions which cause a compile error:
“GraphHopper hopper = new GraphHopper();

GraphStorage graph = hopper.getGraph();”

but when I check the version graphhopper-0.5.0.jar, I cannot find in the class “GraphHopper” a method “getGraph()”

Are you sure that the demo code is correct?

Many thanks!


#4

Ups!

I have fixed this (new getGraphHopperStorage method and without the prepare calll). Would you try again and report if this works now? If not also tell us here please :slight_smile: and look into the working MapMatchingTest for the correct usage.


#5

OK, thanks!
First problem seems to be solved.

But now there is another compile error (already mentioned above):

where can I find the MapMatchingTest?

edit:
ok, found it:


Import error when map matching
#6

Missed that. This has now changed to:

List<GPXEntry> file = new GPXFile().doImport(filename or inputstream).getEntries();

#7

ok thank, I used the following code:

		// do the actual matching, get the GPX entries from a file or via stream
		GPXFile gpxFile = new GPXFile();
		gpxFile.doImport("nice.gpx");
		List<GPXEntry> inputGPXEntries = gpxFile.getEntries();

which results in the following output on the console:

INFO - graph bike|RAM_STORE|2D|NoExt|4,12,3,2,2, details:edges:82 121(3MB), nodes:59 190(1MB), name:(1MB), geo:194 680(1MB), bounds:12.179001692958215,12.599993965029581,51.227002432055826,51.45898688120366
Exception in thread “main” java.lang.IllegalArgumentException: Input GPX list does not contain valid points or outside of imported area!? 129, [48.158627,16.407223,230.9, 1427181633000, 48.158629,16.40721,230.7, 1427181638000, 48.158629,16.40721,230.7, 1427181643000,

So I understand that the coordinates contained in imported the GPX file are outside of the OSM file bounds. indeed the import of the osm file show in the log a bounding box which is completely different:
bounds:12.179001692958215,12.599993965029581,51.227002432055826,51.45898688120366

When I check the bounds of the imported osm file with JOSM I see
16.2 , 16.6 , 48.1 , 48.3 , which would perfectly fit

I assume this is a problem related to projection repectively EPSG, but how can I set this corretly?

Many thanks in advance!


#8

Sorry no idea about project stuff.
You will have to convert this before you do the import or we could also convert while import but that would need a new feature I guess. But I’m pretty sure that JOSM can export it for us properly :slight_smile:


#9

The wrong coordinates problem was caused by the previously imported osm file, after deletion of the generated directories, my osm file was successfully imported and map matching was successfully performed! :sunglasses:

So, change of osm file and/or change vehicle type requries the deletion of the grasshopper generated files!

please find below the comparison (in JOSM) of the recorded GPX file with the map matching result!

Thanks a lot!


How to use graphhopper-osm-id-mapping?
#10

hi

how do you generate the map matching result file?


#11

hi, how to use inputstream ?
thank you