Tom Manterfield
Tom Manterfield

Reputation: 7073

How to add distance from point as an annotation in GeoDjango

I have a Geographic Model with a single PointField, I'm looking to add an annotation for the distance of each model from a given point, which I can later filter on and do additional jiggery pokery.

There's the obvious queryset.distance(to_point) function, but this doesn't actually annotate the queryset, it just adds a distance attribute to each model in the queryset, meaning I can't then apply .filter(distance__lte=some_distance) to it later on.

I'm also aware of filtering by the field and distance itself like so:

queryset.filter(point__distance_lte=(to_point, D(mi=radius)))

but since I will want to do multiple filters (to get counts of models within different distance ranges), I don't really want to make the DB calculate the distance from the given point every time, since that could be expensive.

Any ideas? Specifically, is there a way to add this as a regular annotation rather than an inserted attribute of each model?

Upvotes: 9

Views: 6218

Answers (5)

PowerAktar
PowerAktar

Reputation: 2428

Doing it like this this works for me, ie I can apply a filter on an annotation. Broken up for readability.

from models import Address
from django.contrib.gis.measure import D
from django.contrib.gis.db.models.functions import Distance


intMiles  = 200
destPoint = Point(5, 23)

queryset0 = Address.objects.all().order_by('-postcode')
        
queryset1 = queryset0.annotate(distance=Distance('myPointField' , destPoint ))
queryset2 = queryset1.filter(distance__lte=D(mi=intMiles))

Hope it helps somebody :)

Upvotes: 6

Sofiane Larbi
Sofiane Larbi

Reputation: 73

A way to annotate & sort w/out GeoDjango. This model contains a foreignkey to a Coordinates record which contains lat and lng properties.

def get_nearby_coords(lat, lng, max_distance=10):
        """
        Return objects sorted by distance to specified coordinates
        which distance is less than max_distance given in kilometers
        """
        # Great circle distance formula
        R = 6371
        qs = Precinct.objects.all().annotate(
            distance=Value(R)*Func(
                    Func(
                        F("coordinates__lat")*Value(math.sin(math.pi/180)),
                        function="sin",
                        output_field=models.FloatField()
                    ) * Value(
                        math.sin(lat*math.pi/180)
                    ) + Func(
                        F("coordinates__lat")* Value(math.pi/180),
                        function="cos",
                        output_field=models.FloatField()
                    ) * Value(
                        math.cos(lat*math.pi/180)
                    ) * Func(
                        Value(lng*math.pi/180) - F("coordinates__lng") * Value(math.pi/180),
                        function="cos",
                        output_field=models.FloatField()
                    ),
                    function="acos"
                )
        ).order_by("distance")
        if max_distance is not None:
            qs = qs.filter(distance__lt=max_distance)
        return qs

Upvotes: 0

Eugene Osiptsov
Eugene Osiptsov

Reputation: 51

One of the modern approaches is the set "output_field" arg to avoid «Improper geometry input type: ». Withour output_field django trying to convert ST_Distance_Sphere float result to GEOField and can not.

    queryset = self.objects.annotate(
        distance=Func(
            Func(
                F('addresses__location'),
                Func(
                    Value('POINT(1.022 -42.029)'),
                    function='ST_GeomFromText'
                ),
                function='ST_Distance_Sphere',
                output_field=models.FloatField()
            ),
            function='round'
        )
    )

Upvotes: 5

naren
naren

Reputation: 15233

You can use GeoQuerySet.distance

cities = City.objects.distance(reference_pnt)
for city in cities:
    print city.distance()

Link: GeoDjango distance documentaion

Edit: Adding distance attribute along with distance filter queries

usr_pnt = fromstr('POINT(-92.69 19.20)', srid=4326)
City.objects.filter(point__distance_lte=(usr_pnt, D(km=700))).distance(usr_pnt).order_by('distance')

Supported distance lookups

  • distance_lt
  • distance_lte
  • distance_gt
  • distance_gte
  • dwithin

Upvotes: 2

Tom Manterfield
Tom Manterfield

Reputation: 7073

I couldn't find any baked in way of doing this, so in the end I just created my own Aggregation class:

This only works with post_gis, but making one for another geo db shouldn't be too tricky.

from django.db.models import Aggregate, FloatField
from django.db.models.sql.aggregates import Aggregate as SQLAggregate


class Dist(Aggregate):
    def add_to_query(self, query, alias, col, source, is_summary):
        source = FloatField()
        aggregate = SQLDist(
            col, source=source, is_summary=is_summary, **self.extra)
        query.aggregates[alias] = aggregate


class SQLDist(SQLAggregate):
    sql_function = 'ST_Distance_Sphere'
    sql_template = "%(function)s(ST_GeomFromText('%(point)s'), %(field)s)"

This can be used as follows:

queryset.annotate(distance=Dist('longlat', point="POINT(1.022 -42.029)"))

Anyone knows a better way of doing this, please let me know (or tell me why mine is stupid)

Upvotes: 6

Related Questions