If your surface satisfies the triangle inequality, i.e. is a Euclidean space, the best algorithm for changing the spatial index is a space filling curve or a monster curve. It reduces problem 2d to task 1d and discretizes and solves the space address. Finding similar places is something like O (log (n)). You can find a good hilbert curve nick square blog article. I suggest also looking at n-ari monotonous gray codes. I wrote myself a php implementation, including a 4-direction Hilbert curve and a moore curve.
source share