[GIS] Best GeoDjango MySQL workaround for distance queries

gdalgeodjangoMySQL

I'm using mysql 5.1.50, so the distance QuerySet method is not available to me.

Here is my model:

class Location(models.Model):
    name = models.CharField(max_length='100')
    point = models.PointField(spatial_index=False)

    objects = models.GeoManager()

I would like to find all the locations where the point is within 5 miles of another point (eg. test_point). I've found some potential ways to do this but am unsure of all of them.

Location.objects.filter(point__within=test_point.buffer(1))

But it's not clear to me what the unit is for '1'. It looks like degrees. I can convert miles to degrees but it seems like there's a cleaner way to do this.

Best Answer

You created a geo-model without expliciting the srid. This means that your geometries will default to 4326 (also known as WGS84, units are in degrees of longitude and latitude).

If you would be using PostGis < 1.5 for performing spatial queries using a distance like the buffer method does, you would need first to project your point to a projected system (most general is 900913, but there may be much better ones, depends on the zone of the world where your point is located), then create the buffered polygon, then reproject back the polygon to 4326, finally use this polygon in the within filter.

This should also apply to MySql, AFAIK.

If you would have the possibility of using PostGis 1.5, then you could create your model with geographic data type by using a GeometryField.geography option set to true [0]

[0] http://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/#geography