-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from GispoCoding/define_core
Add base modules to core
- Loading branch information
Showing
6 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
|
||
|
||
def test_plugin_name(): | ||
assert plugin_name() == "FVH-3T" | ||
assert plugin_name() == "TrafficTrajectoryToolkit" |