Search in sources :

Example 6 with PathDetail

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

the class PathTest method testCalcEdgeIdDetails.

@Test
public void testCalcEdgeIdDetails() {
    Path p = new Dijkstra(pathDetailGraph, new ShortestWeighting(encoder), TraversalMode.NODE_BASED).calcPath(1, 5);
    assertTrue(p.isFound());
    Map<String, List<PathDetail>> details = p.calcDetails(Arrays.asList(new String[] { EDGE_ID }), new PathDetailsBuilderFactory(), 0);
    assertTrue(details.size() == 1);
    List<PathDetail> edgeIdDetails = details.get(EDGE_ID);
    assertEquals(4, edgeIdDetails.size());
    assertEquals(0, edgeIdDetails.get(0).getValue());
    // This is out of order because we don't create the edges in order
    assertEquals(2, edgeIdDetails.get(1).getValue());
    assertEquals(3, edgeIdDetails.get(2).getValue());
    assertEquals(1, edgeIdDetails.get(3).getValue());
    assertEquals(0, edgeIdDetails.get(0).getFirst());
    assertEquals(1, edgeIdDetails.get(1).getFirst());
    assertEquals(2, edgeIdDetails.get(2).getFirst());
    assertEquals(3, edgeIdDetails.get(3).getFirst());
    assertEquals(4, edgeIdDetails.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.Test)

Example 7 with PathDetail

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

the class GraphHopperServletIT method testPathDetails.

@Test
public void testPathDetails() throws Exception {
    GraphHopperAPI hopper = new com.graphhopper.api.GraphHopperWeb();
    assertTrue(hopper.load(getTestRouteAPIUrl()));
    GHRequest request = new GHRequest(42.554851, 1.536198, 42.510071, 1.548128);
    request.setPathDetails(Arrays.asList("average_speed", "edge_id", "time"));
    GHResponse rsp = hopper.route(request);
    assertFalse(rsp.getErrors().toString(), rsp.hasErrors());
    assertTrue(rsp.getErrors().toString(), rsp.getErrors().isEmpty());
    Map<String, List<PathDetail>> pathDetails = rsp.getBest().getPathDetails();
    assertFalse(pathDetails.isEmpty());
    assertTrue(pathDetails.containsKey("average_speed"));
    assertTrue(pathDetails.containsKey("edge_id"));
    assertTrue(pathDetails.containsKey("time"));
    List<PathDetail> averageSpeedList = pathDetails.get("average_speed");
    assertEquals(9, averageSpeedList.size());
    assertEquals(30.0, averageSpeedList.get(0).getValue());
    assertEquals(14, averageSpeedList.get(0).getLength());
    assertEquals(60.0, averageSpeedList.get(1).getValue());
    assertEquals(5, averageSpeedList.get(1).getLength());
    List<PathDetail> edgeIdDetails = pathDetails.get("edge_id");
    assertEquals(77, edgeIdDetails.size());
    assertEquals(3759L, edgeIdDetails.get(0).getValue());
    assertEquals(2, edgeIdDetails.get(0).getLength());
    assertEquals(881L, edgeIdDetails.get(1).getValue());
    assertEquals(8, edgeIdDetails.get(1).getLength());
    long expectedTime = rsp.getBest().getTime();
    long actualTime = 0;
    List<PathDetail> timeDetails = pathDetails.get("time");
    for (PathDetail pd : timeDetails) {
        actualTime += (Long) pd.getValue();
    }
    assertEquals(expectedTime, actualTime);
}
Also used : GraphHopperAPI(com.graphhopper.GraphHopperAPI) PathDetail(com.graphhopper.util.details.PathDetail) GHRequest(com.graphhopper.GHRequest) List(java.util.List) GHResponse(com.graphhopper.GHResponse) Test(org.junit.Test)

Example 8 with PathDetail

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

the class GraphHopperWeb method createPathWrapper.

PathWrapper createPathWrapper(JsonNode path, boolean tmpCalcPoints, boolean tmpInstructions, boolean tmpElevation, boolean turnDescription, boolean tmpCalcDetails) {
    PathWrapper pathWrapper = new PathWrapper();
    pathWrapper.addErrors(readErrors(path));
    if (pathWrapper.hasErrors())
        return pathWrapper;
    if (path.has("snapped_waypoints")) {
        String snappedPointStr = path.get("snapped_waypoints").asText();
        PointList snappedPoints = WebHelper.decodePolyline(snappedPointStr, 5, tmpElevation);
        pathWrapper.setWaypoints(snappedPoints);
    }
    if (path.has("ascend")) {
        pathWrapper.setAscend(path.get("ascend").asDouble());
    }
    if (path.has("descend")) {
        pathWrapper.setDescend(path.get("descend").asDouble());
    }
    if (path.has("weight")) {
        pathWrapper.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());
            }
            pathWrapper.setDescription(description);
        } else {
            throw new IllegalStateException("Description has to be an array");
        }
    }
    if (tmpCalcPoints) {
        String pointStr = path.get("points").asText();
        PointList pointList = WebHelper.decodePolyline(pointStr, 100, tmpElevation);
        pathWrapper.setPoints(pointList);
        if (tmpInstructions) {
            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, tmpElevation);
                for (int j = from; j <= to; j++) {
                    instPL.add(pointList, j);
                }
                InstructionAnnotation ia = InstructionAnnotation.EMPTY;
                if (jsonObj.has("annotation_importance") && jsonObj.has("annotation_text")) {
                    ia = new InstructionAnnotation(jsonObj.get("annotation_importance").asInt(), jsonObj.get("annotation_text").asText());
                }
                Instruction instr;
                if (sign == Instruction.USE_ROUNDABOUT || sign == Instruction.LEAVE_ROUNDABOUT) {
                    RoundaboutInstruction ri = new RoundaboutInstruction(sign, text, ia, 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, ia, instPL);
                    tmpInstr.setViaCount(viaCount);
                    viaCount++;
                    instr = tmpInstr;
                } else if (sign == Instruction.FINISH) {
                    instr = new FinishInstruction(text, instPL, 0);
                } else {
                    instr = new Instruction(sign, text, ia, 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);
            }
            pathWrapper.setInstructions(il);
        }
        if (tmpCalcDetails) {
            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<>();
                // see issue #1137
                for (JsonNode pathDetail : detailEntry.getValue()) {
                    if (pathDetail.size() != 3)
                        throw new IllegalStateException("PathDetail must have exactly 3 entries but was " + pathDetail.size());
                    JsonNode from = pathDetail.get(0);
                    JsonNode to = pathDetail.get(1);
                    JsonNode val = pathDetail.get(2);
                    PathDetail pd;
                    if (val.isBoolean())
                        pd = new PathDetail(val.asBoolean());
                    else if (val.isLong())
                        pd = new PathDetail(val.asLong());
                    else if (val.isInt())
                        pd = new PathDetail(val.asInt());
                    else if (val.isDouble())
                        pd = new PathDetail(val.asDouble());
                    else if (val.isTextual())
                        pd = new PathDetail(val.asText());
                    else
                        throw new IllegalStateException("Unsupported type of PathDetail value for key " + detailEntry.getKey() + " and value " + val.toString() + "of class " + val.getClass());
                    pd.setFirst(from.asInt());
                    pd.setLast(to.asInt());
                    pathDetailList.add(pd);
                }
                pathDetails.put(detailEntry.getKey(), pathDetailList);
            }
            pathWrapper.addPathDetails(pathDetails);
        }
    }
    double distance = path.get("distance").asDouble();
    long time = path.get("time").asLong();
    pathWrapper.setDistance(distance).setTime(time);
    return pathWrapper;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) PathWrapper(com.graphhopper.PathWrapper) GHPoint(com.graphhopper.util.shapes.GHPoint) PathDetail(com.graphhopper.util.details.PathDetail)

