1
1
from __future__ import annotations
2
2
3
3
import json
4
+ import logging
5
+ from string import Template
6
+ from textwrap import dedent
4
7
5
8
from qgis .core import QgsExpressionContextUtils , QgsProject , QgsVectorLayer
6
9
from qgis .PyQt .QtWidgets import QDialog , QMessageBox
7
10
from qgis .utils import iface
8
11
9
12
from arho_feature_template .core .lambda_service import LambdaService
10
- from arho_feature_template .core .update_plan import LandUsePlan , update_selected_plan
11
13
from arho_feature_template .gui .load_plan_dialog import LoadPlanDialog
12
14
from arho_feature_template .gui .serialize_plan import SerializePlan
13
15
from arho_feature_template .utils .db_utils import get_existing_database_connection_names
14
16
from arho_feature_template .utils .misc_utils import get_active_plan_id , get_layer_by_name , handle_unsaved_changes
15
17
18
+ logger = logging .getLogger (__name__ )
19
+
16
20
LAYER_NAME_PLAN = "Kaava"
17
21
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
+
18
70
19
71
class PlanManager :
20
72
def __init__ (self ):
@@ -27,10 +79,10 @@ def add_new_plan(self):
27
79
return
28
80
29
81
plan_layer = get_layer_by_name (LAYER_NAME_PLAN )
30
- self .clear_all_filters ()
31
82
32
83
if not plan_layer :
33
84
return
85
+ self .set_active_plan (None )
34
86
35
87
if not plan_layer .isEditable ():
36
88
plan_layer .startEditing ()
@@ -61,18 +113,40 @@ def _feature_added(self):
61
113
return
62
114
63
115
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
+ )
65
120
66
121
if new_feature_id is not None :
67
122
new_feature = plan_layer .getFeature (new_feature_id )
68
123
if new_feature .isValid ():
69
124
feature_id_value = new_feature ["id" ]
70
- update_selected_plan ( LandUsePlan ( feature_id_value ) )
125
+ self . set_active_plan ( feature_id_value )
71
126
else :
72
127
iface .messageBar ().pushMessage ("Error" , "Invalid feature retrieved." , level = 3 )
73
128
else :
74
129
iface .messageBar ().pushMessage ("Error" , "No new feature was added." , level = 3 )
75
130
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
+
76
150
def load_land_use_plan (self ):
77
151
"""Load an existing land use plan using a dialog selection."""
78
152
connections = get_existing_database_connection_names ()
@@ -94,15 +168,7 @@ def load_land_use_plan(self):
94
168
QMessageBox .critical (None , "Error" , "No plan was selected." )
95
169
return
96
170
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 )
106
172
107
173
def commit_all_editable_layers (self ):
108
174
"""Commit all changes in any editable layers."""
0 commit comments