[GIS] GeoDjango – return SRID of geometry in GeoQuerySet

geodjangopostgispython

I'm wondering if there is a function in GeoDjango which would allow me to determine the SRID of a geometry column in a GeoQuerySet.
I know I could do it with a raw SQL query (I'm using PostGIS) but I have not been able to find a GeoDjango function which would allow me to get the SRID easily.

Thanks

Best Answer

Since a GeoQuerySet is a collection of Django model objects, the individual instances should all have the same SRID. The SRID is determined by the SRID of the geometry field in the Django model class for that QuerySet. The default SRID value in Django is 4326.

So to get the SRID of your GeoQuerySet you can get the SRID of any instance in the queryset:

qs = Zipcode.objects.all()
print(qs[0].geom.srid)

If you are not sure whether some instances in your queryset have been altered in cache, or if you want to change the SRID of the all model instances (i.e. the QuerySet), you can use the GeoQuerySet method transform. The example given in the Django documentation:

>>> qs = Zipcode.objects.all().transform() # Transforms to WGS84
>>> qs = Zipcode.objects.all().transform(32140) # Transforming to "NAD83 / Texas South Central"
>>> print(qs[0].geom.srid)
32140
Related Question