[gdal-dev] Adding field to Shapefile via Python
Chaitanya kumar CH
chaitanya.ch at gmail.com
Fri Mar 18 01:45:34 EDT 2011
A new field cannot be added to a feature definition while there are any
features based on that feature definition.
See the docs for OGRFeatureDefn::AddFieldDefn()
On Fri, Mar 18, 2011 at 11:01 AM, Mike Toews <mwtoews at gmail.com> wrote:
> 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)
> 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
> Which successfully updates the data in the existing field, but not the
> added field.
> Where did I go wrong? Thanks in advance.
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
Chaitanya kumar CH.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the gdal-dev