TransWikia.com

Creating/Deleting fields (with expression) for a shapefile in a batch script

Geographic Information Systems Asked by Nokturius on August 2, 2021

I am new to GDAL/Scripting in general.

For a study project, I have to create two new fields to a shapefile/vector file using a certain expression. I also need to delete two fields.

Normally, I would just use the Field Calculator in QGIS, choose "Create a new field" and delete the two other old fields. But I have to automate the process, and as there is no command for the field calculator in GDAL; I do not know what to do. Surely, you could do something with ogr2ogr, but unfortunately I do not find any clue in the documentation there either.

How do I add the expression, which creates the new columns?

It is the following:

case 
when  "COVERTYPE" =  'AGR' then 1
when  "COVERTYPE" =  'BAR' then 2
when  "COVERTYPE" =  'CON' then 3
when  "COVERTYPE" =  'HDW' then 4
when  "COVERTYPE" =   'HEB' then 5
when  "COVERTYPE" =  'MIX' then 6
when  "COVERTYPE" =  'SHB' then 7
when  "COVERTYPE" =  'URB' then 8
when  "COVERTYPE" =  'WAT' then 9
end

Also, the output field type of the two new columns have to be "Whole number (integer 64 bit)".

How do I formulate that for the script?

I’m using QGIS 3.16.3.

UPDATE: I tried it now with the following commands:

ogr2ogr -append -sql "alter table vegetation_two_columns ADD column NVCS_CLASS_NEW INTEGER(64)" "Wildfireanalysis_Santa-CruzVegetationModified_columnsvegetation_modified.dbf" "Wildfireanalysis_Santa-CruzVegetationIrrelevant_data_deletedvegetation_two_columns.dbf"

and

ogr2ogr -update -append -overwrite -sql "alter table vegetation_two_columns ADD column NVCS_CLASS_NEW INTEGER(64)" "Wildfireanalysis_Santa-CruzVegetationModified_columnsvegetation_modified.dbf" "Wildfireanalysis_Santa-CruzVegetationIrrelevant_data_deletedvegetation_two_columns.dbf"

While executing both of the commands, thw following error message appears: "ERROR 6: CreateField : unsupported operation on a read-only datasource."

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP