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

Add a style to gate layer in count trajectories and add a way to create a valid gate layer #71

Merged
merged 5 commits into from
Nov 6, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ start_ide.bat
*/.pytest_cache
__pycache__
fvh3t.egg-info
symbology-style.db
3 changes: 2 additions & 1 deletion fvh3t/core/gate_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def gates(self) -> tuple[Gate, ...]:
return self.__gates

def as_line_layer(self, traveler_class: str, start_time: QDateTime, end_time: QDateTime) -> QgsVectorLayer | None:
line_layer = QgsVectorLayer("LineString?crs=3067", "Line Layer", "memory")
line_layer = QgsVectorLayer("LineString", "Line Layer", "memory")
line_layer.setCrs(self.__layer.crs())

line_layer.startEditing()

Expand Down
18 changes: 0 additions & 18 deletions fvh3t/core/line_layer.py

This file was deleted.

45 changes: 45 additions & 0 deletions fvh3t/core/qgis_layer_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsFeatureRenderer,
QgsField,
QgsFieldConstraints,
QgsReadWriteContext,
QgsVectorLayer,
edit,
)
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtXml import QDomDocument

from fvh3t.qgis_plugin_tools.tools.resources import resources_path


class QgisLayerUtils:
@staticmethod
def set_gate_style(layer: QgsVectorLayer) -> None:
doc = QDomDocument()

with open(resources_path("style", "gate_style.xml")) as style_file:
doc.setContent(style_file.read())

renderer = QgsFeatureRenderer.load(doc.documentElement(), QgsReadWriteContext())
layer.setRenderer(renderer)

@staticmethod
def create_gate_layer(crs: QgsCoordinateReferenceSystem) -> QgsVectorLayer:
layer = QgsVectorLayer("LineString", "Gate Layer", "memory")
layer.setCrs(crs)

with edit(layer):
layer.addAttribute(QgsField("name", QVariant.String))
layer.addAttribute(QgsField("counts_negative", QVariant.Bool))
layer.addAttribute(QgsField("counts_positive", QVariant.Bool))

layer.setFieldConstraint(1, QgsFieldConstraints.ConstraintExpression)
layer.setConstraintExpression(1, '"counts_negative" OR "counts_positive"')

layer.setFieldConstraint(2, QgsFieldConstraints.ConstraintExpression)
layer.setConstraintExpression(2, '"counts_negative" OR "counts_positive"')

QgisLayerUtils.set_gate_style(layer)

return layer
15 changes: 13 additions & 2 deletions fvh3t/fvh3t_processing/count_trajectories.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
QgsProcessingParameterFeatureSink,
QgsProcessingParameterString,
QgsProcessingParameterVectorLayer,
QgsProcessingUtils,
QgsUnitTypes,
)
from qgis.PyQt.QtCore import QCoreApplication, QDateTime

from fvh3t.core.gate_layer import GateLayer
from fvh3t.core.qgis_layer_utils import QgisLayerUtils
from fvh3t.core.trajectory_layer import TrajectoryLayer


Expand All @@ -29,6 +31,8 @@ class CountTrajectories(QgsProcessingAlgorithm):
OUTPUT_GATES = "OUTPUT_GATES"
OUTPUT_TRAJECTORIES = "OUTPUT_TRAJECTORIES"

gate_dest_id: str | None = None

def __init__(self) -> None:
super().__init__()

Expand Down Expand Up @@ -221,7 +225,7 @@ def processAlgorithm( # noqa N802
msg = "Gate layer is None"
raise ValueError(msg)

(sink, gate_dest_id) = self.parameterAsSink(
(sink, self.gate_dest_id) = self.parameterAsSink(
parameters,
self.OUTPUT_GATES,
context,
Expand All @@ -233,4 +237,11 @@ def processAlgorithm( # noqa N802
for feature in exported_gate_layer.getFeatures():
sink.addFeature(feature, QgsFeatureSink.FastInsert)

return {self.OUTPUT_TRAJECTORIES: traj_dest_id, self.OUTPUT_GATES: gate_dest_id}
return {self.OUTPUT_TRAJECTORIES: traj_dest_id, self.OUTPUT_GATES: self.gate_dest_id}

def postProcessAlgorithm(self, context: QgsProcessingContext, feedback: QgsProcessingFeedback) -> dict[str, Any]: # noqa: N802
if self.gate_dest_id:
layer = QgsProcessingUtils.mapLayerFromString(self.gate_dest_id, context)
QgisLayerUtils.set_gate_style(layer)

return super().postProcessAlgorithm(context, feedback)
22 changes: 12 additions & 10 deletions fvh3t/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

from typing import Callable

from qgis.core import QgsApplication
from qgis.core import QgsApplication, QgsProject, QgsVectorLayer
from qgis.PyQt.QtCore import QCoreApplication, QTranslator
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QWidget
from qgis.utils import iface

from fvh3t.core.qgis_layer_utils import QgisLayerUtils
from fvh3t.fvh3t_processing.traffic_trajectory_toolkit_provider import TTTProvider
from fvh3t.qgis_plugin_tools.tools.custom_logging import setup_logger, teardown_logger
from fvh3t.qgis_plugin_tools.tools.i18n import setup_translation
from fvh3t.qgis_plugin_tools.tools.resources import plugin_name
from fvh3t.qgis_plugin_tools.tools.i18n import setup_translation, tr
from fvh3t.qgis_plugin_tools.tools.resources import plugin_name, resources_path


class Plugin:
Expand Down Expand Up @@ -109,11 +110,11 @@ def initProcessing(self): # noqa N802
def initGui(self) -> None: # noqa N802
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
self.add_action(
"",
text=Plugin.name,
callback=self.run,
resources_path("icons", "add_gate.png"),
text=tr("Create gate layer"),
callback=self.create_gate_layer,
parent=iface.mainWindow(),
add_to_toolbar=False,
add_to_toolbar=True,
)
self.initProcessing()

Expand All @@ -128,6 +129,7 @@ def unload(self) -> None:
teardown_logger(Plugin.name)
QgsApplication.processingRegistry().removeProvider(self.provider)

def run(self) -> None:
"""Run method that performs all the real work"""
print("Hello QGIS plugin") # noqa: T201
def create_gate_layer(self) -> None:
layer: QgsVectorLayer = QgisLayerUtils.create_gate_layer(QgsProject.instance().crs())

QgsProject.instance().addMapLayer(layer)
Binary file added fvh3t/resources/icons/add_gate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading