Question

Problem

I have a list of ~5000 locations with latitude and longitude coordinates called A, and a separate subset of this list called B. I want to find all locations from A that are within n miles of any of the locations in B.

Structure

All of this data is stored in a mysql database, and requested via a python script.

Approach

My current approach is to iterate through all locations in B, and request locations within n miles of each location, adding them to the list if they don't exist yet.

This works, but in the worst case, it takes a significant amount of time, and is quite inefficient. I feel like there has to be a better way, but I am at a loss as for how to do it.

Ideas

  1. Load all locations into a list in python, and calculate distances there. This would reduce the number of mysql queries, and likely speed up the operation. It would still be slow though.
Was it helpful?

Solution

Load B into a python list and for each calculate maxlat, minlat, maxlong, minlong that everything outside of the box is definitely outside of your radius, if your radius is in nautical miles and lat/long in degrees. You can then raise an SQL query for points meeting criteria of minlat < lat < maxlat and minlong < long < maxlong. The resulting points can then be checked for exact distance and added to the in range list if they are in range.

I would suggest doing this in multiple processes.

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