avatarAdam Fisher / fisher king (@therightstuff)

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2118

Abstract

aversine” being shorthand for “half of a versed sine” — is a formula for calculating distances on the surface of a sphere. Computationally, this is another relatively simple formula, and is accurate to within 0.3%.</p><h2 id="6eae">Vincenty’s Formula</h2><figure id="f6b6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*gTDpYYMukhywhD6c.png"><figcaption>An iterative method to calculate the distance between two points — <a href="https://community.esri.com/t5/coordinate-reference-systems-blog/distance-on-an-ellipsoid-vincenty-s-formulae/ba-p/902053">Simon Kettle on esri</a></figcaption></figure><p id="ace5"><a href="https://en.wikipedia.org/wiki/Vincenty%27s_formulae">Vincenty’s formula</a> — named after <a href="https://en.wikipedia.org/wiki/Thaddeus_Vincenty">Thaddeus Vincenty</a> — calculates the distance between two points on the surface of our oblate spheroid to an accuracy within <i>0.5mm</i>. This formula is more computationally expensive than the other two formulas.</p><h1 id="9302">The Packages</h1><p id="e964">I initially evaluated <a href="https://www.npmjs.com/package/geo-position.ts"><b>geo-position.ts</b></a> (which uses the great-circle distance formula) and <a href="https://www.npmjs.com/package/node-geo-distance"><b>node-geo-distance</b></a> (which uses the haversine and Vincenty formulas). Soon after deciding that I knew what I wanted to do and beginning to write up this article, I discovered <a href="https://www.npmjs.com/package/geolib"><b>geolib</b></a>.</p><p id="6499">All three packages were easy to use and — for my accuracy needs and what I presume to be “normal” performance requirements — performed more than well enough to be viable options.</p><h2 id="5434">geo-position.ts</h2><p id="a617"><a href="https://www.npmjs.com/package/geo-position.ts"><b>geo-position.ts</b></a> uses the great-circle distance formula, and provides a few useful features to boot. I had a very pleasant interaction with its maintainer when I reached out with some questions!</p><h2 id="6201">node-geo-distance</h2><p id="0248"><a href="https://www.npmjs.com/package/node-ge

Options

o-distance"><b>node-geo-distance</b></a> is as barebones as they come, with an async and sync variation of each of the haversine and Vincenty functions.</p><h2 id="f673">geolib</h2><p id="ed79"><a href="https://www.npmjs.com/package/geolib"><b>geolib</b></a> is by far the most popular of the three, and has the most features in addition to the distance calculations.</p><h2 id="547e">Performance</h2><p id="87bb">I ran a lot of performance tests between the three packages, and my results were fairly consistent. I’m not confident regarding their accuracy, however, but I’m going to post my approximate values anyway: after a lot of runs the performance from best to worst appears to be</p><ol><li><b>geo-position.ts</b>: ~0.75μs)</li><li><b>node-geo-distance</b> :~1.35μs for haversine, ~1.5μs for Vincenty’s</li><li><b>geolib:</b> anywhere between ~2.5–7.5μs depending on your desired level of accuracy.</li></ol><h1 id="2016">Takeaway</h1><p id="13f9">For my use case, even an accuracy within 0.05% is good enough (my app doesn’t really care about what happens near the north and south poles of our planet), and I’m also not (at this stage, at least) bothered by calculations that take less than ten microseconds to complete, so <b>geolib</b>’s greater accuracy combined with its additional functionality make it a great fit for my purposes. Probably most purposes :)</p><p id="02f8">Hope you’ve found this useful!</p><h1 id="7c1a">Level Up Coding</h1><p id="c229">Thanks for being a part of our community! Before you go:</p><ul><li>👏 Clap for the story and follow the author 👉</li><li>📰 View more content in the <a href="https://levelup.gitconnected.com/?utm_source=pub&amp;utm_medium=post">Level Up Coding publication</a></li><li>🔔 Follow us: <a href="https://twitter.com/gitconnected">Twitter</a> | <a href="https://www.linkedin.com/company/gitconnected">LinkedIn</a> | <a href="https://newsletter.levelup.dev">Newsletter</a></li></ul><p id="78f4">🚀👉 <a href="https://jobs.levelup.dev/talent/welcome?referral=true"><b>Join the Level Up talent collective and find an amazing job</b></a></p></article></body>

Locally Calculating The Distance Between Two Coordinates In Node.js

Image by Midjourney

I had an idea for a fun application that needs to calculate the distance “as the crow flies” between two coordinates on a map, so I went looking for a package to take care of that for me. Finding the right package wasn’t as straightforward as I’d expected, so I thought I’d share a short summary of my learnings.

Working with locations and distances is a whole new thing for me, so if you have any advice, suggestions or corrections please be sure to leave them in the comments!

The Formulas

The Great-Circle Distance Formula

The great-circle distance (in red) between two points on a sphere — Wikipedia

The distance along a great circle is the shortest distance between two points on the surface of a sphere, measured along the surface of the sphere.

But the Earth is not a sphere, it’s an oblate spheroid. The shape you get when you hold a soft ball from the top and bottom and squish it…

The great-circle distance formula is computationally simple, but is the least accurate of the three: it’s only accurate to within 0.5%.

The Haversine Formula

Spherical triangle solved by the law of haversines — Wikipedia

The haversine formula — “haversine” being shorthand for “half of a versed sine” — is a formula for calculating distances on the surface of a sphere. Computationally, this is another relatively simple formula, and is accurate to within 0.3%.

Vincenty’s Formula

An iterative method to calculate the distance between two points — Simon Kettle on esri

Vincenty’s formula — named after Thaddeus Vincenty — calculates the distance between two points on the surface of our oblate spheroid to an accuracy within 0.5mm. This formula is more computationally expensive than the other two formulas.

The Packages

I initially evaluated geo-position.ts (which uses the great-circle distance formula) and node-geo-distance (which uses the haversine and Vincenty formulas). Soon after deciding that I knew what I wanted to do and beginning to write up this article, I discovered geolib.

All three packages were easy to use and — for my accuracy needs and what I presume to be “normal” performance requirements — performed more than well enough to be viable options.

geo-position.ts

geo-position.ts uses the great-circle distance formula, and provides a few useful features to boot. I had a very pleasant interaction with its maintainer when I reached out with some questions!

node-geo-distance

node-geo-distance is as barebones as they come, with an async and sync variation of each of the haversine and Vincenty functions.

geolib

geolib is by far the most popular of the three, and has the most features in addition to the distance calculations.

Performance

I ran a lot of performance tests between the three packages, and my results were fairly consistent. I’m not confident regarding their accuracy, however, but I’m going to post my approximate values anyway: after a lot of runs the performance from best to worst appears to be

  1. geo-position.ts: ~0.75μs)
  2. node-geo-distance :~1.35μs for haversine, ~1.5μs for Vincenty’s
  3. geolib: anywhere between ~2.5–7.5μs depending on your desired level of accuracy.

Takeaway

For my use case, even an accuracy within 0.05% is good enough (my app doesn’t really care about what happens near the north and south poles of our planet), and I’m also not (at this stage, at least) bothered by calculations that take less than ten microseconds to complete, so geolib’s greater accuracy combined with its additional functionality make it a great fit for my purposes. Probably most purposes :)

Hope you’ve found this useful!

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job

JavaScript
Geodesic
Nodejs
Distance
Math
Recommended from ReadMedium