<br><br>
<div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">Susana Iraiis Delgado Rodriguez</b> <span dir="ltr"><<a href="mailto:susana.delgado_s@utzmg.edu.mx">susana.delgado_s@utzmg.edu.mx</a>></span><br>
Date: 2010/10/25<br>Subject: Re: [FWTools] Get projection from shp using GDAL and Python<br>To: David Fawcett <<a href="mailto:david.fawcett@gmail.com">david.fawcett@gmail.com</a>><br><br><br>
<div>Thank you David for your help! I tried the code and got the next error:</div>
<div> </div>
<div>
<div class="im">Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on<br>win32<br>Type "help", "copyright", "credits" or "license" for more information.<br>
>>> import crawler_shp<br></div>
<div class="im">Traceback (most recent call last):<br>  File "<stdin>", line 1, in <module><br>  File "crawler_shp.py", line 85, in <module><br></div>    srsText = srsObj.ExportToWkt()<br>
AttributeError: 'NoneType' object has no attribute 'ExportToWkt'<br>>>></div>
<div> </div>
<div>Any idea?<br><br></div>
<div class="gmail_quote">2010/10/25 David Fawcett <span dir="ltr"><<a href="mailto:david.fawcett@gmail.com" target="_blank">david.fawcett@gmail.com</a>></span> 
<div>
<div></div>
<div class="h5"><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">My guess is that you are trying to write out the spatial reference<br>object, not the text representation of that object.<br>
<br>Instead of this line:<br>
<div><br> wksht.row(row).write(8, layer.GetSpatialRef())<br><br></div>Try something like:<br><br>srsObj = layer.GetSpatialRef()<br>srsText = srsObj.ExportToWkt()<br>wksht.row(row).write(8, layer.srsText)<br><br><br>This is a little verbose, but should give you the idea.<br>
<br>David.<br>
<div>
<div></div>
<div><br>On Mon, Oct 25, 2010 at 9:38 AM, Susana Iraiis Delgado Rodriguez<br><<a href="mailto:susana.delgado_s@utzmg.edu.mx" target="_blank">susana.delgado_s@utzmg.edu.mx</a>> wrote:<br>> Hello list!<br>><br>
> I designed a python module to get all the information I need from a<br>> shapefile. I've been using GDAL to work with my script and I already<br>> collected all the information I need. My application saves the values in an<br>
> excel sheet, but I haven't gotten the projection info, the code is:<br>><br>> import os, time, socket<br>> from xlwt import Workbook<br>> from osgeo import ogr,gdal,osr<br>> from dbf import *<br>> gdal.AllRegister()<br>
> file_list = []<br>> folders = None<br>>  # look in this (root) folder for files with specified extension<br>> for root, folders, files in os.walk( "C:\\" ):<br>>  file_list.extend(os.path.join(root,fi) for fi in files if<br>
> fi.endswith(".shp"))<br>> wrkbk = Workbook()<br>> wksht = wrkbk.add_sheet('shp')<br>> wksht.row(0).write(0,'ruta')<br>> wksht.row(0).write(1,'archivo')<br>> wksht.row(0).write(2,'x_min')<br>
> wksht.row(0).write(3,'x_max')<br>> wksht.row(0).write(4,'y_min')<br>> wksht.row(0).write(5,'y_max')<br>> wksht.row(0).write(6,'geometria')<br>> wksht.row(0).write(7,'num_elem')<br>
> wksht.row(0).write(8,'proyeccion')<br>> wksht.row(0).write(9,'prj')<br>> wksht.row(0).write(10,'estructura bd')<br>> wksht.row(0).write(11,'fecha_modificacion')<br>> wksht.row(0).write(12,'maquina_host')<br>
> wksht.row(0).write(13,'usuario')<br>> for row, filepath in enumerate(file_list, start=1):<br>>  #Get path and filename<br>>  wksht.row(row).write(0, filepath)<br>>  #Get filename<br>>  (ruta, filename) = os.path.split(filepath)<br>
>  wksht.row(row).write(1, filename)<br>>  # Get extent<br>>  shapeData = ogr.Open(filepath)<br>>  layer = shapeData.GetLayer()<br>>  feature = layer.GetNextFeature()<br>>  x_min, x_max, y_min, y_max = layer.GetExtent()<br>
>  x_y = layer.GetExtent()<br>>  wksht.row(row).write(2, x_y[0])<br>>  wksht.row(row).write(3, x_y[1])<br>>  wksht.row(row).write(4, x_y[2])<br>>  wksht.row(row).write(5, x_y[3])<br>>  #Get geometry type;1=Point, 2=LineString, 3=Polygon<br>
>  defn = layer.GetLayerDefn()<br>>  wksht.row(row).write(6, defn.GetGeomType())<br>>  #Get featurecount()<br>>  wksht.row(row).write(7, layer.GetFeatureCount())<br>>  #Verify prj from shp<br>>  n = os.path.splitext(filepath)<br>
>  p = n[0]+'.prj'<br>>  if os.path.lexists(p):<br>>   wksht.row(row).write(9, 1)<br>>  else:<br>>   wksht.row(row).write(9, 0)<br>>  #Get host from the file<br>>  wksht.row(row).write(12, socket.gethostname())<br>
><br>>  #Extarct database<br>>  t = n[0]+'_bd.txt'<br>>  d = n[0]+'.dbf'<br>>  if os.path.lexists(d):<br>>   a = open (t,"w+")<br>>   dbf = Dbf(d,new=False)<br>><br>>   for fldName in dbf.fieldDefs:<br>
>    a.write(fldName.name)<br>>    a.write(" || ")<br>>    a.write(fldName.typeCode)<br>>    a.write("\n")<br>>   dbf.close()<br>>   a.close()<br>>   wksht.row(row).write(10, t)<br>
>  else:<br>>   print "El archivo " +n[0]+".shp" " no tiene dbf"<br>>   wksht.row(row).write(10, "Sin bd")<br>><br>> #Get projection, this is the part that fails<br>>  print layer.GetSpatialRef()<br>
>  wksht.row(row).write(8, layer.GetSpatialRef())<br>><br>>  #Get file last modification<br>>  t = time.strftime("%m/%d/%Y %I:%M:%S<br>> %p",time.localtime(os.path.getmtime(filepath)))<br>>  wksht.row(row).write(11, t)<br>
>  #Get username<br>>  wksht.row(row).write(13,os.environ.get("USERNAME"))<br>><br>> wrkbk.save('shp.xls')<br>><br>> SEARCH_PATH = os.getcwd()<br>> TARGET_FILE = os.path.realpath('shp.xls')<br>
> print "Buscando en", SEARCH_PATH, "and writing to", TARGET_FILE<br>> print "Encontrando archivos..."<br>> print "Escribiendo archivo de Excel..."<br>> print "Listo."<br>
><br>> I got the next output:<br>> Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)]<br>> on<br>> win32<br>> Type "help", "copyright", "credits" or "license" for more information.<br>
>>>> import crawler_shp<br>> None<br>> None<br>> PROJCS["WGS_1984_UTM_Zone_13N",<br>>     GEOGCS["GCS_WGS_1984",<br>>         DATUM["WGS_1984",<br>>             SPHEROID["WGS_1984",6378137,298.257223563]],<br>
>         PRIMEM["Greenwich",0],<br>>         UNIT["Degree",0.017453292519943295]],<br>>     PROJECTION["Transverse_Mercator"],<br>>     PARAMETER["latitude_of_origin",0],<br>
>     PARAMETER["central_meridian",-105],<br>>     PARAMETER["scale_factor",0.9996],<br>>     PARAMETER["false_easting",500000],<br>>     PARAMETER["false_northing",0],<br>
>     UNIT["Meter",1]]<br>> Traceback (most recent call last):<br>>   File "<stdin>", line 1, in <module><br>>   File "crawler_shp.py", line 85, in <module><br>>     wksht.row(row).write(8, layer.GetSpatialRef())<br>
>   File "C:\Python26\lib\site-packages\xlwt\Row.py", line 248, in write<br>>     raise Exception("Unexpected data type %r" % type(label))<br>> Exception: Unexpected data type <class 'osgeo.osr.SpatialReference'><br>
>>>><br>> Any idea?<br>><br></div></div>> _______________________________________________<br>> FWTools mailing list<br>> <a href="mailto:FWTools@lists.maptools.org" target="_blank">FWTools@lists.maptools.org</a><br>
> <a href="http://lists.maptools.org/mailman/listinfo/fwtools" target="_blank">http://lists.maptools.org/mailman/listinfo/fwtools</a><br>> <a href="http://fwtools.maptools.org/" target="_blank">http://fwtools.maptools.org/</a><br>
><br>><br></blockquote></div></div></div><br></div><br>