1

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

Loki
  • 1,064
  • 2
  • 26
  • 55

2 Answers2

2

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

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • thank you for answering +1, yes I have these points in geospatial DB on my Geoserver, but did not know how could I do it with Geoserver functions – Loki Oct 16 '20 at 10:55
  • Do you know how could I solve this with Geoserver ? – Loki Oct 16 '20 at 12:15
1

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

iCode
  • 1,254
  • 1
  • 13
  • 16
  • Yes, I have an API I use Geoserver to display those points, so It cointains all points that are shown on Map... But all points that are marked for grouping are in my Postgress DB. Sure will check on gis.stackexchange .. thank you – Loki Oct 16 '20 at 10:38
  • 1
    The DWITHIN operation in Geoserver might be helpful: https://stackoverflow.com/q/30334236/1617161 and https://wiki.state.ma.us/display/massgis/GeoServer+-+WFS+-+Filter+-+DWithin – iCode Oct 16 '20 at 10:50