Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrajLayer: Skip one-node trajectories #59

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading