Search in sources :

Example 1 with Profile

use of com.graphhopper.config.Profile in project graphhopper by graphhopper.

the class GraphHopper method loadOrPrepareLM.

/**
 * For landmarks it is required to always call this method: either it creates the landmark data or it loads it.
 */
protected void loadOrPrepareLM(boolean closeEarly) {
    for (LMProfile profile : lmPreparationHandler.getLMProfiles()) if (!getLMProfileVersion(profile.getProfile()).isEmpty() && !getLMProfileVersion(profile.getProfile()).equals("" + profilesByName.get(profile.getProfile()).getVersion()))
        throw new IllegalArgumentException("LM preparation of " + profile.getProfile() + " already exists in storage and doesn't match configuration");
    // we load landmark storages that already exist and prepare the other ones
    List<LMConfig> lmConfigs = createLMConfigs(lmPreparationHandler.getLMProfiles());
    List<LandmarkStorage> loaded = lmPreparationHandler.load(lmConfigs, ghStorage);
    List<LMConfig> loadedConfigs = loaded.stream().map(LandmarkStorage::getLMConfig).collect(Collectors.toList());
    List<LMConfig> configsToPrepare = lmConfigs.stream().filter(c -> !loadedConfigs.contains(c)).collect(Collectors.toList());
    List<PrepareLandmarks> prepared = prepareLM(closeEarly, configsToPrepare);
    // we map all profile names for which there is LM support to the according LM storages
    landmarks = new LinkedHashMap<>();
    for (LMProfile lmp : lmPreparationHandler.getLMProfiles()) {
        // cross-querying
        String prepProfile = lmp.usesOtherPreparation() ? lmp.getPreparationProfile() : lmp.getProfile();
        Optional<LandmarkStorage> loadedLMS = loaded.stream().filter(lms -> lms.getLMConfig().getName().equals(prepProfile)).findFirst();
        Optional<PrepareLandmarks> preparedLMS = prepared.stream().filter(pl -> pl.getLandmarkStorage().getLMConfig().getName().equals(prepProfile)).findFirst();
        if (loadedLMS.isPresent() && preparedLMS.isPresent())
            throw new IllegalStateException("LM should be either loaded or prepared, but not both: " + prepProfile);
        else if (preparedLMS.isPresent()) {
            setLMProfileVersion(lmp.getProfile(), profilesByName.get(lmp.getProfile()).getVersion());
            landmarks.put(lmp.getProfile(), preparedLMS.get().getLandmarkStorage());
        } else
            loadedLMS.ifPresent(landmarkStorage -> landmarks.put(lmp.getProfile(), landmarkStorage));
    }
}
Also used : com.graphhopper.routing.ev(com.graphhopper.routing.ev) RoundTrip(com.graphhopper.util.Parameters.Algorithms.RoundTrip) com.graphhopper.routing(com.graphhopper.routing) LoggerFactory(org.slf4j.LoggerFactory) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) DirectoryStream(java.nio.file.DirectoryStream) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) LMConfig(com.graphhopper.routing.lm.LMConfig) Path(java.nio.file.Path) DateFormat(java.text.DateFormat) DefaultTagParserFactory(com.graphhopper.routing.util.parsers.DefaultTagParserFactory) PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) Landmark(com.graphhopper.util.Parameters.Landmark) Helper(com.graphhopper.util.Helper) PrepareRoutingSubnetworks(com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) com.graphhopper.reader.dem(com.graphhopper.reader.dem) UncheckedIOException(java.io.UncheckedIOException) GHUtility.readCountries(com.graphhopper.util.GHUtility.readCountries) com.graphhopper.routing.util(com.graphhopper.routing.util) LMProfile(com.graphhopper.config.LMProfile) java.util(java.util) CountryRuleFactory(com.graphhopper.routing.util.countryrules.CountryRuleFactory) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Routing(com.graphhopper.util.Parameters.Routing) PrepareJob(com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks.PrepareJob) CHPreparationHandler(com.graphhopper.routing.ch.CHPreparationHandler) Profile(com.graphhopper.config.Profile) OSMReader(com.graphhopper.reader.osm.OSMReader) com.graphhopper.storage(com.graphhopper.storage) DateRangeParser(com.graphhopper.reader.osm.conditional.DateRangeParser) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) LocationIndex(com.graphhopper.storage.index.LocationIndex) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) CHProfile(com.graphhopper.config.CHProfile) File(java.io.File) TagParserFactory(com.graphhopper.routing.util.parsers.TagParserFactory) LMPreparationHandler(com.graphhopper.routing.lm.LMPreparationHandler) Paths(java.nio.file.Paths) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) Weighting(com.graphhopper.routing.weighting.Weighting) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) BufferedReader(java.io.BufferedReader) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) LMProfile(com.graphhopper.config.LMProfile) LMConfig(com.graphhopper.routing.lm.LMConfig)

