[GIS] GeoDjango and Google Maps

distancegeodjango

I try to calculate distance between two cities and I'm in big confusion because the results are wrong.

I have a model in geodjango like this:

class Address(geo_models.Model):
    coordinates = geo_models.PointField(srid=2180)
    objects = geo_models.GeoManager()

I create Address object using Lat anf Lng from Google Maps i this way

a = Address(coordinate = "POINT(%f %f)" % (lat, lng))
a.save()

and then I calculate distance between Address object and another point (which lat and lng I have also from Google Maps)

coordinates = fromstr("POINT(%f %f)" % (lat, lng), srid=2180)
for a in addresses.distance(coordinates):
    print(a.distance)

The calculation result is 25 km but I know that it is wrong. Right distance is around 18 km also Google Maps tells that it is 18 km.

Could someone tell me where I make a mistake?

EDIT 01:

I've added srid=2180 to model and to reference point and now the result is 0.237103265743 m

Best Answer

This is likely a spatial projection issue. From the GeoDjango (Django) documentation:

Distance calculations with spatial data is tricky because, unfortunately, the Earth is not flat. Some distance queries with fields in a geographic coordinate system may have to be expressed differently because of limitations in PostGIS. Please see the Selecting an SRID section in the GeoDjango Model API documentation for more details.