@turf/rhumb-destination
Advanced tools
Comparing version 4.7.3 to 5.0.0
@@ -1,12 +0,13 @@ | ||
/// <reference types="geojson" /> | ||
import { Point, Feature, Units, Position } from '@turf/helpers'; | ||
import {Units} from '@turf/helpers'; | ||
type Point = GeoJSON.Feature<GeoJSON.Point> | GeoJSON.Point | number[]; | ||
/** | ||
* http://turfjs.org/docs/#rhumb-destination | ||
*/ | ||
declare function rhumbDestination(origin: Point, distance: number, bearing: number, units?: Units): GeoJSON.Feature<GeoJSON.Point>; | ||
declare namespace rhumbDestination { } | ||
export = rhumbDestination; | ||
export default function rhumbDestination( | ||
origin: Feature<Point> | Point | Position, | ||
distance: number, | ||
bearing: number, | ||
options?: { | ||
units?: Units | ||
} | ||
): Feature<Point>; |
84
index.js
// https://en.wikipedia.org/wiki/Rhumb_line | ||
// http://www.movable-type.co.uk/scripts/latlong.html#rhumblines | ||
var helpers = require('@turf/helpers'); | ||
var getCoord = require('@turf/invariant').getCoord; | ||
var GeodesyLatLon = require('geodesy').LatLonSpherical; | ||
var point = helpers.point; | ||
var radiansToDistance = helpers.radiansToDistance; | ||
var distanceToRadians = helpers.distanceToRadians; | ||
import { earthRadius, point, convertDistance, degrees2radians } from '@turf/helpers'; | ||
import { getCoord } from '@turf/invariant'; | ||
/** | ||
* Returns the destination {@link Point} having travelled the given distance along a Rhumb line from the | ||
* origin Point with the (constant) given bearing. | ||
* origin Point with the (varant) given bearing. | ||
* | ||
* @name rhumbDestination | ||
* @param {Geometry|Feature<Point>|Array<number>} origin starting point | ||
* @param {(Geometry|Feature<Point>)|Position} origin starting point | ||
* @param {number} distance distance from the starting point | ||
* @param {number} bearing constant bearing angle ranging from -180 to 180 degrees from north | ||
* @param {string} [units=kilometers] miles, kilometers, degrees, or radians | ||
* @param {number} bearing varant bearing angle ranging from -180 to 180 degrees from north | ||
* @param {Object} [options={}] Optional parameters | ||
* @param {string} [options.units='kilometers'] can be degrees, radians, miles, or kilometers | ||
* @returns {Feature<Point>} Destination point. | ||
* @example | ||
* var point = turf.point([-75.343, 39.984], {"marker-color": "F00"}); | ||
* var pt = turf.point([-75.343, 39.984], {"marker-color": "F00"}); | ||
* var distance = 50; | ||
* var bearing = 90; | ||
* var units = 'miles'; | ||
* | ||
* var destination = turf.rhumbDestination(point, distance, bearing, units); | ||
* var destination = rhumbDestination(pt, distance, bearing, {units: 'miles'}); | ||
* | ||
* //addToMap | ||
* var addToMap = [point, destination] | ||
* var addToMap = [pt, destination] | ||
* destination.properties['marker-color'] = '#00F'; | ||
*/ | ||
module.exports = function (origin, distance, bearing, units) { | ||
function rhumbDestination(origin, distance, bearing, options) { | ||
// validation | ||
@@ -38,13 +33,56 @@ if (!origin) throw new Error('origin is required'); | ||
if (!(distance >= 0)) throw new Error('distance must be greater than 0'); | ||
var units = (typeof options === 'object') ? options.units : options || 'kilometers'; | ||
units = units || 'kilometers'; | ||
var distanceInMeters = radiansToDistance(distanceToRadians(distance, units), 'meters'); | ||
var distanceInMeters = convertDistance(distance, units, 'meters'); | ||
var coords = getCoord(origin); | ||
var pt = new GeodesyLatLon(coords[1], coords[0]); | ||
var destination = pt.rhumbDestinationPoint(distanceInMeters, bearing); | ||
var destination = calculateRhumbDestination(coords, distanceInMeters, bearing); | ||
// compensate the crossing of the 180th meridian (https://macwright.org/2016/09/26/the-180th-meridian.html) | ||
// solution from https://github.com/mapbox/mapbox-gl-js/issues/3250#issuecomment-294887678 | ||
destination.lon += (destination.lon - coords[0] > 180) ? -360 : (coords[0] - destination.lon > 180) ? 360 : 0; | ||
return point([destination.lon, destination.lat]); | ||
}; | ||
destination[0] += (destination[0] - coords[0] > 180) ? -360 : (coords[0] - destination[0] > 180) ? 360 : 0; | ||
return point(destination); | ||
} | ||
/** | ||
* Returns the destination point having travelled along a rhumb line from origin point the given | ||
* distance on the given bearing. | ||
* Adapted from Geodesy: http://www.movable-type.co.uk/scripts/latlong.html#rhumblines | ||
* | ||
* @private | ||
* @param {Array<number>} origin - point | ||
* @param {number} distance - Distance travelled, in same units as earth radius (default: metres). | ||
* @param {number} bearing - Bearing in degrees from north. | ||
* @param {number} [radius=6371e3] - (Mean) radius of earth (defaults to radius in metres). | ||
* @returns {Array<number>} Destination point. | ||
*/ | ||
function calculateRhumbDestination(origin, distance, bearing, radius) { | ||
// φ => phi | ||
// λ => lambda | ||
// ψ => psi | ||
// Δ => Delta | ||
// δ => delta | ||
// θ => theta | ||
radius = (radius === undefined) ? earthRadius : Number(radius); | ||
var delta = distance / radius; // angular distance in radians | ||
var lambda1 = origin[0] * Math.PI / 180; // to radians, but without normalize to 𝜋 | ||
var phi1 = degrees2radians(origin[1]); | ||
var theta = degrees2radians(bearing); | ||
var DeltaPhi = delta * Math.cos(theta); | ||
var phi2 = phi1 + DeltaPhi; | ||
// check for some daft bugger going past the pole, normalise latitude if so | ||
if (Math.abs(phi2) > Math.PI / 2) phi2 = phi2 > 0 ? Math.PI - phi2 : -Math.PI - phi2; | ||
var DeltaPsi = Math.log(Math.tan(phi2 / 2 + Math.PI / 4) / Math.tan(phi1 / 2 + Math.PI / 4)); | ||
var q = Math.abs(DeltaPsi) > 10e-12 ? DeltaPhi / DeltaPsi : Math.cos(phi1); // E-W course becomes ill-conditioned with 0/0 | ||
var DeltaLambda = delta * Math.sin(theta) / q; | ||
var lambda2 = lambda1 + DeltaLambda; | ||
return [((lambda2 * 180 / Math.PI) + 540) % 360 - 180, phi2 * 180 / Math.PI]; // normalise to −180..+180° | ||
} | ||
export default rhumbDestination; |
{ | ||
"name": "@turf/rhumb-destination", | ||
"version": "4.7.3", | ||
"version": "5.0.0", | ||
"description": "turf rhumb-destination module", | ||
"main": "index.js", | ||
"main": "main", | ||
"module": "index", | ||
"jsnext:main": "index", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
"index.d.ts", | ||
"main.js" | ||
], | ||
"scripts": { | ||
"test": "node test.js", | ||
"bench": "node bench.js" | ||
"pretest": "rollup -c ../../rollup.config.js", | ||
"test": "node -r @std/esm test.js", | ||
"posttest": "uglifyjs main.js -o main.min.js", | ||
"bench": "node -r @std/esm bench.js" | ||
}, | ||
@@ -42,13 +47,19 @@ "repository": { | ||
"devDependencies": { | ||
"@turf/truncate": "^4.7.3", | ||
"benchmark": "^2.1.4", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.0.0" | ||
"@turf/truncate": "*", | ||
"benchmark": "*", | ||
"load-json-file": "*", | ||
"tape": "*", | ||
"write-json-file": "*", | ||
"rollup": "*", | ||
"@std/esm": "*", | ||
"uglify-js": "*" | ||
}, | ||
"dependencies": { | ||
"@turf/helpers": "^4.7.3", | ||
"@turf/invariant": "^4.7.3", | ||
"geodesy": "1.1.2" | ||
"@turf/helpers": "5.0.0", | ||
"@turf/invariant": "5.0.0" | ||
}, | ||
"@std/esm": { | ||
"esm": "js", | ||
"cjs": true | ||
} | ||
} |
# @turf/rhumb-destination | ||
# rhumbDestination | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
## rhumbDestination | ||
Returns the destination [Point](http://geojson.org/geojson-spec.html#point) having travelled the given distance along a Rhumb line from the | ||
@@ -10,6 +12,7 @@ origin Point with the (constant) given bearing. | ||
- `origin` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Point](http://geojson.org/geojson-spec.html#point)> | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>)** starting point | ||
- `origin` **(([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<[Point](http://geojson.org/geojson-spec.html#point)>) | Position)** starting point | ||
- `distance` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** distance from the starting point | ||
- `bearing` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** constant bearing angle ranging from -180 to 180 degrees from north | ||
- `units` **\[[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)]** miles, kilometers, degrees, or radians (optional, default `kilometers`) | ||
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** Optional parameters | ||
- `options.units` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** can be degrees, radians, miles, or kilometers (optional, default `'kilometers'`) | ||
@@ -19,11 +22,10 @@ **Examples** | ||
```javascript | ||
var point = turf.point([-75.343, 39.984], {"marker-color": "F00"}); | ||
var pt = turf.point([-75.343, 39.984], {"marker-color": "F00"}); | ||
var distance = 50; | ||
var bearing = 90; | ||
var units = 'miles'; | ||
var destination = turf.rhumbDestination(point, distance, bearing, units); | ||
var destination = rhumbDestination(pt, distance, bearing, {units: 'miles'}); | ||
//addToMap | ||
var addToMap = [point, destination] | ||
var addToMap = [pt, destination] | ||
destination.properties['marker-color'] = '#00F'; | ||
@@ -30,0 +32,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13035
2
6
164
58
8
1
+ Added@turf/helpers@5.0.0(transitive)
+ Added@turf/invariant@5.0.0(transitive)
- Removedgeodesy@1.1.2
- Removed@turf/helpers@4.7.3(transitive)
- Removed@turf/invariant@4.7.3(transitive)
- Removedgeodesy@1.1.2(transitive)
Updated@turf/helpers@5.0.0
Updated@turf/invariant@5.0.0