7

I want to show you something. I am using django+postgis and I am experiencing wrong distance values between two points.

This is my model:

class Location(models.Model):
    name = models.CharField(max_length=255)
    latitude = models.FloatField(blank=True, null=True, verbose_name='Latitude')
    longitude = models.FloatField(blank=True, null=True, verbose_name='Longitude')
    location = models.PointField(blank = True, null=True)

    objects = models.GeoManager()

    def save(self, *args, **kwargs):
        self.location = Point(self.latitude, self.longitude)
        super(Location, self).save(*args, **kwargs)

This is the data:

bogota = Location(name='bogota', latitude='4.61779093242', longitude='-74.0893133545')
bogota.save()
barranquilla = Location(name='barranquilla', latitude='10.9731710819', longitude='-74.8020513916')
barranquilla.save()

This is the query:

ls = Location.objects.distance(bog.location).order_by('distance')
for l in ls: l.name; l.distance.km

This is the output:

Out[29]: u'BOGOTA'
Out[29]: 0.0
Out[29]: u'BARRANQUILLA'
Out[29]: 205.27667079004999

But Barranquilla city is to ~700km from Bogota city, see here.

So, what is wrong? the SRID are the default values.

  • I removed the direct link to the travelmath.com site because it invisibly redirects to some kind of marketing page that isn't working :-(. – whuber Sep 14 '11 at 2:29
8

The order of latitude and longitude has been reversed in the call

self.location = Point(self.latitude, self.longitude)

That is because Points expect the x-coordinate (longitude) to be the first argument.

Indeed, the distance between points at latitude -74 degrees and longitudes at 4.6 and 11.0 degrees is approximately 206 kilometers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.