8

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 explicitely sets the value.

Is there a way to achieve this using standard tools/setting (without python)?

  • 1
    But apart from these virtual fields being not 'real' attributes of my data source (as far as I know only stored with the project), how would I tell a virtual field to react to users edits in a way of setting it's value to, let's say 3.1415926, if the user doesn't set an attributes value? And can such be converted to real attributes? Questions over questions... Besides, 'without python' was not ment mandatory, I would be fine even if I could achieve this with 'a bit of python' ;-) (And where's @Joseph's comment?!) – Jochen Schwarze Sep 19 '16 at 10:57
  • Sorry, removed my comment: "You can use virtual fields" to provide a possible method using a plugin =) – Joseph Sep 19 '16 at 11:02
  • check this question,but is a python code. gis.stackexchange.com/questions/192105/… – Fran Raga Sep 19 '16 at 11:03
15

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.

  • 1
    Definitely of interest ;) – Joseph Sep 19 '16 at 13:27
  • 1
    Just had a look at the feature in 2.17. Exactly what I've been looking for. – Jochen Schwarze Sep 19 '16 at 14:11
6

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.
5

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

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.