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

Susana Iraiis Delgado Rodriguez susana.delgado_s at utzmg.edu.mx
Mon Oct 25 12:57:04 EST 2010


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?
> >>
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/fwtools/attachments/20101025/633a93ca/attachment.htm 


More information about the FWTools mailing list