Search in sources :

Example 1 with CountriesSpatialRuleFactory

use of com.graphhopper.routing.util.spatialrules.CountriesSpatialRuleFactory in project graphhopper by graphhopper.

the class SpatialRuleLookupHelper method buildAndInjectSpatialRuleIntoGH.

public static void buildAndInjectSpatialRuleIntoGH(GraphHopper graphHopper, CmdArgs args) {
    String spatialRuleLocation = args.get("spatial_rules.location", "");
    if (!spatialRuleLocation.isEmpty()) {
        try {
            final BBox maxBounds = BBox.parseBBoxString(args.get("spatial_rules.max_bbox", "-180, 180, -90, 90"));
            final InputStreamReader reader = new InputStreamReader(new FileInputStream(spatialRuleLocation), UTF_CS);
            final SpatialRuleLookup index = SpatialRuleLookupBuilder.buildIndex(new GHJsonFactory().create().fromJson(reader, JsonFeatureCollection.class), "ISO_A3", new CountriesSpatialRuleFactory(), .1, maxBounds);
            logger.info("Set spatial rule lookup with " + index.size() + " rules");
            final FlagEncoderFactory oldFEF = graphHopper.getFlagEncoderFactory();
            graphHopper.setFlagEncoderFactory(new FlagEncoderFactory() {

                @Override
                public FlagEncoder createFlagEncoder(String name, PMap configuration) {
                    if (name.equals(GENERIC)) {
                        return new DataFlagEncoder(configuration).setSpatialRuleLookup(index);
                    }
                    return oldFEF.createFlagEncoder(name, configuration);
                }
            });
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) DataFlagEncoder(com.graphhopper.routing.util.DataFlagEncoder) FlagEncoder(com.graphhopper.routing.util.FlagEncoder) PMap(com.graphhopper.util.PMap) SpatialRuleLookup(com.graphhopper.routing.util.spatialrules.SpatialRuleLookup) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DataFlagEncoder(com.graphhopper.routing.util.DataFlagEncoder) BBox(com.graphhopper.util.shapes.BBox) GHJsonFactory(com.graphhopper.json.GHJsonFactory) JsonFeatureCollection(com.graphhopper.json.geo.JsonFeatureCollection) CountriesSpatialRuleFactory(com.graphhopper.routing.util.spatialrules.CountriesSpatialRuleFactory) FlagEncoderFactory(com.graphhopper.routing.util.FlagEncoderFactory)

Aggregations

GHJsonFactory (com.graphhopper.json.GHJsonFactory)1 JsonFeatureCollection (com.graphhopper.json.geo.JsonFeatureCollection)1 DataFlagEncoder (com.graphhopper.routing.util.DataFlagEncoder)1 FlagEncoder (com.graphhopper.routing.util.FlagEncoder)1 FlagEncoderFactory (com.graphhopper.routing.util.FlagEncoderFactory)1 CountriesSpatialRuleFactory (com.graphhopper.routing.util.spatialrules.CountriesSpatialRuleFactory)1 SpatialRuleLookup (com.graphhopper.routing.util.spatialrules.SpatialRuleLookup)1 PMap (com.graphhopper.util.PMap)1 BBox (com.graphhopper.util.shapes.BBox)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1