<div>Hello list!</div>
<div> </div>
<div>I designed a python module to get all the information I need from a shapefile. I've been using GDAL to work with my script and I already collected all the information I need. My application saves the values in an excel sheet, but I haven't gotten the projection info, the code is:</div>
<div> </div>
<div>import os, time, socket<br>from xlwt import Workbook<br>from osgeo import ogr,gdal,osr<br>from dbf import *</div>
<div>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 fi.endswith(".shp"))</div>
<div>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')</div>
<div><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</div>
<div> (ruta, filename) = os.path.split(filepath) <br> wksht.row(row).write(1, filename)<br> # Get extent</div>
<div> 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</div>
<div> 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)</div>
<div> #Get host from the file</div>
<div> wksht.row(row).write(12, socket.gethostname())<br> <br> #Extarct database</div>
<div> 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</div>
<div> print layer.GetSpatialRef()<br> wksht.row(row).write(8, layer.GetSpatialRef())</div>
<div> <br> #Get file last modification<br> t = time.strftime("%m/%d/%Y %I:%M:%S %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')</div>
<div> </div>
<div>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."</div>
<div> </div>
<div>I got the next output:</div>
<div>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>
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>>>></div>
<div>Any idea?</div>
<div> </div>