Skip to content

Commit

Permalink
added alternative routes: params for API v1 & v2, result set processi…
Browse files Browse the repository at this point in the history
…ng, tests
  • Loading branch information
takb committed Mar 25, 2019
1 parent 53533a7 commit 90c3ed9
Show file tree
Hide file tree
Showing 18 changed files with 605 additions and 72 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Updated api code to use the Spring framework, with the v2 api being added (Issue #233)
- Added support for ISO 3166-1 Alpha-2 / Alpha-3 codes for routing directions option avoid_countries (Issue #195)
- Added support for free hand route option/ skip segments (Issue #167)
- Added support for GH alternative_route algorithm (Issue #377)
- Added check on matrix service to make sure that the requested locations are within the bounding area of the graph (Issue #408)
- Makes docker and docker-compose deployment of openrouteservice more customizable (Issue #434)
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public ResultTest() {
addParameter("preference", "fastest");
addParameter("bikeProfile", "cycling-regular");
addParameter("carProfile", "driving-car");

// query for testing the alternative routes algorithm
addParameter("coordinatesAR", "8.680401,49.437436|8.746362,49.414191");
}

@Test
Expand Down Expand Up @@ -1210,7 +1213,7 @@ public void testDetourFactor() {
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes[0].segments[0].detourfactor", is(1.38f))
.body("routes[0].segments[0].detourfactor", is(1.3f))
.statusCode(200);
}

Expand Down Expand Up @@ -1449,4 +1452,26 @@ public void testSimplifyHasLessWayPoints() {
.body("features[0].geometry.coordinates.size()", is(34))
.statusCode(200);
}

@Test
public void testAlternativeRoutes() {
given()
.param("coordinates", getParameter("coordinatesAR"))
.param("instructions", "true")
.param("preference", getParameter("preference"))
.param("profile", getParameter("carProfile"))
.param("options", "{\"alternative_routes_count\": 2}")
.when().log().ifValidationFails()
.get(getEndPointName())
.then()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes.size()", is(2))
.body("routes[0].summary.distance", is(8178.2f))
.body("routes[0].summary.duration", is(1087.3f))
.body("routes[1].summary.distance", is(10670.8f))
.body("routes[1].summary.duration", is(1414))
.statusCode(200);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ public ParamsTest() {

addParameter("coordinatesLong", coordsLong);

JSONArray coordsVia= new JSONArray();
JSONArray coordv1 = new JSONArray();
coordv1.put(8.680916);
coordv1.put(49.410973);
coordsVia.put(coordv1);
JSONArray coordv2 = new JSONArray();
coordv2.put(8.687782);
coordv2.put(49.424597);
coordsVia.put(coordv2);
JSONArray coordv3 = new JSONArray();
coordv3.put(8.689061);
coordv3.put(49.421752);
coordsVia.put(coordv3);

addParameter("coordinatesWithViaPoint", coordsVia);



JSONArray extraInfo = new JSONArray();
extraInfo.put("surface");
extraInfo.put("suitability");
Expand Down Expand Up @@ -1342,11 +1360,22 @@ public void expectSkipSegmentsWarnings() {
@Test
public void expectErrorWithMultiSegmentsAndSimplify() {
JSONArray coords = (JSONArray) getParameter("coordinatesShort");
coords.put(new JSONArray(new double[] {8.680916, 49.410973}));
coords.put(new JSONArray(new double[]{8.680916, 49.410973}));

JSONObject body = new JSONObject();
body.put("coordinates", coords);
body.put("geometry_simplify", true);
}

@Test
public void expectAlternativeRoutesToRejectMoreThanTwoWaypoints() {
JSONObject body = new JSONObject();
body.put("coordinates", (JSONArray) getParameter("coordinatesWithViaPoint"));
body.put("preference", getParameter("preference"));

JSONObject ar = new JSONObject();
ar.put("target_count", "3");
body.put("alternative_routes", ar);

given()
.header("Accept", "application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ public void testDetourFactor() {
.then().log().all()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes[0].segments[0].detourfactor", is(1.38f))
.body("routes[0].segments[0].detourfactor", is(1.3f))
.statusCode(200);
}

Expand Down Expand Up @@ -2162,6 +2162,67 @@ public void testAvgSpeedValues() {
.statusCode(200);
}

@Test
public void testAlternativeRoutes() {
JSONObject body = new JSONObject();
JSONArray coordinates = new JSONArray();
JSONArray coord1 = new JSONArray();
coord1.put(8.673191);
coord1.put(49.446812);
coordinates.put(coord1);
JSONArray coord2 = new JSONArray();
coord2.put(8.689499);
coord2.put(49.398295);
coordinates.put(coord2);
body.put("coordinates", coordinates);
body.put("preference", "shortest");
JSONObject ar = new JSONObject();
ar.put("target_count", "2");
ar.put("share_factor", "0.5");
body.put("alternative_routes", ar);

given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("carProfile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then().log().all()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes.size()", is(2))
.body("routes[0].summary.distance", is(5939.8f))
.body("routes[0].summary.duration", is(936.4f))
.body("routes[1].summary.distance", is( 6435.0f))
.body("routes[1].summary.duration", is(801.5f))
.statusCode(200);

JSONObject avoidGeom = new JSONObject("{\"type\":\"Polygon\",\"coordinates\":[[[8.685873,49.414421], [8.688169,49.403978], [8.702095,49.407762], [8.695185,49.416013], [8.685873,49.414421]]]}}");
JSONObject options = new JSONObject();
options.put("avoid_polygons", avoidGeom);
body.put("options", options);

System.out.println(getEndPointPath() + "/{profile}");
given()
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.pathParam("profile", getParameter("carProfile"))
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}")
.then().log().all()
.assertThat()
.body("any { it.key == 'routes' }", is(true))
.body("routes.size()", is(1))
.body("routes[0].summary.distance", is( 6435.0f))
.body("routes[0].summary.duration", is(801.5f))
.statusCode(200);


}


private JSONArray constructCoords(String coordString) {
JSONArray coordinates = new JSONArray();
String[] coordPairs = coordString.split("\\|");
Expand Down
61 changes: 31 additions & 30 deletions openrouteservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,34 +171,34 @@
</plugins>
</build>

<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>bintray-nitram509-jbrotli</id>
<name>bintray</name>
<url>http://dl.bintray.com/nitram509/jbrotli</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>opengeo</id>
<name>OpenGeo Maven Repository</name>
<url>http://repo.boundlessgeo.com/main </url>
</repository>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net repository</name>
<url>http://download.java.net/maven/2</url>
</repository>
<repository>
<id>bintray-nitram509-jbrotli</id>
<name>bintray</name>
<url>http://dl.bintray.com/nitram509/jbrotli</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>osgeo</id>
<name>Open Source Geospatial Foundation Repository</name>
<url>http://download.osgeo.org/webdav/geotools/</url>
</repository>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>opengeo</id>
<name>OpenGeo Maven Repository</name>
<url>http://repo.boundlessgeo.com/main </url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
Expand All @@ -207,6 +207,7 @@
</pluginRepository>
</pluginRepositories>


<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -302,13 +303,13 @@
<dependency>
<groupId>com.github.GIScience.graphhopper</groupId>
<artifactId>graphhopper-core</artifactId>
<version>v0.10.1.12</version>
<version>v0.10.1.14</version>
</dependency>

<dependency>
<groupId>com.github.GIScience.graphhopper</groupId>
<artifactId>graphhopper-reader-osm</artifactId>
<version>v0.10.1.12</version>
<version>v0.10.1.14</version>
</dependency>

<!-- remove the comment to enable debugging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public GeoJSONRouteResponse getSimpleGeoJsonRoute(@ApiParam(value = "Specifies t
RouteRequest request = new RouteRequest(start, end);
request.setProfile(profile);

RouteResult result = new RouteRequestHandler().generateRouteFromRequest(request);
RouteResult[] result = new RouteRequestHandler().generateRouteFromRequest(request);

return new GeoJSONRouteResponse(new RouteResult[] { result }, request);
return new GeoJSONRouteResponse(result, request);
}

@PostMapping(value = "/{profile}")
Expand All @@ -119,9 +119,9 @@ public JSONRouteResponse getJsonRoute(
request.setProfile(profile);
request.setResponseType(APIEnums.RouteResponseType.JSON);

RouteResult result = new RouteRequestHandler().generateRouteFromRequest(request);
RouteResult[] result = new RouteRequestHandler().generateRouteFromRequest(request);

return new JSONRouteResponse(new RouteResult[]{result}, request);
return new JSONRouteResponse(result, request);
}

@PostMapping(value = "/{profile}/gpx", produces = "application/gpx+xml;charset=UTF-8")
Expand All @@ -138,9 +138,9 @@ public GPXRouteResponse getGPXRoute(
request.setProfile(profile);
request.setResponseType(APIEnums.RouteResponseType.GPX);

RouteResult result = new RouteRequestHandler().generateRouteFromRequest(request);
RouteResult[] result = new RouteRequestHandler().generateRouteFromRequest(request);

return new GPXRouteResponse(new RouteResult[]{result}, request);
return new GPXRouteResponse(result, request);

}

Expand All @@ -157,9 +157,9 @@ public GeoJSONRouteResponse getGeoJsonRoute(
request.setProfile(profile);
request.setResponseType(APIEnums.RouteResponseType.GEOJSON);

RouteResult result = new RouteRequestHandler().generateRouteFromRequest(request);
RouteResult[] result = new RouteRequestHandler().generateRouteFromRequest(request);

return new GeoJSONRouteResponse(new RouteResult[]{result}, request);
return new GeoJSONRouteResponse(result, request);
}

@ExceptionHandler(MissingServletRequestParameterException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class RouteRequest {
public static final String PARAM_SUPPRESS_WARNINGS = "suppress_warnings";
public static final String PARAM_SIMPLIFY_GEOMETRY = "geometry_simplify";
public static final String PARAM_SKIP_SEGMENTS = "skip_segments";

public static final String PARAM_ALTERNATIVE_ROUTES = "alternative_routes";

@ApiModelProperty(name = PARAM_ID, value = "Arbitrary identification string of the request reflected in the meta information.",
example = "routing_request")
Expand Down Expand Up @@ -239,6 +239,13 @@ public class RouteRequest {
@JsonIgnore
private boolean hasSkipSegments = false;

@ApiModelProperty(name = PARAM_ALTERNATIVE_ROUTES, value = "Specifies whether alternative routes are computed, and parameters for the algorithm determining suitable alternatives.")
@JsonProperty(PARAM_ALTERNATIVE_ROUTES)
private RouteRequestAlternativeRoutes alternativeRoutes;
@JsonIgnore
private boolean hasAlternativeRoutes = false;


@JsonCreator
public RouteRequest(@JsonProperty(value = PARAM_COORDINATES, required = true) List<List<Double>> coordinates) {
this.coordinates = coordinates;
Expand Down Expand Up @@ -488,6 +495,15 @@ public void setSkipSegments(List<Integer> skipSegments){
hasSkipSegments = true;
}

public RouteRequestAlternativeRoutes getAlternativeRoutes() {
return alternativeRoutes;
}

public void setAlternativeRoutes(RouteRequestAlternativeRoutes alternativeRoutes) {
this.alternativeRoutes = alternativeRoutes;
hasAlternativeRoutes = true;
}

public boolean hasIncludeRoundaboutExitInfo() {
return hasIncludeRoundaboutExitInfo;
}
Expand Down Expand Up @@ -557,4 +573,6 @@ public boolean hasSimplifyGeometry() {
}

public boolean hasSkipSegments() { return hasSkipSegments;}

public boolean hasAlternativeRoutes() { return hasAlternativeRoutes; }
}
Loading

0 comments on commit 90c3ed9

Please sign in to comment.