Skip to content

Commit

Permalink
Upgrading to geodesy 2.3.0 (possible now that Turfjs#2489 is merged) …
Browse files Browse the repository at this point in the history
…meaning we can use import rather than require. Tightened up distance module function names and their optional parameters as well.
  • Loading branch information
smallsaucepan committed Sep 14, 2023
1 parent e6dfbd8 commit 13d093c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 45 deletions.
48 changes: 21 additions & 27 deletions packages/turf-distance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import {
degreesToRadians,
convertLength,
} from "@turf/helpers";
// Unable to find an equivalent import statement that works. Fails when building
// index.mjs → turf.min.js in packages/turf/
// import { LatLonEllipsoidal as LatLon } from "geodesy";
const LatLon = require("geodesy").LatLonEllipsoidal;

import LatLon from "geodesy/latlon-ellipsoidal-vincenty.js";
//http://en.wikipedia.org/wiki/Haversine_formula
//http://www.movable-type.co.uk/scripts/latlong.html

Expand Down Expand Up @@ -48,17 +44,20 @@ function distance(
} = {}
) {
if (options?.datum) {
return geodesic_ellipsoid_distance(from, to, options);
return geodesicEllipsoidDistance(from, to, {
datum: options.datum, // Must pass a datum option
...options,
});
} else {
return great_circle_distance(from, to, options);
return greatCircleDistance(from, to, options);
}
}