Example 2 with Profile

use of com.graphhopper.config.Profile in project graphhopper by graphhopper.

the class GraphHopper method loadOrPrepareCH.

protected void loadOrPrepareCH(boolean closeEarly) {
    for (CHProfile profile : chPreparationHandler.getCHProfiles()) if (!getCHProfileVersion(profile.getProfile()).isEmpty() && !getCHProfileVersion(profile.getProfile()).equals("" + profilesByName.get(profile.getProfile()).getVersion()))
        throw new IllegalArgumentException("CH preparation of " + profile.getProfile() + " already exists in storage and doesn't match configuration");
    // we load ch graphs that already exist and prepare the other ones
    List<CHConfig> chConfigs = createCHConfigs(chPreparationHandler.getCHProfiles());
    Map<String, RoutingCHGraph> loaded = chPreparationHandler.load(ghStorage, chConfigs);
    List<CHConfig> configsToPrepare = chConfigs.stream().filter(c -> !loaded.containsKey(c.getName())).collect(Collectors.toList());
    Map<String, PrepareContractionHierarchies.Result> prepared = prepareCH(closeEarly, configsToPrepare);
    // we map all profile names for which there is CH support to the according CH graphs
    chGraphs = new LinkedHashMap<>();
    for (CHProfile profile : chPreparationHandler.getCHProfiles()) {
        if (loaded.containsKey(profile.getProfile()) && prepared.containsKey(profile.getProfile()))
            throw new IllegalStateException("CH graph should be either loaded or prepared, but not both: " + profile.getProfile());
        else if (prepared.containsKey(profile.getProfile())) {
            setCHProfileVersion(profile.getProfile(), profilesByName.get(profile.getProfile()).getVersion());
            PrepareContractionHierarchies.Result res = prepared.get(profile.getProfile());
            chGraphs.put(profile.getProfile(), ghStorage.createCHGraph(res.getCHStorage(), res.getCHConfig()));
        } else if (loaded.containsKey(profile.getProfile())) {
            chGraphs.put(profile.getProfile(), loaded.get(profile.getProfile()));
        } else
            throw new IllegalStateException("CH graph should be either loaded or prepared: " + profile.getProfile());
    }
}
Also used : CHProfile(com.graphhopper.config.CHProfile) com.graphhopper.routing.ev(com.graphhopper.routing.ev) RoundTrip(com.graphhopper.util.Parameters.Algorithms.RoundTrip) com.graphhopper.routing(com.graphhopper.routing) LoggerFactory(org.slf4j.LoggerFactory) LandmarkStorage(com.graphhopper.routing.lm.LandmarkStorage) DirectoryStream(java.nio.file.DirectoryStream) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) LMConfig(com.graphhopper.routing.lm.LMConfig) Path(java.nio.file.Path) DateFormat(java.text.DateFormat) DefaultTagParserFactory(com.graphhopper.routing.util.parsers.DefaultTagParserFactory) PathDetailsBuilderFactory(com.graphhopper.util.details.PathDetailsBuilderFactory) Landmark(com.graphhopper.util.Parameters.Landmark) Helper(com.graphhopper.util.Helper) PrepareRoutingSubnetworks(com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) com.graphhopper.reader.dem(com.graphhopper.reader.dem) UncheckedIOException(java.io.UncheckedIOException) GHUtility.readCountries(com.graphhopper.util.GHUtility.readCountries) com.graphhopper.routing.util(com.graphhopper.routing.util) LMProfile(com.graphhopper.config.LMProfile) java.util(java.util) CountryRuleFactory(com.graphhopper.routing.util.countryrules.CountryRuleFactory) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) LocationIndexTree(com.graphhopper.storage.index.LocationIndexTree) Routing(com.graphhopper.util.Parameters.Routing) PrepareJob(com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks.PrepareJob) CHPreparationHandler(com.graphhopper.routing.ch.CHPreparationHandler) Profile(com.graphhopper.config.Profile) OSMReader(com.graphhopper.reader.osm.OSMReader) com.graphhopper.storage(com.graphhopper.storage) DateRangeParser(com.graphhopper.reader.osm.conditional.DateRangeParser) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) LocationIndex(com.graphhopper.storage.index.LocationIndex) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) CHProfile(com.graphhopper.config.CHProfile) File(java.io.File) TagParserFactory(com.graphhopper.routing.util.parsers.TagParserFactory) LMPreparationHandler(com.graphhopper.routing.lm.LMPreparationHandler) Paths(java.nio.file.Paths) PrepareLandmarks(com.graphhopper.routing.lm.PrepareLandmarks) Weighting(com.graphhopper.routing.weighting.Weighting) CustomWeighting(com.graphhopper.routing.weighting.custom.CustomWeighting) PrepareContractionHierarchies(com.graphhopper.routing.ch.PrepareContractionHierarchies) BufferedReader(java.io.BufferedReader)

