GraphHopper.com | Forum | GitHub | Maps | Blog

Import error when map matching


#1

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.


#2

Please use the latest version 0.8.2 and read also the latest documentation with new GraphHopperOSM as constructor


#3

my pom.xml is

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Matcher</groupId>
<artifactId>MapMatchingDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MapMatchingDemo</name>
<url>http://maven.apache.org</url>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <!-- <scope>test</scope>3.8.1 -->
</dependency>

<!-- https://mvnrepository.com/artifact/com.graphhopper/map-matching -->
<dependency>
<groupId>com.graphhopper</groupId>
<artifactId>map-matching</artifactId>
<version>0.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.graphhopper/graphhopper-map-matching-core -->
<dependency>
<groupId>com.graphhopper</groupId>
<artifactId>graphhopper-map-matching-core</artifactId>
<version>0.8.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.graphhopper/graphhopper -->
<dependency>
<groupId>com.graphhopper</groupId>
<artifactId>graphhopper</artifactId>
<version>0.7.0</version>
</dependency>

<dependency>
  <groupId>com.graphhopper</groupId>
  <artifactId>graphhopper-core</artifactId>
  <version>0.8.2</version>
</dependency>
</dependencies>
</project>

There’s no graphhopper-0.8.2.jar and map-matching-0.8.2.jar in this website mvn-repository, only 0.7.0. But there are 0.8.2 version for graphhopper-core and map-matching-core. I included all the four jars in pom.xml. is that okay?