Search in sources :

Example 16 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class ResponsePathDeserializer method createResponsePath.

public static ResponsePath createResponsePath(ObjectMapper objectMapper, JsonNode path, boolean hasElevation, boolean turnDescription) {
    ResponsePath responsePath = new ResponsePath();
    responsePath.addErrors(readErrors(objectMapper, path));
    if (responsePath.hasErrors())
        return responsePath;
    if (path.has("snapped_waypoints")) {
        JsonNode snappedWaypoints = path.get("snapped_waypoints");
        PointList snappedPoints = deserializePointList(objectMapper, snappedWaypoints, hasElevation);
        responsePath.setWaypoints(snappedPoints);
    }
    if (path.has("ascend")) {
        responsePath.setAscend(path.get("ascend").asDouble());
    }
    if (path.has("descend")) {
        responsePath.setDescend(path.get("descend").asDouble());
    }
    if (path.has("weight")) {
        responsePath.setRouteWeight(path.get("weight").asDouble());
    }
    if (path.has("description")) {
        JsonNode descriptionNode = path.get("description");
        if (descriptionNode.isArray()) {
            List<String> description = new ArrayList<>(descriptionNode.size());
            for (JsonNode descNode : descriptionNode) {
                description.add(descNode.asText());
            }
            responsePath.setDescription(description);
        } else {
            throw new IllegalStateException("Description has to be an array");
        }
    }
    if (path.has("points")) {
        final PointList pointList = deserializePointList(objectMapper, path.get("points"), hasElevation);
        responsePath.setPoints(pointList);
        if (path.has("instructions")) {
            JsonNode instrArr = path.get("instructions");
            InstructionList il = new InstructionList(null);
            int viaCount = 1;
            for (JsonNode jsonObj : instrArr) {
                double instDist = jsonObj.get("distance").asDouble();
                String text = turnDescription ? jsonObj.get("text").asText() : jsonObj.get("street_name").asText();
                long instTime = jsonObj.get("time").asLong();
                int sign = jsonObj.get("sign").asInt();
                JsonNode iv = jsonObj.get("interval");
                int from = iv.get(0).asInt();
                int to = iv.get(1).asInt();
                PointList instPL = new PointList(to - from, hasElevation);
                for (int j = from; j <= to; j++) {
                    instPL.add(pointList, j);
                }
                Instruction instr;
                if (sign == Instruction.USE_ROUNDABOUT || sign == Instruction.LEAVE_ROUNDABOUT) {
                    RoundaboutInstruction ri = new RoundaboutInstruction(sign, text, instPL);
                    if (jsonObj.has("exit_number")) {
                        ri.setExitNumber(jsonObj.get("exit_number").asInt());
                    }
                    if (jsonObj.has("exited")) {
                        if (jsonObj.get("exited").asBoolean())
                            ri.setExited();
                    }
                    if (jsonObj.has("turn_angle")) {
                        // TODO provide setTurnAngle setter
                        double angle = jsonObj.get("turn_angle").asDouble();
                        ri.setDirOfRotation(angle);
                        ri.setRadian((angle < 0 ? -Math.PI : Math.PI) - angle);
                    }
                    instr = ri;
                } else if (sign == Instruction.REACHED_VIA) {
                    ViaInstruction tmpInstr = new ViaInstruction(text, instPL);
                    tmpInstr.setViaCount(viaCount);
                    viaCount++;
                    instr = tmpInstr;
                } else if (sign == Instruction.FINISH) {
                    instr = new FinishInstruction(text, instPL, 0);
                } else {
                    instr = new Instruction(sign, text, instPL);
                    if (sign == Instruction.CONTINUE_ON_STREET) {
                        if (jsonObj.has("heading")) {
                            instr.setExtraInfo("heading", jsonObj.get("heading").asDouble());
                        }
                    }
                }
                // This can be changed by passing <code>turn_description=false</code>.
                if (turnDescription)
                    instr.setUseRawName();
                instr.setDistance(instDist).setTime(instTime);
                il.add(instr);
            }
            responsePath.setInstructions(il);
        }
        if (path.has("details")) {
            JsonNode details = path.get("details");
            Map<String, List<PathDetail>> pathDetails = new HashMap<>(details.size());
            Iterator<Map.Entry<String, JsonNode>> detailIterator = details.fields();
            while (detailIterator.hasNext()) {
                Map.Entry<String, JsonNode> detailEntry = detailIterator.next();
                List<PathDetail> pathDetailList = new ArrayList<>();
                for (JsonNode pathDetail : detailEntry.getValue()) {
                    PathDetail pd = objectMapper.convertValue(pathDetail, PathDetail.class);
                    pathDetailList.add(pd);
                }
                pathDetails.put(detailEntry.getKey(), pathDetailList);
            }
            responsePath.addPathDetails(pathDetails);
        }
    }
    if (path.has("points_order")) {
        responsePath.setPointsOrder((List<Integer>) objectMapper.convertValue(path.get("points_order"), List.class));
    } else {
        List<Integer> list = new ArrayList<>(responsePath.getWaypoints().size());
        for (int i = 0; i < responsePath.getWaypoints().size(); i++) {
            list.add(i);
        }
        responsePath.setPointsOrder(list);
    }
    double distance = path.get("distance").asDouble();
    long time = path.get("time").asLong();
    responsePath.setDistance(distance).setTime(time);
    return responsePath;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) LineString(org.locationtech.jts.geom.LineString) ResponsePath(com.graphhopper.ResponsePath) PathDetail(com.graphhopper.util.details.PathDetail)

