3

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?

john
  • 2,993
  • 4
  • 26
  • 46
  • 4
    http://stackoverflow.com/questions/574691/mysql-great-circle-distance-haversine-formula – carl Jul 27 '11 at 04:03

4 Answers4

2

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.

zeekay
  • 49,858
  • 13
  • 107
  • 105
1

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.

Toofan
  • 150
  • 8
0

I think geoalchemy will resolve your problems

Tarsis Azevedo
  • 1,413
  • 14
  • 20
0

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

MySQL Great Circle Distance (Haversine formula)

Community
  • 1
  • 1
john
  • 2,993
  • 4
  • 26
  • 46