Search in sources :

Example 61 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 62 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 63 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)

Example 64 with GraphHopper

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

the class GraphHopperManager method init.

/**
 * Creates a new GraphHopper for the given map name.
 * @param newMapName the name of the map to load
 */
public static void init(String newMapName) {
    if (newMapName.equals(mapName))
        return;
    mapName = newMapName;
    hopper = new GraphHopper().forDesktop();
    hopper.setOSMFile("osm" + File.separator + mapName + ".osm.pbf");
    // CH does not work with shortest weighting (at the moment)
    hopper.setCHEnabled(false);
    // where to store GH files?
    hopper.setGraphHopperLocation("graphs" + File.separator + mapName);
    hopper.setEncodingManager(new EncodingManager("car"));
    // this may take a few minutes
    hopper.importOrLoad();
}
Also used : EncodingManager(com.graphhopper.routing.util.EncodingManager) GraphHopper(com.graphhopper.GraphHopper)

Example 65 with GraphHopper

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

the class MiniGraphUI method main.

public static void main(String[] strs) {
    PMap args = PMap.read(strs);
    args.putObject("datareader.file", args.getString("datareader.file", "core/files/monaco.osm.gz"));
    args.putObject("graph.location", args.getString("graph.location", "tools/target/mini-graph-ui-gh"));
    args.putObject("graph.flag_encoders", args.getString("graph.flag_encoders", "car"));
    GraphHopperConfig ghConfig = new GraphHopperConfig(args);
    ghConfig.setProfiles(Arrays.asList(new Profile("profile").setVehicle("car").setWeighting("fastest")));
    ghConfig.setCHProfiles(Arrays.asList(new CHProfile("profile")));
    ghConfig.setLMProfiles(Arrays.asList(new LMProfile("profile")));
    GraphHopper hopper = new GraphHopper().init(ghConfig).importOrLoad();
    boolean debug = args.getBool("minigraphui.debug", false);
    boolean useCH = args.getBool("minigraphui.useCH", false);
    new MiniGraphUI(hopper, debug, useCH).visualize();
}
Also used : CHProfile(com.graphhopper.config.CHProfile) PMap(com.graphhopper.util.PMap) LMProfile(com.graphhopper.config.LMProfile) GraphHopper(com.graphhopper.GraphHopper) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile) LMProfile(com.graphhopper.config.LMProfile) GraphHopperConfig(com.graphhopper.GraphHopperConfig)

Aggregations

GraphHopper (com.graphhopper.GraphHopper)101 Test (org.junit.jupiter.api.Test)62 Profile (com.graphhopper.config.Profile)52 LMProfile (com.graphhopper.config.LMProfile)44 CHProfile (com.graphhopper.config.CHProfile)39 GraphHopperTest (com.graphhopper.GraphHopperTest)25 ArrayList (java.util.ArrayList)24 File (java.io.File)16 Test (org.junit.Test)11 SRTMProvider (com.graphhopper.reader.dem.SRTMProvider)10 GHRequest (com.graphhopper.GHRequest)7 GHResponse (com.graphhopper.GHResponse)7 GraphHopperConfig (com.graphhopper.GraphHopperConfig)6 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)6 EncodingManager (com.graphhopper.routing.util.EncodingManager)6 PMap (com.graphhopper.util.PMap)6 GHPoint (com.graphhopper.util.shapes.GHPoint)5 IOException (java.io.IOException)5 Gpx (com.graphhopper.jackson.Gpx)4 MapMatching (com.graphhopper.matching.MapMatching)4