diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/edit/OsmMapUtils.java b/OsmAnd-java/src/main/java/net/osmand/osm/edit/OsmMapUtils.java index cf86ca88ed..c4823cf9ea 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/edit/OsmMapUtils.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/edit/OsmMapUtils.java @@ -619,6 +619,30 @@ public class OsmMapUtils { return new Cell(x / area, y / area, 0, rings); } + public static void simplifyDouglasPeucker(List nodes, int start, int end, List 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 { @Override public int compare(Cell o1, Cell o2) { diff --git a/OsmAnd-java/src/main/java/net/osmand/router/RouteColorize.java b/OsmAnd-java/src/main/java/net/osmand/router/RouteColorize.java index 88b6555e19..6abb9c47c0 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/RouteColorize.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/RouteColorize.java @@ -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 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 sublist = dataList.subList(prevId, currentId);