/** * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. * * @param multiPolygon a {@link MultiPolygon} object * @return a double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]} * @since 2.0.0 */ public static double[] bbox(MultiPolygon multiPolygon) { List<Point> resultCoords = TurfMeta.coordAll(multiPolygon, false); return bboxCalculator(resultCoords); }
/** * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into degrees * Valid units: miles, nauticalmiles, inches, yards, meters, metres, centimeters, kilometres, * feet. * * @param distance in real units * @param units can be degrees, radians, miles, or kilometers inches, yards, metres, meters, * kilometres, kilometers. * @return a double value representing the distance in degrees * @since 3.0.0 */ public static double lengthToDegrees(double distance, @TurfUnitCriteria String units) { return radiansToDegrees(lengthToRadians(distance, units)); }
/** * Convert a distance measurement (assuming a spherical Earth) from a real-world unit into * radians. * * @param distance double representing a distance value assuming the distance units is in * kilometers * @return converted distance to radians value * @since 1.2.0 */ public static double lengthToRadians(double distance) { return lengthToRadians(distance, TurfConstants.UNIT_DEFAULT); }
@Test public void bboxFromMultiPolygon() throws IOException, TurfException { MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON)); double[] bbox = TurfMeasurement.bbox(multiPolygon); assertEquals(4, bbox.length); assertEquals(100, bbox[0], DELTA); assertEquals(0, bbox[1], DELTA); assertEquals(103, bbox[2], DELTA); assertEquals(3, bbox[3], DELTA); }
/** * Converts a distance to the default units. Use * {@link TurfConversion#convertLength(double, String, String)} to specify a unit to convert to. * * @param distance double representing a distance value * @param originalUnit of the distance, must be one of the units defined in * {@link TurfUnitCriteria} * @return converted distance in the default unit * @since 2.2.0 */ public static double convertLength(@FloatRange(from = 0) double distance, @NonNull @TurfUnitCriteria String originalUnit) { return convertLength(distance, originalUnit, TurfConstants.UNIT_DEFAULT); }
/** * Calculates the distance between two points in kilometers. This uses the Haversine formula to * account for global curvature. * * @param point1 first point used for calculating the bearing * @param point2 second point used for calculating the bearing * @return distance between the two points in kilometers * @see <a href="http://turfjs.org/docs/#distance">Turf distance documentation</a> * @since 1.2.0 */ public static double distance(@NonNull Point point1, @NonNull Point point2) { return distance(point1, point2, TurfConstants.UNIT_DEFAULT); }
/** * Convert a distance measurement (assuming a spherical Earth) from radians to a more friendly * unit. The units used here equals the default. * * @param radians a double using unit radian * @return converted radian to distance value * @since 1.2.0 */ public static double radiansToLength(double radians) { return radiansToLength(radians, TurfConstants.UNIT_DEFAULT); }
/** * Takes a {@link Point} and calculates the circle polygon given a radius in degrees, radians, * miles, or kilometers; and steps for precision. This uses the {@link #DEFAULT_STEPS} and * {@link TurfConstants#UNIT_DEFAULT} values. * * @param center a {@link Point} which the circle will center around * @param radius the radius of the circle * @return a {@link Polygon} which represents the newly created circle * @since 3.0.0 */ public static Polygon circle(@NonNull Point center, double radius) { return circle(center, radius, 64, TurfConstants.UNIT_DEFAULT); }
/** * Unwrap a coordinate {@link Point} from a Feature with a Point geometry. * * @param obj any value * @return a coordinate * @see <a href="http://turfjs.org/docs/#getcoord">Turf getCoord documentation</a> * @since 1.2.0 * @deprecated use {@link TurfMeta#getCoord(Feature)} */ @Deprecated public static Point getCoord(Feature obj) { return TurfMeta.getCoord(obj); }
/** * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. * * @param multiLineString a {@link MultiLineString} object * @return a double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]} * @since 2.0.0 */ public static double[] bbox(@NonNull MultiLineString multiLineString) { List<Point> resultCoords = TurfMeta.coordAll(multiLineString); return bboxCalculator(resultCoords); }
@Test public void bboxFromGeometry() throws IOException, TurfException { Geometry geometry = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON)); double[] bbox = TurfMeasurement.bbox(geometry); assertEquals(4, bbox.length); assertEquals(100, bbox[0], DELTA); assertEquals(0, bbox[1], DELTA); assertEquals(103, bbox[2], DELTA); assertEquals(3, bbox[3], DELTA); }
private static double length(List<Point> coords, String units) { double travelled = 0; Point prevCoords = coords.get(0); Point curCoords; for (int i = 1; i < coords.size(); i++) { curCoords = coords.get(i); travelled += distance(prevCoords, curCoords, units); prevCoords = curCoords; } return travelled; }
/** * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. * * @param multiPoint a {@link MultiPoint} object * @return a double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]} * @since 2.0.0 */ public static double[] bbox(@NonNull MultiPoint multiPoint) { List<Point> resultCoords = TurfMeta.coordAll(multiPoint); return bboxCalculator(resultCoords); }
@Test public void bboxFromLine() throws TurfException, IOException { LineString lineString = LineString.fromJson(loadJsonFixture(TURF_BBOX_LINESTRING)); double[] bbox = TurfMeasurement.bbox(lineString); assertEquals(4, bbox.length); assertEquals(102, bbox[0], DELTA); assertEquals(-10, bbox[1], DELTA); assertEquals(130, bbox[2], DELTA); assertEquals(4, bbox[3], DELTA); }
/** * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. * * @param lineString a {@link LineString} object * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]} * @since 2.0.0 */ public static double[] bbox(@NonNull LineString lineString) { List<Point> resultCoords = TurfMeta.coordAll(lineString); return bboxCalculator(resultCoords); }
@Test public void bboxFromMultiLineString() throws IOException, TurfException { MultiLineString multiLineString = MultiLineString.fromJson(loadJsonFixture(TURF_BBOX_MULTILINESTRING)); double[] bbox = TurfMeasurement.bbox(multiLineString); assertEquals(4, bbox.length); assertEquals(100, bbox[0], DELTA); assertEquals(0, bbox[1], DELTA); assertEquals(103, bbox[2], DELTA); assertEquals(3, bbox[3], DELTA); }
/** * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. * * @param point a {@link Point} object * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]} * @since 2.0.0 */ public static double[] bbox(@NonNull Point point) { List<Point> resultCoords = TurfMeta.coordAll(point); return bboxCalculator(resultCoords); }
@Test public void bboxFromPoint() throws IOException, TurfException { Feature feature = Feature.fromJson(loadJsonFixture(TURF_BBOX_POINT)); double[] bbox = TurfMeasurement.bbox((Point) feature.geometry()); assertEquals(4, bbox.length); assertEquals(102, bbox[0], DELTA); assertEquals(0.5, bbox[1], DELTA); assertEquals(102, bbox[2], DELTA); assertEquals(0.5, bbox[3], DELTA); }
/** * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. * * @param polygon a {@link Polygon} object * @return a double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]} * @since 2.0.0 */ public static double[] bbox(@NonNull Polygon polygon) { List<Point> resultCoords = TurfMeta.coordAll(polygon, false); return bboxCalculator(resultCoords); }
@Test public void bboxFromPolygon() throws TurfException, IOException { Feature feature = Feature.fromJson(loadJsonFixture(TURF_BBOX_POLYGON)); double[] bbox = TurfMeasurement.bbox((Polygon) feature.geometry()); assertEquals(4, bbox.length); assertEquals(100, bbox[0], DELTA); assertEquals(0, bbox[1], DELTA); assertEquals(101, bbox[2], DELTA); assertEquals(1, bbox[3], DELTA); }