Check if list of latitude, longitude are withing range

902 views Asked by At

I'm having a problem

I would like to ask what the most efficient way is to check if latitude and longitude coordinates are inside a range (for example 100 meters) from a list of latitudes and longitude points.

For example I have this list of coordinates:

[[48.34483,51.16.24517],[48.484,16.2585],[48.361,51.87739419],[6.38477205,51.87745015],[48.3645,51.16.73167],[6.38391099,51.87755068],[48.3575,16.725],[6.38380232,51.87720004],[6.38376297,51.87708017],[6.38375183,51.87704018],[6.38373055,51.8769829]]

I would like somehow that all points that are in a specific range (100m for example), to be somehow grouped.

Is there any way how I can indicate that for example from the above list:

[48.484,16.2585],[48.361,51.87739419] and [48.3575,16.725]

are in a radius of 100m ( distance between these points is less then 100m) and they should be groped

2

There are 2 answers

2
Adam Hopkinson On

Ideally you'd use a geospatial db for this, to avoid performance issues when dealing with increasing numbers of points. MySQL, Postgres etc all support geospatial functions.

But as you've tagged your question with javascript, I'll post a JS solution. There's an npm package called haversine - with it, you should be able to loop through each point and return the other points that are within 100m. Something like:

// bring in haversine from npm
var haversine = require("haversine");

// define the full list of points
var data = [
    [48.34483,51.1624517],
    [48.484,16.2585],
    [48.361,51.87739419],
    [6.38477205,51.87745015],
    [48.3645,51.1673167],
    [6.38391099,51.87755068],
    [48.3575,16.725],
    [6.38380232,51.87720004],
    [6.38376297,51.87708017],
    [6.38375183,51.87704018],
    [6.38373055,51.8769829]
];

var points = data.map(point => new Object({latitude: point[0], longitude: point[1]}));

// var to store results in
var results = [];

// loop through the points
points.forEach((pair) => {
    var nearby = points;
    // filter the full list to those within 100m of pair
    nearby.filter(point => haversine(pair, point, {unit: 'mile'}) <= 100);
    results.push({
        'point': pair,
        'nearby': nearby
    });
});

console.log(results);

Note: I corrected some of the points in your list, which had double decimals so weren't valid

2
iCode On

Sounds like a great question for a GIS professional; you could perhaps post on gis.stackexchange.com. Are you using a mapping technology where you already have access to an API? The functionality that you're looking for are referred to as geometric operations. I'd start by looking into geometry functions available in an API which calculate the distance between points. You could find the geometric center of all of the points, then request the geometry API to create a buffer around that point. Next, query if each point falls within that buffer.

Found a post which might help with finding the center of the points here: How do I find the center of a number of geographic points?

Also found a post on stackexchange which sounds very similar to yours, only the post is in reference to ArcGIS and the Point Distance (Analysis) tool: https://gis.stackexchange.com/q/91571/81346