Best algorithm for comparing a base coordinate with a list of n coordinates and determining the nearest m coordinates?

I have code doing this right now. It works great with small and medium lists, but when I have a list of size n> 5000, my algorithm can take almost 1 minute on a mobile device. I mainly compare the Coordinate object in Java with the list of (Vector) Coordinate objects.

Here is my basic algorithm:

  • move each item in nx list
  • if the list of “10 closest” has less than 10 elements, add nx to the list and go to the next element
  • if there are already 10 elements in the "10 closest" list, then calculate the distance between nx and the base Coordinates
  • if the distance is less than the farthest distance in the "10 closest list", then remove the most distant element from this list and replace it with nx

I keep looking at it and trying to find a more efficient way to do it. This seems like a sort algorithm problem, so there should be a better way.

Here is my distance calculation method:

public static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {

  double theta = lon1 - lon2;

  double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));

  dist = acos(dist);

  dist = rad2deg(dist);

  dist = dist * 60 * 1.1515;

  if (unit == 'K') {

    dist = dist * 1.609344;

  } else if (unit == 'N') {

    dist = dist * 0.8684;

    }

  return (dist);

}
+3
source share
3 answers

You can save your coordinates in some tree of space splitting .

, , , . , .

:. , 3D- :

dx * dx + dy * dy + dz * dz
+2

, , . , , .

.

0

, - -, , , .

- , lat, lon . , , . , -, 10 ( n) . , 10 ( n) :

n , , .

. , lon - .

.

- lat, lon, 10 . , .

10 "" .

lat, lon, .

, .

, . , , , ( [0] - [N] )).

0

Source: https://habr.com/ru/post/1772377/


All Articles