15

In QGIS 2.14.6 I would like to define a default value for an attribute in a shapefile (or spatialite layer as well) which is applied if the user doesn't explicitly sets the value.

Is there a way to achieve this using standard tools/setting (i.e. without Python)?

0

4 Answers 4

26

QGIS 3

Select the layer in the layer panel and left-click. Select properties. Select the "Attributes Form" as shown below. Select the fields from the "Available Widgets" list as show. Provide an expression for the default value in the "Defaults" dialog. If the "Apply default value on update" is checked, the value will be adjusted every time the feature's geometry or another attribute is changed. This makes things like a "last_modified" timestamp or an updated "area" possible.

Set Field Default Value QGIS 3.0.3


QGIS 2.18

Starting from QGIS 2.18 this is possible without any extra tools.

Just go to the field properties click on the button in the field you would like to assign a default value and enter an expression. It can also take the geometry as input.

enter image description here

Sidenote: I know that the original question targets QGIS 2.14, but I think this is still of interest to future readers of this question.

2
  • 1
    Definitely of interest ;)
    – Joseph
    Commented Sep 19, 2016 at 13:27
  • 1
    Just had a look at the feature in 2.17. Exactly what I've been looking for. Commented Sep 19, 2016 at 14:11
8

There is the AutoFields plugin which allows you to create (or modify) attribute fields and update them automatically with a custom expression.

It has also been tested with various formats and autofields are not considered to be virtual fields (quoted from the plugin's homepage):

Do AutoFields work with ‘X’ vector format?

We’ve already tested AutoFields on PostgreSQL/PostGIS, SpatiaLite, Shapefiles, WFS-T (Transactional Web Feature Service) and Memory layers. Please let me know if you use AutoFields on other vector formats.

Are AutoFields like Virtual Fields?

Nope. Unlike Virtual Fields, AutoFields:

  • Store calculated values in the data;
  • Are independent of QGIS projects, i.e., you can switch between QGIS projects and AutoFields will still work;
  • Only work on editable vector layers.
8

Since I would like to use these attributes values mentioned above to control some aspects of symbology (symbol size, opacity), some further research revealed that with data defined properties there is another great (and flexible) way to achieve this. A (generic) expression

if("attribute_name" is null, @default_value, "attribute_name")

where @default_value is defined as a projects variable, returns a default that can be defined by the user when no value is set for the attribute attribute_name and it's value else.

Define variables in project properties:

enter image description here

Set up an expression with data defined properties (red color with user defined opacity in this case):

enter image description here

0

A workaround allowing to create a new field and initialize it with a constant value for all rows in a single operation is to add a string field in the field calculator, and use concat('TEXT_TO_ADD') to initialize it with a constant :

enter image description here

Your Answer

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

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