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

David Fawcett david.fawcett at gmail.com
Mon Oct 25 14:18:45 EST 2010


You didn't include all of the modified script, so modified my script
to use some of the same logic that you were using.  I can get it to
work.  Without knowing seeing your script, I can't diagnose it.

Here is what works for me.  When I run it against a shapefile that
doesn't include a .prj file, I the the correct output.

from osgeo import ogr,osr
import os

shpPath = "c:/workshop/examples/data/mn_counties.shp"
shpName = "c:/workshop/examples/data/mn_counties"
shapeData = ogr.Open(shpPath)
layer = shapeData.GetLayer()

n = os.path.splitext(shpName)

p = n[0] + '.prj'
if os.path.lexists(p):
    print "It exists"
    srsObj = layer.GetSpatialRef()
    srsText = srsObj.ExportToWkt()
    print srsText
else:
    print "Shapefile has no .prj file"


If your shapefile doesn't have a .prj, it doesn't include SRS info.

On Mon, Oct 25, 2010 at 12:57 PM, Susana Iraiis Delgado Rodriguez
<susana.delgado_s at utzmg.edu.mx> wrote:
> Your observation is rigth, so I added an if cicle to verify the shp has an
> associated prj:
> n = os.path.splitext(filepath)
>  p = n[0]+'.prj'
>  if os.path.lexists(p):
>   wksht.row(row).write(9, 1)
>   srsObj = layer.GetSpatialRef()
>   srsText = srsObj.ExportToWkt()
>   wksht.row(row).write(8,srsText)
>
>  else:
>   wksht.row(row).write(9, 0)
>   wksht.row(row).write(8, 'Sin prj, no se puede determinar la proyeccion')
> I run the completed module, but it showed the same 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 59, in <module>
>     srsText = srsObj.ExportToWkt()
> AttributeError: 'NoneType' object has no attribute 'ExportToWkt'
>>>>
>
> Besides this, I really don't know how to get the srs value from a shp; do
> you know it?
> 2010/10/25 David Fawcett <david.fawcett at gmail.com>
>>
>> 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