@turf/rhumb-distance
Advanced tools
Comparing version 4.7.3 to 5.0.0
@@ -1,12 +0,12 @@ | ||
/// <reference types="geojson" /> | ||
import { Position, Point, Feature, Units } from '@turf/helpers'; | ||
import {Units} from '@turf/helpers'; | ||
type Point = GeoJSON.Feature<GeoJSON.Point> | GeoJSON.Point | number[]; | ||
/** | ||
* http://turfjs.org/docs/#rhumb-distance | ||
*/ | ||
declare function rhumbDistance(from: Point, to: Point, units?: Units): number; | ||
declare namespace rhumbDistance { } | ||
export = rhumbDistance; | ||
export default function rhumbDistance( | ||
from: Feature<Point> | Point | Position, | ||
to: Feature<Point> | Point | Position, | ||
options?: { | ||
units?: Units; | ||
} | ||
): number; |
80
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 radiansToDistance = helpers.radiansToDistance; | ||
var distanceToRadians = helpers.distanceToRadians; | ||
import { convertDistance, earthRadius } from '@turf/helpers'; | ||
import { getCoord } from '@turf/invariant'; | ||
@@ -14,5 +10,6 @@ /** | ||
* @name rhumbDistance | ||
* @param {Geometry|Feature<Point>|Array<number>} from origin point | ||
* @param {Geometry|Feature<Point>|Array<number>} to destination point | ||
* @param {string} [units=kilometers] can be degrees, radians, miles, or kilometers | ||
* @param {(Geometry|Feature<Point>)|Position} from origin point | ||
* @param {(Geometry|Feature<Point>)|Position} to destination point | ||
* @param {Object} [options] Optional parameters | ||
* @param {string} [options.units="kilometers"] can be degrees, radians, miles, or kilometers | ||
* @returns {number} distance between the two points | ||
@@ -30,20 +27,65 @@ * @example | ||
*/ | ||
module.exports = function (from, to, units) { | ||
function rhumbDistance(from, to, options) { | ||
// validation | ||
if (!from) throw new Error('from point is required'); | ||
if (!to) throw new Error('to point is required'); | ||
var units = (typeof options === 'object') ? options.units : options || 'kilometers'; | ||
units = units || 'kilometers'; | ||
var origin = getCoord(from); | ||
var destination = getCoord(to); | ||
var coordsFrom = getCoord(from); | ||
var coordsTo = getCoord(to); | ||
var origin = new GeodesyLatLon(coordsFrom[1], coordsFrom[0]); | ||
var destination = new GeodesyLatLon(coordsTo[1], coordsTo[0]); | ||
// 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[0] += (destination[0] - origin[0] > 180) ? -360 : (origin[0] - destination[0] > 180) ? 360 : 0; | ||
var distanceInMeters = origin.rhumbDistanceTo(destination); | ||
var distance = radiansToDistance(distanceToRadians(distanceInMeters, 'meters'), units); | ||
var distanceInMeters = calculateRhumbDistance(origin, destination); | ||
var distance = convertDistance(distanceInMeters, 'meters', units); | ||
return distance; | ||
}; | ||
} | ||
/** | ||
* Returns the distance travelling from ‘this’ point to destination point along a rhumb line. | ||
* Adapted from Geodesy: https://github.com/chrisveness/geodesy/blob/master/latlon-spherical.js | ||
* | ||
* @private | ||
* @param {Array<number>} origin point. | ||
* @param {Array<number>} destination point. | ||
* @param {number} [radius=6371e3] - (Mean) radius of earth (defaults to radius in metres). | ||
* @returns {number} Distance in km between this point and destination point (same units as radius). | ||
* | ||
* @example | ||
* var p1 = new LatLon(51.127, 1.338); | ||
* var p2 = new LatLon(50.964, 1.853); | ||
* var d = p1.distanceTo(p2); // 40.31 km | ||
*/ | ||
function calculateRhumbDistance(origin, destination, radius) { | ||
// φ => phi | ||
// λ => lambda | ||
// ψ => psi | ||
// Δ => Delta | ||
// δ => delta | ||
// θ => theta | ||
radius = (radius === undefined) ? earthRadius : Number(radius); | ||
// see www.edwilliams.org/avform.htm#Rhumb | ||
var R = radius; | ||
var phi1 = origin[1] * Math.PI / 180; | ||
var phi2 = destination[1] * Math.PI / 180; | ||
var DeltaPhi = phi2 - phi1; | ||
var DeltaLambda = Math.abs(destination[0] - origin[0]) * Math.PI / 180; | ||
// if dLon over 180° take shorter rhumb line across the anti-meridian: | ||
if (DeltaLambda > Math.PI) DeltaLambda -= 2 * Math.PI; | ||
// on Mercator projection, longitude distances shrink by latitude; q is the 'stretch factor' | ||
// q becomes ill-conditioned along E-W line (0/0); use empirical tolerance to avoid it | ||
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); | ||
// distance is pythagoras on 'stretched' Mercator projection | ||
var delta = Math.sqrt(DeltaPhi * DeltaPhi + q * q * DeltaLambda * DeltaLambda); // angular distance in radians | ||
var dist = delta * R; | ||
return dist; | ||
} | ||
export default rhumbDistance; |
{ | ||
"name": "@turf/rhumb-distance", | ||
"version": "4.7.3", | ||
"version": "5.0.0", | ||
"description": "turf rhumb-distance 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" | ||
}, | ||
@@ -40,13 +45,19 @@ "repository": { | ||
"devDependencies": { | ||
"@turf/distance": "^4.7.3", | ||
"benchmark": "2.1.4", | ||
"load-json-file": "2.0.0", | ||
"tape": "4.8.0", | ||
"write-json-file": "2.2.0" | ||
"@turf/distance": "*", | ||
"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-distance | ||
# rhumbDistance | ||
<!-- Generated by documentation.js. Update this documentation by updating the source code. --> | ||
## rhumbDistance | ||
Calculates the distance along a rhumb line between two [points](http://geojson.org/geojson-spec.html#point) in degrees, radians, | ||
@@ -10,5 +12,6 @@ miles, or kilometers. | ||
- `from` **([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)>)** origin point | ||
- `to` **([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)>)** destination point | ||
- `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`) | ||
- `from` **(([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)** origin point | ||
- `to` **(([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)** destination point | ||
- `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"`) | ||
@@ -15,0 +18,0 @@ **Examples** |
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
12260
2
6
171
57
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