[FWTools] Fwd: Get projection from shp using GDAL and Python

David Fawcett david.fawcett at gmail.com
Mon Oct 25 12:23:33 EST 2010


OK, I was just thinking about it and figured out how to reproduce your error...

If the shapefile does not have a .prj file (and therefore no SRS
definition), you will get the error that you are seeing.

If you know what SRS the data is in, even though it doesn't have a
.prj file, you could add some code to detect this condition and add
the SRS info.  You could either grab the SRS from another shapefile of
known SRS or use the ImportFromEPSG() function to populate it with an
EPSG code.

David.

On Mon, Oct 25, 2010 at 11:58 AM, David Fawcett <david.fawcett at gmail.com> wrote:
> Susana,
>
> I just ran this code on my local machine and it works.  It prints the
> text representation of the SRS.  (I am actually running these Python
> modules from OSGEO4W, but it shouldn't really matter.)
>
> from osgeo import ogr,osr
>
> shpPath = "c:/workshop/examples/data/mn_counties.shp"
> shapeData = ogr.Open(shpPath)
> layer = shapeData.GetLayer()
>
> srsObj = layer.GetSpatialRef()
>
> srsText = srsObj.ExportToWkt()
>
> print srsText
>
> On Mon, Oct 25, 2010 at 11:29 AM, Susana Iraiis Delgado Rodriguez
> <susana.delgado_s at utzmg.edu.mx> wrote:
>>
>>
>> ---------- Forwarded message ----------
>> From: Susana Iraiis Delgado Rodriguez <susana.delgado_s at utzmg.edu.mx>
>> Date: 2010/10/25
>> Subject: Re: [FWTools] Get projection from shp using GDAL and Python
>> To: David Fawcett <david.fawcett at gmail.com>
>>
>>
>> Thank you David for your help! I tried the code and got the next error:
>>
>> Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]
>> on
>> win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> import crawler_shp
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>>   File "crawler_shp.py", line 85, in <module>
>>     srsText = srsObj.ExportToWkt()
>> AttributeError: 'NoneType' object has no attribute 'ExportToWkt'
>>>>>
>>
>> Any idea?
>>
>


More information about the FWTools mailing list