Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to calculate the distances between many geo points

I have a matrix having around 1000 geospatial points(Longitude, Latitude) and i am trying to find the points that are in 1KM range.

NOTE: "The points are dynamic, Imagine 1000 vehicles are moving, so i have to re-calculate all distances every few seconds"

I did some searches and read about Graph algorithms like (Floyd–Warshall) to solve this, and I ended up with many keywords, and i am kinda lost now. I am considering the performance and since the search radius is short, I will not consider the curvature of the earth.

Basically, It appears that i have to calculate the distance between every point to every other point then sort the distances starting from every point in the matrix and get the points that are in its range. So if I have 1000 co-ordinates, I have to perfom this process (1000^2-1000) times and I do not beleive this is the optimum solution. Thank You.

like image 575
Alaa Qutaish Avatar asked Jan 19 '12 06:01

Alaa Qutaish


2 Answers

If you make a modell with a grid of 1km spacing:

  0   1    2    3
 ___|____|____|____
0   |    |    |
   c|   b|a   |   d
 ___|____|____|____
1   |    |    |
    |    |f   |
 ___|e___|____|____
2   |    |g   | 

let's assume your starting point is a. If your grid is of 1km size, points in 1km reach have to be in the same cell or one of the 8 neighbours (Points b, d, e, f).

Every other cell can be ignored (c,g).

While d is nearly of the same distance to a as c, c can be dropped early, because there are 2 barriers to cross, while a and d lie on opposite areas of their border, and are therefore nearly 2 km away from each other.

For early dropping of element, you can exclude, it is enough to check the x- or y-part of the coordinate. Since a belongs to (0,2), if x is 0 or smaller, or > 3, the point is already out of range.

After filtering only few candidates, you may use exhaustive search.

like image 83
user unknown Avatar answered Sep 23 '22 03:09

user unknown


In your case, you should be looking at the GeoHash which allows you to quickly query the coordinates within a given distance.

FYI, MongoDB uses geohash internally and it's performing excellently.

like image 27
qiao Avatar answered Sep 23 '22 03:09

qiao