[FWTools] AttributeError: 'NoneType' object has no attribute 'GetGeometryRef'
Susana Iraiis Delgado Rodriguez
susana.delgado_s at utzmg.edu.mx
Thu Oct 14 11:27:41 EST 2010
Hello members:
I start developing a python module to generate a file which will contain
some attributes from a shapefile. My code is:
import os
from xlwt import Workbook
from osgeo import ogr
import time
#This is for a kind of crawler into my system:
file_list = []
folders = None
# look in this (root) folder for files with specified extension
for root, folders, files in os.walk( "C:\\" ):
file_list.extend(os.path.join(root,fi) for fi in files if
fi.endswith(".shp"))
#Create the excel file
wrkbk = Workbook()
#Add named parameter to allow overwriting cells: cell_overwrite_ok=True
wksht = wrkbk.add_sheet('shp')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'archivo')
wksht.row(0).write(2,'x_min')
wksht.row(0).write(3,'y_min')
wksht.row(0).write(4,'x_max')
wksht.row(0).write(5,'y_max')
wksht.row(0).write(6,'geometria')
wksht.row(0).write(7,'num_elem')
wksht.row(0).write(8,'prj')
wksht.row(0).write(9,'estrutura bd')
wksht.row(0).write(10,'extent')
wksht.row(0).write(11,'fecha_modificacion')
wksht.row(0).write(12,'maquina_host')
Start fiiling the cells with data
for row, filepath in enumerate(file_list, start=1):
#Fill cells with the path
wksht.row(row).write(0, filepath)
#Do the featurecount()
shapeData = ogr.Open(filepath)
layer = shapeData.GetLayer()
feature = layer.GetNextFeature()
wksht.row(row).write(7, layer.GetFeatureCount())
#Here is the problem, when I want to get the geometry name,
geometry = feature.GetGeometryRef()
geometryType = geometry.GetGeometryName()
wksht.row(row).write(6, geometryType)
#
wrkbk.save('shp.xls')
When the module gets to the geometryName part, ti throws me the 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 excel_craw
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "excel_craw.py", line 45, in <module>
geometry = feature.GetGeometryRef()
AttributeError: 'NoneType' object has no attribute 'GetGeometryRef'
>>>
I don't undestand the reason of the error, because I run the next lines in
IDLE Python an this is what I got:
>>> import os
>>> from osgeo import ogr
>>> shapeData = ogr.Open('tapalpa_05_plani_point.shp')
>>> layer = shapeData.GetLayer()
>>> feature = layer.GetNextFeature()
>>> geometry = feature.GetGeometryRef()
>>> geometryType = geometry.GetGeometryName()
>>> print geometryType
POINT
Why is my other code wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/fwtools/attachments/20101014/4e30ffa8/attachment.htm
More information about the FWTools
mailing list