pythondjangopostgisgeodjangosrid

GeoDjango + PostGIS calculates wrong Distances


I just installed PostGIS with GeoDjango. Everything worked fine, but now I have a problem and cant find out the reason for this.

I have model like this:

from django.contrib.gis.db import models

class Shop(models.Model):
    name = models.CharField(max_length=80)
    point = models.PointField(null=True, blank=True)
    objects = models.GeoManager()

And I set its point to this position (49.794254,9.927489). Then i create a point like this:

pnt = fromstr('POINT(50.084068 8.238381)')

The distance between this points should be about ~ 125 km, but when i do this:

results = Shop.objects.distance(pnt)
print results[0].distance.km

I'm getting always about 60 km too much in my result, so it returns 190 km! My SRIDs of both points are 4326... probably something wrong with that?

And maybe another interesting fact, when i do this:

pnt.distance(shop.point)

it returns 1.713790... as a result.

What am I doing wrong? Any alternatives for me to use with python + django? If there is a better solution I would not need to use PostGIS.

Hope you can help me!

Chris


Solution

  • I just ran this query in postgis :

    select round(CAST(ST_Distance_Sphere(ST_GeomFromText('POINT(49.794254 9.927489)',4326), ST_GeomFromText('POINT(50.084068 8.238381)',4326)) As numeric)/1000.0,2) as distance_km;
     distance_km 
    -------------
          190.50
    

    the result is in fact 190.50, so it seems there's nothing wrong with your 190 km result

    same result with this awesome page, there is a brief explanation of how to calculate this distances.

    the 1.713790... result seems to be in the same units of the srid, or in other words that number is not in meters.

    EDIT Ooohh I just saw your problem you misplaced the lat and lon, in the WKT format, Longitude comes first so the real query should be:

    select round(CAST(ST_Distance_Sphere(ST_GeomFromText('POINT(9.927489 49.794254)',4326), ST_GeomFromText('POINT(8.238381 50.084068)',4326)) As numeric)/1000.0,2) as distance_km;
     distance_km 
    -------------
          125.10
    

    so the points should be created like this

    POINT(9.927489 49.794254)
    POINT(8.238381 50.084068)