Repository navigation
StepGaps
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not stored in the working file StepGaps.java: the production code stays clean and the explanations live in the wiki.
package com.velitask.plugin.official.processing.gaps;
import static com.velitask.plugin.official.processing.FormulaParameters.decimal;
import static com.velitask.plugin.official.processing.FormulaParameters.integer;
import com.velitask.sdk.data.SensorAtom;
import com.velitask.sdk.properties.DoubleSpinnerProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerProperty;
import com.velitask.sdk.scenario.GapInput;
import com.velitask.sdk.scenario.GapTrack;
import com.velitask.sdk.scenario.GapsStep;
import com.velitask.sdk.scenario.IStepControl;
import java.util.Arrays;
/* Pause formula "Pauses by recording gap": a pause where the gap in time between neighbouring points is too large. */
public final class StepGaps extends GapsStep<SensorAtom> {
/* Stable step name; the localization keys Plugin.ImportStep.stepGaps.* are built from it */
public static final String NAME = "stepGaps";
/* Names of the formula parameters: they are also the keys of the values in scenarios and in saved settings. */
public static final String MIN_STEP_SEC = "minStepSec";
public static final String STEP_FACTOR = "stepFactor";
public static final String MAX_SPEED = "maxSpeed";
/* Prefix of the localization keys of the parameters of all pause formulas: Plugin.ImportStep.gaps. */
private static final String KEY = _KEY + ".gaps";
/* Defaults: minimal gap 10 s, factor 5; moving speed 0 (do not check). */
private static final int DEFAULT_MIN_STEP_SEC = 10;
private static final double DEFAULT_STEP_FACTOR = 5;
/* The parameters are created in defineFormulaParameters(). */
private IntegerProperty mMinStepSec;
private DoubleSpinnerProperty mStepFactor;
private DoubleSpinnerProperty mMaxSpeed;
public StepGaps() {
/* Step name, logic version 1 and the atom class: the formula works with atoms of any sensor. */
super(NAME, 1, SensorAtom.class);
}
@Override
/* The three formula parameters in the order they are shown in the step settings window. */
protected IProperty[] defineFormulaParameters() {
mMinStepSec = integer(MIN_STEP_SEC, localized(KEY + "." + MIN_STEP_SEC + ".title"),
localizedOrEmpty(KEY + "." + MIN_STEP_SEC + ".description"), DEFAULT_MIN_STEP_SEC);
mStepFactor = decimal(STEP_FACTOR, localized(KEY + "." + STEP_FACTOR + ".title"),
localizedOrEmpty(KEY + "." + STEP_FACTOR + ".description"), DEFAULT_STEP_FACTOR);
mMaxSpeed = decimal(MAX_SPEED, localized(KEY + "." + MAX_SPEED + ".title"),
localizedOrEmpty(KEY + "." + MAX_SPEED + ".description"), 0);
return new IProperty[]{mMinStepSec, mStepFactor, mMaxSpeed};
}
@Override
/* pauseAfter[i] = true means a pause between atom i and the one after it. */
protected boolean[] detect(GapInput<SensorAtom> input, IStepControl control) {
long[] times = input.times();
boolean[] pauseAfter = new boolean[times.length];
if (times.length < 2) {
return pauseAfter;
}
/* steps[i] - the time gap between the start of atom i and the start of atom i + 1. */
long[] steps = new long[times.length - 1];
for (int i = 0; i < steps.length; i++) {
steps[i] = times[i + 1] - times[i];
}
long[] sorted = steps.clone();
Arrays.sort(sorted);
/* The usual recording step is the median of all gaps. */
long median = sorted[sorted.length / 2];
/* Lower bound of the threshold: the given minimum, but not less than the recording step of the file. */
long minStepMs = Math.max(mMinStepSec.get() * 1000L, input.recordStepMs());
/* Pause threshold: the larger of the minimum and the median multiplied by the factor. */
long threshold = Math.max(minStepMs, (long) (mStepFactor.get() * median));
double maxSpeed = mMaxSpeed.get();
for (int i = 0; i < steps.length; i++) {
/* A gap is a pause only if the track did not move fast during it. */
boolean moving = movedFast(input.track(), times[i], times[i + 1], steps[i], maxSpeed);
pauseAfter[i] = steps[i] > threshold && !moving;
}
return pauseAfter;
}
/* Speed in the gap: millimetres per millisecond equal metres per second. */
private boolean movedFast(GapTrack track, long from, long to, long stepMs, double maxSpeed) {
if (maxSpeed <= 0 || track == null) {
return false;
}
Long distanceMm = track.distanceMm(from, to);
return distanceMm != null && (double) distanceMm / stepMs >= maxSpeed;
}
}StepGaps is the "Pauses by recording gap" formula (stepGaps): it sets a pause where noticeably more time passed between neighbouring sensor points than is usual in this recording. It is the main formula for finding pauses from the data in the built-in import scenarios: it is needed when the device stops recording while its owner stands still and the file has no explicit pause mark.
Data. The formula takes from the GapInput input the atom times (times()), the recording step of the file (recordStepMs()) and the track (track()). It works with atoms of any sensor (SensorAtom). If there are fewer than two points, there are no pauses.
Parameters. The formula has three parameters:
| Parameter | Key | Default | Meaning |
|---|---|---|---|
| "Minimal gap, s" | minStepSec |
10 | A shorter gap is not a pause. If the device records less often, its recording step serves as the threshold. |
| "Usual step factor" | stepFactor |
5 | A pause is a gap longer than the usual step (the median) multiplied by this number. |
| "Moving speed during a gap, m/s" | maxSpeed |
0 | If during the gap the track moved at this speed or faster, it is not a pause. 0 - do not check. |
The defaults are the same for all sensors: the formula does not change them depending on the sensor type. The built-in scenarios override the values for a sensor: in GPX for GPS and elevation they are 10, 5 and a moving speed of 2 m/s, for temperature (GPX and FIT) 60 and 5; in DJI SRT for GPS and elevation 10 and 5.
Besides the formula parameters, the step has a common parameter of the GapsStep base class, "Stood if moved less than, m" (stoodMeters, default 30). It does not change where the pauses are: it marks an already found pause as a stop if the track moved less than this distance during it. This parameter comes first, the formula parameters after it.
Algorithm.
- The gaps
steps[i] = times[i + 1] - times[i]and their median are computed (the element with indexlength / 2of the sorted array). - The minimal gap in milliseconds:
max(minStepSec * 1000, recordStepMs). - The threshold:
max(minimal gap, stepFactor * median). - A pause after atom
iis set ifsteps[i]is strictly greater than the threshold and the track did not move fast during this gap.
The movement check is on only if maxSpeed is greater than zero and a track exists. The distance between the track positions at the start and at the end of the gap is taken through GapTrack.distanceMm(...) and divided by the length of the gap in milliseconds; millimetres per millisecond equal metres per second. If the track does not cover one of the moments, distanceMm returns null and the gap is considered motionless, that is, a pause.
- No pause is set after the last atom: a pause describes the interval up to the next atom, and the last one has none.
- The median is computed over all gaps of the sensor at once, one for the whole recording; for a recording where the usual step changes, the threshold is still the same.
- The gap is compared with the threshold strictly ("greater") and is measured between the starts of atoms; the duration of an atom is not taken into account.
- For a sensor without coordinates (for example elevation) the movement check works only with an attached track: in the built-in scenarios this is the GPS sensor of the source, role
track. - The default
maxSpeedis 0, so without explicit setup fast movement during a gap is still counted as a pause; in the built-in GPX scenario the check is on with the value 2 m/s. - The parameter names (
minStepSec,stepFactor,maxSpeed) are the keys of the values in scenarios; renaming them breaks the saved settings.