Example 9 with PathDetail

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

the class GraphHopperIT method testMonacoPathDetails.

@Test
public void testMonacoPathDetails() {
    GHRequest request = new GHRequest();
    request.addPoint(new GHPoint(43.727687, 7.418737));
    request.addPoint(new GHPoint(43.74958, 7.436566));
    request.addPoint(new GHPoint(43.727687, 7.418737));
    request.setAlgorithm(ASTAR).setVehicle(vehicle).setWeighting(weightCalcStr);
    request.setPathDetails(Arrays.asList(Parameters.DETAILS.AVERAGE_SPEED));
    GHResponse rsp = hopper.route(request);
    PathWrapper arsp = rsp.getBest();
    Map<String, List<PathDetail>> details = arsp.getPathDetails();
    assertTrue(details.size() == 1);
    List<PathDetail> detailList = details.get(Parameters.DETAILS.AVERAGE_SPEED);
    assertEquals(1, detailList.size());
    assertEquals(5.0, detailList.get(0).getValue());
    assertEquals(0, detailList.get(0).getFirst());
    assertEquals(arsp.getPoints().size() - 1, detailList.get(0).getLast());
}
Also used : PathDetail(com.graphhopper.util.details.PathDetail) ArrayList(java.util.ArrayList) List(java.util.List) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 10 with PathDetail

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

