Ticket #17616: 17616_v6.patch

File 17616_v6.patch, 24.1 KB (added by taylor.smock, 6 years ago)

Get the closest nodes for ways and then make a list of the WaySegments surrounding the closest node, and find the nearest WaySegment from that list. Also make the getFurthestPrimitive/getClosestPrimitive generic, so they always return the type of primitive that is sent in the collection (OsmPrimitive if nothing more specific is passed).

  • 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

     
    88import java.math.BigDecimal;
    99import java.math.MathContext;
    1010import java.util.ArrayList;
     11import java.util.Collection;
    1112import java.util.Collections;
    1213import java.util.Comparator;
    1314import java.util.EnumSet;
     
    3031import org.openstreetmap.josm.data.osm.MultipolygonBuilder.JoinedPolygon;
    3132import org.openstreetmap.josm.data.osm.Node;
    3233import org.openstreetmap.josm.data.osm.NodePositionComparator;
     34import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3335import org.openstreetmap.josm.data.osm.Relation;
    3436import org.openstreetmap.josm.data.osm.Way;
     37import org.openstreetmap.josm.data.osm.WaySegment;
    3538import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
    3639import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
    3740import org.openstreetmap.josm.data.projection.Projection;
     
    10701073        }
    10711074        return new AreaAndPerimeter(Math.abs(area) / 2, perimeter);
    10721075    }
     1076
     1077    /**
     1078     * Get the closest primitive to {@code osm} from the collection of OsmPrimitive {@code primitives}
     1079     * @param <T> The return type of the primitive
     1080     * @param osm The primitive to get the distances from
     1081     * @param primitives The collection of primitives to get the distance to
     1082     * @return The closest {@code OsmPrimitive}
     1083     * @since xxx
     1084     */
     1085    public static <T extends OsmPrimitive> T getClosestPrimitive(OsmPrimitive osm, Collection<T> primitives) {
     1086        double lowestDistance = Double.MAX_VALUE;
     1087        T closest = null;
     1088        for (T primitive : primitives) {
     1089            double distance = getDistance(osm, primitive);
     1090            if (distance < lowestDistance) {
     1091                lowestDistance = distance;
     1092                closest = primitive;
     1093            }
     1094        }
     1095        return closest;
     1096    }
     1097
     1098    /**
     1099     * Get the furthest primitive to {@code osm} from the collection of
     1100     * OsmPrimitive {@code primitives}
     1101     *
     1102     * This measures distance to the closest part of the OsmPrimitives
     1103     * (it does NOT give the furthest primitive based off of the furthest
     1104     * part of that primitive)
     1105     * @param <T> The return type of the primitive
     1106     * @param osm The primitive to get the distances from
     1107     * @param primitives The collection of primitives to get the distance to
     1108     * @return The furthest {@code OsmPrimitive}
     1109     * @since xxx
     1110     */
     1111    public static <T extends OsmPrimitive> T getFurthestPrimitive(OsmPrimitive osm, Collection<T> primitives) {
     1112        double furthestDistance = Double.NEGATIVE_INFINITY;
     1113        T furthest = null;
     1114        for (T primitive : primitives) {
     1115            double distance = getDistance(osm, primitive);
     1116            if (distance > furthestDistance) {
     1117                furthestDistance = distance;
     1118                furthest = primitive;
     1119            }
     1120        }
     1121        return furthest;
     1122    }
     1123
     1124    /**
     1125     * Get the distance between different {@code OsmPrimitive}s
     1126     * @param one The primitive to get the distance from
     1127     * @param two The primitive to get the distance to
     1128     * @return The distance between the primitives
     1129     * @since xxx
     1130     */
     1131    public static double getDistance(OsmPrimitive one, OsmPrimitive two) {
     1132        double rValue = Double.MAX_VALUE;
     1133        if (one == null || two == null) return rValue;
     1134        if (one instanceof Node && two instanceof Node) {
     1135            rValue = ((Node) one).getCoor().greatCircleDistance(((Node) two).getCoor());
     1136        } else if (one instanceof Node && two instanceof Way) {
     1137            rValue = getDistanceWayNode((Way) two, (Node) one);
     1138        } else if (one instanceof Way && two instanceof Node) {
     1139            rValue = getDistanceWayNode((Way) one, (Node) two);
     1140        } else if (one instanceof Way && two instanceof Way) {
     1141            rValue = getDistanceWayWay((Way) one, (Way) two);
     1142        } else if (one instanceof Relation && !(two instanceof Relation)) {
     1143            for (OsmPrimitive osmPrimitive: ((Relation) one).getMemberPrimitives()) {
     1144                double currentDistance = getDistance(osmPrimitive, two);
     1145                if (currentDistance < rValue) rValue = currentDistance;
     1146            }
     1147        } else if (!(one instanceof Relation) && two instanceof Relation) {
     1148            for (OsmPrimitive osmPrimitive : ((Relation) two).getMemberPrimitives()) {
     1149                double currentDistance = getDistance(osmPrimitive, one);
     1150                if (currentDistance < rValue) rValue = currentDistance;
     1151            }
     1152        } else if (one instanceof Relation && two instanceof Relation) {
     1153            for (OsmPrimitive osmPrimitive1 : ((Relation) one).getMemberPrimitives()) {
     1154                for (OsmPrimitive osmPrimitive2 : ((Relation) two).getMemberPrimitives()) {
     1155                    double currentDistance = getDistance(osmPrimitive1, osmPrimitive2);
     1156                    if (currentDistance < rValue) rValue = currentDistance;
     1157                }
     1158            }
     1159        }
     1160        return rValue;
     1161    }
     1162
     1163    /**
     1164     * Get the surrounding {@link WaySegment}s of a node in a way
     1165     * @param way The way that contains the node
     1166     * @param node The node to get the segments around
     1167     * @return The segments that surround the node
     1168     * @since xxx
     1169     */
     1170    public static List<WaySegment> getSurroundingSegments(Way way, Node node) {
     1171        Set<Node> nodes = way.getNeighbours(node);
     1172        List<WaySegment> segments = new ArrayList<>();
     1173        List<Node> wayNodes = way.getNodes();
     1174        int nodeIndex = wayNodes.indexOf(node);
     1175        int nodeLastIndex = wayNodes.lastIndexOf(node);
     1176        for (Node rNode : nodes) {
     1177            int rNodeIndex = wayNodes.indexOf(rNode);
     1178            WaySegment newSegment = new WaySegment(way, Math.min(nodeIndex, rNodeIndex));
     1179            segments.add(newSegment);
     1180            if (nodeIndex != nodeLastIndex) {
     1181                newSegment = new WaySegment(way, Math.min(nodeLastIndex, rNodeIndex));
     1182                segments.add(newSegment);
     1183            }
     1184        }
     1185        return segments;
     1186    }
     1187
     1188    /**
     1189     * Get the distance between a way and a node
     1190     * @param way The way to get the distance from
     1191     * @param node The node to get the distance to
     1192     * @return The distance between the {@code way} and the {@code node}
     1193     * @since xxx
     1194     */
     1195    public static double getDistanceWayNode(Way way, Node node) {
     1196        double rValue = Double.MAX_VALUE;
     1197        if (way.getNodesCount() < 2) return rValue;
     1198        Node closestNode = getClosestPrimitive(node, way.getNodes());
     1199        List<WaySegment> segments = getSurroundingSegments(way, closestNode);
     1200        for (WaySegment segment : segments) {
     1201            EastNorth point = Geometry.closestPointToSegment(segment.getFirstNode().getEastNorth(), segment.getSecondNode().getEastNorth(), node.getEastNorth());
     1202            double distance = point.distance(node.getEastNorth());
     1203            if (distance < rValue) rValue = distance;
     1204        }
     1205        return rValue;
     1206    }
     1207
     1208    /**
     1209     * Get the closest {@code WaySegment} from a way to a primitive
     1210     * @param way The {@code Way} to get the distance from and the {@code WaySegment}
     1211     * @param primitive The {@code Primitive} to get the distance to
     1212     * @return The {@code WaySegment} that is closest to {@code primitive} from {@code way}
     1213     * @since xxx
     1214     */
     1215    public static WaySegment getClosestWaySegment(Way way, OsmPrimitive primitive) {
     1216        double lowestDistance = Double.MAX_VALUE;
     1217        List<Node> nodes = way.getNodes();
     1218        Node closestNode = getClosestPrimitive(primitive, nodes);
     1219        List<WaySegment> segments = getSurroundingSegments(way, closestNode);
     1220        WaySegment closest = null;
     1221        for (WaySegment segment : segments) {
     1222            double distance = getDistance(segment.toWay(), primitive);
     1223            if (distance < lowestDistance) {
     1224                lowestDistance = distance;
     1225                closest = segment;
     1226            }
     1227        }
     1228        return closest;
     1229    }
     1230
     1231    /**
     1232     * Get the distance between different ways
     1233     * @param one The way to get the distance from
     1234     * @param two The {@code Way} to get the distance to
     1235     * @return The shortest distance between the ways
     1236     * @since xxx
     1237     */
     1238    public static double getDistanceWayWay(Way one, Way two) {
     1239        double rValue = Double.MAX_VALUE;
     1240        Node closestNodeOne = getClosestPrimitive(two, one.getNodes());
     1241        Node closestNodeTwo = getClosestPrimitive(closestNodeOne, two.getNodes());
     1242        List<WaySegment> oneSegments = getSurroundingSegments(one, closestNodeOne);
     1243        List<WaySegment> twoSegments = getSurroundingSegments(two, closestNodeTwo);
     1244        for (WaySegment oneSegment : oneSegments) {
     1245            for (WaySegment twoSegment : twoSegments) {
     1246                EastNorth en1 = oneSegment.getFirstNode().getEastNorth();
     1247                EastNorth en2 = oneSegment.getSecondNode().getEastNorth();
     1248                EastNorth en3 = twoSegment.getFirstNode().getEastNorth();
     1249                EastNorth en4 = twoSegment.getSecondNode().getEastNorth();
     1250                if (en1 == null || en2 == null || en3 == null || en4 == null) continue;
     1251                EastNorth intersection = Geometry.getSegmentSegmentIntersection(
     1252                        en1, en2, en3, en4);
     1253                if (intersection != null) return 0.0;
     1254                double distance = getDistanceSegmentSegment(oneSegment, twoSegment);
     1255                if (distance < rValue) rValue = distance;
     1256            }
     1257        }
     1258        return rValue;
     1259    }
     1260
     1261    /**
     1262     * Get the distance between different {@code WaySegment}s
     1263     * @param one A {@code WaySegment} to get the distance from
     1264     * @param two A {@code WaySegment} to get the distance to
     1265     * @return The distance between the two {@code WaySegment}s
     1266     * @since xxx
     1267     */
     1268    public static double getDistanceSegmentSegment(WaySegment one, WaySegment two) {
     1269        EastNorth vectorOne = one.getSecondNode().getEastNorth().subtract(one.getFirstNode().getEastNorth());
     1270        EastNorth vectorTwo = two.getSecondNode().getEastNorth().subtract(two.getFirstNode().getEastNorth());
     1271        EastNorth vectorThree = one.getFirstNode().getEastNorth().subtract(two.getFirstNode().getEastNorth());
     1272        double smallNumber = 0.00000000001;
     1273        double a = dot(vectorOne, vectorOne);
     1274        double b = dot(vectorOne, vectorTwo);
     1275        double c = dot(vectorTwo, vectorTwo);
     1276        double d = dot(vectorOne, vectorThree);
     1277        double e = dot(vectorTwo, vectorThree);
     1278
     1279        double dotCombination = a * c - b * b;
     1280        double sc;
     1281        double sN;
     1282        double sD = d;
     1283        double tc;
     1284        double tN;
     1285        double tD = dotCombination;
     1286        if (dotCombination < smallNumber) {
     1287            sN = 0.0;
     1288            sD = 1.0;
     1289            tN = e;
     1290            tD = c;
     1291        } else {
     1292            sN = (b * e - c * d);
     1293            tN = (a * e - b * d);
     1294            if (sN < 0.0) {
     1295                sN = 0.0;
     1296                tN = e;
     1297                tD = c;
     1298            } else if (sN > sD) {
     1299                sN = sD;
     1300                tN = e + b;
     1301                tD = c;
     1302            }
     1303        }
     1304
     1305        if (tN < 0.0) {
     1306            tN = 0.0;
     1307            if (-d < 0.0) sN = 0.0;
     1308            else if (-d > a) sN = sD;
     1309            else {
     1310                sN = -d;
     1311                sD = a;
     1312            }
     1313        } else if (tN > tD) {
     1314            tN = tD;
     1315            if ((-d + b) < 0.0) sN = 0;
     1316            else if ((-d + b) > a) sN = sD;
     1317            else {
     1318                sN = (-d + b);
     1319                sD = a;
     1320            }
     1321        }
     1322        sc = Math.abs(sN) < smallNumber ? 0.0 : sN / sD;
     1323        tc = Math.abs(tN) < smallNumber ? 0.0 : tN / tD;
     1324        EastNorth p1 = one.getFirstNode().getEastNorth().interpolate(one.getSecondNode().getEastNorth(), sc);
     1325        EastNorth p2 = two.getFirstNode().getEastNorth().interpolate(two.getSecondNode().getEastNorth(), tc);
     1326        return p1.distance(p2);
     1327    }
     1328
     1329    /**
     1330     * Get the dot product of two different EastNorth points
     1331     * @param one The originating EastNorth
     1332     * @param two The final EastNorth
     1333     * @return the dot product of the EastNorths
     1334     * @since xxx
     1335     */
     1336    public static double dot(EastNorth one, EastNorth two) {
     1337        return two.getX() * one.getX() + one.getY() * two.getY();
     1338    }
    10731339}
  • test/unit/org/openstreetmap/josm/tools/GeometryTest.java

     
    44import static org.junit.Assert.assertEquals;
    55
    66import java.io.FileInputStream;
     7import java.util.ArrayList;
    78import java.util.Arrays;
    89import java.util.List;
    910
     
    1516import org.openstreetmap.josm.data.coor.LatLon;
    1617import org.openstreetmap.josm.data.osm.DataSet;
    1718import org.openstreetmap.josm.data.osm.Node;
     19import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1820import org.openstreetmap.josm.data.osm.Relation;
     21import org.openstreetmap.josm.data.osm.RelationMember;
    1922import org.openstreetmap.josm.data.osm.Way;
    2023import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    2124import org.openstreetmap.josm.io.OsmReader;
     
    158161        assertEquals(new EastNorth(125, 300), Geometry.getCentroidEN(Arrays.asList(en1, en2)));
    159162        assertEquals(new EastNorth(150, 266d + 2d/3d), Geometry.getCentroidEN(Arrays.asList(en1, en2, en3)));
    160163    }
     164
     165    /**
     166     * Test of {@link Geometry#getDistance} method.
     167     */
     168    @Test
     169    public void testGetDistance() {
     170        Node node1 = new Node(new LatLon(0, 0));
     171        Node node2 = new Node(new LatLon(0, 1));
     172        Node node3 = new Node(new LatLon(1, 0));
     173        Node node4 = new Node(new LatLon(1, 1));
     174        Way way1 = TestUtils.newWay("", node1, node2);
     175        Way way2 = TestUtils.newWay("", node3, node4);
     176        Relation testRelation1 = new Relation();
     177        Relation testRelation2 = new Relation();
     178        testRelation1.addMember(new RelationMember("", way1));
     179        testRelation1.addMember(new RelationMember("", way2));
     180        testRelation2.addMember(new RelationMember("", node1));
     181        testRelation2.addMember(new RelationMember("", node2));
     182        testRelation2.addMember(new RelationMember("", node3));
     183        testRelation2.addMember(new RelationMember("", node4));
     184
     185        double distance = Geometry.getDistance(null, node3);
     186        assertEquals(Double.MAX_VALUE, distance, 0.1);
     187
     188        distance = Geometry.getDistance(way1, null);
     189        assertEquals(Double.MAX_VALUE, distance, 0.1);
     190
     191        distance = Geometry.getDistance(null, null);
     192        assertEquals(Double.MAX_VALUE, distance, 0.1);
     193
     194        distance = Geometry.getDistance(node1, node2);
     195        assertEquals(111319.49079327357, distance, 0.1);
     196
     197        distance = Geometry.getDistance(way1, node3);
     198        assertEquals(111325.1428663855, distance, 0.1);
     199
     200        distance = Geometry.getDistance(node3, way1);
     201        assertEquals(111325.1428663855, distance, 0.1);
     202
     203        distance = Geometry.getDistance(way1, way2);
     204        assertEquals(111325.1428663855, distance, 0.1);
     205
     206        distance = Geometry.getDistance(testRelation1, new Node(new LatLon(0, 0.5)));
     207        assertEquals(0.0, distance, 0.1);
     208
     209        distance = Geometry.getDistance(new Node(new LatLon(0, 0.5)), testRelation1);
     210        assertEquals(0.0, distance, 0.1);
     211
     212        distance = Geometry.getDistance(testRelation1, testRelation2);
     213        assertEquals(0.0, distance, 0.1);
     214    }
     215
     216    /**
     217     * Test of {@link Geometry#getClosestPrimitive} method
     218     */
     219    @Test
     220    public void testGetClosestPrimitive() {
     221        Node node1 = new Node(new LatLon(0, 0));
     222        Node node2 = new Node(new LatLon(0, 1));
     223        Node node3 = new Node(new LatLon(1, 0));
     224        Node node4 = new Node(new LatLon(1, 1));
     225        Way way1 = TestUtils.newWay("", node1, node2);
     226        Way way2 = TestUtils.newWay("", node3, node4);
     227
     228        List<OsmPrimitive> primitives = new ArrayList<>();
     229        primitives.add(way1);
     230        primitives.add(way2);
     231        OsmPrimitive closest = Geometry.getClosestPrimitive(node1, primitives);
     232        assertEquals(way1, closest);
     233    }
     234
     235    /**
     236     * Test of {@link Geometry#getFurthestPrimitive} method
     237     */
     238    @Test
     239    public void testGetFurthestPrimitive() {
     240        Node node1 = new Node(new LatLon(0, 0));
     241        Node node2 = new Node(new LatLon(0, 1));
     242        Node node3 = new Node(new LatLon(1, 0));
     243        Node node4 = new Node(new LatLon(1, 1));
     244        Way way1 = TestUtils.newWay("", node1, node2);
     245        Way way2 = TestUtils.newWay("", node3, node4);
     246        Way way3 = TestUtils.newWay("", node2, node4);
     247        Way way4 = TestUtils.newWay("", node1, node3);
     248
     249        List<OsmPrimitive> primitives = new ArrayList<>();
     250        primitives.add(way1);
     251        OsmPrimitive furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(0, 0.75)), primitives);
     252        assertEquals(way1, furthest);
     253        primitives.add(way2);
     254        primitives.add(way3);
     255        primitives.add(way4);
     256        furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(0, 0.5)), primitives);
     257        assertEquals(way2, furthest);
     258        furthest = Geometry.getFurthestPrimitive(new Node(new LatLon(.25, 0.5)), primitives);
     259        assertEquals(way2, furthest);
     260    }
     261
     262    /**
     263     * Test of {@link Geometry#getClosestWaySegment} method
     264     */
     265    @Test
     266    public void testGetClosestWaySegment() {
     267        Node node1 = new Node(new LatLon(0, 0));
     268        Node node2 = new Node(new LatLon(0, 1));
     269        Node node3 = new Node(new LatLon(1, 0));
     270        Node node4 = new Node(new LatLon(1, 1));
     271        Way way1 = TestUtils.newWay("", node1, node2, node3, node4);
     272
     273        Way closestSegment = Geometry.getClosestWaySegment(way1, new Node(new LatLon(0, 0.5))).toWay();
     274        Assert.assertTrue(closestSegment.containsNode(node1));
     275        Assert.assertTrue(closestSegment.containsNode(node2));
     276    }
    161277}