Search in sources :

Example 1 with Gpx

use of com.graphhopper.jackson.Gpx in project graphhopper by graphhopper.

the class MatchCommand method run.

@Override
protected void run(Bootstrap<GraphHopperServerConfiguration> bootstrap, Namespace args, GraphHopperServerConfiguration configuration) {
    GraphHopperConfig graphHopperConfiguration = configuration.getGraphHopperConfiguration();
    GraphHopper hopper = new GraphHopper().init(graphHopperConfiguration);
    hopper.importOrLoad();
    PMap hints = new PMap();
    hints.putObject("profile", args.get("profile"));
    MapMatching mapMatching = new MapMatching(hopper, hints);
    mapMatching.setTransitionProbabilityBeta(args.getDouble("transition_probability_beta"));
    mapMatching.setMeasurementErrorSigma(args.getInt("gps_accuracy"));
    StopWatch importSW = new StopWatch();
    StopWatch matchSW = new StopWatch();
    Translation tr = new TranslationMap().doImport().getWithFallBack(Helper.getLocale(args.getString("instructions")));
    final boolean withRoute = !args.getString("instructions").isEmpty();
    XmlMapper xmlMapper = new XmlMapper();
    for (File gpxFile : args.<File>getList("gpx")) {
        try {
            importSW.start();
            Gpx gpx = xmlMapper.readValue(gpxFile, Gpx.class);
            if (gpx.trk == null) {
                throw new IllegalArgumentException("No tracks found in GPX document. Are you using waypoints or routes instead?");
            }
            if (gpx.trk.size() > 1) {
                throw new IllegalArgumentException("GPX documents with multiple tracks not supported yet.");
            }
            List<Observation> measurements = GpxConversions.getEntries(gpx.trk.get(0));
            importSW.stop();
            matchSW.start();
            MatchResult mr = mapMatching.match(measurements);
            matchSW.stop();
            System.out.println(gpxFile);
            System.out.println("\tmatches:\t" + mr.getEdgeMatches().size() + ", gps entries:" + measurements.size());
            System.out.println("\tgpx length:\t" + (float) mr.getGpxEntriesLength() + " vs " + (float) mr.getMatchLength());
            String outFile = gpxFile.getAbsolutePath() + ".res.gpx";
            System.out.println("\texport results to:" + outFile);
            ResponsePath responsePath = new PathMerger(mr.getGraph(), mr.getWeighting()).doWork(PointList.EMPTY, Collections.singletonList(mr.getMergedPath()), hopper.getEncodingManager(), tr);
            if (responsePath.hasErrors()) {
                System.err.println("Problem with file " + gpxFile + ", " + responsePath.getErrors());
                continue;
            }
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFile))) {
                long time = gpx.trk.get(0).getStartTime().map(Date::getTime).orElse(System.currentTimeMillis());
                writer.append(GpxConversions.createGPX(responsePath.getInstructions(), gpx.trk.get(0).name != null ? gpx.trk.get(0).name : "", time, hopper.hasElevation(), withRoute, true, false, Constants.VERSION, tr));
            }
        } catch (Exception ex) {
            importSW.stop();
            matchSW.stop();
            System.err.println("Problem with file " + gpxFile);
            ex.printStackTrace(System.err);
        }
    }
    System.out.println("gps import took:" + importSW.getSeconds() + "s, match took: " + matchSW.getSeconds());
}
Also used : MapMatching(com.graphhopper.matching.MapMatching) FileWriter(java.io.FileWriter) GraphHopper(com.graphhopper.GraphHopper) MatchResult(com.graphhopper.matching.MatchResult) Gpx(com.graphhopper.jackson.Gpx) GraphHopperConfig(com.graphhopper.GraphHopperConfig) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) BufferedWriter(java.io.BufferedWriter) ResponsePath(com.graphhopper.ResponsePath) Observation(com.graphhopper.matching.Observation) File(java.io.File)

Example 2 with Gpx

use of com.graphhopper.jackson.Gpx in project graphhopper by graphhopper.

the class MapMatchingTest method testLoop.

/**
 * This test is to check that loops are maintained. GPX input:
 * https://graphhopper.com/maps/?point=51.343657%2C12.360708&point=51.344982%2C12.364066&point=51.344841%2C12.361223&point=51.342781%2C12.361867&layer=Lyrk
 */
