1111
1212import java .io .IOException ;
1313import java .util .ArrayList ;
14+ import java .util .Arrays ;
1415import java .util .List ;
1516
1617import static junit .framework .TestCase .assertNotNull ;
@@ -26,6 +27,10 @@ public class TurfMiscTest extends TestUtils {
2627 private static final String LINE_SLICE_ROUTE_TWO = "turf-line-slice/route2.geojson" ;
2728 private static final String LINE_SLICE_VERTICAL = "turf-line-slice/vertical.geojson" ;
2829
30+ private static final String LINE_SLICE_ALONG_LINE_ONE = "turf-line-slice-along/line1.geojson" ;
31+ private static final String LINE_SLICE_ALONG_ROUTE_ONE = "turf-line-slice-along/route1.geojson" ;
32+ private static final String LINE_SLICE_ALONG_ROUTE_TWO = "turf-line-slice-along/route2.geojson" ;
33+
2934 @ Rule
3035 public ExpectedException thrown = ExpectedException .none ();
3136
@@ -340,4 +345,126 @@ public void testTurfPointOnLinePointsOnSidesOfLines() throws TurfException {
340345 assertNotEquals (snapped , last );
341346 }
342347 }
348+
349+ @ Test
350+ public void testLineSliceAlongLine1 () throws IOException , TurfException {
351+ Feature line1 = Feature .fromJson (loadJsonFixture (LINE_SLICE_ALONG_LINE_ONE ));
352+ LineString lineStringLine1 = (LineString ) line1 .geometry ();
353+
354+ double start = 500 ;
355+ double stop = 750 ;
356+
357+ Point start_point = TurfMeasurement .along (lineStringLine1 , start , TurfConstants .UNIT_MILES );
358+ Point end_point = TurfMeasurement .along (lineStringLine1 , stop , TurfConstants .UNIT_MILES );
359+ LineString sliced = TurfMisc .lineSliceAlong (line1 , start , stop , TurfConstants .UNIT_MILES );
360+
361+ assertEquals (sliced .coordinates ().get (0 ).coordinates (),
362+ start_point .coordinates ());
363+ assertEquals (sliced .coordinates ().get (sliced .coordinates ().size () - 1 ).coordinates (),
364+ end_point .coordinates ());
365+ }
366+
367+ @ Test
368+ public void testLineSliceAlongOvershootLine1 () throws IOException , TurfException {
369+ Feature line1 = Feature .fromJson (loadJsonFixture (LINE_SLICE_ALONG_LINE_ONE ));
370+ LineString lineStringLine1 = (LineString ) line1 .geometry ();
371+
372+ double start = 500 ;
373+ double stop = 1500 ;
374+
375+ Point start_point = TurfMeasurement .along (lineStringLine1 , start , TurfConstants .UNIT_MILES );
376+ Point end_point = TurfMeasurement .along (lineStringLine1 , stop , TurfConstants .UNIT_MILES );
377+ LineString sliced = TurfMisc .lineSliceAlong (line1 , start , stop , TurfConstants .UNIT_MILES );
378+
379+ assertEquals (sliced .coordinates ().get (0 ).coordinates (),
380+ start_point .coordinates ());
381+ assertEquals (sliced .coordinates ().get (sliced .coordinates ().size () - 1 ).coordinates (),
382+ end_point .coordinates ());
383+ }
384+
385+ @ Test
386+ public void testLineSliceAlongRoute1 () throws IOException , TurfException {
387+ Feature route1 = Feature .fromJson (loadJsonFixture (LINE_SLICE_ALONG_ROUTE_ONE ));
388+ LineString lineStringRoute1 = (LineString )route1 .geometry ();
389+
390+ double start = 500 ;
391+ double stop = 750 ;
392+
393+ Point start_point = TurfMeasurement .along (lineStringRoute1 , start , TurfConstants .UNIT_MILES );
394+ Point end_point = TurfMeasurement .along (lineStringRoute1 , stop , TurfConstants .UNIT_MILES );
395+
396+ LineString sliced = TurfMisc .lineSliceAlong (route1 , start , stop , TurfConstants .UNIT_MILES );
397+
398+ assertEquals (sliced .coordinates ().get (0 ).coordinates (),
399+ start_point .coordinates ());
400+ assertEquals (sliced .coordinates ().get (sliced .coordinates ().size () - 1 ).coordinates (),
401+ end_point .coordinates ());
402+ }
403+
404+ @ Test
405+ public void testLineSliceAlongRoute2 () throws IOException , TurfException {
406+
407+ Feature route2 = Feature .fromJson (loadJsonFixture (LINE_SLICE_ALONG_ROUTE_TWO ));
408+ LineString lineStringRoute2 = (LineString )route2 .geometry ();
409+ double start = 25 ;
410+ double stop = 50 ;
411+
412+ Point start_point = TurfMeasurement .along (lineStringRoute2 , start , TurfConstants .UNIT_MILES );
413+ Point end_point = TurfMeasurement .along (lineStringRoute2 , stop , TurfConstants .UNIT_MILES );
414+ LineString sliced = TurfMisc .lineSliceAlong (route2 , start , stop , TurfConstants .UNIT_MILES );
415+
416+ assertEquals (sliced .coordinates ().get (0 ).coordinates (),
417+ start_point .coordinates ());
418+ assertEquals (sliced .coordinates ().get (sliced .coordinates ().size () - 1 ).coordinates (),
419+ end_point .coordinates ());
420+ }
421+
422+ @ Test
423+ public void testLineAlongStartLongerThanLength () throws Exception {
424+ thrown .expect (TurfException .class );
425+ thrown .expectMessage (startsWith ("Start position is beyond line" ));
426+
427+ Feature line1 = Feature .fromJson (loadJsonFixture (LINE_SLICE_ALONG_LINE_ONE ));
428+
429+ double start = 500000 ;
430+ double stop = 800000 ;
431+ TurfMisc .lineSliceAlong (line1 , start , stop , TurfConstants .UNIT_MILES );
432+ }
433+
434+ @ Test
435+ public void testLineAlongStopLongerThanLength () throws IOException , TurfException {
436+ Feature line1 = Feature .fromJson (loadJsonFixture (LINE_SLICE_ALONG_LINE_ONE ));
437+ LineString lineStringLine1 = (LineString ) line1 .geometry ();
438+
439+ double start = 500 ;
440+ double stop = 800000 ;
441+ Point start_point = TurfMeasurement .along (lineStringLine1 , start , TurfConstants .UNIT_MILES );
442+ List <Point > lineCoordinates = lineStringLine1 .coordinates ();
443+ LineString sliced = TurfMisc .lineSliceAlong (line1 , start , stop , TurfConstants .UNIT_MILES );
444+ assertEquals (sliced .coordinates ().get (0 ).coordinates (),
445+ start_point .coordinates ());
446+ assertEquals (sliced .coordinates ().get (sliced .coordinates ().size () - 1 ).coordinates (),
447+ lineCoordinates .get (lineCoordinates .size () - 1 ).coordinates ());
448+ }
449+
450+ @ Test
451+ public void testShortLine () throws IOException , TurfException {
452+
453+ // Distance between points is about 186 miles
454+ LineString lineStringLine1 = LineString .fromLngLats (Arrays .asList (
455+ Point .fromLngLat (113.99414062499999 , 22.350075806124867 ),
456+ Point .fromLngLat (116.76269531249999 , 23.241346102386135 )));
457+
458+ double start = 50 ;
459+ double stop = 100 ;
460+
461+ Point start_point = TurfMeasurement .along (lineStringLine1 , start , TurfConstants .UNIT_MILES );
462+ Point end_point = TurfMeasurement .along (lineStringLine1 , stop , TurfConstants .UNIT_MILES );
463+ LineString sliced = TurfMisc .lineSliceAlong (lineStringLine1 , start , stop , TurfConstants .UNIT_MILES );
464+
465+ assertEquals (sliced .coordinates ().get (0 ).coordinates (),
466+ start_point .coordinates ());
467+ assertEquals (sliced .coordinates ().get (sliced .coordinates ().size () - 1 ).coordinates (),
468+ end_point .coordinates ());
469+ }
343470}
0 commit comments