Search code examples
pythondjangopostgresqlpostgis

What is the reason for a noticable difference between postgis distance function and google maps distance calculator results?


I'm building a python/django based application in which it is important to measure the distance between two given geo-points with accuracy around at least 10 meters.

The problem is that the results of postgis distance function and google maps distance calculator (between two pins) differentiate a lot.

I placed two pins on Google Maps with the coordinates (52.162919 20.999998) and (52.163557 20.997626) and measured the distance via right-click -> measure distance and I also used postgis distance function to calculate the result in my code.

from django.contrib.gis.db.models.functions import Distance
from django.contrib.gis.geos import GEOSGeometry

qs = queryset.annotate(distance=Distance(
    GEOSGeometry(f'SRID=4326;POINT(52.162919 20.999998)'),
    GEOSGeometry(f'SRID=4326;POINT(52.163557 20.997626)'), 
    spheroid=True)) \
    .values_list('id', 'distance', named=True)

    return qs


class XXXSerializer(serializers.ModelSerializer):
    class Meta:
        model = XXX
        fields = ('id', 'distance')

    distance = serializers.SerializerMethodField()

    def get_distance(self, obj):
        return obj.distance.m

The result of Database-base calculation is 270.868624085953 meters whereas the Google Maps-base result is 175,13 meters!

The difference between those result is huge.

Do you have any ideas what may be the cause, which result is the correct one and how to adjust my code so that it matches with the Google Maps distance result?


Solution

  • In one of the calculations, you have specified latitude and longitude the wrong way around. You can test it here to check the results are repeatable if you switch the values around. It's literally just making sure you know which value is latitude and which is longitude and specifying it correctly to both APIs.

    I don't use the library but this seems to be poorly documented considering how easy this mistake is. You can see more in Does Y mean latitude and X mean longitude in every GIS software?