﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28696	Add the ability to natively filter geometries by geom_type	Geoffrey Fairchild	Ahmed Ibrahim	"First, version information just in case it matters:

* Python 3.6.3
* PostgreSQL 9.6.5
* PostGIS 2.4.0
* Django 1.11.6
* psycopg2 2.7.3.1

I have a some models that essentially look like this:

{{{
#!python
class Location(models.Model):
    name = models.CharField(max_length=255)
    # other fields

class LocationBorder(BoundaryBase):
    geometry = models.GeometryField()
    location = models.ForeignKey(Location, on_delete=models.CASCADE)
}}}

Locations can therefore have multiple borders, and locations can be of different types (e.g., polygon, multipolygon, point).

What I want to do is pull all location borders that are either polygons or multipolygons because I need to run an analysis on just those types of locations. I know all my borders have `geom_type` attributes:

{{{
#!python
> for location_border in LocationBorder.objects.all():
>    print(location_border.geometry.geom_type)
Polygon
MultiPolygon
Polygon
Point
...
}}}

So I thought that I could filter on that attribute, but it turns out I can't:

{{{
#!python
> for location_border in LocationBorder.objects.filter(Q(geometry__geom_type='Polygon') | Q(geometry__geom_type='MultiPolygon'))
>    print(location_border.geometry.geom_type)
...
django.core.exceptions.FieldError: Unsupported lookup 'geom_type' for GeometryField or join on the field not permitted.
}}}

I did some searching, and I came across [https://stackoverflow.com/q/26353830/1269634 this Stack Overflow thread], which essentially says that it's not possible to do this except by using `extra()`. That Stack Overflow thread was originally updated 2014, so I was shocked to see that this still isn't possible. Why is it that I can't filter on the `geom_type` attribute?

I should mention that using `extra()` ''does'' indeed work for me:

{{{
#!python
> for location_border in LocationBorder.objects.extra(where=[""GeometryType(geometry)='POLYGON' OR GeometryType(geometry)='MULTIPOLYGON'""]):
>    print(location_border.geometry.geom_type)
Polygon
MultiPolygon
Polygon
Polygon
...
}}}

While this works, it's verbose and not very Pythonic/Djangonic."	New feature	closed	GIS	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