the class PathTest method testCalcStreetNameDetails.

@Test
public void testCalcStreetNameDetails() {
    Path p = new Dijkstra(pathDetailGraph, new ShortestWeighting(encoder), TraversalMode.NODE_BASED).calcPath(1, 5);
    assertTrue(p.isFound());
    Map<String, List<PathDetail>> details = p.calcDetails(Arrays.asList(new String[] { STREET_NAME }), new PathDetailsBuilderFactory(), 0);
    assertTrue(details.size() == 1);
    List<PathDetail> streetNameDetails = details.get(STREET_NAME);
    assertTrue(details.size() == 1);
    assertEquals(4, streetNameDetails.size());
    assertEquals("1-2", streetNameDetails.get(0).getValue());
    assertEquals("2-3", streetNameDetails.get(1).getValue());
    assertEquals("3-4", streetNameDetails.get(2).getValue());
    assertEquals("4-5", streetNameDetails.get(3).getValue());
    assertEquals(0, streetNameDetails.get(0).getFirst());
    assertEquals(1, streetNameDetails.get(1).getFirst());
    assertEquals(2, streetNameDetails.get(2).getFirst());
    assertEquals(3, streetNameDetails.get(3).getFirst());
    assertEquals(4, streetNameDetails.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.Test)

Aggregations

PathDetail (com.graphhopper.util.details.PathDetail)11 Test (org.junit.Test)6 ShortestWeighting (com.graphhopper.routing.weighting.ShortestWeighting)4 AbstractGraphStorageTester.assertPList (com.graphhopper.storage.AbstractGraphStorageTester.assertPList)4 PathDetailsBuilderFactory (com.graphhopper.util.details.PathDetailsBuilderFactory)4 GHPoint (com.graphhopper.util.shapes.GHPoint)3 List (java.util.List)3 GHRequest (com.graphhopper.GHRequest)2 GHResponse (com.graphhopper.GHResponse)2 PathWrapper (com.graphhopper.PathWrapper)2 ArrayList (java.util.ArrayList)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 GraphHopperAPI (com.graphhopper.GraphHopperAPI)1 GHIntArrayList (com.graphhopper.coll.GHIntArrayList)1 PathDetailsBuilder (com.graphhopper.util.details.PathDetailsBuilder)1 PathDetailsFromEdges (com.graphhopper.util.details.PathDetailsFromEdges)1 Map (java.util.Map)1