2

I'm looking for solutions for auto-fill attribute table fields of spatial layer on QGIS from updates in geometry.

All layers were created as polygon shapefiles. Attribute table fields are dependent on feature geometry. For example, there are fields that return the latitude and longitude of the centroid of polygons, and other fields that calculate how far the geometry are in North-South and East-West directions from a reference point.

Currently, I need to run a sequence of expressions to recalculate those values.

I would like to know if there is a solution for these expressions run in an automated way, as I need to improve the means of maintaining the system so other people can manage the data.

If necessary, I can change the format of the files for geopackage or other formats. I read something about triggering in SQL, but have a great difficulty with the syntax to implement.

Below are the expressions of the attribute table.

LATITUDE

ROUND(Y( centroid( $geometry)),0)

LONGITUDE
ROUND(x( centroid( $geometry)),0)

COD_LAT

case
when abs("LATITUDE" - 7535757) <10 then '0000' || abs("LATITUDE" - 7535757)
when abs("LATITUDE" - 7535757) <100  then '000' || abs("LATITUDE" - 7535757)
when abs("LATITUDE" - 7535757) <1000  then '00' || abs("LATITUDE" - 7535757)
when abs("LATITUDE" - 7535757) <10000 then '0' || abs("LATITUDE" - 7535757)
end

COD_LON

case
when abs("LONGITUDE" - 513632) <10 then '0000' || abs("LONGITUDE" - 513632)
when abs("LONGITUDE" - 513632) <100  then '000' || abs("LONGITUDE" - 513632)
when abs("LONGITUDE" - 513632) <1000  then '00' || abs("LONGITUDE" - 513632)
when abs("LONGITUDE" - 513632) <10000 then '0' || abs("LONGITUDE" - 513632)
end

COD_LATD

case 
when "LATITUDE" >= 7535757 then 'N'
when "LATITUDE" < 7535757 then 'S'
end

COD_LOND


case 
when "LONGITUDE" >= 513632 then 'E'
when "LONGITUDE" < 513632 then 'W'
end

COD_LOTE

CONCAT(COD_LAT,COD_LATD,COD_LOND,COD_LON)
1

1 Answer 1

9

Set a default value in the edit widgets. Check the box for "Apply default value on update."

enter image description here

With this setting, the attribute will be updated whenever a feature's attribute or geometry is changed. The source data is updated as soon as you save the layer edits.


Note: For your use case, a default field value is more useful than a virtual field. Virtual fields exist only in the project. They aren't saved as permanent attributes of the shapefile. So if you used a virtual field, you would still have to manually update the permanent attributes.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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