# Possible solutions how to find all points (given by lat, long) that are within a radius from main point

let's say that I have:

• M - Main point - given by coordinates (lat, long)
• C - Collection of points - also given by coordinates (lat, long)
• R - Radius - maximum search distance

I'm trying to find all points that are within a radius from main point (neighbors).  algorithm(main_point, collection_of_points, radius) --> neighbors 

Obvious solution is to calculate distances and select those points, where distance is equal or smaller than R, but I believe there are better options with better performance.

Do you know any possible solutions? I'm open for every idea (not only the best one).

## EDIT:

I should add that I'm looking for solution where points are strongly dynamic (every point is mobile phone or car). I'd like to update point when it's neccessary (it has been moved) so frequency of updates depends on amount of connected devices (so as queries). Let's say that server will get update request every 5 second and query request every 20 second.

## Visualisation

• This is a well-explored topic. What research have you done? Oct 14, 2016 at 10:38
• @Raphael, I've found a lot of information about R-trees, but it seems overly complicated for my problem (but of course I'm going to read more about them). I like idea of GeoHash (for example Redis database is using it), but this solution has few edge cases. I've asked here, because there is maybe some good solution, that I don't know.
– pfff
Oct 14, 2016 at 10:55
• Define "overly complicated". In general, you will have to trade off different cost measures. Oct 14, 2016 at 12:08
• @Raphael If I understood correctly the idea of R-trees, every insertion action depends on state of tree, so (my data are dynamic) every change will result in the need to rebuild the tree. I don't know if it's a good idea in my situation.
– pfff
Oct 16, 2016 at 14:29

You can speed up your search by using a datastructure like a kd-tree or an r-tree. The basic idea is to divide your space into boxes and store the mapping from points to boxes (and back) in a way that allows for quick lookup times. Then you don't have to calculate the distances to all points to see which are too far away, you can restrict your search to points in the same (or adjacent) boxes.

But note that the naive approach is actually pretty good in this case, even if it's asymptotically not optimal. CPUs are really good at summing squares and it's trivial to throw more cores at the problem if you need more speed. So unless you're dealing with many points, it will be hard to beat.

• Is it good idea to use such a structure when points are dynamic? Let's say that every point represents mobile phone or car.
– pfff
Oct 14, 2016 at 11:55
• @pfff Depends on the ratio of updates and queries, obviously. If you can make any assumptions on that, please add them to the question. That said, in a mobile setting you are unlikely to use global information, anyway; just rely on transmission range limits and find close nodes locally. Oct 14, 2016 at 12:09
• @pfff If your points are reasonably uniformly distributed you can go with a very simple grid instead of a tree structure. Oct 14, 2016 at 12:19
• @Raphael, > If you can make any assumptions on that, please add them to the question. Added. > just rely on transmission range limits and find close nodes locally. Can you elaborate? > you can go with a very simple grid instead of a tree structure So this solution will be similar to GeoHash, right?
– pfff
Oct 14, 2016 at 12:56
• @pfff Connect every node with all others it can reach. Simple. Depends on what your semantics of "close" is, of course. Oct 14, 2016 at 13:47

Depending on your practical situation, once you have examined all the points you might not just determine if they are within the radius, but how long it would at least take for them to either leave or enter the radius. Then you only re-examine a point after that time interval has gone by, and classify it again.

For example, if the radius is 1km, and a car is 2km away, you might figure out that it would take 41 seconds to enter the radius (while not exceeding the speed limit excessively), so if you check every five seconds, you check that car after 45 seconds.

https://en.wikipedia.org/wiki/Spatial_database#Spatial_index

This link describes most popular ways to index spacial data.