Skip to content

Commit

Permalink
fix #20982: Douglas-Peucker implementation is wrong
Browse files Browse the repository at this point in the history
The Douglas-Peucker implementation should calculate the distance to the segment between the end nodes, not the the distance to the line through those nodes.

git-svn-id: https://josm.openstreetmap.de/svn/trunk@18316 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
GerdP authored and GerdP committed Nov 8, 2021
1 parent e9a5488 commit 4d7f493
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/org/openstreetmap/josm/actions/SimplifyWayAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openstreetmap.josm.command.SequenceCommand;
import org.openstreetmap.josm.data.SystemOfMeasurement;
import org.openstreetmap.josm.data.UndoRedoHandler;
import org.openstreetmap.josm.data.coor.EastNorth;
import org.openstreetmap.josm.data.osm.DataSet;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
Expand Down Expand Up @@ -404,24 +405,46 @@ protected static void buildSimplifiedNodeList(List<Node> wnew, int from, int to,

Node fromN = wnew.get(from);
Node toN = wnew.get(to);

EastNorth p1 = fromN.getEastNorth();
EastNorth p2 = toN.getEastNorth();
// Get max xte
int imax = -1;
double xtemax = 0;
for (int i = from + 1; i < to; i++) {
Node n = wnew.get(i);
EastNorth p = n.getEastNorth();
double ldx = p2.getX() - p1.getX();
double ldy = p2.getY() - p1.getY();
double offset;
//segment zero length
if (ldx == 0 && ldy == 0)
offset = 0;
else {
double pdx = p.getX() - p1.getX();
double pdy = p.getY() - p1.getY();
offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);
}
final double distRad;
// CHECKSTYLE.OFF: SingleSpaceSeparator
double xte = Math.abs(Ellipsoid.WGS84.a
* xtd(fromN.lat() * Math.PI / 180, fromN.lon() * Math.PI / 180, toN.lat() * Math.PI / 180,
toN.lon() * Math.PI / 180, n.lat() * Math.PI / 180, n.lon() * Math.PI / 180));
if (offset <= 0) {
distRad = dist(fromN.lat() * Math.PI / 180, fromN.lon() * Math.PI / 180,
n.lat() * Math.PI / 180, n.lon() * Math.PI / 180);
} else if (offset >= 1) {
distRad = dist(toN.lat() * Math.PI / 180, toN.lon() * Math.PI / 180,
n.lat() * Math.PI / 180, n.lon() * Math.PI / 180);
} else {
distRad = xtd(fromN.lat() * Math.PI / 180, fromN.lon() * Math.PI / 180,
toN.lat() * Math.PI / 180, toN.lon() * Math.PI / 180,
n.lat() * Math.PI / 180, n.lon() * Math.PI / 180);
}
// CHECKSTYLE.ON: SingleSpaceSeparator
double xte = Math.abs(distRad);
if (xte > xtemax) {
xtemax = xte;
imax = i;
}
}

if (imax != -1 && xtemax >= threshold) {
if (imax != -1 && Ellipsoid.WGS84.a * xtemax >= threshold) {
// Segment cannot be simplified - try shorter segments
buildSimplifiedNodeList(wnew, from, imax, threshold, simplifiedNodes);
buildSimplifiedNodeList(wnew, imax, to, threshold, simplifiedNodes);
Expand Down

0 comments on commit 4d7f493

Please sign in to comment.