Skip to content

Commit

Permalink
refactor: move createSearchContext out of RoutingProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha Fendrich authored and sfendrich committed Dec 13, 2024
1 parent 0f5941b commit b1a9d20
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.heigit.ors.routing.*;
import org.heigit.ors.util.DebugUtility;
import org.heigit.ors.util.ProfileTools;
import org.heigit.ors.util.TemporaryUtilShelter;
import org.locationtech.jts.geom.Coordinate;

import java.util.*;
Expand Down Expand Up @@ -315,7 +316,7 @@ public IsochroneMap buildIsochrone(IsochroneSearchParameters parameters, Routing
IsochroneMap result;

try {
RouteSearchContext searchCntx = routingProfile.createSearchContext(parameters.getRouteParameters());
RouteSearchContext searchCntx = TemporaryUtilShelter.createSearchContext(parameters.getRouteParameters(), routingProfile);
IsochroneMapBuilderFactory isochroneMapBuilderFactory = new IsochroneMapBuilderFactory(searchCntx);
result = isochroneMapBuilderFactory.buildMap(parameters);
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.heigit.ors.routing.graphhopper.extensions.ORSGraphHopperStorage;
import org.heigit.ors.routing.graphhopper.extensions.ORSWeightingFactory;
import org.heigit.ors.util.ProfileTools;
import org.heigit.ors.util.TemporaryUtilShelter;
import org.locationtech.jts.geom.Coordinate;

public class MatrixRequest extends ServiceRequest {
Expand Down Expand Up @@ -282,7 +283,7 @@ private MatrixResult computeDijkstraMatrix(GraphHopper gh, FlagEncoder flagEncod
private MatrixResult computeCoreMatrix(GraphHopper gh, FlagEncoder flagEncoder, PMap hintsMap, String profileName, RoutingProfile routingProfile) throws Exception {
Weighting weighting = new ORSWeightingFactory(gh.getGraphHopperStorage(), gh.getEncodingManager()).createWeighting(gh.getProfile(profileName), hintsMap, false);
RoutingCHGraph graph = ((ORSGraphHopperStorage) gh.getGraphHopperStorage()).getCoreGraph(profileName);
RouteSearchContext searchCntx = routingProfile.createSearchContext(getSearchParameters());
RouteSearchContext searchCntx = TemporaryUtilShelter.createSearchContext(getSearchParameters(), routingProfile);
PMap additionalHints = searchCntx.getProperties();
EdgeFilter edgeFilter = new ORSEdgeFilterFactory().createEdgeFilter(additionalHints, flagEncoder, gh.getGraphHopperStorage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@
import org.heigit.ors.config.profile.ExecutionProperties;
import org.heigit.ors.config.profile.PreparationProperties;
import org.heigit.ors.config.profile.ProfileProperties;
import org.heigit.ors.exceptions.InternalServerException;
import org.heigit.ors.routing.graphhopper.extensions.*;
import org.heigit.ors.routing.graphhopper.extensions.flagencoders.FlagEncoderNames;
import org.heigit.ors.routing.graphhopper.extensions.manage.ORSGraphManager;
import org.heigit.ors.routing.graphhopper.extensions.storages.builders.BordersGraphStorageBuilder;
import org.heigit.ors.routing.graphhopper.extensions.storages.builders.GraphStorageBuilder;
import org.heigit.ors.routing.graphhopper.extensions.util.ORSParameters;
import org.heigit.ors.routing.parameters.ProfileParameters;
import org.heigit.ors.routing.pathprocessors.ORSPathProcessorFactory;
import org.heigit.ors.util.ProfileTools;
import org.heigit.ors.util.StringUtility;
Expand Down Expand Up @@ -412,85 +409,6 @@ public Double getAstarEpsilon() {
return astarEpsilon;
}

public RouteSearchContext createSearchContext(RouteSearchParameters searchParams) throws InternalServerException {
PMap props = new PMap();

int profileType = searchParams.getProfileType();
String encoderName = RoutingProfileType.getEncoderName(profileType);

if (FlagEncoderNames.UNKNOWN.equals(encoderName))
throw new InternalServerException(RoutingErrorCodes.UNKNOWN, "unknown vehicle profile.");

if (!mGraphHopper.getEncodingManager().hasEncoder(encoderName)) {
throw new IllegalArgumentException("Vehicle " + encoderName + " unsupported. " + "Supported are: "
+ mGraphHopper.getEncodingManager());
}

FlagEncoder flagEncoder = mGraphHopper.getEncodingManager().getEncoder(encoderName);
ProfileParameters profileParams = searchParams.getProfileParameters();

// PARAMETERS FOR PathProcessorFactory

props.putObject("routing_extra_info", searchParams.getExtraInfo());
props.putObject("routing_suppress_warnings", searchParams.getSuppressWarnings());

props.putObject("routing_profile_type", profileType);
props.putObject("routing_profile_params", profileParams);

/*
* PARAMETERS FOR EdgeFilterFactory
* ======================================================================================================
*/

/* Avoid areas */
if (searchParams.hasAvoidAreas()) {
props.putObject("avoid_areas", searchParams.getAvoidAreas());
}

/* Heavy vehicle filter */
if (profileType == RoutingProfileType.DRIVING_HGV) {
props.putObject("edgefilter_hgv", searchParams.getVehicleType());
}

/* Wheelchair filter */
else if (profileType == RoutingProfileType.WHEELCHAIR) {
props.putObject("edgefilter_wheelchair", "true");
}

/* Avoid features */
if (searchParams.hasAvoidFeatures()) {
props.putObject("avoid_features", searchParams);
}

/* Avoid borders of some form */
if ((searchParams.hasAvoidBorders() || searchParams.hasAvoidCountries())
&& (RoutingProfileType.isDriving(profileType) || RoutingProfileType.isCycling(profileType))) {
props.putObject("avoid_borders", searchParams);
if (searchParams.hasAvoidCountries())
props.putObject("avoid_countries", Arrays.toString(searchParams.getAvoidCountries()));
}

if (profileParams != null && profileParams.hasWeightings()) {
props.putObject(ProfileTools.KEY_CUSTOM_WEIGHTINGS, true);
Iterator<ProfileWeighting> iterator = profileParams.getWeightings().getIterator();
while (iterator.hasNext()) {
ProfileWeighting weighting = iterator.next();
if (!weighting.getParameters().isEmpty()) {
String name = ProfileWeighting.encodeName(weighting.getName());
for (Map.Entry<String, Object> kv : weighting.getParameters().toMap().entrySet())
props.putObject(name + kv.getKey(), kv.getValue());
}
}
}

String localProfileName = ProfileTools.makeProfileName(encoderName, WeightingMethod.getName(searchParams.getWeightingMethod()), Boolean.TRUE.equals(profileProperties.getBuild().getEncoderOptions().getTurnCosts()));
String profileNameCH = ProfileTools.makeProfileName(encoderName, WeightingMethod.getName(searchParams.getWeightingMethod()), false);
RouteSearchContext searchCntx = new RouteSearchContext(mGraphHopper, flagEncoder, localProfileName, profileNameCH);
searchCntx.setProperties(props);

return searchCntx;
}

/**
* 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 @@ -361,7 +361,7 @@ public GHResponse computeRoute(double lat0, double lon0, double lat1, double lon
return res;
}
int weightingMethod = searchParams.getWeightingMethod();
RouteSearchContext searchCntx = routingProfile.createSearchContext(searchParams);
RouteSearchContext searchCntx = TemporaryUtilShelter.createSearchContext(searchParams, routingProfile);

int flexibleMode = searchParams.hasFlexibleMode() || Boolean.TRUE.equals(routingProfile.getProfileConfiguration().getService().getForceTurnCosts()) ? ProfileTools.KEY_FLEX_PREPROCESSED : ProfileTools.KEY_FLEX_STATIC;
boolean optimized = searchParams.getOptimized();
Expand Down Expand Up @@ -473,7 +473,7 @@ public GHResponse computeRoundTripRoute(double lat0, double lon0, WayPointBearin
try {
int profileType = searchParams.getProfileType();
int weightingMethod = searchParams.getWeightingMethod();
RouteSearchContext searchCntx = routingProfile.createSearchContext(searchParams);
RouteSearchContext searchCntx = TemporaryUtilShelter.createSearchContext(searchParams, routingProfile);

List<GHPoint> points = new ArrayList<>();
points.add(new GHPoint(lat0, lon0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.storage.ConditionalEdges;
import org.heigit.ors.routing.RouteSearchContext;
import org.heigit.ors.routing.RouteSearchParameters;
import org.heigit.ors.routing.RoutingProfileType;
import com.graphhopper.util.PMap;
import org.heigit.ors.exceptions.InternalServerException;
import org.heigit.ors.routing.*;
import org.heigit.ors.routing.graphhopper.extensions.flagencoders.FlagEncoderNames;
import org.heigit.ors.routing.parameters.ProfileParameters;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

/**
* This class is only a temporary shelter for helper methods that lack appropriate places
* during refactoring.
Expand Down Expand Up @@ -48,4 +53,84 @@ public static boolean hasTimeDependentSpeed(RouteSearchParameters searchParams,
String key = EncodingManager.getKey(flagEncoder, ConditionalEdges.SPEED);
return searchParams.isTimeDependent() && flagEncoder.hasEncodedValue(key);
}

public static RouteSearchContext createSearchContext(RouteSearchParameters searchParams, RoutingProfile routingProfile) throws InternalServerException {
PMap props = new PMap();

int profileType = searchParams.getProfileType();
String encoderName = RoutingProfileType.getEncoderName(profileType);

if (FlagEncoderNames.UNKNOWN.equals(encoderName))
throw new InternalServerException(RoutingErrorCodes.UNKNOWN, "unknown vehicle profile.");

if (!routingProfile.getGraphhopper().getEncodingManager().hasEncoder(encoderName)) {
throw new IllegalArgumentException("Vehicle " + encoderName + " unsupported. " + "Supported are: "
+ routingProfile.getGraphhopper().getEncodingManager());
}

FlagEncoder flagEncoder = routingProfile.getGraphhopper().getEncodingManager().getEncoder(encoderName);
ProfileParameters profileParams = searchParams.getProfileParameters();

// PARAMETERS FOR PathProcessorFactory

props.putObject("routing_extra_info", searchParams.getExtraInfo());
props.putObject("routing_suppress_warnings", searchParams.getSuppressWarnings());

props.putObject("routing_profile_type", profileType);
props.putObject("routing_profile_params", profileParams);

/*
* PARAMETERS FOR EdgeFilterFactory
* ======================================================================================================
*/

/* Avoid areas */
if (searchParams.hasAvoidAreas()) {
props.putObject("avoid_areas", searchParams.getAvoidAreas());
}

/* Heavy vehicle filter */
if (profileType == RoutingProfileType.DRIVING_HGV) {
props.putObject("edgefilter_hgv", searchParams.getVehicleType());
}

/* Wheelchair filter */
else if (profileType == RoutingProfileType.WHEELCHAIR) {
props.putObject("edgefilter_wheelchair", "true");
}

/* Avoid features */
if (searchParams.hasAvoidFeatures()) {
props.putObject("avoid_features", searchParams);
}

/* Avoid borders of some form */
if ((searchParams.hasAvoidBorders() || searchParams.hasAvoidCountries())
&& (RoutingProfileType.isDriving(profileType) || RoutingProfileType.isCycling(profileType))) {
props.putObject("avoid_borders", searchParams);
if (searchParams.hasAvoidCountries())
props.putObject("avoid_countries", Arrays.toString(searchParams.getAvoidCountries()));
}

if (profileParams != null && profileParams.hasWeightings()) {
props.putObject(ProfileTools.KEY_CUSTOM_WEIGHTINGS, true);
Iterator<ProfileWeighting> iterator = profileParams.getWeightings().getIterator();
while (iterator.hasNext()) {
ProfileWeighting weighting = iterator.next();
if (!weighting.getParameters().isEmpty()) {
String name = ProfileWeighting.encodeName(weighting.getName());
for (Map.Entry<String, Object> kv : weighting.getParameters().toMap().entrySet())
props.putObject(name + kv.getKey(), kv.getValue());
}
}
}

String localProfileName = ProfileTools.makeProfileName(encoderName, WeightingMethod.getName(searchParams.getWeightingMethod()),
Boolean.TRUE.equals(routingProfile.getProfileProperties().getBuild().getEncoderOptions().getTurnCosts()));
String profileNameCH = ProfileTools.makeProfileName(encoderName, WeightingMethod.getName(searchParams.getWeightingMethod()), false);
RouteSearchContext searchCntx = new RouteSearchContext(routingProfile.getGraphhopper(), flagEncoder, localProfileName, profileNameCH);
searchCntx.setProperties(props);

return searchCntx;
}
}

0 comments on commit b1a9d20

Please sign in to comment.