Skip to content

Commit

Permalink
TrajLayer: Skip one-node trajectories
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
JuhoErvasti committed Nov 1, 2024
1 parent 3b6d7d4 commit 2636b52
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
10 changes: 7 additions & 3 deletions fvh3t/core/trajectory_layer.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -39,6 +41,8 @@
QMetaType.Type.Float,
]

LOGGER = getLogger(plugin_name())


def digits_in_timestamp_int(num: int):
return int(log10(num)) + 1
Expand Down Expand Up @@ -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))

Expand Down
29 changes: 17 additions & 12 deletions tests/core/test_trajectory_layer.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 2636b52

Please sign in to comment.