I am learning pyQGIS and written a script that may solve this issue.
Start with the original dataset but the timestamps converted to integers (Unix time). You can do it with the epoch()
expression in the Field Calculator.
This is my Attributes Table to test:
To create subgroups of the group numbered 2
, ordered by the timestamp
values, open the Python Console, show the Editor, and paste the following code:
#Layer name
layername = 'test points'
#Field name to ordering
orderfield = 'timestamp'
#Field name to group
groupfield = 'group'
#Field value to group
groupvalue = 2
#Unique values field
uvf = 'fid'
#get the layer
lyr = QgsProject.instance().mapLayersByName(layername)[0]
lyrcrs = lyr.sourceCrs().authid()
#Ordered Request
request = QgsFeatureRequest().addOrderBy(orderfield)
#Features Iterator
iterator = lyr.getFeatures(request)
#Features List
feats = [i for i in iterator]
#New Attributes Logic
groupscounter = 1
hadchanged = 0
for feat in feats:
if feat[groupfield] == groupvalue:
if hadchanged == 1:
groupscounter += 1
hadchanged = 0
subgroup = groupscounter
else:
hadchanged = 1
subgroup = 0
feat.setAttributes([feat[uvf],feat[orderfield],subgroup])
#Create a memory layer
uri = "Point?crs=" + lyrcrs + \
"&field=" + uvf + ":integer" + \
"&field=" + orderfield + ":integer" + \
"&field=subgroup:integer" + \
"&index=yes"
mem_layer = QgsVectorLayer(uri,'reordered','memory')
prov = mem_layer.dataProvider()
prov.addFeatures(feats)
QgsProject.instance().addMapLayer(mem_layer)
What are we doing is request the features of the test points
layer, ordered by timestamp
values.
In logic we are creating subgroups every time a group
value other than 2
appears.
The output is a new memory layer named reordered
, with the same features as the original layer, its fid
and tiemstamp
value and the new subgroups. All values not equal to 2
go to subgroup 0
:
Then, you can run the Points to Path tool against that layer.
Any advice about the code will be welcome.