3
votes

I'd like to be able to put in a GPS coordinate (latitude and longitude) into a python function and have it return the range of latitudes and longitudes which will fall within a certain distance from the original point.

What I ultimately want to do is to take a GPS coordinate and be able to go into my database of GPS coordinates using SQLAlchemy and return the coordinates which fall within a certain range, for example within 1 mile.

Is there any framework that does that or do you have any advice for going about solving this problem?

4

4 Answers

2
votes

You can use PostGIS which adds geographic support to PostgreSQL. The ST_DWithin function lets you find points which are within a certain distance of another point:

SELECT name
FROM some_table
WHERE ST_DWithin(
    geography_1,
    geography_2,
    distance_in_meters
);

GeoAlchemy adds extensions to SQLAlchemy for working with spatial databases. You might want to try using that in conjunction with PostGIS.

1
votes

You can also use the 'bounding box' method. It basically draws a (~square) box around a given point and finds all the points in that box.

For max_distance in miles and z as a point with latitude and longitude you can use:

dist_range = max_distance / 69.172

lat_range = (z.latitude-dist_range, z.latitude+dist_range)
lon_range = (z.longitude-dist_range, z.longitude+dist_range)

Then your query can include the entries for which co-ordinates fall in this range.

This is not very accurate, and its accuracy drops as max_distance increases. However, for simple geo-queries, it works great and doesn't use any location-specific extensions.

0
votes

An answer from the link in the comment that Carl left was actually the solution I went with:

MySQL Great Circle Distance (Haversine formula)

0
votes

I think geoalchemy will resolve your problems