PyQGIS – Activating Temporal Properties of Vector Layer Using PyQGIS

pyqgistemporal-controller

I'm trying to activate the dynamic temporal control on a vector layer with PyQGIS.

I have a function creating a line vector layer from a CSV, I am now trying either to set them as temporal (not temporary) ones during the creation of the layer, or once they are created. Also, I would like to set the configuration, by choosing the Unique Field for date and hour or Separate Field for starting and ending time

Here is my script which creates the layer I need :

class processing(object):
    def __init__(self , layer_name, data, collumn): #data = pandas dataframe
    start_time = time.time()

    self.layer_name = layer_name
    self.frame = data
    df = self.frame[self.frame['id'] == collumn]
    self.data = df.reset_index(drop=True)

    self.ListPointsQGS= [] #list of qgs points to be used later


    for index, row in self.data.iterrows():
        self.ListPointsQGS.append(QgsPoint(float(row['y']),float(row['x'])
        
    self.layer = iface.addVectorLayer("LineString?crs=epsg:4326&field=id:integer&index=yes",   layer_name ,"memory")


   
    self.feature = QgsFeature()      

    self.layer.startEditing()
    self.pr = self.layer.dataProvider()

    self.pr.addAttributes([  
                                QgsField("x",  QVariant.Double),
                                QgsField("y", QVariant.Double),
                                QgsField("date", QVariant.DateTime)])

This creates my layer, to be filled later with data.

I have tried using the QgsVectorLayerTemporalProperties class and the QgsTemporalProperty but I can't understand how to use them from the documentation.
I saw many different classes to create a vector layer and therefore replace the iface.addVectorLayer , some of which allowing building options, but none allowing to set it as a temporal layer.

At the end, my goal is to set up a user interface using the temporal control widget (which will use a column full of dates) in order to allow a user to see vector lines appearing one by one, without having to go in the proprities of all of them to check the box and turn them into temporal layer.

How could I achieve this ?

Best Answer

I have found the answer :

To access the temporal properties of a layer, use the temporalProperties() method of the QgsVectorLayer class. From here, use the setIsActive() method to active temporal properties, setStartField() to choose the field where your dates are (takes a string input), setAccumulateFeatures(Bool) if you like, and setMode() to choose between the available modes : same field for start and end, diferrents fields etc... this method takes a VectorTemporalMode() available here. To use it, just pass an int between 0 and 5 to choose the mode from the list given there. Note that you have to type qgis.core.Qgis.VectorTemporalMode(int) to use it.

Bonus : How to connect the temporal controller widget to a map canvas if you want to create a plugin or a script that opens a new QT window :

canvas = QgsMapCanvas() 
controler = QgsTemporalControllerWidget(the qtobject where you will put it) 
controler_to_be_used_by_canvas =  controler.temporalController()
 
canvas.setTemporalController(controler_to_be_used_by_canvas)

And here you can make a script that opens a new window in QGIS using PyQT5 from QtDesigner, set temporal layers and have a user friendly interface that allows to click and see a route being drawn on the canvas