From 77da53792af01502a44227c9ee9104b6ac50fe7d Mon Sep 17 00:00:00 2001 From: Luc Claustres Date: Wed, 14 Aug 2024 14:14:02 +0200 Subject: [PATCH] feat: Make layer updated in realtime based on service events be aware of time (closes #935) --- map/client/mixins/mixin.feature-service.js | 7 +++++++ map/client/utils/utils.features.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/map/client/mixins/mixin.feature-service.js b/map/client/mixins/mixin.feature-service.js index dbf371ccb..ff7bd5dce 100644 --- a/map/client/mixins/mixin.feature-service.js +++ b/map/client/mixins/mixin.feature-service.js @@ -97,6 +97,9 @@ export const featureService = { if (!layer || !this.isLayerVisible(layer.name)) return // Only possible when not edited by default if ((typeof this.isLayerEdited === 'function') && this.isLayerEdited(layer)) return + // Check for time-based layers if update is in the currently visualized time range + // so that we don't add too much old features + if (!features.isFeatureInQueryInterval(feature, layer)) return // As by default we update the whole layer in fetch and replace mode force add/update only mode // Can only apply to realtime layers as we need to force a data refresh if (typeof this.updateLayer === 'function') { @@ -113,6 +116,10 @@ export const featureService = { if (!layer || !this.isLayerVisible(layer.name)) return // Only possible when not edited by default if ((typeof this.isLayerEdited === 'function') && this.isLayerEdited(layer)) return + // Check for time-based layers if update is in the currently visualized time range ? Should not be relevent in this case. + // Indeed, as time has passed we might have old features that need to be cleaned, + // ie features now outside the request time range but inside the initial time range when they were requested + //if (!features.isFeatureInQueryInterval(feature, layer)) return // Can only apply to realtime layers as we need to force a data refresh if (typeof this.updateLayer === 'function') { // Check if feature should be filtered or not according to layer base query diff --git a/map/client/utils/utils.features.js b/map/client/utils/utils.features.js index 32b4aac41..de5e766a6 100644 --- a/map/client/utils/utils.features.js +++ b/map/client/utils/utils.features.js @@ -216,6 +216,24 @@ export async function getFeaturesQuery (options, queryInterval, queryLevel) { return query } +export function isFeatureInQueryInterval (feature, options) { + // We assume this is not a time-varying layer + if (!feature.time) return true + const queryInterval = getFeaturesQueryInterval(options) + if (!moment.isDuration(queryInterval)) return true + const now = Time.getCurrentTime() + const time = moment.utc(feature.time) + // Depending on the duration format we might have negative or positive values + const gte = (queryInterval.asMilliseconds() > 0 + ? now.clone().subtract(queryInterval) + : now.clone().add(queryInterval)) + const lte = now + // In realtime mode take into account that we don't update time continuously but according to a frequency + // so that we might receive features "in the future" according to the current time + if (Time.isRealtime()) lte.add(Time.get().interval, 's') + return time.isSameOrAfter(gte) && time.isSameOrBefore(lte) +} + export async function getFeaturesFromQuery (options, query) { // Check API to be used in case the layer is coming from a remote "planet" const planetApi = (typeof options.getPlanetApi === 'function' ? options.getPlanetApi() : api)