Skip to content

Instantly share code, notes, and snippets.

@mast4461
Last active July 21, 2022 13:57
Show Gist options
  • Save mast4461/6a49ce36a4656968549d287b888eb671 to your computer and use it in GitHub Desktop.
Save mast4461/6a49ce36a4656968549d287b888eb671 to your computer and use it in GitHub Desktop.
Compute local scales in meters per degree, for use with WGS84 geocoordinates.
const turf = require("@turf/turf")
const points = {
"Bertilsvägen 7, containern": [17.583636, 59.855249],
"Granskogen": [17.570114, 59.864838],
"Skäggesta, korsningen": [17.552488, 59.872742],
"Hågahögen": [17.586869, 59.837368]
}
// A small value for the finite difference derivative approximation.
const eps = 0.001;
// Turf.js gives distances in kilometers, but I want meters
const metersPerKilometer = 1000;
Object.entries(points).forEach(([label, [lng, lat]]) => {
const distanceEW = turf.distance(
turf.point([lng, lat]),
turf.point([lng + eps, lat])
) * metersPerKilometer;
const distanceNS = turf.distance(
turf.point([lng, lat]),
turf.point([lng, lat + eps])
) * metersPerKilometer;
// Local derivative approximations by finite difference.
// Meters per degree longitude and latitude, respectively.
const localScales = {
EW: distanceEW / eps,
NS: distanceNS / eps,
};
console.log(label, localScales);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment