TurfMisc { @NonNull public static Feature nearestPointOnLine(@NonNull Point pt, @NonNull List<Point> coords) { return nearestPointOnLine(pt, coords, null); } private  TurfMisc(); @NonNull static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
                                     @NonNull Feature line); @NonNull static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
                                     @NonNull LineString line); @NonNull static LineString lineSliceAlong(@NonNull Feature line,
                                          @FloatRange(from = 0) double startDist,
                                          @FloatRange(from = 0) double stopDist,
                                          @NonNull @TurfConstants.TurfUnitCriteria String units); @NonNull static LineString lineSliceAlong(@NonNull LineString line,
                                          @FloatRange(from = 0) double startDist,
                                          @FloatRange(from = 0) double stopDist,
                                          @NonNull @TurfConstants.TurfUnitCriteria String units); @NonNull static Feature nearestPointOnLine(@NonNull Point pt, @NonNull List<Point> coords); @NonNull static Feature nearestPointOnLine(@NonNull Point pt, @NonNull List<Point> coords,
                                           @Nullable @TurfConstants.TurfUnitCriteria String units);  }
@Test public void pointOnLine_throwLineMustContainTwoOrMorePoints() throws Exception { thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Turf nearestPointOnLine requires a List of Points made up of at least" + " 2 coordinates.")); List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638)); TurfMisc.nearestPointOnLine(line.get(0), line); }
@Test public void testTurfPointOnLineFirstPoint() throws TurfException { List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638)); line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215)); Point pt = Point.fromLngLat(-122.45717525482178, 37.72003306385638); Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line); Point snapped = (Point) snappedFeature.geometry(); assertEquals(pt, snapped); }
@Test public void testTurfPointOnLinePointsBehindFirstPoint() throws TurfException { List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638)); line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215)); Point first = line.get(0); List<Point> pts = new ArrayList<>(); pts.add(Point.fromLngLat(-122.45717525482178, 37.72009306385638)); pts.add(Point.fromLngLat(-122.45717525482178, 37.82009306385638)); pts.add(Point.fromLngLat(-122.45716525482177, 37.72009306385638)); pts.add(Point.fromLngLat(-122.45516525482178, 37.72009306385638)); for (Point pt : pts) { Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line); Point snapped = (Point) snappedFeature.geometry(); assertEquals(first, snapped); } }
@Test public void testTurfPointOnLinePointsInFrontOfLastPoint() throws TurfException { List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-122.45616137981413, 37.72125936929241)); line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638)); line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215)); Point last = line.get(2); List<Point> pts = new ArrayList<>(); pts.add(Point.fromLngLat(-122.45696067810057, 37.7181405249708)); pts.add(Point.fromLngLat(-122.4573630094528, 37.71813203814049)); pts.add(Point.fromLngLat(-122.45730936527252, 37.71797927502795)); pts.add(Point.fromLngLat(-122.45718061923981, 37.71704571582896)); for (Point pt : pts) { Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line); Point snapped = (Point) snappedFeature.geometry(); assertEquals(last, snapped); } }
@Test public void testTurfPointOnLinePointsOnJoints() throws TurfException { List<Point> line1 = new ArrayList<>(); line1.add(Point.fromLngLat(-122.45616137981413, 37.72125936929241)); line1.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638)); line1.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215)); List<Point> line2 = new ArrayList<>(); line2.add(Point.fromLngLat(26.279296875, 31.728167146023935)); line2.add(Point.fromLngLat(21.796875, 32.69486597787505)); line2.add(Point.fromLngLat(18.80859375, 29.99300228455108)); line2.add(Point.fromLngLat(12.919921874999998, 33.137551192346145)); line2.add(Point.fromLngLat(10.1953125, 35.60371874069731)); line2.add(Point.fromLngLat(4.921875, 36.527294814546245)); line2.add(Point.fromLngLat(-1.669921875, 36.527294814546245)); line2.add(Point.fromLngLat(-5.44921875, 34.74161249883172)); line2.add(Point.fromLngLat(-8.7890625, 32.99023555965106)); List<Point> line3 = new ArrayList<>(); line3.add(Point.fromLngLat(-0.10919809341430663, 51.52204224896724)); line3.add(Point.fromLngLat(-0.10923027992248535, 51.521942114455435)); line3.add(Point.fromLngLat(-0.10916590690612793, 51.52186200668747)); line3.add(Point.fromLngLat(-0.10904788970947266, 51.52177522311313)); line3.add(Point.fromLngLat(-0.10886549949645996, 51.521601655468345)); line3.add(Point.fromLngLat(-0.10874748229980469, 51.52138135712038)); line3.add(Point.fromLngLat(-0.10855436325073242, 51.5206870765674)); line3.add(Point.fromLngLat(-0.10843634605407713, 51.52027984939518)); line3.add(Point.fromLngLat(-0.10839343070983887, 51.519952729849024)); line3.add(Point.fromLngLat(-0.10817885398864746, 51.51957887606202)); line3.add(Point.fromLngLat(-0.10814666748046874, 51.51928513164789)); line3.add(Point.fromLngLat(-0.10789990425109863, 51.518624199789016)); line3.add(Point.fromLngLat(-0.10759949684143065, 51.51778299991493)); List<List<Point>> lines = new ArrayList<>(); lines.add(line1); lines.add(line2); lines.add(line3); for (List<Point> line : lines) { List<Point> linePoint = new ArrayList<>(); for (Point pt : line) { linePoint.add( Point.fromLngLat(pt.longitude(), pt.latitude())); } for (Point pt : line) { Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, linePoint); Point snapped = (Point) snappedFeature.geometry(); assertEquals(pt, snapped); } } }
@Test public void testTurfPointOnLinePointsOnTopOfLine() throws TurfException { List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-0.10919809341430663, 51.52204224896724)); line.add(Point.fromLngLat(-0.10923027992248535, 51.521942114455435)); line.add(Point.fromLngLat(-0.10916590690612793, 51.52186200668747)); line.add(Point.fromLngLat(-0.10904788970947266, 51.52177522311313)); line.add(Point.fromLngLat(-0.10886549949645996, 51.521601655468345)); line.add(Point.fromLngLat(-0.10874748229980469, 51.52138135712038)); line.add(Point.fromLngLat(-0.10855436325073242, 51.5206870765674)); line.add(Point.fromLngLat(-0.10843634605407713, 51.52027984939518)); line.add(Point.fromLngLat(-0.10839343070983887, 51.519952729849024)); line.add(Point.fromLngLat(-0.10817885398864746, 51.51957887606202)); line.add(Point.fromLngLat(-0.10814666748046874, 51.51928513164789)); line.add(Point.fromLngLat(-0.10789990425109863, 51.518624199789016)); line.add(Point.fromLngLat(-0.10759949684143065, 51.51778299991493)); double dist = TurfMeasurement.length(LineString.fromLngLats(line), TurfConstants.UNIT_MILES); double increment = dist / 10; for (int i = 0; i < 10; i++) { Point pt = TurfMeasurement.along( LineString.fromLngLats(line), increment * i, TurfConstants.UNIT_MILES); Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line); Point snapped = (Point) snappedFeature.geometry(); double shift = TurfMeasurement.distance(pt, snapped, TurfConstants.UNIT_MILES); assertTrue(shift < 0.000001); } }
@Test public void testTurfPointOnLinePointAlongLine() throws TurfException { List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-122.45717525482178, 37.7200330638563)); line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215)); Point pt = TurfMeasurement.along( LineString.fromLngLats(line), 0.019, TurfConstants.UNIT_MILES); Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line); Point snapped = (Point) snappedFeature.geometry(); double shift = TurfMeasurement.distance(pt, snapped, TurfConstants.UNIT_MILES); assertTrue(shift < 0.00001); }
@Test public void testTurfPointOnLinePointsOnSidesOfLines() throws TurfException { List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-122.45616137981413, 37.72125936929241)); line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215)); Point first = line.get(0); Point last = line.get(1); List<Point> pts = new ArrayList<>(); pts.add(Point.fromLngLat(-122.45702505111694, 37.71881098149625)); pts.add(Point.fromLngLat(-122.45733618736267, 37.719235317933844)); pts.add(Point.fromLngLat(-122.45686411857605, 37.72027068864082)); pts.add(Point.fromLngLat(-122.45652079582213, 37.72063561093274)); for (Point pt : pts) { Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line); Point snapped = (Point) snappedFeature.geometry(); assertNotEquals(snapped, first); assertNotEquals(snapped, last); } }
@Test public void testTurfPointOnLinePointsOnSidesOfLinesCustomUnit() throws TurfException { List<Point> line = new ArrayList<>(); line.add(Point.fromLngLat(-122.45616137981413, 37.72125936929241)); line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215)); Point first = line.get(0); Point last = line.get(1); List<Point> pts = new ArrayList<>(); pts.add(Point.fromLngLat(-122.45702505111694, 37.71881098149625)); pts.add(Point.fromLngLat(-122.45733618736267, 37.719235317933844)); pts.add(Point.fromLngLat(-122.45686411857605, 37.72027068864082)); pts.add(Point.fromLngLat(-122.45652079582213, 37.72063561093274)); for (Point pt : pts) { Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line, TurfConstants.UNIT_MILES); Point snapped = (Point) snappedFeature.geometry(); assertNotEquals(snapped, first); assertNotEquals(snapped, last); } }