2018-10-26 18:07:14 +02:00
|
|
|
package net.osmand.router;
|
|
|
|
|
2018-10-31 17:00:07 +01:00
|
|
|
import java.util.*;
|
2018-10-26 18:07:14 +02:00
|
|
|
|
|
|
|
public class RouteStatistics {
|
|
|
|
|
|
|
|
private static final String UNDEFINED = "undefined";
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
private final List<RouteSegmentResult> route;
|
|
|
|
|
|
|
|
private RouteStatistics(List<RouteSegmentResult> route) {
|
|
|
|
this.route = route;
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
2018-10-26 18:07:14 +02:00
|
|
|
|
2018-10-31 17:00:07 +01:00
|
|
|
public static RouteStatistics newRouteStatistic(List<RouteSegmentResult> route) {
|
2018-11-02 17:33:33 +01:00
|
|
|
return new RouteStatistics(route);
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
public Statistics getRouteSurfaceStatistic() {
|
2018-11-02 17:33:33 +01:00
|
|
|
RouteStatisticComputer statisticComputer = new RouteSurfaceStatisticComputer(route);
|
|
|
|
return statisticComputer.computeStatistic();
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
public Statistics getRouteSmoothnessStatistic() {
|
2018-11-02 17:33:33 +01:00
|
|
|
RouteStatisticComputer statisticComputer = new RouteSmoothnessStatisticComputer(route);
|
|
|
|
return statisticComputer.computeStatistic();
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
public Statistics getRouteClassStatistic() {
|
2018-11-02 17:33:33 +01:00
|
|
|
RouteStatisticComputer statisticComputer = new RouteClassStatisticComputer(route);
|
|
|
|
return statisticComputer.computeStatistic();
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
public Statistics getRouteSteepnessStatistic() {
|
2018-11-02 17:33:33 +01:00
|
|
|
RouteStatisticComputer statisticComputer = new RouteSteepnessStatisticComputer(route);
|
|
|
|
return statisticComputer.computeStatistic();
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-31 17:00:07 +01:00
|
|
|
|
2018-10-26 18:07:14 +02:00
|
|
|
private abstract static class RouteStatisticComputer {
|
|
|
|
|
2018-10-31 17:00:07 +01:00
|
|
|
private final List<RouteSegmentResult> route;
|
|
|
|
|
|
|
|
public RouteStatisticComputer(List<RouteSegmentResult> route) {
|
|
|
|
this.route = new ArrayList<>(route);
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
private Map<String, RouteSegmentAttribute> makePartition(List<RouteSegmentAttribute> routeAttributes) {
|
|
|
|
Map<String, RouteSegmentAttribute> partition = new HashMap<>();
|
|
|
|
for (RouteSegmentAttribute attribute : routeAttributes) {
|
|
|
|
String key = attribute.getAttribute();
|
|
|
|
RouteSegmentAttribute pattr = partition.get(key);
|
|
|
|
if (pattr == null) {
|
|
|
|
pattr = new RouteSegmentAttribute(attribute.getIndex(), attribute.getAttribute());
|
|
|
|
partition.put(key, pattr);
|
|
|
|
}
|
|
|
|
pattr.incrementDistanceBy(attribute.getDistance());
|
|
|
|
}
|
|
|
|
return partition;
|
|
|
|
}
|
|
|
|
|
|
|
|
private float computeTotalDistance(List<RouteSegmentAttribute> attributes) {
|
|
|
|
float distance = 0f;
|
|
|
|
for (RouteSegmentAttribute attribute : attributes) {
|
|
|
|
distance += attribute.getDistance();
|
|
|
|
}
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<RouteSegmentResult> getRoute() {
|
2018-10-31 17:00:07 +01:00
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
protected List<RouteSegmentAttribute> processRoute() {
|
2018-10-26 18:07:14 +02:00
|
|
|
int index = 0;
|
2018-11-02 17:33:33 +01:00
|
|
|
List<RouteSegmentAttribute> routes = new ArrayList<>();
|
2018-10-26 18:07:14 +02:00
|
|
|
String prev = null;
|
2018-10-31 17:00:07 +01:00
|
|
|
for (RouteSegmentResult segment : getRoute()) {
|
2018-10-26 18:07:14 +02:00
|
|
|
String current = getAttribute(segment);
|
|
|
|
if (current == null) {
|
|
|
|
current = UNDEFINED;
|
|
|
|
}
|
|
|
|
if (prev != null && !prev.equals(current)) {
|
|
|
|
index++;
|
|
|
|
}
|
2018-11-02 17:33:33 +01:00
|
|
|
if (index >= routes.size()) {
|
|
|
|
routes.add(new RouteSegmentAttribute(index, current));
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
2018-11-02 17:33:33 +01:00
|
|
|
RouteSegmentAttribute surface = routes.get(index);
|
2018-10-26 18:07:14 +02:00
|
|
|
surface.incrementDistanceBy(segment.getDistance());
|
|
|
|
prev = current;
|
|
|
|
}
|
2018-11-02 17:33:33 +01:00
|
|
|
return routes;
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
public Statistics computeStatistic() {
|
2018-11-02 17:33:33 +01:00
|
|
|
List<RouteSegmentAttribute> routeAttributes = processRoute();
|
|
|
|
Map<String, RouteSegmentAttribute> partition = makePartition(routeAttributes);
|
|
|
|
float totalDistance = computeTotalDistance(routeAttributes);
|
2018-11-05 16:29:10 +01:00
|
|
|
return new Statistics(routeAttributes, partition, totalDistance);
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract String getAttribute(RouteSegmentResult segment);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
|
|
|
|
private static class RouteSurfaceStatisticComputer extends RouteStatisticComputer {
|
|
|
|
|
|
|
|
public RouteSurfaceStatisticComputer(List<RouteSegmentResult> route) {
|
2018-10-31 17:00:07 +01:00
|
|
|
super(route);
|
|
|
|
}
|
|
|
|
|
2018-10-26 18:07:14 +02:00
|
|
|
@Override
|
|
|
|
public String getAttribute(RouteSegmentResult segment) {
|
2018-10-31 17:00:07 +01:00
|
|
|
String segmentSurface = segment.getSurface();
|
|
|
|
if (segmentSurface == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (RoadSurface roadSurface : RoadSurface.values()) {
|
|
|
|
if (roadSurface.contains(segmentSurface)) {
|
|
|
|
return roadSurface.name().toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
private static class RouteSmoothnessStatisticComputer extends RouteStatisticComputer {
|
2018-10-26 18:07:14 +02:00
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
public RouteSmoothnessStatisticComputer(List<RouteSegmentResult> route) {
|
2018-10-31 17:00:07 +01:00
|
|
|
super(route);
|
|
|
|
}
|
|
|
|
|
2018-10-26 18:07:14 +02:00
|
|
|
@Override
|
|
|
|
public String getAttribute(RouteSegmentResult segment) {
|
|
|
|
return segment.getSmoothness();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
private static class RouteClassStatisticComputer extends RouteStatisticComputer {
|
|
|
|
|
|
|
|
public RouteClassStatisticComputer(List<RouteSegmentResult> route) {
|
2018-10-31 17:00:07 +01:00
|
|
|
super(route);
|
|
|
|
}
|
|
|
|
|
2018-10-26 18:07:14 +02:00
|
|
|
@Override
|
|
|
|
public String getAttribute(RouteSegmentResult segment) {
|
2018-10-31 17:00:07 +01:00
|
|
|
String segmentClass = segment.getHighway();
|
|
|
|
if (segmentClass == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (RoadClass roadClass : RoadClass.values()) {
|
|
|
|
if (roadClass.contains(segmentClass)) {
|
|
|
|
return roadClass.name().toLowerCase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
private static class RouteSteepnessStatisticComputer extends RouteStatisticComputer {
|
|
|
|
|
|
|
|
public RouteSteepnessStatisticComputer(List<RouteSegmentResult> route) {
|
2018-10-31 17:00:07 +01:00
|
|
|
super(route);
|
|
|
|
}
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
private int computeIncline(float prevHeight, float currHeight, float distance) {
|
2018-11-05 16:48:29 +01:00
|
|
|
if (distance == 0f) {
|
|
|
|
return 0;
|
2018-11-02 17:33:33 +01:00
|
|
|
}
|
2018-11-05 16:48:29 +01:00
|
|
|
float incline = (currHeight - prevHeight) / distance * 100;
|
2018-11-05 16:29:10 +01:00
|
|
|
return Math.round(incline);
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
private List<Incline> computeSegmentInclines() {
|
|
|
|
List<Incline> inclines = new ArrayList<>();
|
2018-10-31 17:00:07 +01:00
|
|
|
for (RouteSegmentResult segment : getRoute()) {
|
|
|
|
float[] heights = segment.getHeightValues();
|
|
|
|
if (heights.length == 0) {
|
2018-11-02 17:33:33 +01:00
|
|
|
Incline incline = new Incline(0, segment.getDistance());
|
|
|
|
inclines.add(incline);
|
2018-10-31 17:00:07 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int index = 1; index < heights.length / 2; index++) {
|
|
|
|
int prevHeightIndex = 2 * (index - 1) + 1;
|
|
|
|
int currHeightIndex = 2 * index + 1;
|
|
|
|
int distanceBetweenHeightsIndex = 2 * index;
|
|
|
|
float prevHeight = heights[prevHeightIndex];
|
|
|
|
float currHeight = heights[currHeightIndex];
|
|
|
|
float distanceBetweenHeights = heights[distanceBetweenHeightsIndex];
|
2018-11-05 16:29:10 +01:00
|
|
|
int computedIncline = computeIncline(prevHeight, currHeight, distanceBetweenHeights);
|
2018-11-02 17:33:33 +01:00
|
|
|
Incline incline = new Incline(computedIncline, distanceBetweenHeights);
|
|
|
|
inclines.add(incline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return inclines;
|
|
|
|
}
|
2018-10-31 17:00:07 +01:00
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
@Override
|
|
|
|
public List<RouteSegmentAttribute> processRoute() {
|
|
|
|
List<RouteSegmentAttribute> routeInclines = new ArrayList<>();
|
|
|
|
int index = 0;
|
|
|
|
String prev = null;
|
|
|
|
for (Incline incline : computeSegmentInclines()) {
|
|
|
|
String current = incline.getBoundariesAsString();
|
|
|
|
if (prev != null && !prev.equals(current)) {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
if (index >= routeInclines.size()) {
|
|
|
|
routeInclines.add(new RouteSegmentAttribute(index, current));
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
2018-11-02 17:33:33 +01:00
|
|
|
RouteSegmentAttribute routeIncline = routeInclines.get(index);
|
|
|
|
routeIncline.incrementDistanceBy(incline.getDistance());
|
|
|
|
prev = current;
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
|
|
|
return routeInclines;
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
@Override
|
|
|
|
public String getAttribute(RouteSegmentResult segment) {
|
|
|
|
return null;
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
|
2018-10-26 18:07:14 +02:00
|
|
|
public static class RouteSegmentAttribute {
|
|
|
|
|
|
|
|
private final int index;
|
|
|
|
|
|
|
|
private final String attribute;
|
|
|
|
|
|
|
|
private float distance;
|
|
|
|
|
|
|
|
public RouteSegmentAttribute(int index, String attribute) {
|
|
|
|
this.index = index;
|
|
|
|
this.attribute = attribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getIndex() {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAttribute() {
|
|
|
|
return attribute;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getDistance() {
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void incrementDistanceBy(float distance) {
|
|
|
|
this.distance += distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "{" +
|
|
|
|
"index=" + index +
|
|
|
|
", attribute='" + attribute + '\'' +
|
|
|
|
", distance=" + distance +
|
|
|
|
'}';
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 17:00:07 +01:00
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
private static class Incline {
|
2018-10-31 17:00:07 +01:00
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
private int inclineValue;
|
2018-11-02 17:33:33 +01:00
|
|
|
private final float distance;
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
public Incline(int inclineValue, float distance) {
|
2018-11-02 17:33:33 +01:00
|
|
|
this.inclineValue = inclineValue;
|
|
|
|
this.distance = distance;
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 16:29:10 +01:00
|
|
|
public int getValue() {
|
2018-11-02 17:33:33 +01:00
|
|
|
return inclineValue;
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
|
|
|
|
2018-11-02 17:33:33 +01:00
|
|
|
public float getDistance() {
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getBoundariesAsString() {
|
2018-11-05 16:29:10 +01:00
|
|
|
return String.valueOf(getValue());
|
2018-10-31 17:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2018-11-02 17:33:33 +01:00
|
|
|
return "Incline{" +
|
|
|
|
", incline=" + inclineValue +
|
|
|
|
", distance=" + distance +
|
2018-10-31 17:00:07 +01:00
|
|
|
'}';
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 16:29:10 +01:00
|
|
|
|
|
|
|
public static class Statistics {
|
|
|
|
|
|
|
|
private final List<RouteSegmentAttribute> elements;
|
|
|
|
private final Map<String, RouteSegmentAttribute> partition;
|
|
|
|
private final float totalDistance;
|
|
|
|
|
|
|
|
public Statistics(List<RouteSegmentAttribute> elements,
|
|
|
|
Map<String, RouteSegmentAttribute> partition,
|
|
|
|
float totalDistance) {
|
|
|
|
this.elements = elements;
|
|
|
|
this.partition = partition;
|
|
|
|
this.totalDistance = totalDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float getTotalDistance() {
|
|
|
|
return totalDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<RouteSegmentAttribute> getElements() {
|
|
|
|
return new ArrayList<>(elements);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, RouteSegmentAttribute> getPartition() {
|
|
|
|
return new HashMap<>(partition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum RoadClass {
|
|
|
|
MOTORWAY("motorway", "motorway_link"),
|
|
|
|
STATE_ROAD("trunk", "trunk_link", "primary", "primary_link"),
|
|
|
|
ROAD("secondary", "secondary_link", "tertiary", "tertiary_link", "unclassified"),
|
|
|
|
STREET("residential", "living_street"),
|
|
|
|
SERVICE("service"),
|
|
|
|
TRACK("track", "road"),
|
|
|
|
FOOTWAY("footway"),
|
|
|
|
PATH("path"),
|
|
|
|
CYCLE_WAY("cycleway");
|
|
|
|
|
|
|
|
final Set<String> roadClasses = new TreeSet<>();
|
|
|
|
|
|
|
|
RoadClass(String... classes) {
|
|
|
|
roadClasses.addAll(Arrays.asList(classes));
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean contains(String roadClass) {
|
|
|
|
return roadClasses.contains(roadClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum RoadSurface {
|
|
|
|
PAVED("paved"),
|
|
|
|
UNPAVED("unpaved"),
|
|
|
|
ASPHALT("asphalt"),
|
|
|
|
CONCRETE("concrete"),
|
|
|
|
COMPACTED("compacted"),
|
|
|
|
GRAVEL("gravel"),
|
|
|
|
FINE_GRAVEL("fine_gravel"),
|
|
|
|
PAVING_STONES("paving_stones"),
|
|
|
|
SETT("sett"),
|
|
|
|
COBBLESTONE("cobblestone"),
|
|
|
|
PEBBLESTONE("pebblestone"),
|
|
|
|
STONE("stone"),
|
|
|
|
METAL("metal"),
|
|
|
|
GROUND("ground", "mud"),
|
|
|
|
WOOD("wood"),
|
|
|
|
GRASS_PAVER("grass_paver"),
|
|
|
|
GRASS("grass"),
|
|
|
|
SAND("sand"),
|
|
|
|
SALT("salt"),
|
|
|
|
SNOW("snow"),
|
|
|
|
ICE("ice"),
|
|
|
|
CLAY("clay");
|
|
|
|
|
|
|
|
final Set<String> surfaces = new TreeSet<>();
|
|
|
|
|
|
|
|
RoadSurface(String... surfaces) {
|
|
|
|
this.surfaces.addAll(Arrays.asList(surfaces));
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean contains(String surface) {
|
|
|
|
return surfaces.contains(surface);
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 18:07:14 +02:00
|
|
|
}
|