Skip to content

Commit

Permalink
Merge pull request #44 from GispoCoding/42-add-a-way-to-create-a-new-…
Browse files Browse the repository at this point in the history
…qgis-line-layer-which-can-be-used-as-a-gate-layer

42 add a way to create a new qgis line layer which can be used as a gate layer
  • Loading branch information
JuhoErvasti authored Oct 30, 2024
2 parents b6c7fff + e5e9c33 commit fe48f0b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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

0 comments on commit fe48f0b

Please sign in to comment.