3

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.

6

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.

  • 1
    This solution worked nicely! I was also able to perform the summation with the following: ogrinfo input.shp -dialect SQLite -sql "SELECT SUM(field) from input – geoeye Oct 27 '15 at 15:19
  • Is there a way to write out the sum to a .txt file or another format? – geoeye Oct 27 '15 at 15:41
  • Simply adding > out.txt to the end of my command allows for the result to be written to a text file – geoeye Oct 28 '15 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.