[OSGeo-Discuss] methods for programatically adding fields to shapefiles
tech_dev at wildintellect.com
Wed Oct 29 16:56:11 PDT 2008
Tyler Erickson wrote:
> I am interested in approaches for adding a populated field to a shapefile
> (for example, adding a new field named 'source_url' with the value
> 'http://somewebsite.com'). I would like to do this for several thousand
> At first I thought that I might be able to accomplish it using ogr2org with
> a sql clause, such as:
> ogr2ogr -sql "select *, 'http://somewebsite.com' as source_url from infile"
> outfile.shp infile.shp
> but that didn't work since ogr2ogr supports a limited set of SQL, described
> Any ideas on how to accomplish this? (I would prefer suggestions that can be
> scripted with python.)
> - Tyler
Open a data connection the dbf with python, add a field to the
properties of the table and populate the field.
There are several ways to connect to a dbf depending on the OS, you
could use an ODBC connector, or use some other type of dbf driver.
I haven't looked closely but there might be a dbf driver in the ogr
python bindings already.
I think my quick fix was to use python windows tools to wrap the dbf com
object as a python object I could call, but that was a quick a dirty
solution on windows.
The other thing I've done is to create a blank shapefile with the exact
same scheme + one field. Do an ogr2ogr -f "ESRI Shapefile" -a (append)
of the blank shapefile and your existing one, and now you have a new field.
More information about the Discuss