|
1 | 1 | import sys
|
2 | 2 | import pandas as pd
|
3 | 3 |
|
4 |
| -from movingpandas import TemporalSplitter, ObservationGapSplitter, StopSplitter |
| 4 | +from movingpandas import ( |
| 5 | + TemporalSplitter, |
| 6 | + ObservationGapSplitter, |
| 7 | + StopSplitter, |
| 8 | + ValueChangeSplitter, |
| 9 | +) |
5 | 10 |
|
6 | 11 | from qgis.core import (
|
7 | 12 | QgsProcessingParameterString,
|
8 | 13 | QgsProcessingParameterEnum,
|
9 | 14 | QgsProcessingParameterNumber,
|
| 15 | + QgsProcessingParameterField, |
10 | 16 | )
|
11 | 17 |
|
12 | 18 | sys.path.append("..")
|
@@ -202,3 +208,50 @@ def processTc(self, tc, parameters, context):
|
202 | 208 | self.tc_to_sink(splits)
|
203 | 209 | for split in splits:
|
204 | 210 | self.traj_to_sink(split)
|
| 211 | + |
| 212 | + |
| 213 | +class ValueChangeSplitterAlgorithm(SplitTrajectoriesAlgorithm): |
| 214 | + FIELD = "FIELD" |
| 215 | + |
| 216 | + def __init__(self): |
| 217 | + super().__init__() |
| 218 | + |
| 219 | + def initAlgorithm(self, config=None): |
| 220 | + super().initAlgorithm(config) |
| 221 | + self.addParameter( |
| 222 | + QgsProcessingParameterField( |
| 223 | + name=self.FIELD, |
| 224 | + description=self.tr("Field to check for changing values"), |
| 225 | + parentLayerParameterName=self.INPUT, |
| 226 | + type=QgsProcessingParameterField.Any, |
| 227 | + allowMultiple=False, |
| 228 | + optional=False, |
| 229 | + ) |
| 230 | + ) |
| 231 | + |
| 232 | + def name(self): |
| 233 | + return "split_value_change" |
| 234 | + |
| 235 | + def displayName(self): |
| 236 | + return self.tr("Split trajectories when field value changes") |
| 237 | + |
| 238 | + def shortHelpString(self): |
| 239 | + return self.tr( |
| 240 | + "<p>Splits trajectories into subtrajectories " |
| 241 | + "whenever there is a change in the specified field's value.</p>" |
| 242 | + "<p>For more information on trajectory splitters see: " |
| 243 | + "https://movingpandas.readthedocs.io/en/main/api/trajectorysplitter.html</p>" |
| 244 | + "<p><b>Speed</b> is calculated based on the input layer CRS information and " |
| 245 | + "converted to the desired speed units. For more info on the supported units, " |
| 246 | + "see https://movingpandas.org/units</p>" |
| 247 | + "<p><b>Direction</b> is calculated between consecutive locations. Direction " |
| 248 | + "values are in degrees, starting North turning clockwise.</p>" |
| 249 | + ) |
| 250 | + |
| 251 | + def processTc(self, tc, parameters, context): |
| 252 | + self.field = self.parameterAsFields(parameters, self.FIELD, context)[0] |
| 253 | + for traj in tc.trajectories: |
| 254 | + splits = ValueChangeSplitter(traj).split(col_name=self.field) |
| 255 | + self.tc_to_sink(splits) |
| 256 | + for split in splits: |
| 257 | + self.traj_to_sink(split) |
0 commit comments