In another question (Loop an operation in all features) I asked about calculating the time difference between features, I was told that I could use this code (this code is from BERA):
layer = QgsProject.instance().mapLayersByName("pointswithtime")[0]
timefield = "gpstime"
#Add the tempo field
pr = layer.dataProvider()
fieldname = "tempo"
pr.addAttributes([QgsField(fieldname, QVariant.Int)])
layer.updateFields()
ix = layer.fields().indexFromName(fieldname)
#List all features and sort by timestamp. You might want to sort by something else?
features = [[f.id(), datetime.datetime.strptime(f[timefield], "%d/%m/%Y %H:%M:%S")] for f in layer.getFeatures()]
features.sort(key=lambda x: x[1])
ids = [x[0] for x in features] #Extract the ids
timestamps = [x[1] for x in features] #And timestamps
#Calculate diff between row2-row1, row3-row2, ... and insert a starting None/NULL value
deltas = [None]+[(b-a).seconds for a,b in zip(timestamps, timestamps[1:])]
attributemap = {id : {ix: d} for id, d in zip(ids, deltas)} #A dict of {feature id : {fieldindex to update: value}, ..
pr.changeAttributeValues(attributemap) #Update the values
however, I would like to calculate the time in a filtered layer (that is, the id of the first feature is not 1) and place it in another layer that does not match the ids with the previous one, despite having the same number of features -1, I think I could do this using as a basis to match features not the ID but the feature number, how could I do that?
the layer I want to calculate the time on:
the layer where I want to place the field with the calculated times: