Ticket #17616: 17616-v11.patch

File 17616-v11.patch, 28.1 KB (added by GerdP, 6 years ago)
  • src/org/openstreetmap/josm/data/gpx/GpxDistance.java

     
    3131     */
    3232    public static double getLowestDistance(OsmPrimitive p, GpxData gpxData) {
    3333        return gpxData.getTrackPoints()
    34                 .mapToDouble(tp -> getDistance(p, tp))
     34                .mapToDouble(tp -> Geometry.getDistance(p, new Node(tp.getCoor())))
    3535                .filter(x -> x >= 0)
    3636                .min().orElse(Double.MAX_VALUE);
    3737    }
     
    4141     * @param p OsmPrimitive to get the distance to the WayPoint
    4242     * @param waypoint WayPoint to get the distance from
    4343     * @return The shortest distance between p and waypoint
     44     * @deprecated Use {@code Geometry.getDistance(p, new Node(waypoint.getCoor()))}
     45     * instead
    4446     */
     47    @Deprecated
    4548    public static double getDistance(OsmPrimitive p, WayPoint waypoint) {
    46         if (p instanceof Node) {
    47             return getDistanceNode((Node) p, waypoint);
    48         } else if (p instanceof Way) {
    49             return getDistanceWay((Way) p, waypoint);
    50         } else if (p instanceof Relation) {
    51             return getDistanceRelation((Relation) p, waypoint);
    52         }
    53         return Double.MAX_VALUE;
     49        return Geometry.getDistance(p, new Node(waypoint.getCoor()));
    5450    }
    5551
    5652    /**
     
    5854     * @param relation Relation to get the distance from
    5955     * @param waypoint WayPoint to get the distance to
    6056     * @return The distance between the relation and the waypoint
     57     * @deprecated Use {@code Geometry.getDistance(relation, new Node(waypoint.getCoor()))}
     58     * instead
    6159     */
     60    @Deprecated
    6261    public static double getDistanceRelation(Relation relation, WayPoint waypoint) {
    6362        double shortestDistance = Double.MAX_VALUE;
    6463        List<Node> nodes = new ArrayList<>(relation.getMemberPrimitives(Node.class));
     
    8584     * @param way Way to get the distance from
    8685     * @param waypoint WayPoint to get the distance to
    8786     * @return The distance between the way and the waypoint
     87     * @deprecated Use {@code Geometry.getDistanceWayNode(way, new Node(waypoint.getCoor()))} instead
    8888     */
     89    @Deprecated
    8990    public static double getDistanceWay(Way way, WayPoint waypoint) {
    90         double shortestDistance = Double.MAX_VALUE;
    91         if (way == null || waypoint == null) return shortestDistance;
    92         LatLon llwaypoint = waypoint.getCoor();
    93         EastNorth enwaypoint = new EastNorth(llwaypoint.getY(), llwaypoint.getX());
    94         for (int i = 0; i < way.getNodesCount() - 1; i++) {
    95             double distance = Double.MAX_VALUE;
    96             LatLon llfirst = way.getNode(i).getCoor();
    97             LatLon llsecond = way.getNode(i + 1).getCoor();
    98             EastNorth first = new EastNorth(llfirst.getY(), llfirst.getX());
    99             EastNorth second = new EastNorth(llsecond.getY(), llsecond.getX());
    100             if (first.isValid() && second.isValid()) {
    101                 EastNorth closestPoint = Geometry.closestPointToSegment(first, second, enwaypoint);
    102                 distance = llwaypoint.greatCircleDistance(new LatLon(closestPoint.getX(), closestPoint.getY()));
    103             } else if (first.isValid() && !second.isValid()) {
    104                 distance = getDistanceEastNorth(first, waypoint);
    105             } else if (!first.isValid() && second.isValid()) {
    106                 distance = getDistanceEastNorth(second, waypoint);
    107             } else if (!first.isValid() && !second.isValid()) {
    108                 distance = Double.MAX_VALUE;
    109             }
    110             if (distance < shortestDistance) shortestDistance = distance;
    111 
    112         }
    113         return shortestDistance;
     91        if (way == null || waypoint == null) return Double.MAX_VALUE;
     92        return Geometry.getDistanceWayNode(way, new Node(waypoint.getCoor()));
    11493    }
    11594
    11695    /**
     
    11897     * @param node Node to get the distance from
    11998     * @param waypoint WayPoint to get the distance to
    12099     * @return The distance between the two points
     100     * @deprecated Use {@code Geometry.getDistance(node, new Node(waypoint.getCoor()))}
     101     * instead
    121102     */
     103    @Deprecated
    122104    public static double getDistanceNode(Node node, WayPoint waypoint) {
    123         if (node == null) return Double.MAX_VALUE;
    124         return getDistanceLatLon(node.getCoor(), waypoint);
     105        if (node == null || waypoint == null) return Double.MAX_VALUE;
     106        return Geometry.getDistance(node, new Node(waypoint.getCoor()));
    125107    }
    126108
    127109    /**
     
    129111     * @param en The EastNorth to get the distance to
    130112     * @param waypoint WayPoint to get the distance to
    131113     * @return The distance between the two points
     114     * @deprecated Use {@code Geometry.getDistance(new Node(en), new Node(waypoint.getCoor()))} instead
    132115     */
     116    @Deprecated
    133117    public static double getDistanceEastNorth(EastNorth en, WayPoint waypoint) {
    134         if (en == null || !en.isValid()) return Double.MAX_VALUE;
    135         return getDistanceLatLon(new LatLon(en.getY(), en.getX()), waypoint);
     118        if (en == null || waypoint == null) return Double.MAX_VALUE;
     119        return Geometry.getDistance(new Node(en), new Node(waypoint.getCoor()));
    136120    }
    137121
    138122    /**
     
    140124     * @param latlon LatLon to get the distance from
    141125     * @param waypoint WayPoint to get the distance to
    142126     * @return The distance between the two points
     127     * @deprecated Use {@code Geometry.getDistance(new Node(latlon), new Node(waypoint.getCoor()))} instead
    143128     */
     129    @Deprecated
    144130    public static double getDistanceLatLon(LatLon latlon, WayPoint waypoint) {
    145131        if (latlon == null || waypoint == null || waypoint.getCoor() == null) return Double.MAX_VALUE;
    146         return waypoint.getCoor().greatCircleDistance(latlon);
     132        return Geometry.getDistance(new Node(latlon), new Node(waypoint.getCoor()));
    147133    }
    148134}
  • src/org/openstreetmap/josm/tools/Geometry.java

     
    99import java.math.BigDecimal;
    1010import java.math.MathContext;
    1111import java.util.ArrayList;
     12import java.util.Collection;
    1213import java.util.Collections;
    1314import java.util.Comparator;
     15import java.util.Iterator;
    1416import java.util.LinkedHashSet;
    1517import java.util.List;
    1618import java.util.Set;
     19import java.util.TreeSet;
    1720import java.util.function.Predicate;
    1821import java.util.stream.Collectors;
    1922
     
    3033import org.openstreetmap.josm.data.osm.MultipolygonBuilder.JoinedPolygon;
    3134import org.openstreetmap.josm.data.osm.Node;
    3235import org.openstreetmap.josm.data.osm.NodePositionComparator;
     36import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3337import org.openstreetmap.josm.data.osm.Relation;
    3438import org.openstreetmap.josm.data.osm.Way;
     39import org.openstreetmap.josm.data.osm.WaySegment;
    3540import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
    3641import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
    3742import org.openstreetmap.josm.data.projection.Projection;
     
    11021107        }
    11031108        return new AreaAndPerimeter(Math.abs(area) / 2, perimeter);
    11041109    }
     1110
     1111    /**
     1112     * Get the closest primitive to {@code osm} from the collection of
     1113     * OsmPrimitive {@code primitives}
     1114     *
     1115     * The {@code primitives} should be fully downloaded to ensure accuracy.
     1116     *
     1117     * @param <T> The return type of the primitive
     1118     * @param osm The primitive to get the distances from
     1119     * @param primitives The collection of primitives to get the distance to
     1120     * @return The closest {@link OsmPrimitive}. This is not determinative.
     1121     * To get all primitives that share the same distance, use
     1122     * {@link Geometry#getClosestPrimitives}.
     1123     * @since xxx
     1124     */
     1125    public static <T extends OsmPrimitive> T getClosestPrimitive(OsmPrimitive osm, Collection<T> primitives) {
     1126        Collection<T> collection = getClosestPrimitives(osm, primitives);
     1127        return collection.iterator().next();
     1128    }
     1129
     1130    /**
     1131     * Get the closest primitives to {@code osm} from the collection of
     1132     * OsmPrimitive {@code primitives}
     1133     *
     1134     * The {@code primitives} should be fully downloaded to ensure accuracy.
     1135     *
     1136     * @param <T> The return type of the primitive
     1137     * @param osm The primitive to get the distances from
     1138     * @param primitives The collection of primitives to get the distance to
     1139     * @return The closest {@link OsmPrimitive}s. May be empty.
     1140     * @since xxx
     1141     */
     1142    public static <T extends OsmPrimitive> Collection<T> getClosestPrimitives(OsmPrimitive osm, Collection<T> primitives) {
     1143        double lowestDistance = Double.MAX_VALUE;
     1144        TreeSet<T> closest = new TreeSet<>();
     1145        for (T primitive : primitives) {
     1146            double distance = getDistance(osm, primitive);
     1147            if (Double.isNaN(distance)) continue;
     1148            if (distance < lowestDistance) {
     1149                closest.clear();
     1150                lowestDistance = distance;
     1151                closest.add(primitive);
     1152            } else if (distance == lowestDistance) {
     1153                closest.add(primitive);
     1154            }
     1155        }
     1156        return closest;
     1157    }
     1158
     1159    /**
     1160     * Get the furthest primitive to {@code osm} from the collection of
     1161     * OsmPrimitive {@code primitives}
     1162     *
     1163     * The {@code primitives} should be fully downloaded to ensure accuracy.
     1164     *
     1165     * It does NOT give the furthest primitive based off of the furthest
     1166     * part of that primitive
     1167     * @param <T> The return type of the primitive
     1168     * @param osm The primitive to get the distances from
     1169     * @param primitives The collection of primitives to get the distance to
     1170     * @return The furthest {@link OsmPrimitive}.  This is not determinative.
     1171     * To get all primitives that share the same distance, use
     1172     * {@link Geometry#getFurthestPrimitives}
     1173     * @since xxx
     1174     */
     1175    public static <T extends OsmPrimitive> T getFurthestPrimitive(OsmPrimitive osm, Collection<T> primitives) {
     1176        return getFurthestPrimitives(osm, primitives).iterator().next();
     1177    }
     1178
     1179    /**
     1180     * Get the furthest primitives to {@code osm} from the collection of
     1181     * OsmPrimitive {@code primitives}
     1182     *
     1183     * The {@code primitives} should be fully downloaded to ensure accuracy.
     1184     *
     1185     * It does NOT give the furthest primitive based off of the furthest
     1186     * part of that primitive
     1187     * @param <T> The return type of the primitive
     1188     * @param osm The primitive to get the distances from
     1189     * @param primitives The collection of primitives to get the distance to
     1190     * @return The furthest {@link OsmPrimitive}s. It may return an empty collection.
     1191     * @since xxx
     1192     */
     1193    public static <T extends OsmPrimitive> Collection<T> getFurthestPrimitives(OsmPrimitive osm, Collection<T> primitives) {
     1194        double furthestDistance = Double.NEGATIVE_INFINITY;
     1195        TreeSet<T> furthest = new TreeSet<>();
     1196        for (T primitive : primitives) {
     1197            double distance = getDistance(osm, primitive);
     1198            if (Double.isNaN(distance)) continue;
     1199            if (distance > furthestDistance) {
     1200                furthest.clear();
     1201                furthestDistance = distance;
     1202                furthest.add(primitive);
     1203            } else if (distance == furthestDistance) {
     1204                furthest.add(primitive);
     1205            }
     1206        }
     1207        return furthest;
     1208    }
     1209
     1210    /**
     1211     * Get the distance between different {@link OsmPrimitive}s
     1212     * @param one The primitive to get the distance from
     1213     * @param two The primitive to get the distance to
     1214     * @return The distance between the primitives in meters
     1215     * (or the unit of the current projection, see {@link Projection}).
     1216     * May return {@link Double#NaN} if one of the primitives is incomplete.
     1217     * @since xxx
     1218     */
     1219    public static double getDistance(OsmPrimitive one, OsmPrimitive two) {
     1220        double rValue = Double.MAX_VALUE;
     1221        if (one == null || two == null || one.isIncomplete()
     1222                || two.isIncomplete()) return Double.NaN;
     1223        if (one instanceof Node && two instanceof Node) {
     1224            rValue = ((Node) one).getCoor().greatCircleDistance(((Node) two).getCoor());
     1225        } else if (one instanceof Node && two instanceof Way) {
     1226            rValue = getDistanceWayNode((Way) two, (Node) one);
     1227        } else if (one instanceof Way && two instanceof Node) {
     1228            rValue = getDistanceWayNode((Way) one, (Node) two);
     1229        } else if (one instanceof Way && two instanceof Way) {
     1230            rValue = getDistanceWayWay((Way) one, (Way) two);
     1231        } else if (one instanceof Relation && !(two instanceof Relation)) {
     1232            for (OsmPrimitive osmPrimitive: ((Relation) one).getMemberPrimitives()) {
     1233                double currentDistance = getDistance(osmPrimitive, two);
     1234                if (currentDistance < rValue) rValue = currentDistance;
     1235            }
     1236        } else if (!(one instanceof Relation) && two instanceof Relation) {
     1237            rValue = getDistance(two, one);
     1238        } else if (one instanceof Relation && two instanceof Relation) {
     1239            for (OsmPrimitive osmPrimitive1 : ((Relation) one).getMemberPrimitives()) {
     1240                for (OsmPrimitive osmPrimitive2 : ((Relation) two).getMemberPrimitives()) {
     1241                    double currentDistance = getDistance(osmPrimitive1, osmPrimitive2);
     1242                    if (currentDistance < rValue) rValue = currentDistance;
     1243                }
     1244            }
     1245        }
     1246        return rValue != Double.MAX_VALUE ? rValue : Double.NaN;
     1247    }
     1248
     1249    /**
     1250     * Get the distance between a way and a node
     1251     * @param way The way to get the distance from
     1252     * @param node The node to get the distance to
     1253     * @return The distance between the {@code way} and the {@code node} in
     1254     * meters (or the unit of the current projection, see {@link Projection}).
     1255     * May return {@link Double#NaN} if the primitives are incomplete.
     1256     * @since xxx
     1257     */
     1258    public static double getDistanceWayNode(Way way, Node node) {
     1259        if (way == null || node == null || way.getNodesCount() < 2 || !node.isLatLonKnown())
     1260            return Double.NaN;
     1261
     1262        double smallest = Double.MAX_VALUE;
     1263        EastNorth en0 = node.getEastNorth();
     1264        // go through the nodes as if they were paired
     1265        Iterator<Node> iter = way.getNodes().iterator();
     1266        EastNorth en1 = iter.next().getEastNorth();
     1267        while (iter.hasNext()) {
     1268            EastNorth en2 = iter.next().getEastNorth();
     1269            double distance = getSegmentNodeDistSq(en1, en2, en0);
     1270            if (distance < smallest)
     1271                smallest = distance;
     1272            en1 = en2;
     1273        }
     1274        return Math.sqrt(smallest);
     1275    }
     1276
     1277    /**
     1278     * Get the closest {@link WaySegment} from a way to a primitive.
     1279     * @param way The {@link Way} to get the distance from and the {@link WaySegment}
     1280     * @param primitive The {@link OsmPrimitive} to get the distance to
     1281     * @return The {@link WaySegment} that is closest to {@code primitive} from {@code way}.
     1282     * If there are multiple {@link WaySegment}s with the same distance, the last
     1283     * {@link WaySegment} with the same distance will be returned.
     1284     * May return {@code null} if the way has fewer than two nodes or one
     1285     * of the primitives is incomplete.
     1286     * @since xxx
     1287     */
     1288    public static WaySegment getClosestWaySegment(Way way, OsmPrimitive primitive) {
     1289        if (way == null || primitive == null || way.isIncomplete()
     1290                || primitive.isIncomplete()) return null;
     1291        double lowestDistance = Double.MAX_VALUE;
     1292        Pair<Node, Node> closestNodes = null;
     1293        for (Pair<Node, Node> nodes : way.getNodePairs(false)) {
     1294            Way tWay = new Way();
     1295            tWay.addNode(nodes.a);
     1296            tWay.addNode(nodes.b);
     1297            double distance = getDistance(tWay, primitive);
     1298            if (distance < lowestDistance) {
     1299                lowestDistance = distance;
     1300                closestNodes = nodes;
     1301            }
     1302        }
     1303        if (closestNodes == null) return null;
     1304        return WaySegment.forNodePair(way, closestNodes.a, closestNodes.b);
     1305    }
     1306
     1307    /**
     1308     * Get the distance between different ways. Iterates over the nodes of the ways, complexity is O(n*m)
     1309     * (n,m giving the number of nodes)
     1310     * @param w1 The first {@link Way}
     1311     * @param w2 The second {@link Way}
     1312     * @return The shortest distance between the ways in meters
     1313     * (or the unit of the current projection, see {@link Projection}).
     1314     * May return {@link Double#NaN}.
     1315     * @since xxx
     1316     */
     1317    public static double getDistanceWayWay(Way w1, Way w2) {
     1318        if (w1 == null || w2 == null || w1.getNodesCount() < 2 || w2.getNodesCount() < 2)
     1319            return Double.NaN;
     1320        double rValue = Double.MAX_VALUE;
     1321        Iterator<Node> iter1 = w1.getNodes().iterator();
     1322        Node w1N1 = iter1.next();
     1323        while (iter1.hasNext()) {
     1324            Node w1N2 = iter1.next();
     1325            Iterator<Node> iter2 = w2.getNodes().iterator();
     1326            Node w2N1 = iter2.next();
     1327            while (iter2.hasNext()) {
     1328                Node w2N2 = iter2.next();
     1329                double distance = getDistanceSegmentSegment(w1N1, w1N2, w2N1, w2N2);
     1330                if (distance < rValue)
     1331                    rValue = distance;
     1332                w2N1 = w2N2;
     1333            }
     1334            w1N1 = w1N2;
     1335        }
     1336        return rValue;
     1337    }
     1338
     1339    /**
     1340     * Get the distance between different {@link WaySegment}s
     1341     * @param ws1 A {@link WaySegment}
     1342     * @param ws2 A {@link WaySegment}
     1343     * @return The distance between the two {@link WaySegment}s in meters
     1344     * (or the unit of the current projection, see {@link Projection}).
     1345     * May return {@link Double#NaN}.
     1346     * @since xxx
     1347     */
     1348    public static double getDistanceSegmentSegment(WaySegment ws1, WaySegment ws2) {
     1349        return getDistanceSegmentSegment(ws1.getFirstNode(), ws1.getSecondNode(), ws2.getFirstNode(), ws2.getSecondNode());
     1350    }
     1351
     1352    /**
     1353     * Get the distance between different {@link WaySegment}s
     1354     * @param ws1Node1 The first node of the first WaySegment
     1355     * @param ws1Node2 The second node of the second WaySegment
     1356     * @param ws2Node1 The first node of the second WaySegment
     1357     * @param ws2Node2 The second node of the second WaySegment
     1358     * @return The distance between the two {@link WaySegment}s in meters
     1359     * (or the unit of the current projection, see {@link Projection}).
     1360     * May return {@link Double#NaN}.
     1361     * @since xxx
     1362     */
     1363    public static double getDistanceSegmentSegment(Node ws1Node1, Node ws1Node2, Node ws2Node1, Node ws2Node2) {
     1364        EastNorth enWs1Node1 = ws1Node1.getEastNorth();
     1365        EastNorth enWs1Node2 = ws1Node2.getEastNorth();
     1366        EastNorth enWs2Node1 = ws2Node1.getEastNorth();
     1367        EastNorth enWs2Node2 = ws2Node2.getEastNorth();
     1368        if (enWs1Node1 == null || enWs1Node2 == null || enWs2Node1 == null || enWs2Node2 == null)
     1369            return Double.NaN;
     1370        if (getSegmentSegmentIntersection(enWs1Node1, enWs1Node2, enWs2Node1, enWs2Node2) != null)
     1371            return 0;
     1372
     1373        double dist1sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node1);
     1374        double dist2sq = getSegmentNodeDistSq(enWs1Node1, enWs1Node2, enWs2Node2);
     1375        double dist3sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node1);
     1376        double dist4sq = getSegmentNodeDistSq(enWs2Node1, enWs2Node2, enWs1Node2);
     1377        double smallest = Math.min(Math.min(dist1sq, dist2sq), Math.min(dist3sq, dist4sq));
     1378        return Math.sqrt(smallest);
     1379    }
     1380
     1381    /**
     1382     * Calculate closest distance between a line segment s1-s2 and a point p
     1383     * @param s1 start of segment
     1384     * @param s2 end of segment
     1385     * @param p the point
     1386     * @return the square of the euclidean distance from p to the closest point on the segment
     1387     */
     1388    private static double getSegmentNodeDistSq(EastNorth s1, EastNorth s2, EastNorth p) {
     1389        EastNorth c1 = closestPointTo(s1, s2, p, true);
     1390        return c1.distanceSq(p);
     1391    }
    11051392}
  • test/unit/org/openstreetmap/josm/tools/GeometryTest.java

     
    77import static org.junit.Assert.assertTrue;
    88
    99import java.io.FileInputStream;
     10import java.util.ArrayList;
    1011import java.util.Arrays;
    1112import java.util.List;
    1213
     
    1819import org.openstreetmap.josm.data.coor.LatLon;
    1920import org.openstreetmap.josm.data.osm.DataSet;
    2021import org.openstreetmap.josm.data.osm.Node;
     22import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2123import org.openstreetmap.josm.data.osm.Relation;
    2224import org.openstreetmap.josm.data.osm.RelationMember;
    2325import org.openstreetmap.josm.data.osm.Way;
     
    258260        // now w1 is inside
    259261        assertTrue(Geometry.isPolygonInsideMultiPolygon(w1.getNodes(), mp, null));
    260262    }
     263
     264    /**
     265     * Test of {@link Geometry#getDistance} method.
     266     */
     267    @Test
     268    public void testGetDistance() {
     269        Node node1 = new Node(new LatLon(0, 0));
     270        Node node2 = new Node(new LatLon(0.1, 1));
     271        Node node3 = new Node(new LatLon(1.1, 0.1));
     272        Node node4 = new Node(new LatLon(1, 1.1));
     273        Way way1 = TestUtils.newWay("", node1, node2);
     274        Way way2 = TestUtils.newWay("", node3, node4);
     275        Relation testRelation1 = new Relation();
     276        Relation testRelation2 = new Relation();
     277        testRelation1.addMember(new RelationMember("", way1));
     278        testRelation1.addMember(new RelationMember("", way2));
     279        testRelation2.addMember(new RelationMember("", node1));
     280        testRelation2.addMember(new RelationMember("", node2));
     281        testRelation2.addMember(new RelationMember("", node3));
     282        testRelation2.addMember(new RelationMember("", node4));
     283
     284        double distance = Geometry.getDistance(null, node3);
     285        assertEquals(Double.NaN, distance, 0.1);
     286
     287        distance = Geometry.getDistance(way1, null);
     288        assertEquals(Double.NaN, distance, 0.1);
     289
     290        distance = Geometry.getDistance(null, null);
     291        assertEquals(Double.NaN, distance, 0.1);
     292
     293        distance = Geometry.getDistance(node1, node2);
     294        assertEquals(111874.6474307704, distance, 0.1);
     295
     296        distance = Geometry.getDistance(way1, node3);
     297        assertEquals(120743.55085962385, distance, 0.1);
     298
     299        distance = Geometry.getDistance(node3, way1);
     300        assertEquals(120743.55085962385, distance, 0.1);
     301
     302        distance = Geometry.getDistance(way1, way2);
     303        assertEquals(100803.63714283936, distance, 0.1);
     304
     305        distance = Geometry.getDistance(testRelation1, new Node(new LatLon(0, 0.5)));
     306        assertEquals(5538.354450686605, distance, 0.1);
     307
     308        distance = Geometry.getDistance(new Node(new LatLon(0, 0.5)), testRelation1);
     309        assertEquals(5538.354450686605, distance, 0.1);
     310
     311        distance = Geometry.getDistance(testRelation1, testRelation2);
     312        assertEquals(0.0, distance, 0.1);
     313    }
     314
     315    /**
     316     * Test of {@link Geometry#getClosestPrimitive} method
     317     */
     318    @Test
     319    public void testGetClosestPrimitive() {
     320        Node node1 = new Node(new LatLon(0, 0));
     321        Node node2 = new Node(new LatLon(0.1, 1));
     322        Node node3 = new Node(new LatLon(1.1, 0.1));
     323        Node node4 = new Node(new LatLon(1, 1.1));
     324        Way way1 = TestUtils.newWay("", node1, node2);
     325        Way way2 = TestUtils.newWay("", node3, node4);
     326
     327        List<OsmPrimitive> primitives = new ArrayList<>();
     328        primitives.add(way1);
     329        primitives.add(way2);
     330        OsmPrimitive closest = Geometry.getClosestPrimitive(node1, primitives);
     331        assertEquals(way1, closest);
     332    }
     333
     334    /**
     335     * Test of {@link Geometry#getFurthestPrimitive} method
     336     */
     337    @Test
     338    public void testGetFurthestPrimitive() {
     339        Node node1 = new Node(new LatLon(0, 0));
     340        Node node2 = new Node(new LatLon(0, 1.1));
     341        Node node3 = new Node(new LatLon(1, 0.1));
     342        Node node4 = new Node(new LatLon(1.1, 1));
     343        Way way1 = TestUtils.newWay("", node1, node2);
     344        Way way2 = TestUtils.newWay("", node3, node4);
     345        Way way3 = TestUtils.newWay("", node2, node4);
     346        Way way4 = TestUtils.newWay("", node1, node3);
     347
     348        List<OsmPrimitive> primitives = new ArrayList<>();
     349        primitives.add(way1);
     350        OsmPrimitive furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(0, 0.75)), primitives);
     351        assertEquals(way1, furthest);
     352        primitives.add(way2);
     353        primitives.add(way3);
     354        primitives.add(way4);
     355        furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(0, 0.5)), primitives);
     356        assertEquals(way2, furthest);
     357        furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(.25, 0.5)), primitives);
     358        assertEquals(way2, furthest);
     359    }
     360
     361    /**
     362     * Test of {@link Geometry#getClosestWaySegment} method
     363     */
     364    @Test
     365    public void testGetClosestWaySegment() {
     366        Node node1 = new Node(new LatLon(0, 0));
     367        Node node2 = new Node(new LatLon(0, 1));
     368        Node node3 = new Node(new LatLon(0.3, 0.5));
     369        Node node4 = new Node(new LatLon(0.1, 0));
     370        Way way1 = TestUtils.newWay("", node1, node2, node3, node4);
     371
     372        Way closestSegment = Geometry.getClosestWaySegment(way1, new Node(new LatLon(0, 0.5))).toWay();
     373        Assert.assertTrue(closestSegment.containsNode(node1));
     374        Assert.assertTrue(closestSegment.containsNode(node2));
     375    }
     376
     377    /**
     378     * Test of {@link Geometry#getDistanceSegmentSegment} method
     379     */
     380    @Test
     381    public void testGetDistanceSegmentSegment() {
     382        Node node1 = new Node(new LatLon(2.0, 2.0));
     383        Node node2 = new Node(new LatLon(2.0, 3.0));
     384        Node node3 = new Node(new LatLon(2.3, 2.5));
     385        Node node4 = new Node(new LatLon(2.1, 2.0));
     386
     387        // connected segments
     388        assertEquals(0.0, Geometry.getDistanceSegmentSegment(node1, node2, node3, node1), 0.000001);
     389
     390        // distance between node 1 and node4 is the shortest
     391        double expected = node1.getEastNorth().distance(node4.getEastNorth());
     392        assertEquals(expected, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 0.000001);
     393
     394        // crossing segments
     395        node4.setCoor(new LatLon(1.9998192774806864, 2.0004056993230455));
     396        assertEquals(0, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 0.000001);
     397
     398        // usual case
     399        node4.setCoor(new LatLon(2.0002098170882276, 2.0000778643530537));
     400        assertEquals(23.4, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 1.0);
     401
     402        // similar segments, reversed direction
     403        node3.setCoor(node2.getCoor());
     404        node4.setCoor(node1.getCoor());
     405        assertEquals(0.0, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 0.000001);
     406
     407        // overlapping segments
     408        node3.setCoor(new LatLon(2.0, 2.2));
     409        node4.setCoor(new LatLon(2.0, 2.3));
     410        assertEquals(0.0, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 0.000001);
     411
     412        // parallel segments, n1 and n3 at same longitude
     413        node3.setCoor(new LatLon(2.1, 2.0));
     414        node4.setCoor(new LatLon(2.1, 2.3));
     415        expected = node1.getEastNorth().distance(node3.getEastNorth());
     416        assertEquals(expected, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 0.000001);
     417
     418        // parallel segments
     419        node3.setCoor(new LatLon(2.1, 2.1));
     420        assertEquals(expected, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 0.000001);
     421
     422        // almost parallel segments
     423        node3.setCoor(new LatLon(2.09999999, 2.1));
     424        assertEquals(expected, Geometry.getDistanceSegmentSegment(node1, node2, node3, node4), 0.01);
     425        assertTrue(expected > Geometry.getDistanceSegmentSegment(node1, node2, node3, node4));
     426    }
     427
    261428}