Skip to content

Commit

Permalink
Merge branch 'main' into 42-add-a-way-to-create-a-new-qgis-line-layer…
Browse files Browse the repository at this point in the history
…-which-can-be-used-as-a-gate-layer
  • Loading branch information
msorvoja committed Oct 30, 2024
2 parents 7aa83be + b6c7fff commit e5e9c33
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions fvh3t/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ class InvalidTrajectoryException(QgsPluginException):

class InvalidFeatureException(QgsPluginException):
pass


class InvalidSegmentException(QgsPluginException):
pass
36 changes: 35 additions & 1 deletion fvh3t/core/trajectory_segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

from typing import TYPE_CHECKING

from qgis.core import QgsGeometry
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransformContext,
QgsDistanceArea,
QgsGeometry,
QgsUnitTypes,
)

from fvh3t.core.exceptions import InvalidSegmentException

if TYPE_CHECKING:
from datetime import timedelta

from fvh3t.core.gate import Gate
from fvh3t.core.trajectory import TrajectoryNode

Expand All @@ -19,6 +29,30 @@ def __init__(self, node_a: TrajectoryNode, node_b: TrajectoryNode) -> None:
self.node_a = node_a
self.node_b = node_b

def speed(self, crs: QgsCoordinateReferenceSystem) -> float:
da = QgsDistanceArea()
da.setSourceCrs(crs, QgsCoordinateTransformContext())

convert: bool = da.lengthUnits() != QgsUnitTypes.DistanceUnit.DistanceMeters

distance_m: float = da.measureLine(self.node_b.point, self.node_a.point)
if convert:
distance_m = da.convertLengthMeasurement(distance_m, QgsUnitTypes.DistanceUnit.DistanceMeters)

time_difference: timedelta = self.node_b.timestamp - self.node_a.timestamp
seconds: float = distance_m / time_difference.total_seconds()

if seconds > 0:
meters_per_second = distance_m / seconds

return round(meters_per_second * 3.6, 2)

if seconds < 0:
msg = "Node A must be earlier than node B, timewise!"
raise InvalidSegmentException(msg)

return 0.0

def as_geometry(self) -> QgsGeometry:
return QgsGeometry.fromPolylineXY([self.node_a.point, self.node_b.point])

Expand Down
6 changes: 6 additions & 0 deletions tests/core/test_trajectory_segment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import TYPE_CHECKING

from qgis.core import QgsCoordinateReferenceSystem

if TYPE_CHECKING:
from fvh3t.core.trajectory_segment import TrajectorySegment

Expand Down Expand Up @@ -42,3 +44,7 @@ def test_trajectory_as_segments(two_node_trajectory, three_node_trajectory):

def test_trajectory_segment_as_geometry(trajectory_segment):
assert trajectory_segment.as_geometry().asWkt() == "LineString (0 0, 0 1)"


def test_trajectory_segment_speed(trajectory_segment):
assert trajectory_segment.speed(QgsCoordinateReferenceSystem("EPSG:3067")) == 3.6

0 comments on commit e5e9c33

Please sign in to comment.