code is combined from function public void testDoWork() of github-MapMatchingTest.java and forum-problem.
import java.util.List;
import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
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.matching.MatchResult;
import com.graphhopper.routing.AlgorithmOptions;
import com.graphhopper.routing.util.CarFlagEncoder;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.weighting.FastestWeighting;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.GraphStorage;
import com.graphhopper.util.GPXEntry;
import com.graphhopper.util.Instruction;
import com.graphhopper.util.InstructionList;
import com.graphhopper.util.Parameters;
import com.graphhopper.util.PointList;
public class Test2 {
public static void main(String[] args) {
GraphHopper hopper = new GraphHopper();
hopper.setDataReaderFile("berlin-latest.osm.pbf");
hopper.setGraphHopperLocation("mapmatchingtest");
CarFlagEncoder encoder = new CarFlagEncoder();
hopper.setEncodingManager(new EncodingManager(encoder));
hopper.getCHFactoryDecorator().setEnabled(false);
hopper.importOrLoad();
// create MapMatching object, can and should be shared accross threads
String algorithm = Parameters.Algorithms.DIJKSTRA_BI;
Weighting weighting = new FastestWeighting(encoder);
AlgorithmOptions algoOptions = new AlgorithmOptions(algorithm, weighting);
MapMatching mapMatching = new MapMatching(hopper, algoOptions);
// do the actual matching, get the GPX entries from a file or via stream
List<GPXEntry> inputGPXEntries = new GPXFile().doImport("nice.gpx").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 there’s something wrong.
Exception in thread "main" java.lang.UnsupportedOperationException: Cannot create DataReader. Solutions: avoid import via calling load directly, provide a DataReader or use e.g. GraphHopperOSM or a different subclass
at com.graphhopper.GraphHopper.createReader(GraphHopper.java:717)
at com.graphhopper.GraphHopper.importData(GraphHopper.java:710)
at com.graphhopper.GraphHopper.process(GraphHopper.java:681)
at com.graphhopper.GraphHopper.importOrLoad(GraphHopper.java:659)
at Test2.main(Test2.java:32)
at Test2.main(Test2.java:32):
hopper.importOrLoad();
The code is in maven project with graphhopper-0.7.0.jar and map-matching-0.7.0.jar.
Thanks for answering.