Opened 13 years ago
Closed 9 years ago
#17635 closed New feature (fixed)
Missing ability to cast georaphy to geometry when using GeoDjango and PostgresSQL
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | GIS | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Looking at http://postgis.refractions.net/docs/ch08.html#PostGIS_TypeFunctionMatrix you will see that a lot of functions only work on geometric fields (ie: geography=False).
That means something like Markers.objects.extent() won't work if the coordinate field of Marker have geography set to True because the underlying function (ST_Extent()) only work with geometric fields.
A workaround (if you don't really care about precision) is to cast geography to geometry:
SELECT ST_Extent(coordinates::geometry) FROM markers;
That will perfectly work. However, you can't really do that with django. The best you can do seems to be:
qs.extra(select={'extent': 'ST_Extent("coordinates"::geometry)'}).values('extent')
But that's really far from perfect.
Here are some propositions:
- add a way to cast to geometry, that would allow to do:
qs.extent(field_name=coordinates__geometry)
(the trailing__geometry
cast a GeometryField from geography to geometry) - another way to cast to geometry would be:
qs.geometry('coordinates').extent()
- automatically cast geography to geometry if a function only support geometry (hum...)
Change History (7)
comment:1 by , 13 years ago
Description: | modified (diff) |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:3 by , 9 years ago
Once #24932 is fixed, we should be able to leverage the Cast()
database function to achieve this.
comment:4 by , 9 years ago
Has patch: | set |
---|
To ensure the new Cast
database function can be applied in this context, I added a test in this PR. Looks promising!
comment:5 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:7 by , 9 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
I've encountered the same problem (with
point_on_surface
andenvelope
) and I didn't even manage to resolve it with Django's public APIs.Here's what I ended up with:
Maybe I should have started with geometry fields instead of geography fields, but I didn't expect to need these functions...
While I'm interested in a solution to this problem, I'm not convinced we should allow these kind of approximations.
Marking as DDN for now, the GIS maintainer may have a stronger opinion on this matter.