Skip to content

Commit

Permalink
refactor: move buildIsochrone from RoutingProfile to IsochroneRequest
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 542eaf7 commit 0f5941b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@
*/
package org.heigit.ors.isochrones;

import org.apache.log4j.Logger;
import org.heigit.ors.common.ServiceRequest;
import org.heigit.ors.common.TravelRangeType;
import org.heigit.ors.common.TravellerInfo;
import org.heigit.ors.exceptions.InternalServerException;
import org.heigit.ors.isochrones.statistics.StatisticsProvider;
import org.heigit.ors.isochrones.statistics.StatisticsProviderConfiguration;
import org.heigit.ors.routing.RoutingProfile;
import org.heigit.ors.routing.RoutingProfileManager;
import org.heigit.ors.routing.RoutingProfileType;
import org.heigit.ors.routing.WeightingMethod;
import org.heigit.ors.isochrones.statistics.StatisticsProviderFactory;
import org.heigit.ors.routing.*;
import org.heigit.ors.util.DebugUtility;
import org.heigit.ors.util.ProfileTools;
import org.locationtech.jts.geom.Coordinate;

import java.util.*;

public class IsochroneRequest extends ServiceRequest {
public static final Logger LOGGER = Logger.getLogger(IsochroneRequest.class);
private String profileName;
private final List<TravellerInfo> travellers;
private String calcMethod;
Expand Down Expand Up @@ -290,9 +294,83 @@ public IsochroneMapCollection computeIsochrones(RoutingProfileManager routingPro
for (int i = 0; i < getTravellers().size(); ++i) {
IsochroneSearchParameters searchParams = getSearchParameters(i);
RoutingProfile rp = routingProfileManager.getRoutingProfile(searchParams.getRouteParameters().getProfileName());
IsochroneMap isochroneMap = rp.buildIsochrone(searchParams);
IsochroneMap isochroneMap = buildIsochrone(searchParams, rp);
isoMaps.add(isochroneMap);
}
return isoMaps;
}

/**
* This function creates the actual {@link IsochroneMap}.
* So the first step in the function is a checkup on that.
*
* @param parameters The input are {@link IsochroneSearchParameters}
* @param routingProfile
* @return The return will be an {@link IsochroneMap}
* @throws Exception
*/
public IsochroneMap buildIsochrone(IsochroneSearchParameters parameters, RoutingProfile routingProfile) throws Exception {
// TODO: refactor buildIsochrone as to not need to pass the SearchParameters as they are already present
// in IsochroneRequest. maybe merge with computeIsochrones
IsochroneMap result;

try {
RouteSearchContext searchCntx = routingProfile.createSearchContext(parameters.getRouteParameters());
IsochroneMapBuilderFactory isochroneMapBuilderFactory = new IsochroneMapBuilderFactory(searchCntx);
result = isochroneMapBuilderFactory.buildMap(parameters);
} catch (Exception ex) {
if (DebugUtility.isDebug()) {
LOGGER.error(ex);
}
throw new InternalServerException(IsochronesErrorCodes.UNKNOWN, "Unable to build an isochrone map.");
}

if (result.getIsochronesCount() > 0) {
if (parameters.hasAttribute(ProfileTools.KEY_TOTAL_POP)) {
try {
Map<StatisticsProviderConfiguration, List<String>> mapProviderToAttrs = new HashMap<>();
StatisticsProviderConfiguration provConfig = parameters.getStatsProviders().get(ProfileTools.KEY_TOTAL_POP);
if (provConfig != null) {
List<String> attrList = new ArrayList<>();
attrList.add(ProfileTools.KEY_TOTAL_POP);
mapProviderToAttrs.put(provConfig, attrList);
}
for (Map.Entry<StatisticsProviderConfiguration, List<String>> entry : mapProviderToAttrs.entrySet()) {
provConfig = entry.getKey();
StatisticsProvider provider = StatisticsProviderFactory.getProvider(provConfig.getName(), provConfig.getParameters());
String[] provAttrs = provConfig.getMappedProperties(entry.getValue());

for (Isochrone isochrone : result.getIsochrones()) {

double[] attrValues = provider.getStatistics(isochrone, provAttrs);
isochrone.setAttributes(entry.getValue(), attrValues, provConfig.getAttribution());

}
}
} catch (Exception ex) {
LOGGER.error(ex);

throw new InternalServerException(IsochronesErrorCodes.UNKNOWN, "Unable to compute isochrone total_pop attribute.");
}
}
if (parameters.hasAttribute("reachfactor") || parameters.hasAttribute("area")) {
for (Isochrone isochrone : result.getIsochrones()) {
String units = parameters.getUnits();
String areaUnits = parameters.getAreaUnits();
if (areaUnits != null) units = areaUnits;
double area = isochrone.calcArea(units);
if (parameters.hasAttribute("area")) {
isochrone.setArea(area);
}
if (parameters.hasAttribute("reachfactor")) {
double reachfactor = isochrone.calcReachfactor(units);
// reach factor could be > 1, which would confuse people
reachfactor = (reachfactor > 1) ? 1 : reachfactor;
isochrone.setReachfactor(reachfactor);
}
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
import org.heigit.ors.config.profile.PreparationProperties;
import org.heigit.ors.config.profile.ProfileProperties;
import org.heigit.ors.exceptions.InternalServerException;
import org.heigit.ors.isochrones.*;
import org.heigit.ors.isochrones.statistics.StatisticsProvider;
import org.heigit.ors.isochrones.statistics.StatisticsProviderConfiguration;
import org.heigit.ors.isochrones.statistics.StatisticsProviderFactory;
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;
Expand All @@ -47,7 +43,6 @@
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.DebugUtility;
import org.heigit.ors.util.ProfileTools;
import org.heigit.ors.util.StringUtility;
import org.heigit.ors.util.TimeUtility;
Expand Down Expand Up @@ -545,77 +540,6 @@ boolean requiresTimeDependentAlgorithm(RouteSearchParameters searchParams, Route
|| mGraphHopper.isTrafficEnabled();
}

/**
* This function creates the actual {@link IsochroneMap}.
* So the first step in the function is a checkup on that.
*
* @param parameters The input are {@link IsochroneSearchParameters}
* @return The return will be an {@link IsochroneMap}
* @throws Exception
*/
public IsochroneMap buildIsochrone(IsochroneSearchParameters parameters) throws Exception {
IsochroneMap result;

try {
RouteSearchContext searchCntx = createSearchContext(parameters.getRouteParameters());
IsochroneMapBuilderFactory isochroneMapBuilderFactory = new IsochroneMapBuilderFactory(searchCntx);
result = isochroneMapBuilderFactory.buildMap(parameters);
} catch (Exception ex) {
if (DebugUtility.isDebug()) {
LOGGER.error(ex);
}
throw new InternalServerException(IsochronesErrorCodes.UNKNOWN, "Unable to build an isochrone map.");
}

if (result.getIsochronesCount() > 0) {
if (parameters.hasAttribute(ProfileTools.KEY_TOTAL_POP)) {
try {
Map<StatisticsProviderConfiguration, List<String>> mapProviderToAttrs = new HashMap<>();
StatisticsProviderConfiguration provConfig = parameters.getStatsProviders().get(ProfileTools.KEY_TOTAL_POP);
if (provConfig != null) {
List<String> attrList = new ArrayList<>();
attrList.add(ProfileTools.KEY_TOTAL_POP);
mapProviderToAttrs.put(provConfig, attrList);
}
for (Map.Entry<StatisticsProviderConfiguration, List<String>> entry : mapProviderToAttrs.entrySet()) {
provConfig = entry.getKey();
StatisticsProvider provider = StatisticsProviderFactory.getProvider(provConfig.getName(), provConfig.getParameters());
String[] provAttrs = provConfig.getMappedProperties(entry.getValue());

for (Isochrone isochrone : result.getIsochrones()) {

double[] attrValues = provider.getStatistics(isochrone, provAttrs);
isochrone.setAttributes(entry.getValue(), attrValues, provConfig.getAttribution());

}
}
} catch (Exception ex) {
LOGGER.error(ex);

throw new InternalServerException(IsochronesErrorCodes.UNKNOWN, "Unable to compute isochrone total_pop attribute.");
}
}
if (parameters.hasAttribute("reachfactor") || parameters.hasAttribute("area")) {
for (Isochrone isochrone : result.getIsochrones()) {
String units = parameters.getUnits();
String areaUnits = parameters.getAreaUnits();
if (areaUnits != null) units = areaUnits;
double area = isochrone.calcArea(units);
if (parameters.hasAttribute("area")) {
isochrone.setArea(area);
}
if (parameters.hasAttribute("reachfactor")) {
double reachfactor = isochrone.calcReachfactor(units);
// reach factor could be > 1, which would confuse people
reachfactor = (reachfactor > 1) ? 1 : reachfactor;
isochrone.setReachfactor(reachfactor);
}
}
}
}
return result;
}

public boolean equals(Object o) {
return o != null && o.getClass().equals(RoutingProfile.class) && this.hashCode() == o.hashCode();
}
Expand Down

0 comments on commit 0f5941b

Please sign in to comment.