Fix inconsistency of track geometry

This commit is contained in:
cepprice 2021-04-11 18:46:33 +05:00
parent 3da32a65cf
commit 74a534c4f3
2 changed files with 30 additions and 4 deletions

View file

@ -619,6 +619,30 @@ public class OsmMapUtils {
return new Cell(x / area, y / area, 0, rings);
}
public static void simplifyDouglasPeucker(List<Node> nodes, int start, int end, List<Node> survivedNodes, double epsilon) {
double dmax = Double.NEGATIVE_INFINITY;
int index = -1;
Node startPt = nodes.get(start);
Node endPt = nodes.get(end);
for (int i = start + 1; i < end; i++) {
Node pt = nodes.get(i);
double d = MapUtils.getOrthogonalDistance(pt.getLatitude(), pt.getLongitude(),
startPt.getLatitude(), startPt.getLongitude(), endPt.getLatitude(), endPt.getLongitude());
if (d > dmax) {
dmax = d;
index = i;
}
}
if (dmax > epsilon) {
simplifyDouglasPeucker(nodes, start, index, survivedNodes, epsilon);
simplifyDouglasPeucker(nodes, index, end, survivedNodes, epsilon);
} else {
survivedNodes.add(nodes.get(end));
}
}
private static class CellComparator implements Comparator<Cell> {
@Override
public int compare(Cell o1, Cell o2) {

View file

@ -35,6 +35,7 @@ public class RouteColorize {
public static final int RED = rgbaToDecimal(243, 55, 77, 255);
public static final int[] colors = new int[] {GREEN, YELLOW, RED};
private static final float DEFAULT_BASE = 17.2f;
private static final int MAX_SLOPE_VALUE = 25;
public enum ColorizationType {
@ -241,7 +242,6 @@ public class RouteColorize {
if (dataList == null) {
dataList = new ArrayList<>();
for (int i = 0; i < latitudes.length; i++) {
//System.out.println(latitudes[i] + " " + longitudes[i] + " " + values[i]);
dataList.add(new RouteColorizationPoint(i, latitudes[i], longitudes[i], values[i]));
}
}
@ -250,11 +250,13 @@ public class RouteColorize {
for (RouteColorizationPoint data : dataList) {
nodes.add(new net.osmand.osm.edit.Node(data.lat, data.lon, data.id));
}
OsmMapUtils.simplifyDouglasPeucker(nodes, zoom + 5, 1, result, true);
double epsilon = Math.pow(2.0, DEFAULT_BASE - zoom);
result.add(nodes.get(0));
OsmMapUtils.simplifyDouglasPeucker(nodes, 0, nodes.size() - 1, result, epsilon);
List<RouteColorizationPoint> simplified = new ArrayList<>();
for (int i = 1; i < result.size() - 1; i++) {
for (int i = 1; i < result.size(); i++) {
int prevId = (int) result.get(i - 1).getId();
int currentId = (int) result.get(i).getId();
List<RouteColorizationPoint> sublist = dataList.subList(prevId, currentId);