5

I am looking for a way to use the Processing modeler to add a date field to an output dataset in QGIS 2.6.1

I have tried using the field calculator algorithm without success. (Also the advanced python field calculator without success).

I have a vector layer and I add a field calculator algorithm and set the field type to Date, field length to 30, precision to 0, create new field and set the formula to $now I have also tried datetime.date.today()

The error I get is;

Prepare algorithm 0: Field calculator Running Field calculator [1/1] Parameters: INPUT_LAYER =C:/Users/lee/Desktop/TEMP/test.shp, FIELD_NAME =loadDate, FIELD_TYPE =3, FIELD_LENGTH =30, FIELD_PRECISION =0, NEW_FIELD =True, FORMULA =$now Failed Error executing algorithm 0 underlying C/C++ object has been deleted

(I am looking to automate the process of adding a 'loadDate' field to features so that users know when a feature was loaded into a postgis table. I want to repeat it over a number of layers, and do it on a regular basis hence the use of the modeler).

2 Answers 2

6

I have found the answer I need by using the advanced python field calculator.

I set the following parameters;

Field type:String

Field length: 10

Global Expression: import time

Formula: value = time.strftime("%Y-%m-%d")

This gave me a new field with today's date added at the end.

2
  • Awesome! Glad you got it to work.
    – Joseph
    Jan 16, 2015 at 11:44
  • how to add it with python console Jan 19, 2022 at 18:39
3

I think there's a bug somewhere as I also use QGIS 2.6.1. I tried a few times to do this in Model Builder using the $now command and received an empty column. I then exported the model as a python script, made some edits and ran it. Again I received NULL values. Then suddenly, when I ran the model again, it works... I cannot explain how but it's weird. I've included a screenshot of the options I used in the Model Builder:

Model Builder

Here's the script I used which you can add via Processing Toolbox > Scripts > Tools > Create new script:

##Date=name
##Layer=vector
##Result=output vector
processing.runalg("qgis:fieldcalculator", Layer, "loadDate", 3, 10, 0, True, '$now', Result)

Hope this helps somehow!

1
  • $now is working in QGIS 3.2
    – BERA
    Nov 4, 2021 at 9:17

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.