I am bastardizing Matt Perry's text2shp.py and running into an error. <br><br>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.
<br><br>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]).
<br><br>D:\datascripts>python csv2shp.py<br><br>['stn_type', 'stn_name', 'X', 'Y', 'col_meth', 'col_date', 'org_name', 'org_type<br>', 'gnd_elev', 'uniq_id', 'tos_elev', 'bos_elev', 'comments']<br><br>[4, 4, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4]
<br><br>0 Monitoring Well<br><br>Traceback (most recent call last):<br> File "csv2shp.py", line 129, in ?<br> f.setField(colNum, cols[colNum])<br> File "C:\PROGRA~1\FWTOOL~1.5\pymod\ogr.py", line 755, in __getattr__
<br> raise AttributeError, name<br>AttributeError: setField<br><br>__________________________________________-<br>here is the relevant part of the script <br>__________________________________________<br><br>ds = driver.CreateDataSource
(outputFile)<br>layer = ds.CreateLayer(outputFile, geom_type=ogr.wkbPoint)<br><br>print colNames<br>print colTypes<br>#create fields<br>for j in range(len(colNames)):<br> fd = ogr.FieldDefn(colNames[j].strip(), colTypes[j])
<br> fd.SetWidth(maxWidth[j] + 12)<br> fd.SetPrecision(precision[j]+1)<br> layer.CreateField(fd)<br><br>#second pass through data file<br>myFile = open(inputFile, 'r')<br><br>#skip the header row<br>line = myFile.readline
()<br>#read the first data row <br>line = myFile.readline()<br><br>while line:<br> cleanline = line.replace('\n','')<br> cols = cleanline.split(delimiter)<br><br> #create the feature<br> f= ogr.Feature
(feature_def=layer.GetLayerDefn())<br><br> # Fill in the attribute fields<br> for colNum in range(len(cols)):<br> print colNum, cols[colNum]<br> f.setField(colNum, cols[colNum]) # this is line 129 where the error is being raised
<br><br> # if there is a coordinate, use it to create a geometry<br> if cols[xColNum] and cols[yColNum]:<br> x = float(cols[xColNum])<br> y = float(cols[yColNum])<br><br> #create point feature<br>
wkt = 'POINT(%f %f)' % (x,y)<br> g = ogr.CreateGeometryFromWkt(wkt)<br><br> f.SetGeometryDirectly(g)<br> layer.CreateFeature(f)<br> f.Destroy()<br><br>#destroying the data source closes the output shapefile
<br>ds.Destroy()<br>