Skip to content

Commit

Permalink
refactor(RoutingProfile): move computeRoundTrip to RoutingRequest (#1850
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aoles authored Sep 27, 2024
2 parents 84b6611 + b1fa79d commit 557c7b6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ RELEASING:

### Added
### Changed
- refactor: cleanup routing profile management ([#1850](https://github.com/GIScience/openrouteservice/pull/1850)
### Deprecated
### Removed
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.google.common.base.Strings;
import com.graphhopper.GHRequest;
import com.graphhopper.GHResponse;
import com.graphhopper.config.CHProfile;
import com.graphhopper.config.LMProfile;
import com.graphhopper.config.Profile;
Expand All @@ -24,7 +23,6 @@
import com.graphhopper.storage.*;
import com.graphhopper.util.*;
import com.graphhopper.util.shapes.BBox;
import com.graphhopper.util.shapes.GHPoint;
import com.typesafe.config.Config;
import org.apache.log4j.Logger;
import org.heigit.ors.config.EngineConfig;
Expand Down Expand Up @@ -611,67 +609,6 @@ else if (profileType == RoutingProfileType.WHEELCHAIR) {
return searchCntx;
}

public GHResponse computeRoundTripRoute(double lat0, double lon0, WayPointBearing
bearing, RouteSearchParameters searchParams, Boolean geometrySimplify) throws Exception {
GHResponse resp;

try {
int profileType = searchParams.getProfileType();
int weightingMethod = searchParams.getWeightingMethod();
RouteSearchContext searchCntx = createSearchContext(searchParams);

List<GHPoint> points = new ArrayList<>();
points.add(new GHPoint(lat0, lon0));
List<Double> bearings = new ArrayList<>();
GHRequest req;

if (bearing != null) {
bearings.add(bearing.getValue());
req = new GHRequest(points, bearings);
} else {
req = new GHRequest(points);
}

req.setProfile(searchCntx.profileName());
req.getHints().putObject(Parameters.Algorithms.RoundTrip.DISTANCE, searchParams.getRoundTripLength());
req.getHints().putObject(Parameters.Algorithms.RoundTrip.POINTS, searchParams.getRoundTripPoints());

if (searchParams.getRoundTripSeed() > -1) {
req.getHints().putObject(Parameters.Algorithms.RoundTrip.SEED, searchParams.getRoundTripSeed());
}

PMap props = searchCntx.getProperties();
req.setAdditionalHints(props);

if (props != null && !props.isEmpty())
req.getHints().putAll(props);

if (TemporaryUtilShelter.supportWeightingMethod(profileType))
ProfileTools.setWeightingMethod(req.getHints(), weightingMethod, profileType, false);
else
throw new IllegalArgumentException("Unsupported weighting " + weightingMethod + " for profile + " + profileType);

//Roundtrip not possible with preprocessed edges.
setSpeedups(req, false, false, true, searchCntx.profileNameCH());

if (astarEpsilon != null)
req.getHints().putObject("astarbi.epsilon", astarEpsilon);
if (astarApproximation != null)
req.getHints().putObject("astarbi.approximation", astarApproximation);
//Overwrite algorithm selected in setSpeedups
req.setAlgorithm(Parameters.Algorithms.ROUND_TRIP);

mGraphHopper.getRouterConfig().setSimplifyResponse(geometrySimplify);
resp = mGraphHopper.route(req);

} catch (Exception ex) {
LOGGER.error(ex);
throw new InternalServerException(RoutingErrorCodes.UNKNOWN, "Unable to compute a route");
}

return resp;
}

/**
* Set the speedup techniques used for calculating the route.
* Reults in usage of CH, Core or ALT/AStar, if they are enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public RouteResult[] computeRoundTripRoute(RoutingRequest req) throws Exception
bearing = searchParams.getBearings()[0];
}

GHResponse gr = rp.computeRoundTripRoute(c0.y, c0.x, bearing, searchParams, req.getGeometrySimplify());
GHResponse gr = req.computeRoundTripRoute(c0.y, c0.x, bearing, searchParams, req.getGeometrySimplify(), rp);

if (gr.hasErrors()) {
if (!gr.getErrors().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,65 @@ else if (bearings[1] == null)

return resp;
}

public GHResponse computeRoundTripRoute(double lat0, double lon0, WayPointBearing
bearing, RouteSearchParameters searchParams, Boolean geometrySimplify, RoutingProfile routingProfile) throws Exception {
GHResponse resp;

try {
int profileType = searchParams.getProfileType();
int weightingMethod = searchParams.getWeightingMethod();
RouteSearchContext searchCntx = routingProfile.createSearchContext(searchParams);

List<GHPoint> points = new ArrayList<>();
points.add(new GHPoint(lat0, lon0));
List<Double> bearings = new ArrayList<>();
GHRequest req;

if (bearing != null) {
bearings.add(bearing.getValue());
req = new GHRequest(points, bearings);
} else {
req = new GHRequest(points);
}

req.setProfile(searchCntx.profileName());
req.getHints().putObject(Parameters.Algorithms.RoundTrip.DISTANCE, searchParams.getRoundTripLength());
req.getHints().putObject(Parameters.Algorithms.RoundTrip.POINTS, searchParams.getRoundTripPoints());

if (searchParams.getRoundTripSeed() > -1) {
req.getHints().putObject(Parameters.Algorithms.RoundTrip.SEED, searchParams.getRoundTripSeed());
}

PMap props = searchCntx.getProperties();
req.setAdditionalHints(props);

if (props != null && !props.isEmpty())
req.getHints().putAll(props);

if (TemporaryUtilShelter.supportWeightingMethod(profileType))
ProfileTools.setWeightingMethod(req.getHints(), weightingMethod, profileType, false);
else
throw new IllegalArgumentException("Unsupported weighting " + weightingMethod + " for profile + " + profileType);

//Roundtrip not possible with preprocessed edges.
routingProfile.setSpeedups(req, false, false, true, searchCntx.profileNameCH());

if (routingProfile.getAstarEpsilon() != null)
req.getHints().putObject("astarbi.epsilon", routingProfile.getAstarEpsilon());
if (routingProfile.getAstarApproximation() != null)
req.getHints().putObject("astarbi.approximation", routingProfile.getAstarApproximation());
//Overwrite algorithm selected in setSpeedups
req.setAlgorithm(Parameters.Algorithms.ROUND_TRIP);

routingProfile.getGraphhopper().getRouterConfig().setSimplifyResponse(geometrySimplify);
resp = routingProfile.getGraphhopper().route(req);

} catch (Exception ex) {
LOGGER.error(ex);
throw new InternalServerException(RoutingErrorCodes.UNKNOWN, "Unable to compute a route");
}

return resp;
}
}

0 comments on commit 557c7b6

Please sign in to comment.