[FWTools] python ogr - setField attribute error

Bryan Keith bryan at geomega.com
Mon Oct 23 12:06:58 EDT 2006


David,

Maybe this is a typo is your e-mail, but I think the s is setField 
should be S:

f.SetField(colNum, cols[colNum])

Bryan

David Fawcett wrote:
> I am bastardizing Matt Perry's text2shp.py and running into an error.
> 
> I am taking values from a text file and building a shapefile.  The error
> occurs when I try to set the value of first field of a feature.  I am not
> sure what would cause this error.
> 
> Below is the shell output.  The first list printed is the column names, the
> second list is the field types. The last line before the error is the column
> number and the value to be inserted into that field (print colNum,
> cols[colNum]).
> 
> D:\datascripts>python csv2shp.py
> 
> ['stn_type', 'stn_name', 'X', 'Y', 'col_meth', 'col_date', 'org_name',
> 'org_type
> ', 'gnd_elev', 'uniq_id', 'tos_elev', 'bos_elev', 'comments']
> 
> [4, 4, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4]
> 
> 0 Monitoring Well
> 
> Traceback (most recent call last):
>   File "csv2shp.py", line 129, in ?
>     f.setField(colNum, cols[colNum])
>   File "C:\PROGRA~1\FWTOOL~1.5\pymod\ogr.py", line 755, in __getattr__
>     raise AttributeError, name
> AttributeError: setField
> 
> __________________________________________-
> here is the relevant part of the script
> __________________________________________
> 
> ds = driver.CreateDataSource(outputFile)
> layer = ds.CreateLayer(outputFile, geom_type=ogr.wkbPoint)
> 
> print colNames
> print colTypes
> #create fields
> for j in range(len(colNames)):
>     fd = ogr.FieldDefn(colNames[j].strip(), colTypes[j])
>     fd.SetWidth(maxWidth[j] + 12)
>     fd.SetPrecision(precision[j]+1)
>     layer.CreateField(fd)
> 
> #second pass through data file
> myFile = open(inputFile, 'r')
> 
> #skip the header row
> line = myFile.readline()
> #read the first data row
> line = myFile.readline()
> 
> while line:
>     cleanline = line.replace('\n','')
>     cols = cleanline.split(delimiter)
> 
>     #create the feature
>     f= ogr.Feature(feature_def=layer.GetLayerDefn())
> 
>     # Fill in the attribute fields
>     for colNum in range(len(cols)):
>         print colNum, cols[colNum]
>         f.setField(colNum, cols[colNum])         # this is line 129 where
> the error is being raised
> 
>     # if there is a coordinate, use it to create a geometry
>     if cols[xColNum] and cols[yColNum]:
>         x = float(cols[xColNum])
>         y = float(cols[yColNum])
> 
>         #create point feature
>         wkt = 'POINT(%f %f)' % (x,y)
>         g = ogr.CreateGeometryFromWkt(wkt)
> 
>         f.SetGeometryDirectly(g)
>         layer.CreateFeature(f)
>     f.Destroy()
> 
> #destroying the data source closes the output shapefile
> ds.Destroy()
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> FWTools mailing list
> FWTools at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/fwtools
> http://fwtools.maptools.org/


More information about the FWTools mailing list