From 2636b520e67880eb77367624760c454d7aa46c3e Mon Sep 17 00:00:00 2001 From: Juho Ervasti Date: Fri, 1 Nov 2024 10:23:55 +0200 Subject: [PATCH] TrajLayer: Skip one-node trajectories Fixes #54 --- fvh3t/core/trajectory_layer.py | 10 +++++++--- tests/core/test_trajectory_layer.py | 29 +++++++++++++++++------------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/fvh3t/core/trajectory_layer.py b/fvh3t/core/trajectory_layer.py index 19358d2..85e2bd5 100644 --- a/fvh3t/core/trajectory_layer.py +++ b/fvh3t/core/trajectory_layer.py @@ -1,6 +1,7 @@ from __future__ import annotations from datetime import datetime, timezone +from logging import getLogger from math import log10 from typing import Any @@ -19,8 +20,9 @@ ) from qgis.PyQt.QtCore import QMetaType, QVariant -from fvh3t.core.exceptions import InvalidFeatureException, InvalidLayerException, InvalidTrajectoryException +from fvh3t.core.exceptions import InvalidFeatureException, InvalidLayerException from fvh3t.core.trajectory import Trajectory, TrajectoryNode +from fvh3t.qgis_plugin_tools.tools.resources import plugin_name UNIX_TIMESTAMP_UNIT_THRESHOLD = 13 N_NODES_MIN = 2 @@ -39,6 +41,8 @@ QMetaType.Type.Float, ] +LOGGER = getLogger(plugin_name()) + def digits_in_timestamp_int(num: int): return int(log10(num)) + 1 @@ -168,8 +172,8 @@ def create_trajectories(self) -> None: ) if len(nodes) < N_NODES_MIN: - msg = "Trajectory must consist of at least two nodes." - raise InvalidTrajectoryException(msg) + LOGGER.info('Trajectory with id "{%s}" has only one node, skipping...', str(identifier)) + continue trajectories.append(Trajectory(tuple(nodes), self)) diff --git a/tests/core/test_trajectory_layer.py b/tests/core/test_trajectory_layer.py index fea1a11..9894b07 100644 --- a/tests/core/test_trajectory_layer.py +++ b/tests/core/test_trajectory_layer.py @@ -1,9 +1,10 @@ +import logging from typing import TYPE_CHECKING import pytest from qgis.core import QgsUnitTypes -from fvh3t.core.exceptions import InvalidLayerException, InvalidTrajectoryException +from fvh3t.core.exceptions import InvalidLayerException from fvh3t.core.trajectory_layer import TrajectoryLayer if TYPE_CHECKING: @@ -147,14 +148,18 @@ def test_is_field_valid(qgis_point_layer_no_additional_fields, qgis_point_layer_ ) -def test_create_trajectory_layer_single_trajectory_node(qgis_single_point_layer): - with pytest.raises(InvalidTrajectoryException, match="Trajectory must consist of at least two nodes."): - TrajectoryLayer( - qgis_single_point_layer, - "id", - "timestamp", - "width", - "length", - "height", - QgsUnitTypes.TemporalUnit.TemporalMilliseconds, - ) +def test_create_trajectory_layer_single_trajectory_node(qgis_single_point_layer, caplog): + caplog.set_level(logging.INFO) + + layer = TrajectoryLayer( + qgis_single_point_layer, + "id", + "timestamp", + "width", + "length", + "height", + QgsUnitTypes.TemporalUnit.TemporalMilliseconds, + ) + + assert len(layer.trajectories()) == 0 + assert 'Trajectory with id "1" has only one node, skipping...' in caplog.text