I have a vector file "data.shp" containing the following fields:
ID, value1, AREA
I want to add a new column "value2" and set it as value1/AREA. Since I want to do that for many columns and files I want to use SQL.
I tried with virtual layer in QGIS with no success. I wrote in the query the following :
ALTER TABLE data
ADD value2 float;
UPDATE data
SET value2 = value1/AREA
Are alter and update commands working with virtual layer? Is there any other way?
1 Answer 1
If you want to use just SQL, do it with ogrinfo and -sql paramater.
First command must use the GDAL OGR SQL dialect https://www.gdal.org/ogr_sql.html
ogrinfo -sql "alter table my_shape add column value2 double" my_shape.shp
Second command must use the GDAL SQLite SQL dialect https://www.gdal.org/ogr_sql_sqlite.html
ogrinfo -dialect SQLite -sql "update my_shape set value2=value/(ST_Area(geometry)" my_shape.shp
-
thank you. The problem I have if I use ogrinfo is that it does not calculate the geography area but only the planimetric one (geometry). I have already made a question about this without finding any solution.Nat– Nat2019年02月22日 08:49:58 +00:00Commented Feb 22, 2019 at 8:49
-
2If you have GDAL that is compiled with SpatiaLite that is compiled with LWGeom you should be able to compute also geography area gaia-gis.it/gaia-sins/spatialite-sql-latest.html#p8. Getting such GDAL build may be difficult. When SpatiaLite 5 is released that should be easier because LWGeom is no more needed.user30184– user301842019年02月22日 09:59:16 +00:00Commented Feb 22, 2019 at 9:59
SELECT col1, col2 [AS col_name] [, FUNC(col3) AS other_col_name], geometry, ... FROM <shapefile_name> [WHERE ...];
in the Virtual Layer SQL dialog (or DB Manager).