Calculate new field from existing fields using expression in gdal/ogr and append to shapefile
I'm trying to calculate a new field from existing fields based on a simple expression for a set of polygons in GDAL/OGR.
The expression is basically:
new field = (field 1 / field 2) * field 3
After calculating, I need to append the new field on to the existing set of polygons and sum all the values in that new field.
This is very straightforward in ArcGIS or using ArcPy, but I can't find a solution for GDAL/OGR.
Here are the one-liners for you, please check them for field/filenames etc.
Add a new field:
ogrinfo input.shp -sql "ALTER TABLE input ADD COLUMN field3 integer(5)"
Do the math (you will need your SQLite driver to be compiled with SQLite SQL dialect):
ogrinfo input.shp -dialect SQLite -sql "UPDATE input SET field3 = field1/field2"
I don't see where you need to Join in this case, but here an example for you:
ogr2ogr -sql "select input.*, table.* from input LEFT JOIN 'table.dbf'.table ON input.id = table.IDINT" output.shp input.shp
I didn't include summing up as it seems to be too trivial.