Search in sources :

Example 1 with JsonFeatureCollection

use of com.graphhopper.json.geo.JsonFeatureCollection in project graphhopper by graphhopper.

the class DefaultModule method buildIndex.

static SpatialRuleLookup buildIndex(Reader reader, BBox graphBBox) {
    GHJson ghJson = new GHJsonBuilder().create();
    JsonFeatureCollection jsonFeatureCollection = ghJson.fromJson(reader, JsonFeatureCollection.class);
    return new SpatialRuleLookupBuilder().build(Arrays.asList(new GermanySpatialRule(), new AustriaSpatialRule()), jsonFeatureCollection, graphBBox, 1, true);
}
Also used : AustriaSpatialRule(com.graphhopper.routing.util.spatialrules.countries.AustriaSpatialRule) GHJsonBuilder(com.graphhopper.json.GHJsonBuilder) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) GHJson(com.graphhopper.json.GHJson) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) SpatialRuleLookupBuilder(com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)

Example 2 with JsonFeatureCollection

use of com.graphhopper.json.geo.JsonFeatureCollection 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 = gson.fromJson(new InputStreamReader(httpReq.getInputStream(), Helper.UTF_CS), JsonFeatureCollection.class);
        // TODO make asynchronous!
        ChangeGraphResponse rsp = hopper.changeGraph(collection.getFeatures());
        JSONObject resObject = new JSONObject();
        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) JSONObject(org.json.JSONObject) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) ChangeGraphResponse(com.graphhopper.storage.change.ChangeGraphResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) StopWatch(com.graphhopper.util.StopWatch)

Example 3 with JsonFeatureCollection

use of com.graphhopper.json.geo.JsonFeatureCollection in project graphhopper by graphhopper.

the class DefaultModule method createGraphHopper.

/**
     * @return an initialized GraphHopper instance
     */
protected GraphHopper createGraphHopper(CmdArgs args) {
    GraphHopper tmp = new GraphHopperOSM() {

        @Override
        protected void loadOrPrepareLM() {
            if (!getLMFactoryDecorator().isEnabled() || getLMFactoryDecorator().getPreparations().isEmpty())
                return;
            try {
                String location = args.get(Parameters.Landmark.PREPARE + "split_area_location", "");
                Reader reader = location.isEmpty() ? new InputStreamReader(LandmarkStorage.class.getResource("map.geo.json").openStream()) : new FileReader(location);
                JsonFeatureCollection jsonFeatureCollection = new GHJsonBuilder().create().fromJson(reader, JsonFeatureCollection.class);
                if (!jsonFeatureCollection.getFeatures().isEmpty()) {
                    SpatialRuleLookup ruleLookup = new SpatialRuleLookupBuilder().build("country", new SpatialRuleLookupBuilder.SpatialRuleDefaultFactory(), jsonFeatureCollection, getGraphHopperStorage().getBounds(), 0.1, true);
                    for (PrepareLandmarks prep : getLMFactoryDecorator().getPreparations()) {
                        prep.setSpatialRuleLookup(ruleLookup);
                    }
                }
            } catch (IOException ex) {
                logger.error("Problem while reading border map GeoJSON. Skipping this.", ex);
            }
            super.loadOrPrepareLM();
        }
    }.forServer().init(args);
    String location = args.get("spatial_rules.location", "");
    if (!location.isEmpty()) {
        if (!tmp.getEncodingManager().supports(("generic"))) {
            logger.warn("spatial_rules.location was specified but 'generic' encoder is missing to utilize the index");
        } else
            try {
                SpatialRuleLookup index = buildIndex(new FileReader(location), tmp.getGraphHopperStorage().getBounds());
                if (index != null) {
                    logger.info("Set spatial rule lookup with " + index.size() + " rules");
                    ((DataFlagEncoder) tmp.getEncodingManager().getEncoder("generic")).setSpatialRuleLookup(index);
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
    }
    tmp.importOrLoad();
    logger.info("loaded graph at:" + tmp.getGraphHopperLocation() + ", data_reader_file:" + tmp.getDataReaderFile() + ", flag_encoders:" + tmp.getEncodingManager() + ", " + tmp.getGraphHopperStorage().toDetailsString());
    return tmp;
}
Also used : GHJsonBuilder(com.graphhopper.json.GHJsonBuilder) InputStreamReader(java.io.InputStreamReader) GraphHopperOSM(com.graphhopper.reader.osm.GraphHopperOSM) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) GraphHopper(com.graphhopper.GraphHopper) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) IOException(java.io.IOException) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) FileReader(java.io.FileReader) SpatialRuleLookupBuilder(com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)

Example 4 with JsonFeatureCollection

use of com.graphhopper.json.geo.JsonFeatureCollection in project graphhopper by graphhopper.

the class DataFlagEncoderTest method testSpatialId.

