Question

i'm trying to query for the nearest locations for a specified location i've considered getting the distance between each point and the one i have but i think it would take a lot of time, may be O(n^2), is there any better way

Was it helpful?

Solution 2

Look into using a K-D tree or a quad-tree or oct-tree. They're actually not too difficult to implement and there are good examples on the web.

This is called Nearest-Neighbor search, and Wikipedia has a host of information on it.

You can in general do much better than O(n2) using these methods.

The basic outline of a K-D tree is this:

  1. Sort your coordinates on X and choose the value in the middle. This becomes the root node of a tree. Other values are split left and right.

  2. On the left and right subtrees, sort the coordinates in y, and choose the value in the middle, this becomes the root of the subtree.

  3. Repeat on further subtrees making sure to split on X, then Y, then X, then Y.

  4. When querying your tree, you can simply go down it like you would a binary search tree based on how the data was split. The nearest neighbor will be the last node you land on or its parent (I think, it's been a little while).

Edit: If you're looking for the K-nearest neighbors, when you search your K-D tree, you search in a range specified by a rectangle. You can search the subtree in a same way, but instead you must make sure the entire rectangle would be left, right, above, or below a node (based on how you split). If a node is contained in the rectangle, recurse on its subtrees. You can still in principle do better than O(n2) using this approach.

OTHER TIPS

A quadkey reduce the dimensions and it's similar to a fractal. A quadkey is also used for tiling for example in Bing maps. You can try my php solution hilbert-curve @ phpclasses.org. It gives the hilbert curve but also the quadkey. But it's not an exact search. If you want to refine the search you need the harvesine formula. You can use a bounding box to speed up the search.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top