Skip to content

Commit f128511

Browse files
authored
Split stop duration field into two inputs, fixes #36
1 parent 48ec82a commit f128511

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

qgis_processing/splitTrajectoriesAlgorithm.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,36 @@ def groupId(self):
2727

2828
class ObservationGapSplitterAlgorithm(SplitTrajectoriesAlgorithm):
2929
TIME_GAP = "TIME_GAP"
30+
TIME_DELTA_UNITS = "TIME_DELTA_UNITS"
31+
TIME_DELTA_UNITS_OPTIONS = [
32+
"Weeks",
33+
"Days",
34+
"Hours",
35+
"Minutes",
36+
"Seconds",
37+
"Milliseconds"
38+
]
3039

3140
def __init__(self):
3241
super().__init__()
3342

3443
def initAlgorithm(self, config=None):
3544
super().initAlgorithm(config)
3645
self.addParameter(
37-
QgsProcessingParameterString(
46+
QgsProcessingParameterNumber(
3847
name=self.TIME_GAP,
39-
description=self.tr("Time gap (timedelta, e.g. 1 hours, 15 minutes)"),
40-
defaultValue="1 hours",
41-
optional=True,
48+
description=self.tr("Time gap value"),
49+
defaultValue=1,
4250
)
4351
)
52+
self.addParameter(
53+
QgsProcessingParameterEnum(
54+
name=self.TIME_DELTA_UNITS,
55+
description=self.tr("Time gap unit"),
56+
defaultValue=3,
57+
options=self.TIME_DELTA_UNITS_OPTIONS,
58+
)
59+
)
4460

4561
def name(self):
4662
return "split_gap"
@@ -51,9 +67,7 @@ def displayName(self):
5167
def shortHelpString(self):
5268
return self.tr(
5369
"<p>Splits trajectories into subtrajectories "
54-
"whenever there is a gap in the observations "
55-
"(for supported time gap formats see: "
56-
"https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_timedelta.html)</p>"
70+
"whenever there is a gap in the observations</p>"
5771
"<p>For more information on trajectory splitters see: "
5872
"https://movingpandas.readthedocs.io/en/main/api/trajectorysplitter.html</p>"
5973
"<p><b>Speed</b> is calculated based on the input layer CRS information and "
@@ -64,8 +78,12 @@ def shortHelpString(self):
6478
)
6579

6680
def processTc(self, tc, parameters, context):
67-
time_gap = self.parameterAsString(parameters, self.TIME_GAP, context)
68-
time_gap = pd.Timedelta(time_gap).to_pytimedelta()
81+
time_gap = self.parameterAsDouble(parameters, self.TIME_GAP, context)
82+
td_units = self.parameterAsInt(parameters, self.TIME_DELTA_UNITS, context)
83+
td_units = self.TIME_DELTA_UNITS_OPTIONS[td_units]
84+
if td_units == "Weeks":
85+
td_units = "W"
86+
time_gap = pd.Timedelta(f"{time_gap} {td_units}").to_pytimedelta()
6987
for traj in tc.trajectories:
7088
splits = ObservationGapSplitter(traj).split(gap=time_gap)
7189
self.tc_to_sink(splits)

0 commit comments

Comments
 (0)