Skip to content

Commit

Permalink
Merge pull request #1 from GispoCoding/define_core
Browse files Browse the repository at this point in the history
Add base modules to core
  • Loading branch information
msorvoja authored Oct 21, 2024
2 parents a13fb46 + b3dab2b commit fd94fc0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
Empty file added fvh3t/core/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions fvh3t/core/gate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from qgis.core import QgsGeometry

class Gate:
"""
A wrapper class around a QgsGeometry which represents a
gate through which trajectories can pass. The geometry
must be a line.
"""

def __init__(self, geom: QgsGeometry) -> None:
self.__geom: QgsGeometry = geom

def geometry(self) -> QgsGeometry:
return self.__geom

61 changes: 61 additions & 0 deletions fvh3t/core/trajectory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from typing import NamedTuple

from qgis.core import QgsGeometry, QgsPointXY, QgsVectorLayer, QgsWkbTypes

from fvh3t.core.gate import Gate

class TrajectoryNode(NamedTuple):
"""
A simple data container representing one node in a
trajectory.
"""

point: QgsPointXY
timestamp: int

@classmethod
def from_coordinates(cls, x: float, y: float, timestamp: int):
return cls(QgsPointXY(x, y), timestamp)


class Trajectory:
"""
Class representing a trajectory which consists
of nodes which have a location and a timestamp
"""

def __init__(self, nodes: tuple[TrajectoryNode, ...]) -> None:
self.__nodes: tuple[TrajectoryNode, ...] = nodes

def as_geometry(self) -> QgsGeometry:
return QgsGeometry.fromPolylineXY([node.point for node in self.__nodes])

def intersects_gate(self, other: Gate) -> bool:
return self.as_geometry().intersects(other.geometry())

def average_speed(self) -> float:
# TODO: implement function
return 0.0


class TrajectoryLayer:
"""
Wrapper around a QgsVectorLayer object from which trajectories
can be instantiated, i.e.
1. is a point layer
2. has a valid identifier field
3. has a valid timestamp field
"""

def __init__(self, layer: QgsVectorLayer, id_field: str, timestamp_field: str) -> None:
self.__layer: QgsVectorLayer = layer
self.__id_field: str = id_field
self.__timestamp_field: str = timestamp_field

def is_valid(self) -> bool:
is_point_layer: bool = self.__layer.geometryType() == QgsWkbTypes.GeometryType.PointGeometry
id_field_exists: bool = self.__layer.fields().indexFromName(self.__id_field) != -1
timestamp_field_exists: bool = self.__layer.fields().indexFromName(self.__timestamp_field) != -1

return is_point_layer and id_field_exists and timestamp_field_exists
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,17 @@
This should be used with tests that add stuff to QgsProject.
"""

import pytest
from fvh3t.core.gate import Gate

from fvh3t.core.trajectory import Trajectory, TrajectoryNode
from qgis.core import QgsGeometry, QgsPointXY

@pytest.fixture
def two_node_trajectory():
return Trajectory((TrajectoryNode.from_coordinates(0, 0, 1000), TrajectoryNode.from_coordinates(0, 1, 2000)))

@pytest.fixture
def two_point_gate():
return Gate(QgsGeometry.fromPolylineXY([QgsPointXY(-0.5, 0.5), QgsPointXY(0.5, 0.5)]))
17 changes: 17 additions & 0 deletions tests/core/test_trajectory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from fvh3t.core.trajectory import Trajectory
from fvh3t.core.gate import Gate
from qgis.core import QgsGeometry, QgsPointXY

def test_trajectory_as_geometry(two_node_trajectory: Trajectory) -> None:
assert two_node_trajectory.as_geometry().asWkt() == "LineString (0 0, 0 1)"

def test_trajectory_intersects_gate(two_node_trajectory):
geom1 = QgsGeometry.fromPolylineXY([QgsPointXY(-0.5, 0.5), QgsPointXY(0.5, 0.5)])
geom2 = QgsGeometry.fromPolylineXY([QgsPointXY(-1, -1), QgsPointXY(-2, -2)])

gate1 = Gate(geom1)
gate2 = Gate(geom2)

assert two_node_trajectory.intersects_gate(gate1)
assert not two_node_trajectory.intersects_gate(gate2)

2 changes: 1 addition & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_plugin_name():
assert plugin_name() == "FVH-3T"
assert plugin_name() == "TrafficTrajectoryToolkit"

0 comments on commit fd94fc0

Please sign in to comment.