SimpleWaySnap { public NicGeoPoint snap(NicGeoPoint point) { if (edges != null) { edgesByDistance = new TreeMap<Float, Edge>(); nearestPoints = new HashMap<Edge, NicGeoPoint>(); for (final Edge edge : edges) { final float distance = distance(edge, point); if ((!edgesByDistance.containsKey(distance)) || (edge == lastEdge)) { edgesByDistance.put(distance, edge); } } lastEdge = edgesByDistance.get(edgesByDistance.firstKey()); point = nearestPoints.get(lastEdge); } return point; } SimpleWaySnap(PointFactory factory, final Collection<Edge> edges); NicGeoPoint snap(NicGeoPoint point);  }
@Test public void testThatSnapReturnsTheGeoPointAsItWasWhenEdgesIsNull() { Set<Edge> edges = null; SimpleWaySnap simpleWaySnap = new SimpleWaySnap(new XYPointFactory(), edges); NicGeoPoint geoPointIn = new XYPoint(1000000, 1000000); NicGeoPoint geoPointOut = simpleWaySnap.snap(geoPointIn); assertEquals(geoPointIn.getLatitudeE6(), geoPointOut.getLatitudeE6()); assertEquals(geoPointIn.getLongitudeE6(), geoPointOut.getLongitudeE6()); }
@Test public void testThatSnapReturnsTheCorrectClippedGeoPoints() { final SimpleWaySnap simpleWaySnap = new SimpleWaySnap(new XYPointFactory(), edges); NicGeoPoint geoPointIn1 = new XYPoint(50720000, 7061000); NicGeoPoint geoPointOut1 = simpleWaySnap.snap(geoPointIn1); assertEquals(50721722, geoPointOut1.getLatitudeE6()); assertEquals(7061680, geoPointOut1.getLongitudeE6()); }
@Test public void testThatSnapReturnsTheCorrectGeoPointsOnTheEdge() { final SimpleWaySnap simpleWaySnap = new SimpleWaySnap(new XYPointFactory(), edges); NicGeoPoint geoPointIn = new XYPoint(50721722, 7061714); NicGeoPoint geoPointOut = simpleWaySnap.snap(geoPointIn); assertEquals(50721722, geoPointOut.getLatitudeE6()); assertEquals(7061714, geoPointOut.getLongitudeE6()); }
@Test public void testThatSnapSnapsToTheLineWhenEitherLatOrLongCrossesALine() { SimpleWaySnap simpleWaySnap = new SimpleWaySnap(new XYPointFactory(), edges); NicGeoPoint geoPointIn1 = new XYPoint(50721752, 7061710); NicGeoPoint geoPointOut1 = simpleWaySnap.snap(geoPointIn1); assertEquals(50721722, geoPointOut1.getLatitudeE6()); assertEquals(7061710, geoPointOut1.getLongitudeE6()); NicGeoPoint geoPointIn2 = new XYPoint(50722081, 7061520); NicGeoPoint geoPointOut2 = simpleWaySnap.snap(geoPointIn2); assertEquals(50722081, geoPointOut2.getLatitudeE6()); assertEquals(7061511, geoPointOut2.getLongitudeE6()); }
@Test public void testThatSnapSnapsCorrectlyToADiagonalLine() { SimpleWaySnap simpleWaySnap = new SimpleWaySnap(new XYPointFactory(), edges); NicGeoPoint geoPointIn1 = new XYPoint(50722200, 7061630); NicGeoPoint geoPointOut1 = simpleWaySnap.snap(geoPointIn1); assertEquals(50722199, geoPointOut1.getLatitudeE6()); assertEquals(7061635, geoPointOut1.getLongitudeE6()); }
@Test public void testCornerScenario(){ Set<Edge> cornerEdges = new HashSet<Edge>(); cornerEdges.add(new Edge(createXYPoint(1,1), createXYPoint(1,4))); cornerEdges.add(new Edge(createXYPoint(1,1), createXYPoint(4,1))); SimpleWaySnap simpleWaySnap = new SimpleWaySnap(new XYPointFactory(), cornerEdges); NicGeoPoint geoPointIn1 = createXYPoint(2,4); NicGeoPoint geoPointOut1 = simpleWaySnap.snap(geoPointIn1); NicGeoPoint expected1 = createXYPoint(1,4); assertEquals(expected1, geoPointOut1); NicGeoPoint geoPointIn2 = createXYPoint(4, 2); NicGeoPoint geoPointOut2 = simpleWaySnap.snap(geoPointIn2); NicGeoPoint expected2 = createXYPoint(4,1); assertEquals(expected2, geoPointOut2); NicGeoPoint geoPointIn3 = createXYPoint(2,2); NicGeoPoint geoPointOut3 = simpleWaySnap.snap(geoPointIn3); NicGeoPoint expected3 = createXYPoint(2,1); assertEquals(expected3, geoPointOut3); }
@Test public void testJumpToDifferentCorner() { Set<Edge> cornerEdges = new HashSet<Edge>(); cornerEdges.add(new Edge(createXYPoint(0,0), createXYPoint(0,4))); cornerEdges.add(new Edge(createXYPoint(0,0), createXYPoint(4,0))); cornerEdges.add(new Edge(createXYPoint(4,4), createXYPoint(0,4))); SimpleWaySnap simpleWaySnap = new SimpleWaySnap(new XYPointFactory(), cornerEdges); NicGeoPoint geoPointIn1 = createXYPoint(2,5); NicGeoPoint geoPointOut1 = simpleWaySnap.snap(geoPointIn1); NicGeoPoint expected1 = createXYPoint(2,4); assertEquals(expected1, geoPointOut1); NicGeoPoint geoPointIn2 = createXYPoint(1,1); NicGeoPoint geoPointOut2 = simpleWaySnap.snap(geoPointIn2); NicGeoPoint expected2a = createXYPoint(1, 0); NicGeoPoint expected2b = createXYPoint(0,1); assertTrue(geoPointOut2.equals(expected2a) || geoPointOut2.equals(expected2b)); NicGeoPoint expected3 = geoPointOut2; NicGeoPoint geoPointOut3 = simpleWaySnap.snap(geoPointIn2); assertEquals(expected3, geoPointOut3); }