Search in sources :

Example 21 with GraphHopper

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

the class OSMReaderTest method testReadEleFromDataProvider.

@Test
public void testReadEleFromDataProvider() {
    GraphHopper hopper = new GraphHopperFacade("test-osm5.xml");
    // get N10E046.hgt.zip
    ElevationProvider provider = new SRTMProvider(GraphHopperIT.DIR);
    hopper.setElevationProvider(provider);
    hopper.importOrLoad();
    Graph graph = hopper.getGraphHopperStorage();
    int n10 = AbstractGraphStorageTester.getIdOf(graph, 49.501);
    int n30 = AbstractGraphStorageTester.getIdOf(graph, 49.5011);
    int n50 = AbstractGraphStorageTester.getIdOf(graph, 49.5001);
    EdgeIteratorState edge = GHUtility.getEdge(graph, n50, n30);
    assertEquals(Helper.createPointList3D(49.5001, 11.501, 426, 49.5002, 11.5015, 441, 49.5011, 11.502, 410.0), edge.fetchWayGeometry(3));
    edge = GHUtility.getEdge(graph, n10, n50);
    assertEquals(Helper.createPointList3D(49.501, 11.5001, 383.0, 49.5001, 11.501, 426.0), edge.fetchWayGeometry(3));
}
Also used : ElevationProvider(com.graphhopper.reader.dem.ElevationProvider) GraphHopper(com.graphhopper.GraphHopper) GHPoint(com.graphhopper.util.shapes.GHPoint) SRTMProvider(com.graphhopper.reader.dem.SRTMProvider) Test(org.junit.Test)

Example 22 with GraphHopper

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

the class RoutingAlgorithmWithOSMIT method runAlgo.

/**
 * @param withCH if true also the CH and LM algorithms will be tested which need
 *               preparation and takes a bit longer
 */
Graph runAlgo(TestAlgoCollector testCollector, String osmFile, String graphFile, List<OneRun> forEveryAlgo, String importVehicles, boolean withCH, String vehicle, String weightStr, boolean is3D) {
    // for different weightings we need a different storage, otherwise we would need to remove the graph folder
    // everytime we come with a different weighting
    // graphFile += weightStr;
    AlgoHelperEntry algoEntry = null;
    OneRun tmpOneRun = null;
    try {
        Helper.removeDir(new File(graphFile));
        GraphHopper hopper = new GraphHopperOSM().setStoreOnFlush(true).setCHEnabled(false).setDataReaderFile(osmFile).setGraphHopperLocation(graphFile).setEncodingManager(new EncodingManager(importVehicles));
        if (osmFile.contains("krautsand"))
            hopper.setMinNetworkSize(0, 0);
        // avoid that path.getDistance is too different to path.getPoint.calcDistance
        hopper.setWayPointMaxDistance(0);
        // always enable landmarks
        hopper.getLMFactoryDecorator().addWeighting(weightStr).setEnabled(true).setDisablingAllowed(true);
        if (withCH)
            hopper.getCHFactoryDecorator().addWeighting(weightStr).setEnabled(true).setDisablingAllowed(true);
        if (is3D)
            hopper.setElevationProvider(new SRTMProvider(DIR));
        hopper.importOrLoad();
        TraversalMode tMode = importVehicles.contains("turn_costs=true") ? TraversalMode.EDGE_BASED_2DIR : TraversalMode.NODE_BASED;
        FlagEncoder encoder = hopper.getEncodingManager().getEncoder(vehicle);
        HintsMap hints = new HintsMap().setWeighting(weightStr).setVehicle(vehicle);
        Collection<AlgoHelperEntry> prepares = RoutingAlgorithmIT.createAlgos(hopper, hints, tMode);
        EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
        for (AlgoHelperEntry entry : prepares) {
            algoEntry = entry;
            LocationIndex idx = entry.getIdx();
            for (OneRun oneRun : forEveryAlgo) {
                tmpOneRun = oneRun;
                List<QueryResult> list = oneRun.getList(idx, edgeFilter);
                testCollector.assertDistance(algoEntry, list, oneRun);
            }
        }
        return hopper.getGraphHopperStorage();
    } catch (Exception ex) {
        if (algoEntry == null)
            throw new RuntimeException("cannot handle file " + osmFile + ", " + ex.getMessage(), ex);
        throw new RuntimeException("cannot handle " + algoEntry.toString() + ", for " + tmpOneRun + ", file " + osmFile + ", " + ex.getMessage(), ex);
    } finally {
    // Helper.removeDir(new File(graphFile));
    }
}
Also used : AlgoHelperEntry(com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry) OneRun(com.graphhopper.routing.util.TestAlgoCollector.OneRun) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) GraphHopper(com.graphhopper.GraphHopper) LocationIndex(com.graphhopper.storage.index.LocationIndex) IOException(java.io.IOException) SRTMProvider(com.graphhopper.reader.dem.SRTMProvider) QueryResult(com.graphhopper.storage.index.QueryResult) File(java.io.File)

