8

I have some objects that are geo-localized (I have for each object the latitude + longitude). My App needs to display the objects that are 3 kilometers around the GPS position of the mobile device. I have several thousands of objects and they are localized in large area (for example, several US state, several small country), meaning in my list of objects I can have one located in NYC and another one in Miami but I can have also objects that are very close (few meters).

Currently, my App performs an iterative search. For each objects I compute the distance with the GPS position and if distance is <= 3KM then I keep the object else I ignore it. This algorithm is not very efficient and I'm looking for an Algorithm that will give better performance.

I suppose there's a way to sort my objects using the geo coord and next to find more quickly the objects that are located around the GPS position.

My current idea is just to compute rectangle with the "extreme points", North / South / East / West (from 3km of the GPS position) to limit the search zone. Next I will compute the distance only for the objects inside this box. I think something better could be done but I don't have the idea...

Any proposal will be appreciated ;-) Thanks,

Séb.

sebastien
  • 2,489
  • 5
  • 26
  • 47

3 Answers3

6

Sounds like a nearest neighbor search, but not with a maximum number of neighbors (as in kNN), but with a maximum distance threshold.

A common approach is to put the objects into a special data structure to allow ruling out large parts of the search space fast. However, these are usually made with euclidean spaces in mind, and not for the spherical (lat/lon-)plane (wrap-around issues). Therefore, you'd probably need to convert your coordinates to 3d coordinates in a cartesian system relative to the center of the sphere before you can apply one of the following data structures to search efficiently for your objects:

moooeeeep
  • 31,622
  • 22
  • 98
  • 187
  • 2
    I think a quadtree directly in lat/lon works for almost all scenarios. If the longitude is 0-360 then I'd change it so that the "seam" in the data is on the date line rather than at zero (so all issues would be only at the north pole, south pole and in the pacific). – Anders Forsgren Jul 13 '12 at 07:16
  • Really thanks, I will study Octree and kd-tree. If not too complex for my small brain, it can probably do something with it! – sebastien Jul 13 '12 at 18:40
1

The other answers mentioning spatial indexes are correct, but not necessarily the easiest solution for you.

I would consider something simpler: Group the items by country, then by state, region, city, and finally - by a few landmarks in dense cities (where you have a lot o objects).

Then you would only need to perform a few queries (check which country am I in, which state, region, etc.) to limit yourself to a very small set of objects, without implementing advanced data structures in your mobile app.

Guy Adini
  • 5,188
  • 5
  • 32
  • 34
0

One way of doing this without a specialized datastructure would appear to be sorting two copies of your data - once by longitude, once by latitude. Anything that binary searches down to close on both lat and long, is close.

Similarly, you could use your usual treap (fast) or red-black tree (low variability).

But there are probably advantages to using an r-tree or kd-tree. What I've described is probably only for avoiding taking new dependencies or avoiding coding a new datastructure from scratch.

user1277476
  • 2,871
  • 12
  • 10