Wednesday, December 21, 2016

Re: GeoDjango: Filter by Area

If you don't get an answer here, you can also ask on the geodjango list: https://groups.google.com/forum/#!forum/geodjango.

On Monday, December 19, 2016 at 6:59:43 AM UTC-5, Sanjay Bhangar wrote:
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bd72b2f9-1efb-4b68-946b-917c0b539f54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment