1

Made this expression and put it in the defaults value box.:

CASE
---cardinal
WHEN ("CATCAM" = '1')THEN'2,6' 
WHEN ("CATCAM" = '2')THEN'2,6,2'
WHEN ("CATCAM" = '3')THEN'6,2'
WHEN ("CATCAM" = '4')THEN'6,2,6'
---lateral
WHEN ("MARSYS" = '1' AND "CATLAM" = '1')THEN '3'
WHEN ("MARSYS" = '11' AND "CATLAM" = '1')THEN '3'
WHEN ("MARSYS" = '2' AND "CATLAM" = '2')THEN '3'
WHEN ("MARSYS" = '1' AND "CATLAM" = '2')THEN '4'
WHEN ("MARSYS" = '11' AND "CATLAM" = '2')THEN '4'
WHEN ("MARSYS" = '2' AND "CATLAM" = '1')THEN '4'
WHEN ("MARSYS"='11' AND "CATLAM"='15')THEN '3,1,3,1'
WHEN ("MARSYS"='11' AND "CATLAM"='16')THEN '4,1,4,1'
WHEN ("MARSYS"='11' AND "CATLAM"='3')THEN '3,4'
WHEN ("MARSYS"='11' AND "CATLAM"='4')THEN '4,3'
WHEN ("MARSYS"='11' AND "CATLAM"='8')THEN '3,4,3,'
WHEN ("MARSYS"='1' AND "CATLAM"='3')THEN '3,4,3'
WHEN ("MARSYS"='2' AND "CATLAM"='4')THEN '3,4,3'
WHEN ("MARSYS"='1' AND "CATLAM"='4')THEN '4,3,4'
WHEN ("MARSYS"='2' AND "CATLAM"='3')THEN '4,3,4'
---special
WHEN ("ATONTYPE"='3')THEN '6'
---safewater
WHEN ("ATONTYPE"='4')THEN '3,1,3,1'
---isolated_danger
WHEN ("ATONTYPE"='5')THEN '2,3'
---new_wreck
WHEN ("ATONTYPE"='6')THEN '5,6'
END

The preview under the expression gives the right value, but when I make a new feature the value doesn't write to the designated column, i selected the apply on update. (field stays empty) What am I doing wrong?

enter image description here

I use QGIS 3.6, the layer is stored in a Geopackage, the field is a text field with enough room to write the value.

4

The default value is calculated before you enter the values for the other fields. Since the default value is calculated while those field values are NULL, none of the conditions are true. If you add an ELSE to the end of your CASE WHEN statement, you'll see that the field is always filled with the else value. The default value is not re-calculated when you enter attribute values immediately after creating a feature.

enter image description here

You have a couple of options:

  1. One workaround is to check the box for "Apply default value on update".

    enter image description here

    The default value will be re-calculated whenever you change one of the other field values. When you create a new feature, don't add any attributes. Then select the feature and enter its field values. The default value will then be calculated using the field values you entered. You might find it helpful to enable the option "Suppress attribute form pop-up after feature creation"

    enter image description here

  2. Another workaround is to use a virtual field instead of a regular field with a default value. Create a new field with the Field Calculator, check the box for "Virtual Field" and use the same expression you were using for the default field.

    enter image description here

    A virtual field is always kept up-to-date. The downside of a virtual field is that it's part of the QGIS project file, so it's not saved in the geopackage file. You can copy the virtual field value into a regular field with the Field Calculator if you want it saved as part of the geopackage. You'll have to do this every time you update the layer. (You can try putting the virtual field name in a default field value and set the default field to "Apply default value on update." I'm not sure what happens if do this. It's probably worth trying, but be sure to save everything before you try this in case it causes a crash.)

  • I chose the update function, is there a way to update the features by a key or command? Instead of having to change the geometry? – CaptainAhab Mar 26 at 18:21
  • I don't know, but have a look at the Keyboard Shortcut controls in the Settings menu. If that's not a built-in option it can probably be done by creating an action in the Layer Properties, but that requires Python programming which is outside my area of expertise. – csk Mar 26 at 20:41
  • Also, I think it should update if you just change one of the attributes, without needing to change the geometry. That's why I suggested inputting the attributes as a separate step from creating the feature. – csk Mar 26 at 20:44

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.