GeoDjango: Filter by Area

188 views
Skip to first unread message

Sanjay Bhangar

unread,
Dec 19, 2016, 6:59:43 AM12/19/16
to django...@googlegroups.com
Hey folks,

I am trying to use the 'Area' function documented here: https://docs.djangoproject.com/en/1.10/ref/contrib/gis/functions/#area to annotate my geo queryset results with Area of the polygon geometries in my database.

This works great with something like:

  annotated_qset = GeoModel.objects.annotate(area=Area('geom'))

I then have a property called 'area` which I can use and it's all great.

However, I then try and filter by Area by following the documentation for filtering by Length on the documentation page, so I try something like:

  filtered_qset = GeoModel.objects.annotate(area=Area('geom')).filter(area__gt=1)

This gives an error like "AttributeError: 'AreaField' object has no attribute 'get_lookup".

It makes sense that this works for the Length function since Length uses the FloatField class, so supports lookups and filters, whereas the AreaField does not: https://github.com/django/django/blob/master/django/contrib/gis/db/models/sql/conversion.py#L24

I could be totally naive here and this may just not be a trivial thing for the back-end (PostGIS in my case) to support, but just wondering if anyone has any work-arounds to filter by area size of feature, and / or if there are plans to support this in future versions.

Thank you all again for an amazing community and amazing piece of software :-)

-Sanjay

Tim Graham

unread,
Dec 21, 2016, 11:38:31 AM12/21/16
to Django users
If you don't get an answer here, you can also ask on the geodjango list: https://groups.google.com/forum/#!forum/geodjango.

Sanjay Bhangar

unread,
Dec 22, 2016, 5:23:42 AM12/22/16
to django...@googlegroups.com
On Wed, Dec 21, 2016 at 10:08 PM, Tim Graham <timog...@gmail.com> wrote:
If you don't get an answer here, you can also ask on the geodjango list: https://groups.google.com/forum/#!forum/geodjango.


Will do. Thanks Tim, you're the best!
Reply all
Reply to author
Forward
0 new messages