Example 3 with Profile

use of com.graphhopper.config.Profile in project graphhopper by graphhopper.

the class GraphHopper method buildEncodingManager.

private EncodingManager buildEncodingManager(GraphHopperConfig ghConfig) {
    if (profilesByName.isEmpty())
        throw new IllegalStateException("no profiles exist but assumed to create EncodingManager. E.g. provide them in GraphHopperConfig when calling GraphHopper.init");
    String flagEncodersStr = ghConfig.getString("graph.flag_encoders", "");
    Map<String, String> flagEncoderMap = new LinkedHashMap<>();
    for (String encoderStr : flagEncodersStr.split(",")) {
        String key = encoderStr.split("\\|")[0];
        if (!key.isEmpty()) {
            if (flagEncoderMap.containsKey(key))
                throw new IllegalArgumentException("FlagEncoder " + key + " needs to be unique");
            flagEncoderMap.put(key, encoderStr);
        }
    }
    Map<String, String> implicitFlagEncoderMap = new HashMap<>();
    for (Profile profile : profilesByName.values()) {
        emBuilder.add(Subnetwork.create(profile.getName()));
        if (!flagEncoderMap.containsKey(profile.getVehicle()) && // overwrite key in implicit map if turn cost support required
        (!implicitFlagEncoderMap.containsKey(profile.getVehicle()) || profile.isTurnCosts()))
            implicitFlagEncoderMap.put(profile.getVehicle(), profile.getVehicle() + (profile.isTurnCosts() ? "|turn_costs=true" : ""));
    }
    flagEncoderMap.putAll(implicitFlagEncoderMap);
    flagEncoderMap.values().forEach(s -> emBuilder.addIfAbsent(flagEncoderFactory, s));
    String encodedValueStr = ghConfig.getString("graph.encoded_values", "");
    for (String tpStr : encodedValueStr.split(",")) {
        if (!tpStr.isEmpty())
            emBuilder.addIfAbsent(tagParserFactory, tpStr);
    }
    return emBuilder.build();
}
Also used : LMProfile(com.graphhopper.config.LMProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CHProfile(com.graphhopper.config.CHProfile)

Example 4 with Profile

use of com.graphhopper.config.Profile in project graphhopper by graphhopper.

the class IsochroneResource method doGet.

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response doGet(@Context UriInfo uriInfo, @QueryParam("profile") String profileName, @QueryParam("buckets") @Range(min = 1, max = 20) @DefaultValue("1") IntParam nBuckets, @QueryParam("reverse_flow") @DefaultValue("false") boolean reverseFlow, @QueryParam("point") @NotNull GHPointParam point, @QueryParam("time_limit") @DefaultValue("600") LongParam timeLimitInSeconds, @QueryParam("distance_limit") @DefaultValue("-1") LongParam distanceLimitInMeter, @QueryParam("weight_limit") @DefaultValue("-1") LongParam weightLimit, @QueryParam("type") @DefaultValue("json") ResponseType respType, @QueryParam("tolerance") @DefaultValue("0") double toleranceInMeter, @QueryParam("full_geometry") @DefaultValue("false") boolean fullGeometry) {
    StopWatch sw = new StopWatch().start();
    PMap hintsMap = new PMap();
    RouteResource.initHints(hintsMap, uriInfo.getQueryParameters());
    hintsMap.putObject(Parameters.CH.DISABLE, true);
    hintsMap.putObject(Parameters.Landmark.DISABLE, true);
    if (Helper.isEmpty(profileName)) {
        profileName = profileResolver.resolveProfile(hintsMap).getName();
        removeLegacyParameters(hintsMap);
    }
    errorIfLegacyParameters(hintsMap);
    Profile profile = graphHopper.getProfile(profileName);
    if (profile == null)
        throw new IllegalArgumentException("The requested profile '" + profileName + "' does not exist");
    LocationIndex locationIndex = graphHopper.getLocationIndex();
    Graph graph = graphHopper.getGraphHopperStorage();
    Weighting weighting = graphHopper.createWeighting(profile, hintsMap);
    BooleanEncodedValue inSubnetworkEnc = graphHopper.getEncodingManager().getBooleanEncodedValue(Subnetwork.key(profileName));
    if (hintsMap.has(Parameters.Routing.BLOCK_AREA)) {
        GraphEdgeIdFinder.BlockArea blockArea = GraphEdgeIdFinder.createBlockArea(graph, locationIndex, Collections.singletonList(point.get()), hintsMap, new FiniteWeightFilter(weighting));
        weighting = new BlockAreaWeighting(weighting, blockArea);
    }
    Snap snap = locationIndex.findClosest(point.get().lat, point.get().lon, new DefaultSnapFilter(weighting, inSubnetworkEnc));
    if (!snap.isValid())
        throw new IllegalArgumentException("Point not found:" + point);
    QueryGraph queryGraph = QueryGraph.create(graph, snap);
    TraversalMode traversalMode = profile.isTurnCosts() ? EDGE_BASED : NODE_BASED;
    ShortestPathTree shortestPathTree = new ShortestPathTree(queryGraph, queryGraph.wrapWeighting(weighting), reverseFlow, traversalMode);
    double limit;
    if (weightLimit.get() > 0) {
        limit = weightLimit.get();
        shortestPathTree.setWeightLimit(limit + Math.max(limit * 0.14, 2_000));
    } else if (distanceLimitInMeter.get() > 0) {
        limit = distanceLimitInMeter.get();
        shortestPathTree.setDistanceLimit(limit + Math.max(limit * 0.14, 2_000));
    } else {
        limit = timeLimitInSeconds.get() * 1000;
        shortestPathTree.setTimeLimit(limit + Math.max(limit * 0.14, 200_000));
    }
    ArrayList<Double> zs = new ArrayList<>();
    double delta = limit / nBuckets.get();
    for (int i = 0; i < nBuckets.get(); i++) {
        zs.add((i + 1) * delta);
    }
    ToDoubleFunction<ShortestPathTree.IsoLabel> fz;
    if (weightLimit.get() > 0) {
        fz = l -> l.weight;
    } else if (distanceLimitInMeter.get() > 0) {
        fz = l -> l.distance;
    } else {
        fz = l -> l.time;
    }
    Triangulator.Result result = triangulator.triangulate(snap, queryGraph, shortestPathTree, fz, degreesFromMeters(toleranceInMeter));
    ContourBuilder contourBuilder = new ContourBuilder(result.triangulation);
    ArrayList<Geometry> isochrones = new ArrayList<>();
    for (Double z : zs) {
        logger.info("Building contour z={}", z);
        MultiPolygon isochrone = contourBuilder.computeIsoline(z, result.seedEdges);
        if (fullGeometry) {
            isochrones.add(isochrone);
        } else {
            Polygon maxPolygon = heuristicallyFindMainConnectedComponent(isochrone, isochrone.getFactory().createPoint(new Coordinate(point.get().lon, point.get().lat)));
            isochrones.add(isochrone.getFactory().createPolygon(((LinearRing) maxPolygon.getExteriorRing())));
        }
    }
    ArrayList<JsonFeature> features = new ArrayList<>();
    for (Geometry isochrone : isochrones) {
        JsonFeature feature = new JsonFeature();
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("bucket", features.size());
        if (respType == geojson) {
            properties.put("copyrights", ResponsePathSerializer.COPYRIGHTS);
        }
        feature.setProperties(properties);
        feature.setGeometry(isochrone);
        features.add(feature);
    }
    ObjectNode json = JsonNodeFactory.instance.objectNode();
    sw.stop();
    ObjectNode finalJson = null;
    if (respType == geojson) {
        json.put("type", "FeatureCollection");
        json.putPOJO("features", features);
        finalJson = json;
    } else {
        json.putPOJO("polygons", features);
        final ObjectNode info = json.putObject("info");
        info.putPOJO("copyrights", ResponsePathSerializer.COPYRIGHTS);
        info.put("took", Math.round((float) sw.getMillis()));
        finalJson = json;
    }
    logger.info("took: " + sw.getSeconds() + ", visited nodes:" + shortestPathTree.getVisitedNodes());
    return Response.ok(finalJson).header("X-GH-Took", "" + sw.getSeconds() * 1000).build();
}
Also used : ProfileResolver(com.graphhopper.routing.ProfileResolver) LoggerFactory(org.slf4j.LoggerFactory) Subnetwork(com.graphhopper.routing.ev.Subnetwork) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Range(org.hibernate.validator.constraints.Range) Inject(javax.inject.Inject) MediaType(javax.ws.rs.core.MediaType) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) IntParam(io.dropwizard.jersey.params.IntParam) Profile(com.graphhopper.config.Profile) TraversalMode(com.graphhopper.routing.util.TraversalMode) Graph(com.graphhopper.storage.Graph) NODE_BASED(com.graphhopper.routing.util.TraversalMode.NODE_BASED) GraphHopper(com.graphhopper.GraphHopper) org.locationtech.jts.geom(org.locationtech.jts.geom) com.graphhopper.util(com.graphhopper.util) Logger(org.slf4j.Logger) Context(javax.ws.rs.core.Context) ResponsePathSerializer(com.graphhopper.jackson.ResponsePathSerializer) LocationIndex(com.graphhopper.storage.index.LocationIndex) RouteResource.errorIfLegacyParameters(com.graphhopper.resources.RouteResource.errorIfLegacyParameters) LongParam(io.dropwizard.jersey.params.LongParam) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) NotNull(javax.validation.constraints.NotNull) ResponseType.geojson(com.graphhopper.resources.IsochroneResource.ResponseType.geojson) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) GHPointParam(com.graphhopper.http.GHPointParam) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) ContourBuilder(com.graphhopper.isochrone.algorithm.ContourBuilder) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Weighting(com.graphhopper.routing.weighting.Weighting) ToDoubleFunction(java.util.function.ToDoubleFunction) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) Snap(com.graphhopper.storage.index.Snap) UriInfo(javax.ws.rs.core.UriInfo) EDGE_BASED(com.graphhopper.routing.util.TraversalMode.EDGE_BASED) Collections(java.util.Collections) RouteResource.removeLegacyParameters(com.graphhopper.resources.RouteResource.removeLegacyParameters) GraphEdgeIdFinder(com.graphhopper.storage.GraphEdgeIdFinder) Triangulator(com.graphhopper.isochrone.algorithm.Triangulator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) TraversalMode(com.graphhopper.routing.util.TraversalMode) Snap(com.graphhopper.storage.index.Snap) Profile(com.graphhopper.config.Profile) FiniteWeightFilter(com.graphhopper.routing.util.FiniteWeightFilter) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefaultSnapFilter(com.graphhopper.routing.util.DefaultSnapFilter) LocationIndex(com.graphhopper.storage.index.LocationIndex) Graph(com.graphhopper.storage.Graph) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph) BlockAreaWeighting(com.graphhopper.routing.weighting.BlockAreaWeighting) Weighting(com.graphhopper.routing.weighting.Weighting) BooleanEncodedValue(com.graphhopper.routing.ev.BooleanEncodedValue) ContourBuilder(com.graphhopper.isochrone.algorithm.ContourBuilder) ShortestPathTree(com.graphhopper.isochrone.algorithm.ShortestPathTree) QueryGraph(com.graphhopper.routing.querygraph.QueryGraph)

