/** * Takes a line, a start {@link Point}, and a stop point and returns the line in between those * points. * * @param startPt Starting point. * @param stopPt Stopping point. * @param line Line to slice. * @return Sliced line. * @throws TurfException signals that a Turf exception of some sort has occurred. * @see <a href="http://turfjs.org/docs/#lineslice">Turf Line slice documentation</a> * @since 1.2.0 */ @NonNull public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt, @NonNull Feature line) { checkNotNull(line.geometry(), "Feature.geometry() == null"); if (!line.geometry().type().equals("LineString")) { throw new TurfException("input must be a LineString Feature or Geometry"); } return lineSlice(startPt, stopPt, (LineString) line.geometry()); }
@Test public void lineSlice_returnsEmptyLineStringRatherThanNull() throws Exception { List<Point> coords = new ArrayList<>(); coords.add(Point.fromLngLat(1.0, 1.0)); coords.add(Point.fromLngLat(2.0, 2.0)); LineString lineString = LineString.fromLngLats(coords); assertNotNull(TurfMisc.lineSlice(coords.get(0), coords.get(1), lineString)); }
@Test public void testTurfLineSliceLine2() throws TurfException { Point start = Point.fromLngLat(0, 0.1); Point stop = Point.fromLngLat(.9, .8); ArrayList<Point> coordinates = new ArrayList<>(); coordinates.add(Point.fromLngLat(0, 0)); coordinates.add(Point.fromLngLat(1, 1)); LineString line2 = LineString.fromLngLats(coordinates); LineString sliced = TurfMisc.lineSlice(start, stop, line2); assertNotNull(sliced); }
@Test public void lineSlice_throwLineMustContainTwoOrMorePoints() throws Exception { thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Start and stop points in Turf lineSlice cannot equal each " + "other.")); List<Point> coords = new ArrayList<>(); coords.add(Point.fromLngLat(1.0, 1.0)); coords.add(Point.fromLngLat(2.0, 2.0)); Point point = Point.fromLngLat(1.0, 1.0); LineString lineString = LineString.fromLngLats(coords); TurfMisc.lineSlice(point, point, lineString); }
@Test public void lineSlice_throwsStartStopPointException() throws Exception { thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Turf lineSlice requires a LineString made up of at least 2 " + "coordinates.")); List<Point> coords = new ArrayList<>(); coords.add(Point.fromLngLat(1.0, 1.0)); Point point = Point.fromLngLat(1.0, 1.0); Point point2 = Point.fromLngLat(2.0, 2.0); LineString lineString = LineString.fromLngLats(coords); TurfMisc.lineSlice(point, point2, lineString); }
@Test public void testTurfLineSliceVertical() throws IOException, TurfException { Point start = Point.fromLngLat(-121.25447809696198, 38.70582415504791); Point stop = Point.fromLngLat(-121.25447809696198, 38.70634324369764); Feature vertical = Feature.fromJson(loadJsonFixture(LINE_SLICE_VERTICAL)); LineString sliced = TurfMisc.lineSlice(start, stop, vertical); assertNotNull(sliced); // No duplicated coords assertEquals(2, sliced.coordinates().size()); // Vertical slice does not collapse to 1st coord assertNotEquals(sliced.coordinates().get(0), sliced.coordinates().get(1)); }
@Test public void testTurfLineSliceRoute2() throws IOException, TurfException { Point start = Point.fromLngLat(-112.60660171508789, 45.96021963947196); Point stop = Point.fromLngLat(-111.97265625, 48.84302835299516); Feature route2 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ROUTE_TWO)); LineString sliced = TurfMisc.lineSlice(start, stop, route2); assertNotNull(sliced); }
@Test public void testTurfLineSliceLine1() throws IOException, TurfException { Point start = Point.fromLngLat(-97.79617309570312, 22.254624939561698); Point stop = Point.fromLngLat(-97.72750854492188, 22.057641623615734); Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ONE)); LineString sliced = TurfMisc.lineSlice(start, stop, line1); assertNotNull(sliced); }
@Test public void testTurfLineSliceRoute1() throws IOException, TurfException { Point start = Point.fromLngLat(-79.0850830078125, 37.60117623656667); Point stop = Point.fromLngLat(-77.7667236328125, 38.65119833229951); Feature route1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ROUTE_ONE)); LineString sliced = TurfMisc.lineSlice(start, stop, route1); assertNotNull(sliced); }
@Test public void testTurfLineSliceRawGeometry() throws IOException, TurfException { Point start = Point.fromLngLat(-97.79617309570312, 22.254624939561698); Point stop = Point.fromLngLat(-97.72750854492188, 22.057641623615734); Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ONE)); LineString sliced = TurfMisc.lineSlice(start, stop, (LineString) line1.geometry()); assertNotNull(sliced); }