OsmAnd/OsmAnd-java/src/main/java/net/osmand/router/RouteStatisticsHelper.java

311 lines
11 KiB
Java
Raw Normal View History

package net.osmand.router;
2019-07-07 19:31:38 +02:00
import net.osmand.binary.BinaryMapRouteReaderAdapter;
import net.osmand.binary.RouteDataObject;
2019-03-07 18:02:41 +01:00
import net.osmand.render.RenderingRuleSearchRequest;
import net.osmand.render.RenderingRulesStorage;
2019-03-01 12:35:59 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
2019-07-07 18:30:01 +02:00
public class RouteStatisticsHelper {
public static final String UNDEFINED_ATTR = "undefined";
2019-07-07 23:13:36 +02:00
private static final double H_STEP = 5;
private static final double H_SLOPE_APPROX = 100;
private static final int MIN_INCLINE = -100;
private static final int MIN_DIVIDED_INCLINE = -20;
private static final int MAX_INCLINE = 100;
private static final int MAX_DIVIDED_INCLINE = 20;
private static final int STEP = 4;
private static final int NUM;
private static final int[] BOUNDARIES_ARRAY;
static {
NUM = ((MAX_DIVIDED_INCLINE - MIN_DIVIDED_INCLINE) / STEP) + 3;
BOUNDARIES_ARRAY = new int[NUM];
BOUNDARIES_ARRAY[0] = MIN_INCLINE;
for (int i = 1; i < NUM - 1; i++) {
BOUNDARIES_ARRAY[i] = MIN_DIVIDED_INCLINE + (i - 1) * STEP;
}
BOUNDARIES_ARRAY[NUM - 1] = MAX_INCLINE;
}
public static class RouteStatistics {
public final List<RouteSegmentAttribute> elements;
public final Map<String, RouteSegmentAttribute> partition;
public final float totalDistance;
private RouteStatistics(List<RouteSegmentAttribute> elements,
Map<String, RouteSegmentAttribute> partition,
float totalDistance) {
this.elements = elements;
this.partition = partition;
this.totalDistance = totalDistance;
}
}
private static class RouteSegmentWithIncline {
RouteDataObject obj;
float dist;
float h;
float[] interpolatedHeightByStep;
float[] slopeByStep;
}
2019-03-07 18:02:41 +01:00
2019-07-07 18:30:01 +02:00
public static List<RouteStatistics> calculateRouteStatistic(List<RouteSegmentResult> route, RenderingRulesStorage currentRenderer,
RenderingRulesStorage defaultRenderer, RenderingRuleSearchRequest currentSearchRequest,
RenderingRuleSearchRequest defaultSearchRequest) {
2019-07-07 23:13:36 +02:00
List<RouteSegmentWithIncline> routeSegmentWithInclines = calculateInclineRouteSegments(route);
List<String> attributeNames = new ArrayList<>();
for (String s : currentRenderer.getRenderingAttributeNames()) {
if (s.startsWith("routeInfo_")) {
attributeNames.add(s);
}
}
if(attributeNames.isEmpty()) {
for (String s : defaultRenderer.getRenderingAttributeNames()) {
if (s.startsWith("routeInfo_")) {
attributeNames.add(s);
}
}
}
2019-03-07 18:02:41 +01:00
2019-07-07 23:13:36 +02:00
// "steepnessColor", "surfaceColor", "roadClassColor", "smoothnessColor"
// steepness=-19_-16
List<RouteStatistics> result = new ArrayList<>();
for(String attributeName : attributeNames) {
2019-07-07 19:43:13 +02:00
RouteStatisticComputer statisticComputer =
2019-07-07 23:13:36 +02:00
new RouteStatisticComputer(currentRenderer, defaultRenderer, currentSearchRequest, defaultSearchRequest);
result.add(statisticComputer.computeStatistic(routeSegmentWithInclines, attributeName));
2019-07-07 18:30:01 +02:00
}
2019-03-07 18:02:41 +01:00
2019-07-07 18:30:01 +02:00
return result;
2019-03-07 18:02:41 +01:00
}
2019-07-07 23:13:36 +02:00
private static List<RouteSegmentWithIncline> calculateInclineRouteSegments(List<RouteSegmentResult> route) {
List<RouteSegmentWithIncline> input = new ArrayList<>();
float prevHeight = 0;
int totalArrayHeightsLength = 0;
for(RouteSegmentResult r : route) {
float[] heightValues = r.getHeightValues();
RouteSegmentWithIncline incl = new RouteSegmentWithIncline();
incl.dist = r.getDistance();
incl.obj = r.getObject();
input.add(incl);
float prevH = prevHeight;
int indStep = 0;
if(incl.dist > H_STEP) {
incl.interpolatedHeightByStep = new float[(int) (incl.dist / H_STEP) - 1];
totalArrayHeightsLength += incl.interpolatedHeightByStep.length;
}
if(heightValues != null && heightValues.length > 0) {
int indH = 2;
float distCum = 0;
prevH = heightValues[1];
incl.h = prevH ;
while(indStep < incl.interpolatedHeightByStep.length && indH < heightValues.length) {
float dist = heightValues[indH] + distCum;
if(dist > (indStep + 1) * H_STEP) {
if(dist == distCum) {
incl.interpolatedHeightByStep[indStep] = prevH;
} else {
incl.interpolatedHeightByStep[indStep] = (float) (prevH +
((indStep + 1) * H_STEP - distCum) *
(heightValues[indH + 1] - prevH) / (dist - distCum));
}
indStep++;
} else {
distCum = dist;
prevH = heightValues[indH + 1];
indH += 2;
}
}
2019-07-07 19:31:38 +02:00
2019-07-07 23:13:36 +02:00
} else {
incl.h = prevH;
}
while(indStep < incl.interpolatedHeightByStep.length) {
incl.interpolatedHeightByStep[indStep++] = prevH;
}
prevHeight = prevH;
}
int slopeSmoothShift = (int) (H_SLOPE_APPROX / (2 * H_STEP));
float[] heightArray = new float[totalArrayHeightsLength];
int iter = 0;
for(int i = 0; i < input.size(); i ++) {
RouteSegmentWithIncline rswi = input.get(i);
for(int k = 0; rswi.interpolatedHeightByStep != null &&
k < rswi.interpolatedHeightByStep.length; k++) {
heightArray[iter++] = rswi.interpolatedHeightByStep[k];
}
}
iter = 0;
for(int i = 0; i < input.size(); i ++) {
RouteSegmentWithIncline rswi = input.get(i);
if(rswi.interpolatedHeightByStep != null) {
rswi.slopeByStep = new float[rswi.interpolatedHeightByStep.length];
for (int k = 0; k < rswi.slopeByStep.length; k++) {
if (iter > slopeSmoothShift && iter + slopeSmoothShift < heightArray.length) {
rswi.slopeByStep[k] =
(float) ((heightArray[iter + slopeSmoothShift] - heightArray[iter - slopeSmoothShift]) * 100
/ H_SLOPE_APPROX);
}
iter++;
}
}
}
return input;
2019-07-07 19:31:38 +02:00
}
2019-07-07 23:13:36 +02:00
2019-07-07 19:43:13 +02:00
private static class RouteStatisticComputer {
2019-03-07 18:02:41 +01:00
final RenderingRulesStorage currentRenderer;
final RenderingRulesStorage defaultRenderer;
final RenderingRuleSearchRequest currentRenderingRuleSearchRequest;
final RenderingRuleSearchRequest defaultRenderingRuleSearchRequest;
2019-03-07 18:02:41 +01:00
2019-07-07 23:13:36 +02:00
RouteStatisticComputer(RenderingRulesStorage currentRenderer, RenderingRulesStorage defaultRenderer,
2019-07-07 18:30:01 +02:00
RenderingRuleSearchRequest currentRenderingRuleSearchRequest, RenderingRuleSearchRequest defaultRenderingRuleSearchRequest) {
this.currentRenderer = currentRenderer;
this.defaultRenderer = defaultRenderer;
this.currentRenderingRuleSearchRequest = currentRenderingRuleSearchRequest;
this.defaultRenderingRuleSearchRequest = defaultRenderingRuleSearchRequest;
2019-03-07 18:02:41 +01:00
}
2019-07-07 23:13:36 +02:00
public RouteStatistics computeStatistic(List<RouteSegmentWithIncline> route, String attribute) {
List<RouteSegmentAttribute> routeAttributes = processRoute(route, attribute);
Map<String, RouteSegmentAttribute> partition = makePartition(routeAttributes);
float totalDistance = computeTotalDistance(routeAttributes);
return new RouteStatistics(routeAttributes, partition, totalDistance);
}
2019-07-07 19:43:13 +02:00
Map<String, RouteSegmentAttribute> makePartition(List<RouteSegmentAttribute> routeAttributes) {
Map<String, RouteSegmentAttribute> partition = new TreeMap<>();
for (RouteSegmentAttribute attribute : routeAttributes) {
RouteSegmentAttribute attr = partition.get(attribute.propertyName);
if (attr == null) {
attr = new RouteSegmentAttribute(attribute);
partition.put(attribute.propertyName, attr);
2019-03-07 18:02:41 +01:00
}
2019-07-07 19:43:13 +02:00
attr.incrementDistanceBy(attribute.getDistance());
2019-03-07 18:02:41 +01:00
}
return partition;
}
2019-07-07 19:43:13 +02:00
private float computeTotalDistance(List<RouteSegmentAttribute> attributes) {
2019-03-07 18:02:41 +01:00
float distance = 0f;
for (RouteSegmentAttribute attribute : attributes) {
distance += attribute.getDistance();
}
return distance;
}
2019-07-07 23:13:36 +02:00
protected List<RouteSegmentAttribute> processRoute(List<RouteSegmentWithIncline> route, String attribute) {
2019-07-07 19:31:38 +02:00
List<RouteSegmentAttribute> routes = new ArrayList<>();
2019-07-07 23:13:36 +02:00
RouteSegmentAttribute prev = null;
for (RouteSegmentWithIncline segment : route) {
RouteSegmentAttribute current = classifySegment(attribute, segment);
if (prev != null && prev.getPropertyName() != null &&
prev.getPropertyName().equals(current.getPropertyName())) {
prev.incrementDistanceBy(current.distance);
} else {
routes.add(current);
prev = current;
2019-03-07 18:02:41 +01:00
}
}
return routes;
}
2019-07-07 19:43:13 +02:00
2019-07-07 23:13:36 +02:00
public RouteSegmentAttribute classifySegment(String attribute, RouteSegmentWithIncline segment) {
RouteSegmentAttribute res = new RouteSegmentAttribute(UNDEFINED_ATTR, 0);
RenderingRuleSearchRequest currentRequest = new RenderingRuleSearchRequest(currentRenderingRuleSearchRequest);
if (searchRenderingAttribute(attribute, currentRenderer, currentRequest, segment)) {
res = new RouteSegmentAttribute(currentRequest.getStringPropertyValue(currentRenderer.PROPS.R_ATTR_STRING_VALUE),
currentRequest.getIntPropertyValue(currentRenderer.PROPS.R_ATTR_COLOR_VALUE));
} else {
2019-07-07 23:13:36 +02:00
RenderingRuleSearchRequest defaultRequest = new RenderingRuleSearchRequest(defaultRenderingRuleSearchRequest);
if (searchRenderingAttribute(attribute, defaultRenderer, defaultRequest, segment)) {
res = new RouteSegmentAttribute(defaultRequest.getStringPropertyValue(defaultRenderer.PROPS.R_ATTR_STRING_VALUE),
defaultRequest.getIntPropertyValue(defaultRenderer.PROPS.R_ATTR_COLOR_VALUE));
}
}
2019-07-07 23:13:36 +02:00
res.distance = segment.dist;
2019-07-07 19:31:38 +02:00
return res;
}
2019-03-07 18:02:41 +01:00
2019-07-07 23:13:36 +02:00
protected boolean searchRenderingAttribute(String attribute,
RenderingRulesStorage rrs, RenderingRuleSearchRequest req, RouteSegmentWithIncline segment) {
2019-07-07 19:31:38 +02:00
//String additional = attrName + "=" + attribute;
2019-07-07 23:13:36 +02:00
RouteDataObject obj = segment.obj;
2019-07-07 19:31:38 +02:00
int[] tps = obj.getTypes();
String additional = "";
2019-07-07 19:58:44 +02:00
for (int k = 0; k < tps.length; k++) {
2019-07-07 19:31:38 +02:00
BinaryMapRouteReaderAdapter.RouteTypeRule tp = obj.region.quickGetEncodingRule(tps[k]);
2019-07-07 19:58:44 +02:00
if (tp.getTag().equals("highway") || tp.getTag().equals("route") ||
2019-07-07 19:31:38 +02:00
tp.getTag().equals("railway") || tp.getTag().equals("aeroway") || tp.getTag().equals("aerialway")) {
req.setStringFilter(rrs.PROPS.R_TAG, tp.getTag());
req.setStringFilter(rrs.PROPS.R_TAG, tp.getValue());
} else {
2019-07-07 19:58:44 +02:00
if (additional.length() > 0) {
2019-07-07 19:31:38 +02:00
additional += ";";
}
2019-07-07 19:58:44 +02:00
additional += tp.getTag() + "=" + tp.getValue();
2019-07-07 19:31:38 +02:00
}
}
req.setStringFilter(rrs.PROPS.R_ADDITIONAL, additional);
2019-07-07 23:13:36 +02:00
return req.searchRenderingAttribute(attribute);
2019-07-07 19:31:38 +02:00
}
2019-03-07 18:02:41 +01:00
}
2019-07-07 19:31:38 +02:00
public static class RouteSegmentAttribute {
2019-03-07 18:02:41 +01:00
private final int color;
private final String propertyName;
private float distance;
2019-07-07 23:13:36 +02:00
RouteSegmentAttribute(String propertyName, int color) {
2019-03-07 18:02:41 +01:00
this.propertyName = propertyName;
this.color = color;
}
2019-07-07 19:31:38 +02:00
RouteSegmentAttribute(RouteSegmentAttribute segmentAttribute) {
2019-03-07 18:02:41 +01:00
this.propertyName = segmentAttribute.getPropertyName();
this.color = segmentAttribute.getColor();
}
public float getDistance() {
return distance;
}
public void incrementDistanceBy(float distance) {
this.distance += distance;
}
public String getPropertyName() {
return propertyName;
}
public int getColor() {
return color;
}
@Override
public String toString() {
return "RouteSegmentAttribute{" +
2019-07-07 23:13:36 +02:00
"propertyName='" + propertyName + '\'' +
2019-03-07 18:02:41 +01:00
", color='" + color + '\'' +
", distance=" + distance +
'}';
2019-03-01 12:35:59 +01:00
}
}
2019-03-07 18:02:41 +01:00
}