com.graphhopper.util.CustomModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphhopper-web-api Show documentation
Show all versions of graphhopper-web-api Show documentation
JSON Representation of the API classes
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.util;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.graphhopper.jackson.CustomModelAreasDeserializer;
import com.graphhopper.json.Statement;
import java.util.*;
import java.util.stream.Collectors;
public class CustomModel {
public static final String KEY = "custom_model";
// 'Double' instead of 'double' is required to know if it was 0 or not specified in the request.
private Double distanceInfluence;
private Double headingPenalty;
private boolean internal;
private List speedStatements = new ArrayList<>();
private List priorityStatements = new ArrayList<>();
private JsonFeatureCollection areas = new JsonFeatureCollection();
public CustomModel() {
}
public CustomModel(CustomModel toCopy) {
this.headingPenalty = toCopy.headingPenalty;
this.distanceInfluence = toCopy.distanceInfluence;
// do not copy "internal" boolean
speedStatements = deepCopy(toCopy.getSpeed());
priorityStatements = deepCopy(toCopy.getPriority());
addAreas(toCopy.getAreas());
}
public static Map getAreasAsMap(JsonFeatureCollection areas) {
Map map = new HashMap<>(areas.getFeatures().size());
areas.getFeatures().forEach(f -> {
if (map.put(f.getId(), f) != null)
throw new IllegalArgumentException("Cannot handle duplicate area " + f.getId());
});
return map;
}
public void addAreas(JsonFeatureCollection externalAreas) {
Set indexed = areas.getFeatures().stream().map(JsonFeature::getId).collect(Collectors.toSet());
for (JsonFeature ext : externalAreas.getFeatures()) {
if (!JsonFeature.isValidId("in_" + ext.getId()))
throw new IllegalArgumentException("The area '" + ext.getId() + "' has an invalid id. Only letters, numbers and underscore are allowed.");
if (indexed.contains(ext.getId()))
throw new IllegalArgumentException("area " + ext.getId() + " already exists");
areas.getFeatures().add(ext);
indexed.add(ext.getId());
}
}
/**
* This method is for internal usage only! Parsing a CustomModel is expensive and so we cache the result, which is
* especially important for fast landmark queries (hybrid mode). Now this method ensures that all server-side custom
* models are cached in a special internal cache which does not remove seldom accessed entries.
*/
public CustomModel internal() {
this.internal = true;
return this;
}
public boolean isInternal() {
return internal;
}
private T deepCopy(T originalObject) {
if (originalObject instanceof List) {
List