Skip to content

Commit 8bba326

Browse files
committed
Add filtering to new layers
1 parent f1c8127 commit 8bba326

File tree

2 files changed

+79
-76
lines changed

2 files changed

+79
-76
lines changed

arho_feature_template/core/plan_manager.py

+79-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,72 @@
11
from __future__ import annotations
22

33
import json
4+
import logging
5+
from string import Template
6+
from textwrap import dedent
47

58
from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer
69
from qgis.PyQt.QtWidgets import QDialog, QMessageBox
710
from qgis.utils import iface
811

912
from arho_feature_template.core.lambda_service import LambdaService
10-
from arho_feature_template.core.update_plan import LandUsePlan, update_selected_plan
1113
from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
1214
from arho_feature_template.gui.serialize_plan import SerializePlan
1315
from arho_feature_template.utils.db_utils import get_existing_database_connection_names
1416
from arho_feature_template.utils.misc_utils import get_active_plan_id, get_layer_by_name, handle_unsaved_changes
1517

18+
logger = logging.getLogger(__name__)
19+
1620
LAYER_NAME_PLAN = "Kaava"
1721

22+
PLAN_FILTER_TEMPLATES = {
23+
"Kaava": Template("id = '$plan_id'"),
24+
"Maankäytön kohteet": Template("plan_id = '$plan_id'"),
25+
"Muut pisteet": Template("plan_id = '$plan_id'"),
26+
"Viivat": Template("plan_id = '$plan_id'"),
27+
"Aluevaraus": Template("plan_id = '$plan_id'"),
28+
"Osa-alue": Template("plan_id = '$plan_id'"),
29+
"Kaavamääräysryhmät": Template("plan_id = '$plan_id'"),
30+
"Kaavamääräysryhmien assosiaatiot": Template(
31+
dedent(
32+
"""\
33+
EXISTS (
34+
SELECT 1
35+
FROM hame.plan_regulation_group prg
36+
WHERE
37+
hame.regulation_group_association.plan_regulation_group_id = prg.id
38+
AND prg.plan_id = '$plan_id'
39+
)"""
40+
)
41+
),
42+
"Kaavamääräys": Template(
43+
dedent(
44+
"""\
45+
EXISTS (
46+
SELECT 1
47+
FROM hame.plan_regulation_group prg
48+
WHERE
49+
hame.plan_regulation.plan_regulation_group_id = prg.id
50+
AND prg.plan_id = '$plan_id'
51+
)"""
52+
)
53+
),
54+
"Kaavasuositus": Template(
55+
dedent(
56+
"""\
57+
EXISTS (
58+
SELECT 1
59+
FROM hame.plan_regulation_group rg
60+
WHERE
61+
hame.plan_proposition.plan_regulation_group_id = rg.id
62+
AND rg.plan_id = '$plan_id'
63+
)"""
64+
)
65+
),
66+
"Asiakirjat": Template("plan_id = '$plan_id'"),
67+
"Lähtötietoaineistot": Template("plan_id = '$plan_id'"),
68+
}
69+
1870

1971
class PlanManager:
2072
def __init__(self):
@@ -27,10 +79,10 @@ def add_new_plan(self):
2779
return
2880

2981
plan_layer = get_layer_by_name(LAYER_NAME_PLAN)
30-
self.clear_all_filters()
3182

3283
if not plan_layer:
3384
return
85+
self.set_active_plan(None)
3486

3587
if not plan_layer.isEditable():
3688
plan_layer.startEditing()
@@ -61,18 +113,40 @@ def _feature_added(self):
61113
return
62114

63115
feature_ids_after_commit = plan_layer.allFeatureIds()
64-
new_feature_id = next((fid for fid in feature_ids_after_commit if fid not in feature_ids_before_commit), None)
116+
new_feature_id = next(
117+
(fid for fid in feature_ids_after_commit if fid not in feature_ids_before_commit),
118+
None,
119+
)
65120

66121
if new_feature_id is not None:
67122
new_feature = plan_layer.getFeature(new_feature_id)
68123
if new_feature.isValid():
69124
feature_id_value = new_feature["id"]
70-
update_selected_plan(LandUsePlan(feature_id_value))
125+
self.set_active_plan(feature_id_value)
71126
else:
72127
iface.messageBar().pushMessage("Error", "Invalid feature retrieved.", level=3)
73128
else:
74129
iface.messageBar().pushMessage("Error", "No new feature was added.", level=3)
75130

131+
def set_active_plan(self, plan_id: str | None):
132+
"""Update the project layers based on the selected land use plan."""
133+
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "active_plan_id", plan_id)
134+
135+
for layer_name, filter_template in PLAN_FILTER_TEMPLATES.items():
136+
"""Set a filter for the given vector layer."""
137+
filter_expression = filter_template.substitute(plan_id=plan_id) if plan_id else None
138+
layer = get_layer_by_name(layer_name)
139+
if not layer:
140+
logger.warning("Layer %s not found", layer_name)
141+
continue
142+
result = layer.setSubsetString(filter_expression)
143+
if result is False:
144+
iface.messageBar().pushMessage(
145+
"Error",
146+
f"Failed to filter layer {layer_name} with query {filter_expression}",
147+
level=3,
148+
)
149+
76150
def load_land_use_plan(self):
77151
"""Load an existing land use plan using a dialog selection."""
78152
connections = get_existing_database_connection_names()
@@ -94,15 +168,7 @@ def load_land_use_plan(self):
94168
QMessageBox.critical(None, "Error", "No plan was selected.")
95169
return
96170

97-
plan = LandUsePlan(selected_plan_id)
98-
update_selected_plan(plan)
99-
100-
def clear_all_filters(self):
101-
"""Clear active_plan_id and filters for all vector layers in the project."""
102-
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "active_plan_id", None)
103-
for layer in QgsProject.instance().mapLayers().values():
104-
if isinstance(layer, QgsVectorLayer):
105-
layer.setSubsetString("")
171+
self.set_active_plan(selected_plan_id)
106172

107173
def commit_all_editable_layers(self):
108174
"""Commit all changes in any editable layers."""

arho_feature_template/core/update_plan.py

-63
This file was deleted.

0 commit comments

Comments
 (0)