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

42 add a way to create a new qgis line layer which can be used as a gate layer #44

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
17 changes: 17 additions & 0 deletions fvh3t/core/line_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from __future__ import annotations

from qgis.core import QgsField, QgsVectorLayer
from qgis.PyQt.QtCore import QVariant


def create_line_layer() -> QgsVectorLayer:
layer = QgsVectorLayer("LineString?crs=EPSG:3067", "Line Layer", "memory")

layer.startEditing()

layer.addAttribute(QgsField("counts_left", QVariant.Bool))
layer.addAttribute(QgsField("counts_right", QVariant.Bool))

layer.commitChanges()

return layer
40 changes: 40 additions & 0 deletions tests/core/test_gate_layer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
from qgis.core import QgsFeature, QgsGeometry, QgsPointXY

from fvh3t.core.exceptions import InvalidLayerException
from fvh3t.core.gate_layer import GateLayer
from fvh3t.core.line_layer import create_line_layer


def test_gate_layer_create_gates(qgis_gate_line_layer):
Expand Down Expand Up @@ -51,3 +53,41 @@ def test_is_field_valid(qgis_gate_line_layer, qgis_gate_line_layer_wrong_field_t
"counts_left",
"counts_right",
)


def test_create_valid_gate_from_empty_line_layer():
layer = create_line_layer()

layer.startEditing()

gate = QgsFeature(layer.fields())
gate.setAttributes([True, True])
gate.setGeometry(
QgsGeometry.fromPolylineXY(
[
QgsPointXY(0, 0),
QgsPointXY(0, 1),
]
)
)

layer.addFeature(gate)

layer.commitChanges()

gate_layer = GateLayer(
layer,
"counts_left",
"counts_right",
)

gates = gate_layer.gates()

assert len(gates) == 1

gate = gates[0]

assert gate.geometry().asWkt() == "LineString (0 0, 0 1)"
assert gate.counts_left()
assert gate.counts_right()
assert len(gate.segments()) == 1
Loading