Citricguy.com

How To Check If GPS Coordinates Are Inside a Polygon Using Node JS

Posted January 11, 2020 in Tech

Working with GPS coordinates is simple in Node. Let’s say you would like to add some metadata to a location based on it being inside a bounding box (polygon), here is how you can quickly do this using Node.

First, we are going to need a few things. We need the Geolib NPM module, a set of coordinates and a polygon.

Plugin Installation

In your node project install the Geolib NPM module.

npm install geolib --save

Polygon Generation

Now, lets build our polygon. There are several sites out there that make this very easy, but here are two that I recommend.

Polygons can be very detailed, but when all is said and done they are just a collection of points that makeup the bounds of our polygon.

Using the Headwall Polygon Tool I was able to come up with the following (truncated) polygon for the district of Wailea in Maui Hawaii. This is what we will query against to see if a given set of coordinates are in that district or not.

const waileaPoly = [
    {latitude: 20.702118515215012, longitude: -156.4460783429987},
    {latitude: 20.702499885981297, longitude: -156.4448337980158},
    {latitude: 20.70242461551134, longitude: -156.4447265096552},
	...
    {latitude: 20.696405275297256, longitude: -156.44608871560388},
    {latitude: 20.698796534549484, longitude: -156.44635729273625},
    {latitude: 20.70081883658266, longitude: -156.44673280199834},
];

Putting It All Together

Finally, here is ultra simple example of how to put it all together.

const geolib = require('geolib');

const waileaPoly = [
    {latitude: 20.702118515215012, longitude: -156.4460783429987},
    {latitude: 20.702499885981297, longitude: -156.4448337980158},
    {latitude: 20.70242461551134, longitude: -156.4447265096552},
    {latitude: 20.7056888105619, longitude: -156.43717340906926},
    {latitude: 20.705795441420662, longitude: -156.4367120691187},
    {latitude: 20.705718290865594, longitude: -156.43585912665196},
    {latitude: 20.70566967972335, longitude: -156.43530390938588},
    {latitude: 20.70569555339776, longitude: -156.43483318170377},
    {latitude: 20.705638239282735, longitude: -156.43469437738725},
    {latitude: 20.68827667120142, longitude: -156.43012959292003},
    {latitude: 20.67575070185169, longitude: -156.43150573378273},
    {latitude: 20.67582114799139, longitude: -156.43294411027648},
    {latitude: 20.675781178032953, longitude: -156.43442540211447},
    {latitude: 20.6758771873992, longitude: -156.4348251403154},
    {latitude: 20.676394784353043, longitude: -156.43597589704052},
    {latitude: 20.676277119856785, longitude: -156.43653884730804},
    {latitude: 20.67579669943605, longitude: -156.43704562799905},
    {latitude: 20.675255353077826, longitude: -156.43721318765608},
    {latitude: 20.67491441399553, longitude: -156.43746862886155},
    {latitude: 20.674794133640773, longitude: -156.43791821454607},
    {latitude: 20.674814383739584, longitude: -156.43841071557483},
    {latitude: 20.674779810218727, longitude: -156.43884997060206},
    {latitude: 20.674148539793563, longitude: -156.44009326108255},
    {latitude: 20.67411062447185, longitude: -156.44105634732307},
    {latitude: 20.67419540080697, longitude: -156.44165214413272},
    {latitude: 20.67453559790333, longitude: -156.44454962268549},
    {latitude: 20.67612943279004, longitude: -156.44496943292518},
    {latitude: 20.678215097417212, longitude: -156.44612953285298},
    {latitude: 20.68070223401588, longitude: -156.44625966451906},
    {latitude: 20.682125368313287, longitude: -156.445105018067},
    {latitude: 20.684030279580902, longitude: -156.44540949331906},
    {latitude: 20.686113651351388, longitude: -156.44636176657707},
    {latitude: 20.688112329916994, longitude: -156.44594888450422},
    {latitude: 20.68981642816984, longitude: -156.44559621854762},
    {latitude: 20.69152050728149, longitude: -156.4461018594758},
    {latitude: 20.693523482987846, longitude: -156.44569693497226},
    {latitude: 20.696405275297256, longitude: -156.44608871560388},
    {latitude: 20.698796534549484, longitude: -156.44635729273625},
    {latitude: 20.70081883658266, longitude: -156.44673280199834},
];

const lat = '20.6869465'
const lng = '-156.4398942'

if (geolib.isPointInPolygon({latitude: lat, longitude: lng}, waileaPoly)) {
	console.log('In Wailea')
} else {
	console.log('Not In Wailea')
}

Once you run the code you should get the following output.

$ In Wailea

It’s really that simple. Thanks to Manuel Bieh for maintaining the Geolib module. It truely makes it simple to work with coordinates in node!


© 2020 - Josh Sommers