Search in sources :

Example 11 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class LocationIndexTreeCHTest method testCHGraph.

@Test
public void testCHGraph() {
    GraphHopperStorage ghStorage = createGHStorage(new RAMDirectory(), encodingManager, false);
    CHGraph lg = ghStorage.getGraph(CHGraph.class);
    // 0
    // 1
    // 2
    //  3
    //   4
    NodeAccess na = ghStorage.getNodeAccess();
    na.setNode(0, 1, 0);
    na.setNode(1, 0.5, 0);
    na.setNode(2, 0, 0);
    na.setNode(3, -1, 1);
    na.setNode(4, -2, 2);
    EdgeIteratorState iter1 = ghStorage.edge(0, 1, 10, true);
    EdgeIteratorState iter2 = ghStorage.edge(1, 2, 10, true);
    EdgeIteratorState iter3 = ghStorage.edge(2, 3, 14, true);
    EdgeIteratorState iter4 = ghStorage.edge(3, 4, 14, true);
    // create shortcuts
    ghStorage.freeze();
    FlagEncoder car = encodingManager.getEncoder("car");
    long flags = car.setProperties(60, true, true);
    CHEdgeIteratorState iter5 = lg.shortcut(0, 2);
    iter5.setDistance(20).setFlags(flags);
    iter5.setSkippedEdges(iter1.getEdge(), iter2.getEdge());
    CHEdgeIteratorState iter6 = lg.shortcut(2, 4);
    iter6.setDistance(28).setFlags(flags);
    iter6.setSkippedEdges(iter3.getEdge(), iter4.getEdge());
    CHEdgeIteratorState tmp = lg.shortcut(0, 4);
    tmp.setDistance(40).setFlags(flags);
    tmp.setSkippedEdges(iter5.getEdge(), iter6.getEdge());
    LocationIndex index = createIndex(ghStorage, -1);
    assertEquals(2, findID(index, 0, 0.5));
}
Also used : CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) EdgeIteratorState(com.graphhopper.util.EdgeIteratorState) CHEdgeIteratorState(com.graphhopper.util.CHEdgeIteratorState) Test(org.junit.Test)

Example 12 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class DepthFirstSearchTest method testDFS2.

@Test
public void testDFS2() {
    DepthFirstSearch dfs = new DepthFirstSearch() {

        @Override
        public boolean goFurther(int v) {
            counter++;
            assertTrue("v " + v + " is already contained in set. iteration:" + counter, !set.contains(v));
            set.add(v);
            list.add(v);
            return super.goFurther(v);
        }
    };
    EncodingManager em = new EncodingManager("car");
    FlagEncoder fe = em.getEncoder("car");
    Graph g = new GraphBuilder(em).create();
    g.edge(1, 2, 1, false);
    g.edge(1, 4, 1, true);
    g.edge(1, 3, 1, false);
    g.edge(2, 3, 1, false);
    g.edge(4, 3, 1, true);
    dfs.start(g.createEdgeExplorer(new DefaultEdgeFilter(fe, false, true)), 1);
    assertTrue(counter > 0);
    assertEquals("[1, 2, 3, 4]", list.toString());
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) Graph(com.graphhopper.storage.Graph) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GraphBuilder(com.graphhopper.storage.GraphBuilder) DefaultEdgeFilter(com.graphhopper.routing.util.DefaultEdgeFilter) Test(org.junit.Test)

Example 13 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class GraphHopperServlet method doGet.