/**
* Calculates the distance between two {@link Point|points} in degrees, radians, miles, or kilometers.
* Performs a great circle calculation using the [Haversine formula](http://en.wikipedia.org/wiki/Haversine_formula) to account for global curvature.
*
* @name distance
* @name greatCircleDistance
* @param {Coord | Point} from origin point or coordinate
* @param {Coord | Point} to destination point or coordinate
* @param {Object} [options={}] Optional parameters
Expand All @@ -69,14 +68,14 @@ function distance(
* var to = turf.point([-75.534, 39.123]);
* var options = {units: 'miles'};
*
* var distance = turf.distance(from, to, options);
* var distance = turf.greatCircleDistance(from, to, options);
*
* //addToMap
* var addToMap = [from, to];
* from.properties.distance = distance;
* to.properties.distance = distance;
*/
function great_circle_distance(
function greatCircleDistance(
from: Coord | Point,
to: Coord | Point,
options: {
Expand Down Expand Up @@ -104,7 +103,7 @@ function great_circle_distance(
* Calculates the distance between two {@link Point|points}.
* Performs a geodesic ellipsoid calculation using [Vincenty's formulae](https://en.wikipedia.org/wiki/Vincenty%27s_formulae) to account for speroidal curvature.
*
* @name geodesic_ellipsoid_distance
* @name geodesicEllipsoidDistance
* @param {Coord | Point} from origin point or coordinate
* @param {Coord | Point} to destination point or coordinate
* @param {Object} [options={}] Optional parameters
Expand All @@ -116,40 +115,35 @@ function great_circle_distance(
* var to = turf.point([-75.534, 39.123]);
* var options = {units: 'miles', datum: datums.WGS84};
*
* var distance = turf.distance(from, to, options);
* var distance = turf.geodesicEllipsoidDistance(from, to, options);
*
* //addToMap
* var addToMap = [from, to];
* from.properties.distance = distance;
* to.properties.distance = distance;
*/
function geodesic_ellipsoid_distance(
function geodesicEllipsoidDistance(
from: Coord | Point,
to: Coord | Point,
options: {
units?: Units;
datum?: Datum;
} = {}
datum: Datum;
} = { datum: datums.WGS84 }
) {
const fromCoord = getCoord(from);
const toCoord = getCoord(to);

const datum = options.datum;

const fromLatLon = new LatLon(fromCoord[1], fromCoord[0]);
// datum on from point sets the tone.
fromLatLon.datum = datum;
const toLatLon = new LatLon(toCoord[1], toCoord[0]);

if (options?.datum) {
// datum on from point sets the tone.
fromLatLon.datum = options.datum;
} else {
fromLatLon.datum = datums.WGS84;
}

// LatLonEllipsoidal types don't properly list all base LatLon functions
// e.g. distanceTo. Should be able to remove when we can move to a newer
// version of geodesy.
const meters = (fromLatLon as any).distanceTo(toLatLon);
const meters = fromLatLon.distanceTo(toLatLon);
// geodesy lib result is in meters
return convertLength(meters, "meters", options.units);
}

export { great_circle_distance, geodesic_ellipsoid_distance };
export { greatCircleDistance, geodesicEllipsoidDistance };
export default distance;
4 changes: 2 additions & 2 deletions packages/turf-distance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"test:tape": "tsx test.js"
},
"devDependencies": {
"@types/geodesy": "1.1.3",
"@types/geodesy": "^2.2.3",
"@types/tape": "*",
"benchmark": "*",
"load-json-file": "*",
Expand All @@ -61,7 +61,7 @@
"dependencies": {
"@turf/helpers": "^7.0.0-alpha.0",
"@turf/invariant": "^7.0.0-alpha.0",
"geodesy": "1.1.3",
"geodesy": "^2.3.0",
"tslib": "^2.3.0"
}
}
21 changes: 20 additions & 1 deletion packages/turf-distance/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const load = require("load-json-file");
const write = require("write-json-file");
const { datums, point } = require("@turf/helpers");
const distance = require("./index").default;
const { geodesicEllipsoidDistance } = require("./index");

const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
Expand Down Expand Up @@ -86,7 +87,25 @@ test("distance -- Issue #1726 line near equator", (t) => {
});

const expected = 2248334.18; // m from QGIS
const tolerance = 1; // 1 cm expressed as m
const tolerance = 0.01; // 1 cm expressed as m
t.true(
Math.abs(overallDistance - expected) < tolerance,
`${overallDistance} within ${tolerance} of ${expected}`
);

t.end();
});

test("distance -- calling ellipsoid distance with empty options", (t) => {
// Make sure calling geodesicEllipsoidDistance directly without a datum
// option defaults to WGS84.
const p1 = point([-33.6, 81.1]);
const p2 = point([64.5, -80.8]);

let overallDistance = geodesicEllipsoidDistance(p1, p2);

const expected = 18682.436875; // m from QGIS as km
const tolerance = 0.00001; // 1 cm expressed as km
t.true(
Math.abs(overallDistance - expected) < tolerance,
`${overallDistance} within ${tolerance} of ${expected}`
Expand Down
7 changes: 2 additions & 5 deletions packages/turf-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import {
GeoJsonProperties,
} from "geojson";
import type { Datum } from "geodesy";
// Unable to find an equivalent import statement that works. Fails when building
// index.mjs → turf.min.js in packages/turf/
// import { LatLonEllipsoidal as LatLon } from "geodesy";
const LatLon = require("geodesy").LatLonEllipsoidal;
import LatLonEllipsoidalVincenty from "geodesy/latlon-ellipsoidal-vincenty.js";

import { Id } from "./lib/geojson";
export * from "./lib/geojson";
Expand Down Expand Up @@ -126,7 +123,7 @@ export const areaFactors: Record<AreaUnits, number> = {
* @memberof helpers
* @type {Object}
*/
const datums = LatLon.datum;
const datums = LatLonEllipsoidalVincenty.datums;
export { datums };

// Re-export type from geodesy so clients don't need to refer directly to
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
},
"devDependencies": {
"@types/geodesy": "1.1.3",
"@types/geodesy": "^2.2.3",
"@types/tape": "*",
"benchmark": "*",
"npm-run-all": "*",
Expand All @@ -64,7 +64,7 @@
"typescript": "*"
},
"dependencies": {
"geodesy": "1.1.3",
"geodesy": "^2.3.0",
"tslib": "^2.3.0"
}
}
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2327,10 +2327,10 @@
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==

"@types/geodesy@1.1.3":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@types/geodesy/-/geodesy-1.1.3.tgz#26999ade11f47cada7b529d19e38bba76f164665"
integrity sha512-migQ5GRDj53SNVU+HZ8ajxRoPtTWY/vXwvQFP6OIL7WJywtBquRgtHwAUuOS/C+fLNtIi+XixdqghAvT2JeRqA==
"@types/geodesy@^2.2.3":
version "2.2.3"
resolved "https://registry.yarnpkg.com/@types/geodesy/-/geodesy-2.2.3.tgz#70ff892715cb69607469c50347421151331a9198"
integrity sha512-zpgJ6W/PH8nQN5fyiynfRGLa7X9Fij8QuvBzZhN+/63+nrLaj6sLF7KbgKf55HBGryCuyuA8fEB8QUeAFCPWrg==

"@types/geojson-equality@^0.2.0":
version "0.2.0"
Expand Down Expand Up @@ -5113,10 +5113,10 @@ gensync@^1.0.0-beta.1, gensync@^1.0.0-beta.2:
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==

geodesy@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/geodesy/-/geodesy-1.1.3.tgz#a0cbd5db5c0cf8ac045e749f754f61b450382de9"
integrity sha512-H/0XSd1KjKZGZ2YGZcOYzRyY/foYAawwTEumNSo+YUwf+u5d4CfvBRg2i2Qimrx9yUEjWR8hLvMnhghuVFN0Zg==
geodesy@^2.3.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/geodesy/-/geodesy-2.4.0.tgz#6a0ad3dab3c1f95d8430aaa04fbe620b639c3e48"
integrity sha512-tHjQ1sXq8UAIEg1V0Pa6mznUxGU0R+3H5PIF6NULr0yPCAVLKqJro93Bbr19jSE18BMfyjN4osWDI4sm92m0kw==

[email protected]:
version "0.1.6"
Expand Down

0 comments on commit 13d093c

Please sign in to comment.