Skip to content

Commit a9797c5

Browse files
committed
allow embedded custom models graphhopper#2232
1 parent 03cfaa4 commit a9797c5

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

web-bundle/src/main/java/com/graphhopper/http/GraphHopperManaged.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,29 @@ public GraphHopperManaged(GraphHopperConfig configuration, ObjectMapper objectMa
101101
newProfiles.add(profile);
102102
continue;
103103
}
104+
Object cm = profile.getHints().getObject("custom_model", null);
105+
if (cm != null) {
106+
try {
107+
// custom_model can be an object tree (read from config) or an object (e.g. from tests)
108+
CustomModel customModel = jsonOM.readValue(jsonOM.writeValueAsBytes(cm), CustomModel.class);
109+
newProfiles.add(new CustomProfile(profile).setCustomModel(customModel));
110+
continue;
111+
} catch (Exception ex) {
112+
throw new RuntimeException("Cannot load custom_model from " + cm + " for profile " + profile.getName(), ex);
113+
}
114+
}
104115
String customModelLocation = profile.getHints().getString("custom_model_file", "");
105116
if (customModelLocation.isEmpty())
106-
throw new IllegalArgumentException("Missing 'custom_model_file' field in profile '" + profile.getName() + "' if you want an empty custom model set it to 'empty'");
117+
throw new IllegalArgumentException("Missing 'custom_model' or 'custom_model_file' field in profile '"
118+
+ profile.getName() + "'. To use default specify custom_model_file: empty");
107119
if ("empty".equals(customModelLocation))
108120
newProfiles.add(new CustomProfile(profile).setCustomModel(new CustomModel()));
109121
else
110122
try {
111123
CustomModel customModel = (customModelLocation.endsWith(".json") ? jsonOM : yamlOM).readValue(new File(customModelLocation), CustomModel.class);
112124
newProfiles.add(new CustomProfile(profile).setCustomModel(customModel));
113125
} catch (Exception ex) {
114-
throw new RuntimeException("Cannot load custom_model from " + customModelLocation + " for profile " + profile.getName(), ex);
126+
throw new RuntimeException("Cannot load custom_model from location " + customModelLocation + " for profile " + profile.getName(), ex);
115127
}
116128
}
117129
configuration.setProfiles(newProfiles);

web/src/test/java/com/graphhopper/http/resources/CustomWeightingRouteResourceTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import java.util.Collections;
2828

2929
import static com.graphhopper.http.util.TestUtils.clientTarget;
30+
import static com.graphhopper.json.Statement.If;
31+
import static com.graphhopper.json.Statement.Op.LIMIT;
32+
import static com.graphhopper.json.Statement.Op.MULTIPLY;
3033
import static org.junit.jupiter.api.Assertions.assertEquals;
3134
import static org.junit.jupiter.api.Assertions.assertFalse;
3235

@@ -52,7 +55,12 @@ private static GraphHopperServerConfiguration createConfig() {
5255
new CustomProfile("cargo_bike").setVehicle("bike").
5356
putHint("custom_model_file", "./src/test/resources/com/graphhopper/http/resources/cargo_bike.yml"),
5457
new CustomProfile("json_bike").setVehicle("bike").
55-
putHint("custom_model_file", "./src/test/resources/com/graphhopper/http/resources/json_bike.json"))).
58+
putHint("custom_model_file", "./src/test/resources/com/graphhopper/http/resources/json_bike.json"),
59+
new CustomProfile("custom_bike").
60+
setCustomModel(new CustomModel().
61+
addToSpeed(If("road_class == PRIMARY", LIMIT, 28)).
62+
addToPriority(If("max_width < 1.2", MULTIPLY, 0))).
63+
setVehicle("bike"))).
5664
setCHProfiles(Collections.singletonList(new CHProfile("truck")));
5765
return config;
5866
}
@@ -144,6 +152,21 @@ public void testJsonBike() {
144152
assertEquals(path.get("distance").asDouble(), 660, 10);
145153
}
146154

155+
@Test
156+
public void customBikeShouldBeLikeJsonBike() {
157+
String jsonQuery = "{" +
158+
" \"points\": [[11.58199, 50.0141], [11.5865, 50.0095]]," +
159+
" \"profile\": \"custom_bike\"" +
160+
"}";
161+
final Response response = clientTarget(app, "/route-custom").request().post(Entity.json(jsonQuery));
162+
assertEquals(200, response.getStatus());
163+
JsonNode json = response.readEntity(JsonNode.class);
164+
JsonNode infoJson = json.get("info");
165+
assertFalse(infoJson.has("errors"));
166+
JsonNode path = json.get("paths").get(0);
167+
assertEquals(path.get("distance").asDouble(), 660, 10);
168+
}
169+
147170
static String yamlToJson(String yaml) {
148171
try {
149172
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());

0 commit comments

Comments
 (0)