Example 5 with Profile

use of com.graphhopper.config.Profile in project graphhopper by graphhopper.

the class GraphHopperManaged method resolveCustomModelFiles.

public static List<Profile> resolveCustomModelFiles(String customModelFolder, List<Profile> profiles) {
    ObjectMapper yamlOM = Jackson.initObjectMapper(new ObjectMapper(new YAMLFactory()));
    ObjectMapper jsonOM = Jackson.newObjectMapper();
    List<Profile> newProfiles = new ArrayList<>();
    for (Profile profile : profiles) {
        if (!CustomWeighting.NAME.equals(profile.getWeighting())) {
            newProfiles.add(profile);
            continue;
        }
        Object cm = profile.getHints().getObject("custom_model", null);
        if (cm != null) {
            try {
                // custom_model can be an object tree (read from config) or an object (e.g. from tests)
                CustomModel customModel = jsonOM.readValue(jsonOM.writeValueAsBytes(cm), CustomModel.class);
                newProfiles.add(new CustomProfile(profile).setCustomModel(customModel));
                continue;
            } catch (Exception ex) {
                throw new RuntimeException("Cannot load custom_model from " + cm + " for profile " + profile.getName(), ex);
            }
        }
        String customModelFileName = profile.getHints().getString("custom_model_file", "");
        if (customModelFileName.isEmpty())
            throw new IllegalArgumentException("Missing 'custom_model' or 'custom_model_file' field in profile '" + profile.getName() + "'. To use default specify custom_model_file: empty");
        if ("empty".equals(customModelFileName))
            newProfiles.add(new CustomProfile(profile).setCustomModel(new CustomModel()));
        else {
            if (customModelFileName.contains(File.separator))
                throw new IllegalArgumentException("Use custom_model_folder for the custom_model_file parent");
            // Somehow dropwizard makes it very hard to find out the folder of config.yml -> use an extra parameter for the folder
            File file = Paths.get(customModelFolder).resolve(customModelFileName).toFile();
            try {
                CustomModel customModel = (customModelFileName.endsWith(".json") ? jsonOM : yamlOM).readValue(file, CustomModel.class);
                newProfiles.add(new CustomProfile(profile).setCustomModel(customModel));
            } catch (Exception ex) {
                throw new RuntimeException("Cannot load custom_model from location " + customModelFileName + " for profile " + profile.getName(), ex);
            }
        }
    }
    return newProfiles;
}
Also used : ArrayList(java.util.ArrayList) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) CustomProfile(com.graphhopper.routing.weighting.custom.CustomProfile) Profile(com.graphhopper.config.Profile) CustomModel(com.graphhopper.util.CustomModel) YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Profile (com.graphhopper.config.Profile)203 LMProfile (com.graphhopper.config.LMProfile)167 CHProfile (com.graphhopper.config.CHProfile)164 Test (org.junit.jupiter.api.Test)138 CustomProfile (com.graphhopper.routing.weighting.custom.CustomProfile)84 GraphHopper (com.graphhopper.GraphHopper)54 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)54 GHPoint (com.graphhopper.util.shapes.GHPoint)35 ArrayList (java.util.ArrayList)29 File (java.io.File)25 GraphHopperServerConfiguration (com.graphhopper.application.GraphHopperServerConfiguration)19 GraphHopperServerTestConfiguration (com.graphhopper.application.util.GraphHopperServerTestConfiguration)19 PMap (com.graphhopper.util.PMap)14 SRTMProvider (com.graphhopper.reader.dem.SRTMProvider)11 BeforeAll (org.junit.jupiter.api.BeforeAll)10 Weighting (com.graphhopper.routing.weighting.Weighting)9 LocationIndexTree (com.graphhopper.storage.index.LocationIndexTree)8 TranslationMap (com.graphhopper.util.TranslationMap)7 Snap (com.graphhopper.storage.index.Snap)6 GHRequest (com.graphhopper.GHRequest)5