Example 23 with GraphHopper

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

the class ChangeGraphServlet method doPost.

@Override
protected void doPost(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
    String infoStr = httpReq.getRemoteAddr() + " " + httpReq.getPathInfo() + " " + httpReq.getQueryString();
    float took = -1;
    StopWatch sw = new StopWatch().start();
    try {
        JsonFeatureCollection collection = ghJson.fromJson(new InputStreamReader(httpReq.getInputStream(), Helper.UTF_CS), JsonFeatureCollection.class);
        // TODO put changeGraph on GraphHopperAPI interface and remove cast (or some other solution)
        if (!(graphHopper instanceof GraphHopper)) {
            throw new IllegalStateException("Graph change API not supported with public transit.");
        }
        // TODO make asynchronous!
        ChangeGraphResponse rsp = ((GraphHopper) graphHopper).changeGraph(collection.getFeatures());
        ObjectNode resObject = objectMapper.createObjectNode();
        resObject.put("updates", rsp.getUpdateCount());
        // prepare the consumer to get some changes not immediately when returning after POST
        resObject.put("scheduled_updates", 0);
        httpRes.setHeader("X-GH-Took", "" + Math.round(took * 1000));
        writeJson(httpReq, httpRes, resObject);
        took = sw.stop().getSeconds();
        logger.info(infoStr + " " + took);
    } catch (IllegalArgumentException ex) {
        took = sw.stop().getSeconds();
        logger.warn(infoStr + " " + took + ", " + ex.getMessage());
        writeError(httpRes, 400, "Wrong arguments for endpoint /change, " + infoStr);
    } catch (Exception ex) {
        took = sw.stop().getSeconds();
        logger.error(infoStr + " " + took, ex);
        writeError(httpRes, 500, "Error at endpoint /change, " + infoStr);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) ChangeGraphResponse(com.graphhopper.storage.change.ChangeGraphResponse) GraphHopper(com.graphhopper.GraphHopper) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) StopWatch(com.graphhopper.util.StopWatch)

Example 24 with GraphHopper

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

the class RoutingAlgorithmIT method testPerformance.

