I have a point: pnt = GEOSGeometry('POINT({0} {1})'.format(lon, lat)), and tried pnt.transform(900913), and it gives error:

GEOSException: Calling transform() with no SRID set is not supported

Then I tried transform() with my points in the database:

qs = T.objects.all().transform(900913)

and it works fine:

>>print qs[0].geom
>>POINT (-10887469.2136471699923277 3564159.8204717375338078)

Here's my model T:

class T(models.Model):
    name = models.CharField(max_length=200)
    geom = models.PointField(srid=4326, blank=True, null=True, default=None)
    objects = models.GeoManager()

Any idea?

  • What are you trying to accomplish and where does it go wrong? What have you tried to do in order to solve it? – Martin Apr 30 '14 at 19:50
  • Hi @Martin Thanks for your attention. I'm very new to GIS and Django, so I'm not sure if I'm using the function correctly. It's hard to find example of this function besides the document, which suggests me to try qs = T.objects.all().transform(900913). But I don't know if the point in model T and the point I defined pnt = GEOSGeometry('POINT({0} {1})'.format(lon, lat)) are the same format? I suppose they are the same, but the function works on one but the other, which confuse me. – odieatla Apr 30 '14 at 20:12

The reason why you get this error message is that the GEOSGeometry does not have a default SRID specified. When you instantiate an instance of a GEOSGeometry you have to explicitly specify the SRID of the geometry you are creating. To transform a geometry GEOS needs an "original" SRID and a "new" SRID to which it will transform to. Here is where you get the error, because there is no default, it does not know what the "original" SRID is.

This behavior is different for a GeoDjango Model like the one you defined. For a GeoDjango model there is a default srid, namely 4326. This is described in the GeoDjango Model reference. So for a specific instance of T or a queryset of T objects, GeoDjango assumes that the input geometry was in SRID 4326.

Here is an example how you can define a GEOSGeometry with an SRID specified. With the SRID also the transform function works fine.

(lon, lat) = ('1', '1')
pnt = GEOSGeometry('POINT({0} {1})'.format(lon, lat), srid=4326)
>> u'POINT (111319.4907932723144768 111325.1428663848637370)'
  • Hi @yellowcap , Thanks for your answer. But I tried exactly the same thing, however it didn't work. this is the import : from django.contrib.gis.geos import GEOSGeometry, Point. and after I ran pnt.transform(900913) in the django shell, it gave me error: Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/lib/python2.6/site-packages/django/contrib/gis/geos/geometry.py", line 513, in transform raise GEOSException("GDAL library is not available to transform() geometry.") GEOSException: GDAL library is not available to transform() geometry. Thanks! – odieatla May 15 '14 at 16:46
  • That looks like you don't have GDAL installed. For some operations, GeoDjango relies on GDAL, which is a geospatial library similar to GEOS. Do you have GDAL installed? – yellowcap May 16 '14 at 12:55
  • how can I know if GDAL is installed? In django shell, I tried from django.contrib.gis.gdal import DataSource and it gives me error: Traceback (most recent call last): File "<console>", line 1, in <module> ImportError: cannot import name DataSource. But this works: from django.contrib.gis.gdal import *. Thanks very much! – odieatla May 24 '14 at 0:52
  • You can see which version of GDAL you have installed by running gdal-config --version in a normal terminal (i.e. not python shell). Try to run that command in your terminal. – yellowcap May 26 '14 at 8:30
  • looks like I did have it installed! Thanks very much! – odieatla May 27 '14 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.