[gdal-dev] Adding field to Shapefile via Python
Mike Toews
mwtoews at gmail.com
Fri Mar 18 01:31:00 EDT 2011
In a Python script, I am updating an exiting shapefile with data by
adding a column. However, my changes don't seem to be saved. Here is
what I have:
from osgeo import ogr
obs_file = 'myfile.shp'
source = ogr.Open(obs_file, 1)
layer = source.GetLayer()
layer_defn = layer.GetLayerDefn()
new_field = ogr.FieldDefn('MYFLD', ogr.OFTInteger)
layer_defn.AddFieldDefn(new_field)
source = None
However, when I check the resulting file, I do not see the new field.
I have also tried inserting the following (before "source=None")
feature = layer.GetNextFeature()
feature.GetField('EXIST') # an existing float field with data
feature.SetField('EXIST', 111.1) # works
feature.SetField('MYFLD', 123) # does nothing
layer.SetFeature(feature)
Which successfully updates the data in the existing field, but not the
added field.
Where did I go wrong? Thanks in advance.
-Mike
More information about the gdal-dev
mailing list