/** * 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)); }
/** * Takes a Point and calculates the location of a destination point given a distance in * degrees, radians, miles, or kilometers; and bearing in degrees. This uses the Haversine * formula to account for global curvature. * * @param point starting point used for calculating the destination * @param distance distance from the starting point * @param bearing ranging from -180 to 180 in decimal degrees * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria} * @return destination {@link Point} result where you specified * @see <a href="http://turfjs.org/docs/#destination">Turf Destination documetation</a> * @since 1.2.0 */ @NonNull public static Point destination(@NonNull Point point, @FloatRange(from = 0) double distance, @FloatRange(from = -180, to = 180) double bearing, @NonNull @TurfConstants.TurfUnitCriteria String units) { double longitude1 = degreesToRadians(point.longitude()); double latitude1 = degreesToRadians(point.latitude()); double bearingRad = degreesToRadians(bearing); double radians = TurfConversion.lengthToRadians(distance, units); double latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians) + Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearingRad)); double longitude2 = longitude1 + Math.atan2(Math.sin(bearingRad) * Math.sin(radians) * Math.cos(latitude1), Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2)); return Point.fromLngLat( radiansToDegrees(longitude2), radiansToDegrees(latitude2)); }
/** * Takes two {@link Point}s and finds the geographic bearing between them. * * @param point1 first point used for calculating the bearing * @param point2 second point used for calculating the bearing * @return bearing in decimal degrees * @see <a href="http://turfjs.org/docs/#bearing">Turf Bearing documentation</a> * @since 1.3.0 */ public static double bearing(@NonNull Point point1, @NonNull Point point2) { double lon1 = degreesToRadians(point1.longitude()); double lon2 = degreesToRadians(point2.longitude()); double lat1 = degreesToRadians(point1.latitude()); double lat2 = degreesToRadians(point2.latitude()); double value1 = Math.sin(lon2 - lon1) * Math.cos(lat2); double value2 = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1); return radiansToDegrees(Math.atan2(value1, value2)); }