public GraphHopperManaged(CmdArgs configuration, ObjectMapper objectMapper) { String splitAreaLocation = configuration.get(Parameters.Landmark.PREPARE + "split_area_location", ""); JsonFeatureCollection landmarkSplittingFeatureCollection; try (Reader reader = splitAreaLocation.isEmpty() ? new InputStreamReader(LandmarkStorage.class.getResource("map.geo.json").openStream(), UTF_CS) : new InputStreamReader(new FileInputStream(splitAreaLocation), UTF_CS)) { landmarkSplittingFeatureCollection = objectMapper.readValue(reader, JsonFeatureCollection.class); } catch (IOException e1) { logger.error("Problem while reading border map GeoJSON. Skipping this.", e1); landmarkSplittingFeatureCollection = null; } graphHopper = new GraphHopperOSM(landmarkSplittingFeatureCollection).forServer(); String spatialRuleLocation = configuration.get("spatial_rules.location", ""); if (!spatialRuleLocation.isEmpty()) { final BBox maxBounds = BBox.parseBBoxString(configuration.get("spatial_rules.max_bbox", "-180, 180, -90, 90")); try (final InputStreamReader reader = new InputStreamReader(new FileInputStream(spatialRuleLocation), UTF_CS)) { JsonFeatureCollection jsonFeatureCollection = objectMapper.readValue(reader, JsonFeatureCollection.class); SpatialRuleLookupHelper.buildAndInjectSpatialRuleIntoGH(graphHopper, maxBounds, jsonFeatureCollection); } catch (IOException e) { throw new RuntimeException(e); } } graphHopper.init(configuration); }
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); } } }