Auto populate fields with different values from another field in QGIS?

by webg   Last Updated October 09, 2019 18:22 PM - source

Is it possible to populate a number of fields in a shapefile in QGIS with different values based on the values of one other field? Essentially what I want to be able to do is to populate fields to allow export of features from a shapefile as a GeoJSON into a separate database with a number of fields populated to a certain format. So for example I enter a text value, let’s say “AA_BB” via a value map drop down in the attribute table and I want another text field to automatically enter “XXX” and two numeric fields to enter a value eg “4” and “22”. I know it’s weird but it’s to try to relate two completely different styling formats between two completely different systems. I was thinking Value Relations would maybe do it but I can’t figure out how to configure that. Anyone any suggestions? I don't know how to script.

Answers 1

Use the Field Calculator to create or update the new field.

Write an expression that achieves what you want. Using your example, write an expression like this:

 when "field1" = 'AA_BB' then 'XXX'
 when "field1" = 'AA_CC' then 'XXY'
 when "field1" = 'BB_CC' then 'XYZ'

 else 'other'

Add as many when [condition] then [result] lines as needed to cover all the possible conditions. The else line is optional - use this to cover any conditions you didn't cover.

If you want the field to auto-populate every time you add a new feature, it must be a virtual field. Virtual fields are saved in the QGIS project file. They're not saved as part of the source data. When you export the file, be sure to choose the export option to "replace raw field values with displayed values".

October 09, 2019 18:20 PM

Related Questions

Get all Attributes and Objects in View

Updated June 07, 2019 10:22 AM