/** * Takes a {@link LineString} and measures its length in the specified units. * * @param lineString geometry to measure * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria} * @return length of the input line in the units specified * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> * @since 1.2.0 */ public static double length(@NonNull LineString lineString, @NonNull @TurfConstants.TurfUnitCriteria String units) { List<Point> coordinates = lineString.coordinates(); return length(coordinates, units); }
/** * Takes a {@link Polygon} and measures its perimeter in the specified units. if the polygon * contains holes, the perimeter will also be included. * * @param polygon geometry to measure * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria} * @return total perimeter of the input polygon in the units specified * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> * @since 1.2.0 */ public static double length(@NonNull Polygon polygon, @NonNull @TurfConstants.TurfUnitCriteria String units) { double len = 0; for (List<Point> points : polygon.coordinates()) { len += length(points, units); } return len; }
/** * Takes a {@link MultiLineString} and measures its length in the specified units. * * @param multiLineString geometry to measure * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria} * @return length of the input lines combined, in the units specified * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> * @since 1.2.0 */ public static double length(@NonNull MultiLineString multiLineString, @NonNull @TurfConstants.TurfUnitCriteria String units) { double len = 0; for (List<Point> points : multiLineString.coordinates()) { len += length(points, units); } return len; }
/** * Takes a {@link MultiPolygon} and measures each polygons perimeter in the specified units. if * one of the polygons contains holes, the perimeter will also be included. * * @param multiPolygon geometry to measure * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria} * @return total perimeter of the input polygons combined, in the units specified * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> * @since 1.2.0 */ public static double length(@NonNull MultiPolygon multiPolygon, @NonNull @TurfConstants.TurfUnitCriteria String units) { double len = 0; List<List<List<Point>>> coordinates = multiPolygon.coordinates(); for (List<List<Point>> coordinate : coordinates) { for (List<Point> theCoordinate : coordinate) { len += length(theCoordinate, units); } } return len; }
@Test public void lineDistance_returnsZeroWhenRouteIsPoint() throws Exception { List<Point> coords = new ArrayList<>(); coords.add(Point.fromLngLat(1.0, 1.0)); LineString lineString = LineString.fromLngLats(coords); double distance = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS); assertEquals(0d, distance, DELTA); }
@Test public void testLineDistanceMultiLineString() throws IOException, TurfException { Feature feature = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_MULTILINESTRING)); assertEquals(4705d, Math.round(1000 * TurfMeasurement.length((MultiLineString) feature.geometry(), TurfConstants.UNIT_KILOMETERS)), DELTA); }
@Test public void testLineDistanceWithGeometries() throws IOException, TurfException { Feature route1 = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_ROUTE_ONE)); Feature route2 = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_ROUTE_TWO)); assertEquals(202, Math.round(TurfMeasurement.length((LineString) route1.geometry(), TurfConstants.UNIT_MILES))); Assert.assertEquals(741.7787396994203, TurfMeasurement.length((LineString) route2.geometry(), TurfConstants.UNIT_KILOMETERS), DELTA); }
@Test public void testLineDistancePolygon() throws IOException, TurfException { Feature feature = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_POLYGON)); assertEquals(5599, Math.round(1000 * TurfMeasurement.length((Polygon) feature.geometry(), TurfConstants.UNIT_KILOMETERS))); }
@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); // pt did not shift far assertTrue(shift < 0.000001); } }