Search in sources :

Example 11 with ResponsePath

use of com.graphhopper.ResponsePath 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 12 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class MapMatchingTest method testDistantPoints.

/**
 * This test is to check behavior over large separated routes: it should
 * work if the user sets the maxVisitedNodes large enough. Input path:
 * https://graphhopper.com/maps/?point=51.23%2C12.18&point=51.45%2C12.59&layer=Lyrk
 * <p>
 * Update: Seems to me that this test only tests a long route, not one with
 * distant input points. createRandomGPXEntries currently creates very close input points.
 * The length of the route doesn't seem to matter.
 */
@ParameterizedTest
@ArgumentsSource(FixtureProvider.class)
public void testDistantPoints(PMap hints) {
    // OK with 1000 visited nodes:
    MapMatching mapMatching = new MapMatching(graphHopper, hints);
    ResponsePath route = graphHopper.route(new GHRequest(new GHPoint(51.23, 12.18), new GHPoint(51.45, 12.59)).setProfile("my_profile")).getBest();
    List<Observation> inputGPXEntries = createRandomGPXEntriesAlongRoute(route);
    MatchResult mr = mapMatching.match(inputGPXEntries);
    assertEquals(route.getDistance(), mr.getMatchLength(), 2);
    // GraphHopper travel times aren't exactly additive
    assertThat(Math.abs(route.getTime() - mr.getMatchMillis()), is(lessThan(1000L)));
    // not OK when we only allow a small number of visited nodes:
    PMap opts = new PMap(hints).putObject(Parameters.Routing.MAX_VISITED_NODES, 1);
    mapMatching = new MapMatching(graphHopper, opts);
    try {
        mr = mapMatching.match(inputGPXEntries);
        fail("Expected sequence to be broken due to maxVisitedNodes being too small");
    } catch (RuntimeException e) {
        assertTrue(e.getMessage().startsWith("Sequence is broken for submitted track"));
    }
}
Also used : MapMatching(com.graphhopper.matching.MapMatching) ResponsePath(com.graphhopper.ResponsePath) GHRequest(com.graphhopper.GHRequest) Observation(com.graphhopper.matching.Observation) PMap(com.graphhopper.util.PMap) GHPoint(com.graphhopper.util.shapes.GHPoint) MatchResult(com.graphhopper.matching.MatchResult) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 13 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class MapMatchingTest method testDoWork.

/**
 * TODO: split this test up into smaller units with better names?
 */
@ParameterizedTest
@ArgumentsSource(FixtureProvider.class)
public void testDoWork(PMap hints) {
    MapMatching mapMatching = new MapMatching(graphHopper, hints);
    ResponsePath route2 = graphHopper.route(new GHRequest(new GHPoint(51.358735, 12.360574), new GHPoint(51.358594, 12.360032)).setProfile("my_profile")).getBest();
    List<Observation> inputGPXEntries = createRandomGPXEntriesAlongRoute(route2);
    MatchResult mr = mapMatching.match(inputGPXEntries);
    // make sure no virtual edges are returned
    int edgeCount = graphHopper.getGraphHopperStorage().getAllEdges().length();
    for (EdgeMatch em : mr.getEdgeMatches()) {
        assertTrue(em.getEdgeState().getEdge() < edgeCount, "result contains virtual edges:" + em.getEdgeState().toString());
    }
    // create street names
    assertEquals(Arrays.asList("Platnerstraße"), fetchStreets(mr.getEdgeMatches()));
    assertEquals(mr.getGpxEntriesLength(), mr.getMatchLength(), 1.5);
    ResponsePath route1 = graphHopper.route(new GHRequest(new GHPoint(51.33099, 12.380267), new GHPoint(51.330531, 12.380396)).setProfile("my_profile")).getBest();
    inputGPXEntries = createRandomGPXEntriesAlongRoute(route1);
    mapMatching.setMeasurementErrorSigma(5);
    mr = mapMatching.match(inputGPXEntries);
    assertEquals(Arrays.asList("Windmühlenstraße", "Bayrischer Platz"), fetchStreets(mr.getEdgeMatches()));
    assertEquals(mr.getGpxEntriesLength(), mr.getMatchLength(), .1);
    ResponsePath route = graphHopper.route(new GHRequest(new GHPoint(51.377781, 12.338333), new GHPoint(51.323317, 12.387085)).setProfile("my_profile")).getBest();
    inputGPXEntries = createRandomGPXEntriesAlongRoute(route);
    mapMatching = new MapMatching(graphHopper, hints);
    mapMatching.setMeasurementErrorSigma(20);
    mr = mapMatching.match(inputGPXEntries);
    assertEquals(route.getDistance(), mr.getMatchLength(), 0.5);
    // GraphHopper travel times aren't exactly additive
    assertThat(Math.abs(route.getTime() - mr.getMatchMillis()), is(lessThan(1000L)));
    assertEquals(142, mr.getEdgeMatches().size());
}
Also used : MapMatching(com.graphhopper.matching.MapMatching) ResponsePath(com.graphhopper.ResponsePath) EdgeMatch(com.graphhopper.matching.EdgeMatch) GHRequest(com.graphhopper.GHRequest) Observation(com.graphhopper.matching.Observation) GHPoint(com.graphhopper.util.shapes.GHPoint) MatchResult(com.graphhopper.matching.MatchResult) GHPoint(com.graphhopper.util.shapes.GHPoint) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 14 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method testNoPoints.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void testNoPoints(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.509225, 1.534728)).addPoint(new GHPoint(42.512602, 1.551558)).putHint("vehicle", "car");
    req.putHint("instructions", false);
    req.putHint("calc_points", false);
    GHResponse rsp = gh.route(req);
    assertFalse(rsp.hasErrors(), "errors:" + rsp.getErrors().toString());
    ResponsePath res = rsp.getBest();
    assertEquals(0, res.getPoints().size());
    isBetween(1750, 1800, res.getDistance());
}
Also used : ResponsePath(com.graphhopper.ResponsePath) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with ResponsePath

