[gdal-dev] Is it possible to add new fields to a shapefile and populate then?

jratike80 jukka.rahkonen at maanmittauslaitos.fi
Mon Apr 3 21:37:04 PDT 2017


Stephen Woodbridge wrote
> Hi All,
> 
> I have a shapefile(s) and I want to read the features, generate some 
> metrics about each feature and then add them to the that feature. I'm 
> using python and one obvious way to to in effect create a new shapefile 
> with the columns I need to add, then copy the existing shapefile to the 
> new one adding the additional metrics.
> 
> I wondering if I can add the new columns to the shapefile, then do a 
> read and update.
> 
> Pros and cons to these approaches? and how to do the later one if
> possible?
> 
> I looked at the python gdal/ogr cookbook and didn't this specific 
> example. But I thinking of something along the lines of:
> 
> 1. open the dataset for read/write
> 2. layer.CreateField() to add the new fields
> 3. loop through the features in the layer
> 4.    and use feature.SetField()
> 5.    and feature = None to commit the changes
> 
> will this work?
> 
> Thanks,
>    -Steve

Hi,

You can demonstrate that it is possible with ogrinfo:

ogrinfo  -sql "alter table test add column foo integer" test.shp
ogrinfo  -dialect sqlite -sql "update test set foo=2" test.shp


-Jukka Rahkonen-






--
View this message in context: http://osgeo-org.1560.x6.nabble.com/gdal-dev-Is-it-possible-to-add-new-fields-to-a-shapefile-and-populate-then-tp5315568p5315579.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list