Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
These libraries started life (a long time ago) as simple ‘latitude/longitude’ code fragments intended to help people who had little experience of geodesy, and perhaps limited programming experience.
The intention was to have clear, simple illustrative code samples which could be adapted and re-used in other projects (whether those be coded in JavaScript, Java, C++, Excel VBA, Fortran(!), or anything else...). With its untyped C-style syntax, JavaScript reads remarkably close to pseudo-code, exposing the algorithms with a minimum of syntactic distractions
While still valid for that purpose, they have grown since then into considerable libraries, based around:
Complementing these are various mapping-related functions covering:
There are also supporting libraries:
The spherical-earth model provides simple formulae covering most ‘everyday’ accuracy requirements; the ellipsoidal-earth model provides more accurate formulae at the expense of complexity; the vector-based functions provide for an alternative (largely trig-free) approach.
The three core libraries (latlon-spherical.js, latlon-ellipsoidal.js, latlon-vectors.js) are mutually exclusive: only one can be used at a time. The mapping-related libraries depend on the ellipsoidal model.
These functions are as language-agnostic as possible, avoiding (as far as possible) JavaScript-specific language features which would not be recognised by users of other languages (and which might be difficult to translate to other languages). I use Greek letters in variables representing maths symbols conventionally presented as Greek letters: I value the great benefit in legibility over the minor inconvenience in typing.
Geodesy functions for working with points and paths (distances, bearings, destinations, etc) on a spherical-model earth, including (orthodromic) great-circle geodesics and (loxodromic) rhumb lines. All calculations are done using simple spherical trigonometric formulae.
new LatLon(lat, lon)
latlon.distanceTo(point[, radius])
latlon.bearingTo(point)
latlon.finalBearingTo(point)
latlon.midpointTo(point)
intermediatePointTo(point, fraction)
latlon.destinationPoint(distance, bearing[, radius])
LatLon.intersection(point1, bearing1, point2, bearing2)
LatLon.crossTrackDistanceTo(pathStart, pathEnd, radius)
LatLon.maxLatitude(bearing)
LatLon.crossingParallels(point1, point2, latitude)
latlon.rhumbDistanceTo(point[, radius])
latlon.rhumbBearingTo(point)
latlon.rhumbDestinationPoint(distance, bearing[, radius])
latlon.rhumbMidpointTo(point)
LatLon.areaOf(polygon[, radius])
latlon.equals(point)
latlon.toString([format[, decimals]])
Full details are available at www.movable-type.co.uk/scripts/latlong.html.
Notes: previously named simply latlon.js; radius moved from constructor to distance calculation methods; distances previously in kilometres now default to metres, order of arguments to destination point methods reversed.
Datum conversions etc on an ellipsoidal-model earth.
new LatLon(lat, lon[, datum])
datum
ellipsoid
latlon.convertDatum(datum)
latlon.toCartesian()
vector3d.toLatLon([datum])
latlon.toString([format[, decimals]])
Notes: LatLonE
now simply LatLon
.
More information at www.movable-type.co.uk/scripts/latlong-convert-coords.html.
Direct and inverse solutions of geodesics on the ellipsoid using Vincenty formulae (accurate to within 0.5mm distance, 0.000015″ bearing).
latlon.distanceTo(point)
latlon.bearingTo(point)
latlon.finalBearingTo(point)
latlon.destinationPoint(distance, bearing)
latlon.finalBearingOn(distance, initialBearing)
Full details are available at www.movable-type.co.uk/scripts/latlong-vincenty.html.
Sometimes geodesy calculations can be easier using vectors rather than spherical trigonometry; this
provides some basic functions. The toVector
and toLatLon
functions are equally applicable to
a spherical model and an ellipsoidal model (using ‘n-vectors’); other functions are applicable
to a spherical earth model only.
new LatLon(lat, lon)
latlon.toVector()
vector3d.toLatLonS()
latlon.greatCircle(bearing)
latlon.distanceTo(point[, radius])
latlon.bearingTo(point)
latlon.midpointTo(point)
latlon.destinationPoint(distance, bearing[, radius])
latlon.intersection(path1start, path1brngEnd, path2start, path2brngEnd)
latlon.crossTrackDistanceTo(pathStart, pathBrngEnd[, radius])
latlon.nearestPointOnSegment(point1, point2)
latlon.isBetween(point1, point2)
latlon.enclosedBy(points)
latlon.meanOf(points)
latlon.equals(point)
latlon.toString([format[, decimals]])
Notes: LatLonE
now simply LatLon
; order of arguments to destination point method reversed.
More thought is required on which of these functions operate on spherical model, which on n-vector
(geodetic) ellipsoidal model, and which equally on either.
More information at www.movable-type.co.uk/scripts/latlong-vectors.html.
Generic 3-d vector functions, not tied to geodesy applications.
new Vector3d(x, y, z)
vector3d.plus(v)
vector3d.minus(v)
vector3d.times(x)
vector3d.dividedBy(x)
vector3d.dot(v)
vector3d.cross(v)
vector3d.negate()
vector3d.length()
vector3d.unit()
vector3d.angleTo(v, vSign)
vector3d.rotateAround(axis, theta)
vector3d.toString(decimals)
Conversions between UTM coordinates and latitude-longitude points using Karney’s calculations (accurate to 5nm).
new Utm(zone, hemisphere, easting, northing[, datum[, convergence, scale]])
latlon.toUtm()
utm.toLatLonE()
Utm.parse([utmCoord])
utm.toString([digits])
More information at www.movable-type.co.uk/scripts/latlong-utm-mgrs.html.
Conversions between MGRS/NATO grid references and UTM coordinates.
new Mgrs(zone, band, e100k, n100k, easting, northing[, datum])
Utm.toMgrs()
mgrs.toUtm()
Mgrs.parse(mgrsGridRef)
mgrs.toString([digits])
More information at www.movable-type.co.uk/scripts/latlong-utm-mgrs.html.
Conversions between UK OS grid references and (OSGB36) latitude/longitude points (based on Ordnance Survey formulae).
new OsGridRef(easting, northing)
OsGridRef.latLonToOsGrid(point)
OsGridRef.osGridToLatLon(gridref, datum)
OsGridRef.parse(gridref)
osGridRef.toString([digits])
More information at www.movable-type.co.uk/scripts/latlong-gridref.html.
Conversions between decimal degrees and (sexagesimal) degrees-minutes-seconds (and compass points).
Dms.parseDMS(dmsStr)
Dms.toDms(degrees[, format[, decimals]])
Dms.toLat(degrees[, format[, decimals]])
Dms.toLon(degrees[, format[, decimals]])
Dms.toBrng(degrees[, format[, decimals]])
Dms.compassPoint(bearing[, precision])
Notes: renamed from Geo
(geo.js)
Documentation for all these methods is available at www.movable-type.co.uk/scripts/js/geodesy/docs.
While originally intended as illustrative code fragments, these functions can be used ‘as-is’, either client-side in-browser or with Node.js.
The scripts can be used in the browser simply by including them within <script>
tags. This involves
an appreciation of dependencies, the order of loading is significant. I believe browserify, bower,
etc can use npm and/or github directly, but I have no experience of using front-end package managers.
eg for various calculations on a spherical model earth:
<!doctype html><title>spherical</title><meta charset="utf-8">
<script src="js/geodesy/latlon-spherical.js"></script>
<script src="js/geodesy/dms.js"></script>
<script>
var p1 = new LatLon(50.06632, -5.71475);
var p2 = new LatLon(58.64402, -3.07009);
var d = p1.distanceTo(p2);
console.assert(d.toFixed(3) == '968874.704');
var mid = p1.midpointTo(p2);
console.assert(mid.toString() == '54°21′44″N, 004°31′51″W');
</script>
eg for geodesic distance using Vincenty’s algorithm:
<!doctype html><title>vincenty</title><meta charset="utf-8">
<script src="js/geodesy/vector3d.js"></script>
<script src="js/geodesy/latlon-ellipsoidal.js"></script>
<script src="js/geodesy/latlon-vincenty.js"></script>
<script src="js/geodesy/dms.js"></script>
<script>
var p1 = new LatLon(50.06632, -5.71475);
var p2 = new LatLon(58.64402, -3.07009);
var d = p1.distanceTo(p2);
console.assert(d.toFixed(3) == '969954.166');
</script>
eg for UTM conversions:
<!doctype html><title>utm</title><meta charset="utf-8">
<script src="js/geodesy/vector3d.js"></script>
<script src="js/geodesy/latlon-ellipsoidal.js"></script>
<script src="js/geodesy/utm.js"></script>
<script src="js/geodesy/dms.js"></script>
<script>
var utm = Utm.parse('48 N 377298.745 1483034.794');
var latlon = utm.toLatLonE();
console.assert(latlon.toString('dms', 2) == '13°24′45.00″N, 103°52′00.00″E');
</script>
eg for OS grid references:
<!doctype html><title>osgridref</title><meta charset="utf-8">
<script src="js/geodesy/vector3d.js"></script>
<script src="js/geodesy/latlon-ellipsoidal.js"></script>
<script src="js/geodesy/osgridref.js"></script>
<script src="js/geodesy/dms.js"></script>
<script>
var gridref = new OsGridRef(651409.903, 313177.270);
var pWgs84 = OsGridRef.osGridToLatLon(gridref);
console.assert(pWgs84.toString('dms', 4) == '52°39′28.7230″N, 001°42′57.7870″E');
var pOsgb = OsGridRef.osGridToLatLon(gridref, LatLon.datum.OSGB36);
console.assert(pOsgb.toString('dms', 4) == '52°39′27.2531″N, 001°43′04.5177″E');
</script>
I’ve also made a packaged-up npm package available:
npm install geodesy
eg for various calculations on a spherical model earth:
var LatLon = require('geodesy').LatLonSpherical;
var p1 = new LatLon(50.06632, -5.71475);
var p2 = new LatLon(58.64402, -3.07009);
var d = p1.distanceTo(p2);
console.assert(d.toFixed(3) == '968874.704');
var mid = p1.midpointTo(p2);
console.assert(mid.toString() == '54°21′44″N, 004°31′51″W');
eg for geodesic distance:
var LatLon = require('geodesy').LatLonEllipsoidal;
var p1 = new LatLon(50.06632, -5.71475);
var p2 = new LatLon(58.64402, -3.07009);
var d = p1.distanceTo(p2);
console.assert(d.toFixed(3) == '969954.166');
eg for UTM conversions:
var LatLon = require('geodesy').LatLonEllipsoidal;
var Utm = require('geodesy').Utm;
var Dms = require('geodesy').Dms;
var utm = Utm.parse('48 N 377298.745 1483034.794');
var latlon = utm.toLatLonE();
console.assert(latlon.toString('dms', 2) == '13°24′45.00″N, 103°52′00.00″E');
eg for OS grid references:
var OsGridRef = require('geodesy').OsGridRef;
var gridref = new OsGridRef(651409.903, 313177.270);
var pWgs84 = OsGridRef.osGridToLatLon(gridref);
console.assert(pWgs84.toString('dms', 4) == '52°39′28.7230″N, 001°42′57.7870″E');
var pOsgb = OsGridRef.osGridToLatLon(gridref, LatLon.datum.OSGB36);
console.assert(pOsgb.toString('dms', 4) == '52°39′27.2531″N, 001°43′04.5177″E');
FAQs
Libraries of geodesy functions
The npm package geodesy receives a total of 16,120 weekly downloads. As such, geodesy popularity was classified as popular.
We found that geodesy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.