use of com.graphhopper.ResponsePath in project graphhopper by graphhopper.

the class RouteResourceClientHCTest method testAlternativeRoute.

@ParameterizedTest
@EnumSource(value = TestParam.class)
public void testAlternativeRoute(TestParam p) {
    GraphHopperWeb gh = createGH(p);
    GHRequest req = new GHRequest().addPoint(new GHPoint(42.505041, 1.521864)).addPoint(new GHPoint(42.509074, 1.537936)).putHint("vehicle", "car").setAlgorithm("alternative_route").putHint("instructions", true).putHint("calc_points", true).putHint("ch.disable", true);
    GHResponse res = gh.route(req);
    assertFalse(res.hasErrors(), "errors:" + res.getErrors().toString());
    List<ResponsePath> paths = res.getAll();
    assertEquals(2, paths.size());
    ResponsePath path = paths.get(0);
    assertEquals(35, path.getPoints().size());
    assertEquals(1689, path.getDistance(), 1);
    assertTrue(path.getInstructions().toString().contains("Avinguda de Tarragona"), path.getInstructions().toString());
    path = paths.get(1);
    assertEquals(30, path.getPoints().size());
    assertEquals(1759, path.getDistance(), 1);
    assertTrue(path.getInstructions().toString().contains("Avinguda Prat de la Creu"), path.getInstructions().toString());
}
Also used : ResponsePath(com.graphhopper.ResponsePath) GHRequest(com.graphhopper.GHRequest) GraphHopperWeb(com.graphhopper.api.GraphHopperWeb) GHPoint(com.graphhopper.util.shapes.GHPoint) GHResponse(com.graphhopper.GHResponse) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ResponsePath (com.graphhopper.ResponsePath)29 GHResponse (com.graphhopper.GHResponse)15 GHRequest (com.graphhopper.GHRequest)13 GHPoint (com.graphhopper.util.shapes.GHPoint)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 Test (org.junit.jupiter.api.Test)7 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MapMatching (com.graphhopper.matching.MapMatching)4 MatchResult (com.graphhopper.matching.MatchResult)4 Observation (com.graphhopper.matching.Observation)4 EnumSource (org.junit.jupiter.params.provider.EnumSource)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Path (com.graphhopper.routing.Path)3 QueryGraph (com.graphhopper.routing.querygraph.QueryGraph)3 Snap (com.graphhopper.storage.index.Snap)3 PathDetail (com.graphhopper.util.details.PathDetail)3 ArgumentsSource (org.junit.jupiter.params.provider.ArgumentsSource)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2