@Test
public void testPerformance() throws IOException {
    int N = 10;
    int noJvmWarming = N / 4;
    Random rand = new Random(0);
    final EncodingManager eManager = new EncodingManager("car");
    final GraphHopperStorage graph = new GraphBuilder(eManager).create();
    String bigFile = "10000EWD.txt.gz";
    new PrincetonReader(graph).setStream(new GZIPInputStream(PrincetonReader.class.getResourceAsStream(bigFile))).read();
    GraphHopper hopper = new GraphHopper() {

        {
            setCHEnabled(false);
            setEncodingManager(eManager);
            loadGraph(graph);
        }

        @Override
        protected LocationIndex createLocationIndex(Directory dir) {
            return new LocationIndexTree(graph, dir);
        }
    };
    Collection<AlgoHelperEntry> prepares = createAlgos(hopper, new HintsMap().setWeighting("shortest").setVehicle("car"), TraversalMode.NODE_BASED);
    for (AlgoHelperEntry entry : prepares) {
        StopWatch sw = new StopWatch();
        for (int i = 0; i < N; i++) {
            int node1 = Math.abs(rand.nextInt(graph.getNodes()));
            int node2 = Math.abs(rand.nextInt(graph.getNodes()));
            RoutingAlgorithm d = entry.createRoutingFactory().createAlgo(graph, entry.getAlgorithmOptions());
            if (i >= noJvmWarming)
                sw.start();
            Path p = d.calcPath(node1, node2);
            // avoid jvm optimization => call p.distance
            if (i >= noJvmWarming && p.getDistance() > -1)
                sw.stop();
        // System.out.println("#" + i + " " + name + ":" + sw.getSeconds() + " " + p.nodes());
        }
        float perRun = sw.stop().getSeconds() / ((float) (N - noJvmWarming));
        System.out.println("# " + getClass().getSimpleName() + " " + entry + ":" + sw.stop().getSeconds() + ", per run:" + perRun);
        assertTrue("speed to low!? " + perRun + " per run", perRun < 0.08);
    }
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) AlgoHelperEntry(com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry) HintsMap(com.graphhopper.routing.util.HintsMap) PrincetonReader(com.graphhopper.reader.PrincetonReader) GraphHopper(com.graphhopper.GraphHopper) GraphHopperStorage(com.graphhopper.storage.GraphHopperStorage) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) StopWatch(com.graphhopper.util.StopWatch) GZIPInputStream(java.util.zip.GZIPInputStream) Random(java.util.Random) GraphBuilder(com.graphhopper.storage.GraphBuilder) Directory(com.graphhopper.storage.Directory) Test(org.junit.Test)

Example 25 with GraphHopper

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

the class GraphHopperLandmarksIT method testLandmarkDisconnect.

@Test
public void testLandmarkDisconnect() throws Exception {
    // if one algorithm is disabled then the following chain is executed: CH -> LM -> flexible
    // disconnected for landmarks
    JsonNode json = query("point=55.99022,29.129734&point=56.007787,29.208355&ch.disable=true", 400);
    JsonNode errorJson = json.get("message");
    assertTrue(errorJson.toString(), errorJson.toString().contains("Different subnetworks"));
    // without landmarks it should work
    GraphHopper hopper = getInstance(GraphHopper.class);
    hopper.getLMFactoryDecorator().setDisablingAllowed(true);
    json = query("point=55.99022,29.129734&point=56.007787,29.208355&ch.disable=true&lm.disable=true", 200);
    JsonNode path = json.get("paths").get(0);
    double distance = path.get("distance").asDouble();
    assertEquals("distance wasn't correct:" + distance, 5790, distance, 100);
    hopper.getLMFactoryDecorator().setDisablingAllowed(false);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) GraphHopper(com.graphhopper.GraphHopper) Test(org.junit.Test)

Aggregations

GraphHopper (com.graphhopper.GraphHopper)42 Test (org.junit.Test)34 GHPoint (com.graphhopper.util.shapes.GHPoint)17 IOException (java.io.IOException)8 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)7 GHRequest (com.graphhopper.GHRequest)4 GHResponse (com.graphhopper.GHResponse)4 File (java.io.File)4 EncodingManager (com.graphhopper.routing.util.EncodingManager)3 AlgoHelperEntry (com.graphhopper.routing.util.TestAlgoCollector.AlgoHelperEntry)3 Weighting (com.graphhopper.routing.weighting.Weighting)3 CmdArgs (com.graphhopper.util.CmdArgs)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)2 ReaderNode (com.graphhopper.reader.ReaderNode)2 ReaderWay (com.graphhopper.reader.ReaderWay)2 SRTMProvider (com.graphhopper.reader.dem.SRTMProvider)2 PrepareContractionHierarchies (com.graphhopper.routing.ch.PrepareContractionHierarchies)2 PrepareLandmarks (com.graphhopper.routing.lm.PrepareLandmarks)2 OneRun (com.graphhopper.routing.util.TestAlgoCollector.OneRun)2