@Test
public void testSpatialId() {
    List<SpatialRule> rules = Collections.<SpatialRule>singletonList(new GermanySpatialRule());
    final BBox bbox = new BBox(0, 1, 0, 1);
    JsonFeatureCollection jsonFeatures = new JsonFeatureCollection() {

        @Override
        public List<JsonFeature> getFeatures() {
            Geometry geometry = new GeoJsonPolygon().addPolygon(new Polygon(new double[] { 0, 0, 1, 1 }, new double[] { 0, 1, 1, 0 }));
            Map<String, Object> properties = new HashMap<>();
            properties.put("ISO_A3", "DEU");
            return Collections.singletonList(new JsonFeature("x", "Polygon", bbox, geometry, properties));
        }
    };
    SpatialRuleLookup index = new SpatialRuleLookupBuilder().build(rules, jsonFeatures, bbox, 1, false);
    DataFlagEncoder encoder = new DataFlagEncoder(new PMap().put("spatial_rules", index.size()));
    encoder.setSpatialRuleLookup(index);
    EncodingManager em = new EncodingManager(encoder);
    ReaderWay way = new ReaderWay(27l);
    way.setTag("highway", "track");
    way.setTag("estimated_center", new GHPoint(0.005, 0.005));
    ReaderWay way2 = new ReaderWay(28l);
    way2.setTag("highway", "track");
    way2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
    ReaderWay livingStreet = new ReaderWay(29l);
    livingStreet.setTag("highway", "living_street");
    livingStreet.setTag("estimated_center", new GHPoint(0.005, 0.005));
    ReaderWay livingStreet2 = new ReaderWay(30l);
    livingStreet2.setTag("highway", "living_street");
    livingStreet2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
    Graph graph = new GraphBuilder(em).create();
    EdgeIteratorState e1 = graph.edge(0, 1, 1, true);
    EdgeIteratorState e2 = graph.edge(0, 2, 1, true);
    EdgeIteratorState e3 = graph.edge(0, 3, 1, true);
    EdgeIteratorState e4 = graph.edge(0, 4, 1, true);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.00, 0.00);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, -0.01, -0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.01, 0.01);
    AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, -0.01, -0.01);
    e1.setFlags(encoder.handleWayTags(way, 1, 0));
    e2.setFlags(encoder.handleWayTags(way2, 1, 0));
    e3.setFlags(encoder.handleWayTags(livingStreet, 1, 0));
    e4.setFlags(encoder.handleWayTags(livingStreet2, 1, 0));
    assertEquals(index.getSpatialId(new GermanySpatialRule()), encoder.getSpatialId(e1.getFlags()));
    assertEquals(index.getSpatialId(SpatialRule.EMPTY), encoder.getSpatialId(e2.getFlags()));
    assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, encoder.getAccessValue(e1.getFlags()));
    assertEquals(AccessValue.ACCESSIBLE, encoder.getAccessValue(e2.getFlags()));
    assertEquals(5, encoder.getMaxspeed(e3, -1, false), .1);
    assertEquals(-1, encoder.getMaxspeed(e4, -1, false), .1);
}
Also used : GeoJsonPolygon(com.graphhopper.json.geo.GeoJsonPolygon) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) ReaderWay(com.graphhopper.reader.ReaderWay) JsonFeature(com.graphhopper.json.geo.JsonFeature) Geometry(com.graphhopper.json.geo.Geometry) Graph(com.graphhopper.storage.Graph) BBox(com.graphhopper.util.shapes.BBox) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) GraphBuilder(com.graphhopper.storage.GraphBuilder) GeoJsonPolygon(com.graphhopper.json.geo.GeoJsonPolygon) GHPoint(com.graphhopper.util.shapes.GHPoint) GermanySpatialRule(com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule) Test(org.junit.Test)

Aggregations

JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)4 GHJsonBuilder (com.graphhopper.json.GHJsonBuilder)2 SpatialRuleLookupBuilder (com.graphhopper.routing.util.spatialrules.SpatialRuleLookupBuilder)2 GermanySpatialRule (com.graphhopper.routing.util.spatialrules.countries.GermanySpatialRule)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 GraphHopper (com.graphhopper.GraphHopper)1 GHJson (com.graphhopper.json.GHJson)1 GeoJsonPolygon (com.graphhopper.json.geo.GeoJsonPolygon)1 Geometry (com.graphhopper.json.geo.Geometry)1 JsonFeature (com.graphhopper.json.geo.JsonFeature)1 ReaderWay (com.graphhopper.reader.ReaderWay)1 GraphHopperOSM (com.graphhopper.reader.osm.GraphHopperOSM)1 LandmarkStorage (com.graphhopper.routing.lm.LandmarkStorage)1 PrepareLandmarks (com.graphhopper.routing.lm.PrepareLandmarks)1 SpatialRuleLookup (com.graphhopper.routing.util.spatialrules.SpatialRuleLookup)1 AustriaSpatialRule (com.graphhopper.routing.util.spatialrules.countries.AustriaSpatialRule)1 Graph (com.graphhopper.storage.Graph)1 GraphBuilder (com.graphhopper.storage.GraphBuilder)1 ChangeGraphResponse (com.graphhopper.storage.change.ChangeGraphResponse)1