Example 17 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class ResponsePath method merge.

/**
 * Merges <code>otherDetails</code> into the <code>pathDetails</code>.
 * <p>
 * This method makes sure that Entry list around via points are merged correctly.
 * See #1091 and the misplaced PathDetail after waypoints.
 */
public static void merge(List<PathDetail> pathDetails, List<PathDetail> otherDetails) {
    // Make sure that the PathDetail list is merged correctly at via points
    if (!pathDetails.isEmpty() && !otherDetails.isEmpty()) {
        PathDetail lastDetail = pathDetails.get(pathDetails.size() - 1);
        boolean extend = lastDetail.getValue() != null ? lastDetail.getValue().equals(otherDetails.get(0).getValue()) : otherDetails.get(0).getValue() != null;
        if (extend) {
            lastDetail.setLast(otherDetails.get(0).getLast());
            otherDetails.remove(0);
        }
    }
    pathDetails.addAll(otherDetails);
}
Also used : PathDetail(com.graphhopper.util.details.PathDetail)

Example 18 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class PathTest method testCalcDistanceDetails.

@Test
public void testCalcDistanceDetails() {
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    Path p = new Dijkstra(pathDetailGraph, weighting, TraversalMode.NODE_BASED).calcPath(1, 5);
    assertTrue(p.isFound());
    Map<String, List<PathDetail>> details = PathDetailsFromEdges.calcDetails(p, carManager, weighting, Arrays.asList(DISTANCE), new PathDetailsBuilderFactory(), 0);
    assertTrue(details.size() == 1);
    List<PathDetail> distanceDetails = details.get(DISTANCE);
    assertEquals(5D, distanceDetails.get(0).getValue());
    assertEquals(5D, distanceDetails.get(1).getValue());
    assertEquals(10D, distanceDetails.get(2).getValue());
    assertEquals(5D, distanceDetails.get(3).getValue());
}
Also used : PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) PathDetail(com.graphhopper.util.details.PathDetail) AbstractGraphStorageTester.assertPList(com.graphhopper.storage.AbstractGraphStorageTester.assertPList) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.jupiter.api.Test)

Example 19 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class PathTest method testCalcEdgeKeyDetailsForward.

