Wednesday 21 June 2017

QGIS - adding timestamps to spatialite data during data collection


I am trying to do field data collection using QGIS. One of the features that I regard as necessary for data management is timestamps. For example, a timestamp can indicate that an inspection record has been updated.


I have figured out how to add triggers using Spatialite to add a timestamp on update, however the timestamp is added when the edited records are saved and not when the identify dialog box is closed. This means that timestamps can be out by many minutes. One solution would be to force the updated record to be saved by using a custom form with an OK button (Can do the form with Qt-Designer but don't know how to add the save command to the button yet.) However my preferred solution would be to take a timestamp for new records, when the point or line is created. The reason for this is if I am surveying a road, I can stand on the road, collect the point and them move to the side to complete data collection.


I have been unable to work out how to get Spatialite to run a trigger that puts in the start of feature editing. Perhaps this is also something that needs to be done by code in a custom form. Is there an On-dirty event or signal in QGIS that indicates when someone starts editing a record and that can be used to capture the time? Could I also ask what the Python code (and what to hook it to) that would put such a timestamp in the timestamp field.


AndrewM



Answer



Note: This will only work in 1.9 (2.0) due to a bug in 1.8 not passing the full feature correctly


Take this code:



from PyQt4.QtCore import QDateTime

def onopen(dialog, layer, feature):
if feature.id() < 0:
# This is a new record because it
# doesn't have a vaild id yet
feature['createdon'] = QDateTime.currentDateTime()

Save it somewhere QGIS Python can find it, normally in the same folder as the project is good. Lets call it datestamper.py


In Layer Properties -> Fields set the Python Init Function to datestamper.onopen



enter image description here


Now when ever the form is opened, auto generated or custom, that function will be called and the 'createdon' field will be set to the current date time.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...