@Override
public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
    List<GHPoint> requestPoints = getPoints(httpReq, "point");
    GHResponse ghRsp = new GHResponse();
    double minPathPrecision = getDoubleParam(httpReq, WAY_POINT_MAX_DISTANCE, 1d);
    boolean writeGPX = "gpx".equalsIgnoreCase(getParam(httpReq, "type", "json"));
    boolean enableInstructions = writeGPX || getBooleanParam(httpReq, INSTRUCTIONS, true);
    boolean calcPoints = getBooleanParam(httpReq, CALC_POINTS, true);
    boolean enableElevation = getBooleanParam(httpReq, "elevation", false);
    boolean pointsEncoded = getBooleanParam(httpReq, "points_encoded", true);
    String vehicleStr = getParam(httpReq, "vehicle", "car");
    String weighting = getParam(httpReq, "weighting", "fastest");
    String algoStr = getParam(httpReq, "algorithm", "");
    String localeStr = getParam(httpReq, "locale", "en");
    StopWatch sw = new StopWatch().start();
    if (!ghRsp.hasErrors()) {
        try {
            List<Double> favoredHeadings = Collections.EMPTY_LIST;
            try {
                favoredHeadings = getDoubleParamList(httpReq, "heading");
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("heading list in wrong format: " + e.getMessage());
            }
            if (!hopper.getEncodingManager().supports(vehicleStr)) {
                throw new IllegalArgumentException("Vehicle not supported: " + vehicleStr);
            } else if (enableElevation && !hopper.hasElevation()) {
                throw new IllegalArgumentException("Elevation not supported!");
            } else if (favoredHeadings.size() > 1 && favoredHeadings.size() != requestPoints.size()) {
                throw new IllegalArgumentException("The number of 'heading' parameters must be <= 1 " + "or equal to the number of points (" + requestPoints.size() + ")");
            }
            List<String> pointHints = new ArrayList<String>(Arrays.asList(getParams(httpReq, POINT_HINT)));
            if (pointHints.size() > 0 && pointHints.size() != requestPoints.size()) {
                throw new IllegalArgumentException("If you pass " + POINT_HINT + ", you need to pass a hint for every point, empty hints will be ignored");
            }
            FlagEncoder algoVehicle = hopper.getEncodingManager().getEncoder(vehicleStr);
            GHRequest request;
            if (favoredHeadings.size() > 0) {
                // if only one favored heading is specified take as start heading
                if (favoredHeadings.size() == 1) {
                    List<Double> paddedHeadings = new ArrayList<Double>(Collections.nCopies(requestPoints.size(), Double.NaN));
                    paddedHeadings.set(0, favoredHeadings.get(0));
                    request = new GHRequest(requestPoints, paddedHeadings);
                } else {
                    request = new GHRequest(requestPoints, favoredHeadings);
                }
            } else {
                request = new GHRequest(requestPoints);
            }
            initHints(request.getHints(), httpReq.getParameterMap());
            request.setVehicle(algoVehicle.toString()).setWeighting(weighting).setAlgorithm(algoStr).setLocale(localeStr).setPointHints(pointHints).getHints().put(CALC_POINTS, calcPoints).put(INSTRUCTIONS, enableInstructions).put(WAY_POINT_MAX_DISTANCE, minPathPrecision);
            ghRsp = hopper.route(request);
        } catch (IllegalArgumentException ex) {
            ghRsp.addError(ex);
        }
    }
    float took = sw.stop().getSeconds();
    String infoStr = httpReq.getRemoteAddr() + " " + httpReq.getLocale() + " " + httpReq.getHeader("User-Agent");
    String logStr = httpReq.getQueryString() + " " + infoStr + " " + requestPoints + ", took:" + took + ", " + algoStr + ", " + weighting + ", " + vehicleStr;
    httpRes.setHeader("X-GH-Took", "" + Math.round(took * 1000));
    int alternatives = ghRsp.getAll().size();
    if (writeGPX && alternatives > 1)
        ghRsp.addError(new IllegalArgumentException("Alternatives are currently not yet supported for GPX"));
    if (ghRsp.hasErrors()) {
        logger.error(logStr + ", errors:" + ghRsp.getErrors());
    } else {
        PathWrapper altRsp0 = ghRsp.getBest();
        logger.info(logStr + ", alternatives: " + alternatives + ", distance0: " + altRsp0.getDistance() + ", time0: " + Math.round(altRsp0.getTime() / 60000f) + "min" + ", points0: " + altRsp0.getPoints().getSize() + ", debugInfo: " + ghRsp.getDebugInfo());
    }
    if (writeGPX) {
        if (ghRsp.hasErrors()) {
            httpRes.setStatus(SC_BAD_REQUEST);
            httpRes.getWriter().append(errorsToXML(ghRsp.getErrors()));
        } else {
            // no error => we can now safely call getFirst
            String xml = createGPXString(httpReq, httpRes, ghRsp.getBest());
            writeResponse(httpRes, xml);
        }
    } else {
        Map<String, Object> map = routeSerializer.toJSON(ghRsp, calcPoints, pointsEncoded, enableElevation, enableInstructions);
        Object infoMap = map.get("info");
        if (infoMap != null)
            ((Map) infoMap).put("took", Math.round(took * 1000));
        if (ghRsp.hasErrors())
            writeJsonError(httpRes, SC_BAD_REQUEST, new JSONObject(map));
        else {
            writeJson(httpReq, httpRes, new JSONObject(map));
        }
    }
}
Also used : FlagEncoder(com.graphhopper.routing.util.FlagEncoder) GHResponse(com.graphhopper.GHResponse) GHPoint(com.graphhopper.util.shapes.GHPoint) StopWatch(com.graphhopper.util.StopWatch) PathWrapper(com.graphhopper.PathWrapper) JSONObject(org.json.JSONObject) GHRequest(com.graphhopper.GHRequest) JSONObject(org.json.JSONObject) GHPoint(com.graphhopper.util.shapes.GHPoint)

