Skip to content

Commit 5238732

Browse files
committed
add util functions to misc_utils.py, fix additional_information type in PlanRegulationDefinition
1 parent c99d770 commit 5238732

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed

arho_feature_template/core/plan_manager.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@
55
from arho_feature_template.core.update_plan import LandUsePlan, update_selected_plan
66
from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
77
from arho_feature_template.utils.db_utils import get_existing_database_connection_names
8-
from arho_feature_template.utils.misc_utils import handle_unsaved_changes
8+
from arho_feature_template.utils.misc_utils import get_layer_by_name, handle_unsaved_changes
99

1010

1111
class PlanManager:
1212
def __init__(self):
13-
self.kaava_layer = self.get_layer_by_name("Kaava")
14-
15-
def get_layer_by_name(self, layer_name):
16-
"""Retrieve a layer by name from the project."""
17-
layers = QgsProject.instance().mapLayersByName(layer_name)
18-
if layers:
19-
return layers[0]
20-
iface.messageBar().pushMessage("Error", f"Layer '{layer_name}' not found", level=3)
21-
return None
13+
self.kaava_layer = get_layer_by_name("Kaava")
2214

2315
def add_new_plan(self):
2416
"""Initiate the process to add a new plan to the Kaava layer."""

arho_feature_template/core/plan_regulation_config.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
from typing import TYPE_CHECKING, cast
99

1010
import yaml
11-
from qgis.core import QgsProject
1211
from qgis.utils import iface
1312

1413
from arho_feature_template.qgis_plugin_tools.tools.resources import resources_path
14+
from arho_feature_template.utils.misc_utils import get_layer_by_name
1515

1616
if TYPE_CHECKING:
1717
from numbers import Number
1818
from typing import Literal
1919

20-
from qgis.core import QgsMapLayer
2120
from qgis.gui import QgisInterface
2221

2322
iface: QgisInterface = cast("QgisInterface", iface) # type: ignore[no-redef]
@@ -56,16 +55,6 @@ class Unit(Enum):
5655
DECIBEL = "dB"
5756

5857

59-
# TODO: Same as in PlanManager, should refactor
60-
def get_layer_by_name(layer_name: str) -> QgsMapLayer | None:
61-
"""Retrieve a layer by name from the project."""
62-
layers = QgsProject.instance().mapLayersByName(layer_name)
63-
if layers:
64-
return layers[0]
65-
iface.messageBar().pushMessage("Error", f"Layer '{layer_name}' not found", level=3)
66-
return None
67-
68-
6958
def get_name_mapping_for_plan_regulations(layer_name: str) -> dict[str, dict[str, str]] | None:
7059
layer = get_layer_by_name(layer_name)
7160
if not layer:
@@ -188,7 +177,9 @@ class PlanRegulationDefinition:
188177

189178
regulation_config: PlanRegulationConfig
190179
default_value: str | Number | None
191-
additional_info: dict[str, str | Number | None] # NOTE: Correct typing for additional information values?
180+
additional_information: list[
181+
dict[str, str | Number | None]
182+
] # NOTE: Correct typing for additional information values?
192183
regulation_number: int | None
193184
attached_files: list[Path]
194185

@@ -197,7 +188,7 @@ def from_dict(cls, data: dict) -> PlanRegulationDefinition:
197188
return cls(
198189
regulation_config=data["config"],
199190
default_value=data.get("default_value"),
200-
additional_info=data.get("additional_info", {}),
191+
additional_information=data.get("additional_information", []),
201192
regulation_number=data.get("regulation_number"),
202193
attached_files=data.get("attached_files", []),
203194
)

arho_feature_template/gui/plan_regulation_widget.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
)
2121

2222
from arho_feature_template.core.plan_regulation_config import PlanRegulationConfig, Unit, ValueType
23+
from arho_feature_template.utils.misc_utils import get_additional_information_name
2324

2425
if TYPE_CHECKING:
2526
from numbers import Number

arho_feature_template/utils/misc_utils.py

+42
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
1+
from __future__ import annotations
2+
13
import os
4+
from typing import TYPE_CHECKING, cast
25

36
from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer
47
from qgis.PyQt.QtCore import QSettings
58
from qgis.PyQt.QtWidgets import QMessageBox
9+
from qgis.utils import iface
10+
11+
if TYPE_CHECKING:
12+
from typing import Literal
13+
14+
from qgis.core import QgsMapLayer
15+
from qgis.gui import QgisInterface
16+
17+
iface: QgisInterface = cast("QgisInterface", iface) # type: ignore[no-redef]
618

719
PLUGIN_PATH = os.path.dirname(os.path.dirname(__file__))
820

921

22+
# NOTE: Consider creating "layer_utils.py" or similar for layer related utils in the future
23+
def get_layer_by_name(layer_name: str) -> QgsMapLayer | None:
24+
"""
25+
Retrieve a layer by name from the project.
26+
27+
If multiple layers with the same name exist, returns the first one. Returns None
28+
if layer with a matching name is not found.
29+
"""
30+
layers = QgsProject.instance().mapLayersByName(layer_name)
31+
if layers:
32+
return layers[0]
33+
iface.messageBar().pushWarning("Error", f"Layer '{layer_name}' not found")
34+
return None
35+
36+
37+
def get_additional_information_name(info_type: str, language: Literal["fin", "eng", "swe"] = "fin") -> str:
38+
"""
39+
Retrieve name of input additional information type from associated QGIS layer.
40+
41+
Returns input `info_type` if name is not found.
42+
"""
43+
type_of_additional_information_layer_name = "Lisätiedonlaji"
44+
layer = get_layer_by_name(type_of_additional_information_layer_name)
45+
if layer:
46+
for feature in layer.getFeatures():
47+
if feature["value"] == info_type:
48+
return feature["name"][language]
49+
return info_type
50+
51+
1052
def check_layer_changes() -> bool:
1153
"""Check if there are unsaved changes in any QGIS layers."""
1254
project = QgsProject.instance()

0 commit comments

Comments
 (0)