@Test
public void testCalcEdgeKeyDetailsForward() {
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    Path p = new Dijkstra(pathDetailGraph, weighting, TraversalMode.NODE_BASED).calcPath(1, 5);
    assertTrue(p.isFound());
    Map<String, List<PathDetail>> details = PathDetailsFromEdges.calcDetails(p, carManager, weighting, Arrays.asList(EDGE_KEY), new PathDetailsBuilderFactory(), 0);
    List<PathDetail> edgeKeyDetails = details.get(EDGE_KEY);
    assertEquals(4, edgeKeyDetails.size());
    assertEquals(0, edgeKeyDetails.get(0).getValue());
    assertEquals(4, edgeKeyDetails.get(1).getValue());
    assertEquals(6, edgeKeyDetails.get(2).getValue());
    assertEquals(2, edgeKeyDetails.get(3).getValue());
}
Also used : PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) PathDetail(com.graphhopper.util.details.PathDetail) AbstractGraphStorageTester.assertPList(com.graphhopper.storage.AbstractGraphStorageTester.assertPList) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.jupiter.api.Test)

Example 20 with PathDetail

use of com.graphhopper.util.details.PathDetail in project graphhopper by graphhopper.

the class PathTest method testCalcAverageSpeedDetails.

@Test
public void testCalcAverageSpeedDetails() {
    ShortestWeighting weighting = new ShortestWeighting(encoder);
    Path p = new Dijkstra(pathDetailGraph, weighting, TraversalMode.NODE_BASED).calcPath(1, 5);
    assertTrue(p.isFound());
    Map<String, List<PathDetail>> details = PathDetailsFromEdges.calcDetails(p, carManager, weighting, Arrays.asList(AVERAGE_SPEED), new PathDetailsBuilderFactory(), 0);
    assertTrue(details.size() == 1);
    List<PathDetail> averageSpeedDetails = details.get(AVERAGE_SPEED);
    assertEquals(4, averageSpeedDetails.size());
    assertEquals(45.0, averageSpeedDetails.get(0).getValue());
    assertEquals(90.0, averageSpeedDetails.get(1).getValue());
    assertEquals(10.0, averageSpeedDetails.get(2).getValue());
    assertEquals(45.0, averageSpeedDetails.get(3).getValue());
    assertEquals(0, averageSpeedDetails.get(0).getFirst());
    assertEquals(1, averageSpeedDetails.get(1).getFirst());
    assertEquals(2, averageSpeedDetails.get(2).getFirst());
    assertEquals(3, averageSpeedDetails.get(3).getFirst());
    assertEquals(4, averageSpeedDetails.get(3).getLast());
}
Also used : PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) PathDetail(com.graphhopper.util.details.PathDetail) AbstractGraphStorageTester.assertPList(com.graphhopper.storage.AbstractGraphStorageTester.assertPList) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) Test(org.junit.jupiter.api.Test)

Aggregations

PathDetail (com.graphhopper.util.details.PathDetail)30 Test (org.junit.jupiter.api.Test)14 PathDetailsBuilderFactory (com.graphhopper.util.details.PathDetailsBuilderFactory)10 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)8 AbstractGraphStorageTester.assertPList (com.graphhopper.storage.AbstractGraphStorageTester.assertPList)8 GHPoint (com.graphhopper.util.shapes.GHPoint)8 GHRequest (com.graphhopper.GHRequest)7 GHResponse (com.graphhopper.GHResponse)7 ResponsePath (com.graphhopper.ResponsePath)5 List (java.util.List)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 GraphHopperWeb (com.graphhopper.api.GraphHopperWeb)3 PathDetailsFromEdges (com.graphhopper.util.details.PathDetailsFromEdges)3 GTFSFeed (com.conveyal.gtfs.GTFSFeed)2 Stop (com.conveyal.gtfs.model.Stop)2 StopTime (com.conveyal.gtfs.model.StopTime)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Iterables (com.google.common.collect.Iterables)2 Lists (com.google.common.collect.Lists)2 Maps (com.google.common.collect.Maps)2