@ParameterizedTest
@ArgumentsSource(FixtureProvider.class)
public void testLoop(PMap hints) throws IOException {
    MapMatching mapMatching = new MapMatching(graphHopper, hints);
    // Need to reduce GPS accuracy because too many GPX are filtered out otherwise.
    mapMatching.setMeasurementErrorSigma(40);
    Gpx gpx = xmlMapper.readValue(getClass().getResourceAsStream("/tour2-with-loop.gpx"), Gpx.class);
    MatchResult mr = mapMatching.match(GpxConversions.getEntries(gpx.trk.get(0)));
    assertEquals(Arrays.asList("Gustav-Adolf-Straße", "Leibnizstraße", "Hinrichsenstraße", "Tschaikowskistraße"), fetchStreets(mr.getEdgeMatches()));
    assertEquals(mr.getGpxEntriesLength(), mr.getMatchLength(), 5);
}
Also used : MapMatching(com.graphhopper.matching.MapMatching) MatchResult(com.graphhopper.matching.MatchResult) Gpx(com.graphhopper.jackson.Gpx) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 3 with Gpx

use of com.graphhopper.jackson.Gpx in project graphhopper by graphhopper.

the class MapMatchingTest method testUTurns.

/**
 * This test is to check that U-turns are avoided when it's just measurement
 * error, though do occur when a point goes up a road further than the
 * measurement error. GPX input:
 * https://graphhopper.com/maps/?point=51.343618%2C12.360772&point=51.34401%2C12.361776&point=51.343977%2C12.362886&point=51.344734%2C12.36236&point=51.345233%2C12.362055&layer=Lyrk
 */
@ParameterizedTest
@ArgumentsSource(FixtureProvider.class)
public void testUTurns(PMap hints) throws IOException {
    hints = new PMap(hints).putObject(Parameters.Routing.HEADING_PENALTY, 50);
    MapMatching mapMatching = new MapMatching(graphHopper, hints);
    Gpx gpx = xmlMapper.readValue(getClass().getResourceAsStream("/tour4-with-uturn.gpx"), Gpx.class);
    // with large measurement error, we expect no U-turn
    mapMatching.setMeasurementErrorSigma(50);
    MatchResult mr = mapMatching.match(GpxConversions.getEntries(gpx.trk.get(0)));
    assertEquals(Arrays.asList("Gustav-Adolf-Straße", "Funkenburgstraße"), fetchStreets(mr.getEdgeMatches()));
    // with small measurement error, we expect the U-turn
    mapMatching.setMeasurementErrorSigma(10);
    mr = mapMatching.match(GpxConversions.getEntries(gpx.trk.get(0)));
    assertEquals(Arrays.asList("Gustav-Adolf-Straße", "Funkenburgstraße"), fetchStreets(mr.getEdgeMatches()));
}
Also used : MapMatching(com.graphhopper.matching.MapMatching) PMap(com.graphhopper.util.PMap) MatchResult(com.graphhopper.matching.MatchResult) Gpx(com.graphhopper.jackson.Gpx) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 4 with Gpx

use of com.graphhopper.jackson.Gpx in project graphhopper by graphhopper.

the class TrkTest method testNoTrk.

@Test
public void testNoTrk() throws IOException {
    Gpx gpx = xmlMapper.readValue(getClass().getResourceAsStream("/no_trk.gpx"), Gpx.class);
    assertTrue(gpx.trk.isEmpty());
}
Also used : Gpx(com.graphhopper.jackson.Gpx) Test(org.junit.jupiter.api.Test)

Example 5 with Gpx

use of com.graphhopper.jackson.Gpx in project graphhopper by graphhopper.

the class TrkTest method test1.

@Test
public void test1() throws IOException {
    Gpx gpx = xmlMapper.readValue(getClass().getResourceAsStream("/test1.gpx"), Gpx.class);
    List<Observation> gpxEntries = GpxConversions.getEntries(gpx.trk.get(0));
    assertEquals(264, gpxEntries.size());
    assertEquals(new Observation(new GHPoint(51.377719, 12.338217)), gpxEntries.get(0));
    assertEquals(new Observation(new GHPoint(51.371482, 12.363795)), gpxEntries.get(50));
}
Also used : Observation(com.graphhopper.matching.Observation) GHPoint(com.graphhopper.util.shapes.GHPoint) Gpx(com.graphhopper.jackson.Gpx) Test(org.junit.jupiter.api.Test)

Aggregations

Gpx (com.graphhopper.jackson.Gpx)14 Test (org.junit.jupiter.api.Test)9 MapMatching (com.graphhopper.matching.MapMatching)8 MatchResult (com.graphhopper.matching.MatchResult)8 GraphHopper (com.graphhopper.GraphHopper)4 Observation (com.graphhopper.matching.Observation)4 PMap (com.graphhopper.util.PMap)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)4 LMProfile (com.graphhopper.config.LMProfile)3 Profile (com.graphhopper.config.Profile)3 EdgeMatch (com.graphhopper.matching.EdgeMatch)3 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 GraphHopperConfig (com.graphhopper.GraphHopperConfig)1 ResponsePath (com.graphhopper.ResponsePath)1 GHPoint (com.graphhopper.util.shapes.GHPoint)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1