Example 14 with FlagEncoder

use of com.graphhopper.routing.util.FlagEncoder in project graphhopper by graphhopper.

the class LMAlgoFactoryDecoratorTest method addWeighting.

@Test
public void addWeighting() {
    LMAlgoFactoryDecorator dec = new LMAlgoFactoryDecorator().setEnabled(true);
    dec.addWeighting("fastest");
    assertEquals(Arrays.asList("fastest"), dec.getWeightingsAsStrings());
    // special parameters like the maximum weight
    dec = new LMAlgoFactoryDecorator().setEnabled(true);
    dec.addWeighting("fastest|maximum=65000");
    dec.addWeighting("shortest|maximum=20000");
    assertEquals(Arrays.asList("fastest", "shortest"), dec.getWeightingsAsStrings());
    FlagEncoder car = new CarFlagEncoder();
    EncodingManager em = new EncodingManager(car);
    dec.addWeighting(new FastestWeighting(car)).addWeighting(new ShortestWeighting(car));
    dec.createPreparations(new GraphHopperStorage(new RAMDirectory(), em, false, new GraphExtension.NoOpExtension()), TraversalMode.NODE_BASED, null);
    assertEquals(1, dec.getPreparations().get(0).getLandmarkStorage().getFactor(), .1);
    assertEquals(0.3, dec.getPreparations().get(1).getLandmarkStorage().getFactor(), .1);
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) FastestWeighting(com.graphhopper.routing.weighting.FastestWeighting) CarFlagEncoder(com.graphhopper.routing.util.CarFlagEncoder) ShortestWeighting(com.graphhopper.routing.weighting.ShortestWeighting) RAMDirectory(com.graphhopper.storage.RAMDirectory) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) Test(org.junit.Test)

Aggregations

FlagEncoder (com.graphhopper.routing.util.FlagEncoder)14 Test (org.junit.Test)8 EncodingManager (com.graphhopper.routing.util.EncodingManager)6 IntArrayList (com.carrotsearch.hppc.IntArrayList)4 GHIntArrayList (com.graphhopper.coll.GHIntArrayList)4 DefaultEdgeFilter (com.graphhopper.routing.util.DefaultEdgeFilter)3 GraphBuilder (com.graphhopper.storage.GraphBuilder)3 GraphHopperStorage (com.graphhopper.storage.GraphHopperStorage)3 LocationIndex (com.graphhopper.storage.index.LocationIndex)3 ArrayList (java.util.ArrayList)3 CarFlagEncoder (com.graphhopper.routing.util.CarFlagEncoder)2 HintsMap (com.graphhopper.routing.util.HintsMap)2 Graph (com.graphhopper.storage.Graph)2 RAMDirectory (com.graphhopper.storage.RAMDirectory)2 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)2 EdgeIteratorState (com.graphhopper.util.EdgeIteratorState)2 IntIndexedContainer (com.carrotsearch.hppc.IntIndexedContainer)1 GHRequest (com.graphhopper.GHRequest)1 GHResponse (com.graphhopper.GHResponse)1 GraphHopper (com.graphhopper.GraphHopper)1