Skip to content

Commit

Permalink
dynamicWeights check, cleanup & refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
takb committed Feb 18, 2019
1 parent 963c441 commit 74a086f
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 731 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ResultTest() {
addParameter("carProfile", "driving-car");

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

@Test
Expand Down Expand Up @@ -1513,18 +1513,19 @@ public void testAlternativeRoutes() {
.param("instructions", "true")
.param("preference", getParameter("preference"))
.param("profile", getParameter("carProfile"))
.param("options", "{\"alternative_routes\": 2}")
.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))
.body("routes[0].summary.distance", is(5942.1f))
.body("routes[0].summary.duration", is(776.1f))
.body("routes[1].summary.distance", is(6435))
.body("routes[1].summary.duration", is(801.5f))
.statusCode(200);
}

}

152 changes: 91 additions & 61 deletions openrouteservice/src/main/java/heigit/ors/routing/RouteResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,123 +14,109 @@
package heigit.ors.routing;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.graphhopper.util.PointList;
import com.graphhopper.util.shapes.BBox;
import com.vividsolutions.jts.geom.Coordinate;
import heigit.ors.common.DistanceUnit;
import heigit.ors.util.FormatUtility;
import heigit.ors.util.GeomUtility;

public class RouteResult
{
private RouteSummary _summary;
private Coordinate[] _geometry;
private List<RouteSegment> _segments;
private List<RouteExtraInfo> _extraInfo;
private int[] _wayPointsIndices = null;
private int _locationIndex = 0;
private RouteSummary summary;
private Coordinate[] geometry;
private List<RouteSegment> segments;
private List<RouteExtraInfo> extraInfo;
private List<Integer> wayPointsIndices;
private List<RouteWarning> routeWarnings;
private PointList pointlist;

public RouteResult(int routeExtras) throws Exception
{
_segments = new ArrayList<RouteSegment>();
_summary = new RouteSummary();

public RouteResult(int routeExtras) {
segments = new ArrayList<>();
summary = new RouteSummary();
if (routeExtras != 0)
_extraInfo = new ArrayList<RouteExtraInfo>();

extraInfo = new ArrayList<>();
routeWarnings = new ArrayList<>();
wayPointsIndices = new ArrayList<>();
}

public void addSegment(RouteSegment seg)
{
_segments.add(seg);
segments.add(seg);
}

public List<RouteSegment> getSegments()
{
return _segments;
return segments;
}

public RouteSummary getSummary()
{
return _summary;
return summary;
}

public Coordinate[] getGeometry() {
return _geometry;
return geometry;
}

public void addPoints(PointList points, boolean skipFirstPoint, boolean includeElevation)
public void addPointsToGeometry(PointList points, boolean skipFirstPoint, boolean includeElevation)
{
int index = skipFirstPoint ? 1 : 0;

if (_geometry == null)
if (geometry == null)
{
int newSize = points.size() - index;
_geometry = new Coordinate[newSize];
geometry = new Coordinate[newSize];

if (includeElevation && points.is3D())
{
for (int i = index; i < newSize; ++i)
_geometry[i] = new Coordinate(points.getLon(i), points.getLat(i), points.getEle(i));
geometry[i] = new Coordinate(points.getLon(i), points.getLat(i), points.getEle(i));
}
else
{
for (int i= index; i < newSize; ++i)
_geometry[i] = new Coordinate(points.getLon(i), points.getLat(i));
geometry[i] = new Coordinate(points.getLon(i), points.getLat(i));
}
}
else
{
int oldSize = _geometry.length;
int oldSize = geometry.length;
int pointsSize = points.size() - index;
int newSize = oldSize + pointsSize;
Coordinate[] coords = new Coordinate[newSize];

for (int i = 0; i < oldSize; i++)
coords[i] = _geometry[i];
for (int i = 0; i < oldSize; i++) {
coords[i] = geometry[i];
}

if (includeElevation && points.is3D())
{
for (int i = 0; i < pointsSize; ++i)
{
int j = i + index;
for (int i = 0; i < pointsSize; ++i) {
int j = i + index;
if (includeElevation && points.is3D())
coords[oldSize + i] = new Coordinate(points.getLon(j), points.getLat(j), points.getEle(j));
}
}
else
{
for (int i = 0; i < pointsSize; ++i)
{
int j = i + index;
else
coords[oldSize + i] = new Coordinate(points.getLon(j), points.getLat(j));
}
}
_geometry = coords;

geometry = coords;
}
}


public List<RouteExtraInfo> getExtraInfo()
{
return _extraInfo;
return extraInfo;
}

public void addExtraInfo(RouteExtraInfo info)
private void addExtraInfo(RouteExtraInfo info)
{
if(_extraInfo == null)
_extraInfo = new ArrayList<>();
_extraInfo.add(info);
if(extraInfo == null)
extraInfo = new ArrayList<>();
extraInfo.add(info);
}

public void addExtraInfo(Collection<RouteExtraInfo> infos)
{
if(_extraInfo == null)
_extraInfo = new ArrayList<>();
_extraInfo.addAll(infos);
}

public void addWarning(RouteWarning warning) {
routeWarnings.add(warning);
}
Expand All @@ -139,19 +125,63 @@ public List<RouteWarning> getWarnings() {
return routeWarnings;
}

public int[] getWayPointsIndices() {
return _wayPointsIndices;
public void addPointlist(PointList pointlistToAdd) {
if (pointlist == null) {
pointlist = pointlistToAdd;
} else {
pointlist.add(pointlistToAdd);
}
}

public List<Integer> getWayPointsIndices() {
return wayPointsIndices;
}

public void setWayPointsIndices(int[] wayPointsIndices) {
_wayPointsIndices = wayPointsIndices;
public void addWayPointIndex(int index) {
wayPointsIndices.add(index);
}

public int getLocationIndex() {
return _locationIndex;
void addExtras(RoutingRequest request, List<RouteExtraInfo> extras) {
if (extras == null)
return;
// add the extras if they generate a "warning" or they were requested
for (RouteExtraInfo extra : extras) {
if (extra.isUsedForWarnings() && extra.getWarningGraphExtension().generatesWarning(extra)) {
addWarning(extra.getWarningGraphExtension().getWarning());
addExtraInfo(extra);
} else if (RouteExtraInfoFlag.isSet(request.getExtraInfo(), RouteExtraInfoFlag.getFromString(extra.getName()))) {
addExtraInfo(extra);
}
}
}

public void setLocationIndex(int locationIndex) {
_locationIndex = locationIndex;
/**
* set route summary values according to segments in this route and request parameters
* @param request for parameter lookup (consider traffic, units, include elevation)
*/
void calculateRouteSummary(RoutingRequest request) {
double distance = 0.0;
double duration = 0.0;
for (RouteSegment seg : getSegments()) {
distance += seg.getDistance();
duration += request.getSearchParameters().getConsiderTraffic() ? seg.getDurationTraffic() : seg.getDuration();
}
summary.setDuration(duration);
summary.setDistance(FormatUtility.roundToDecimalsForUnits(distance, request.getUnits()));
double averageSpeed = 0;
if (duration > 0)
averageSpeed = distance / (request.getUnits() == DistanceUnit.Meters ? 1000 : 1) / (duration / 3600);
summary.setAverageSpeed(FormatUtility.roundToDecimals(averageSpeed, 1));
summary.setBBox(GeomUtility.calculateBoundingBox(pointlist));
if (request.getIncludeElevation()) {
double ascent = 0.0;
double descent = 0.0;
for (RouteSegment seg : getSegments()) {
ascent += seg.getAscent();
descent += seg.getDescent();
}
summary.setAscent(FormatUtility.roundToDecimals(ascent, 1));
summary.setDescent(FormatUtility.roundToDecimals(descent, 1));
}
}
}
Loading

0 comments on commit 74a086f

Please sign in to comment.