-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan_manager.py
446 lines (349 loc) · 16.9 KB
/
plan_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
from __future__ import annotations
import json
import logging
from typing import TYPE_CHECKING
from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer, QgsWkbTypes
from qgis.gui import QgsMapToolDigitizeFeature
from qgis.PyQt.QtWidgets import QDialog, QMessageBox
from arho_feature_template.core.lambda_service import LambdaService
from arho_feature_template.core.models import (
FeatureTemplateLibrary,
PlanFeature,
RegulationGroupCategory,
RegulationGroupLibrary,
)
from arho_feature_template.exceptions import UnsavedChangesError
from arho_feature_template.gui.dialogs.load_plan_dialog import LoadPlanDialog
from arho_feature_template.gui.dialogs.new_plan_regulation_group_form import NewPlanRegulationGroupForm
from arho_feature_template.gui.dialogs.plan_attribute_form import PlanAttributeForm
from arho_feature_template.gui.dialogs.plan_feature_form import PlanFeatureForm
from arho_feature_template.gui.dialogs.serialize_plan import SerializePlan
from arho_feature_template.gui.docks.new_feature_dock import NewFeatureDock
from arho_feature_template.project.layers.code_layers import PlanRegulationGroupTypeLayer
from arho_feature_template.project.layers.plan_layers import (
LandUseAreaLayer,
LandUsePointLayer,
LineLayer,
OtherAreaLayer,
OtherPointLayer,
PlanFeatureLayer,
PlanLayer,
PlanRegulationLayer,
RegulationGroupAssociationLayer,
RegulationGroupLayer,
plan_layers,
)
from arho_feature_template.resources.libraries.feature_templates import feature_template_library_config_files
from arho_feature_template.resources.libraries.regulation_groups import regulation_group_library_config_files
from arho_feature_template.utils.db_utils import get_existing_database_connection_names
from arho_feature_template.utils.misc_utils import (
check_layer_changes,
get_active_plan_id,
handle_unsaved_changes,
iface,
)
if TYPE_CHECKING:
from qgis.core import QgsFeature
from arho_feature_template.core.models import Plan, Regulation, RegulationGroup
logger = logging.getLogger(__name__)
FEATURE_LAYER_NAME_TO_CLASS_MAP: dict[str, type[PlanFeatureLayer]] = {
LandUsePointLayer.name: LandUsePointLayer,
OtherAreaLayer.name: OtherAreaLayer,
OtherPointLayer.name: OtherPointLayer,
LandUseAreaLayer.name: LandUseAreaLayer,
LineLayer.name: LineLayer,
}
class PlanDigitizeMapTool(QgsMapToolDigitizeFeature): ...
class PlanFeatureDigitizeMapTool(QgsMapToolDigitizeFeature):
def __init__(self, mode: QgsMapToolDigitizeFeature.CaptureMode):
super().__init__(iface.mapCanvas(), iface.cadDockWidget(), mode)
class PlanManager:
def __init__(self):
self.json_plan_path = None
self.json_plan_outline_path = None
# Initialize libraries
self.feature_template_libraries = [
FeatureTemplateLibrary.from_config_file(file) for file in feature_template_library_config_files()
]
self.regulation_group_libraries = [
RegulationGroupLibrary.from_config_file(file) for file in regulation_group_library_config_files()
]
# Initialize new feature dock
self.new_feature_dock = NewFeatureDock(self.feature_template_libraries)
self.new_feature_dock.tool_activated.connect(self.add_new_plan_feature)
self.new_feature_dock.hide()
# Initialize digitize tools
self.digitize_map_tool = PlanDigitizeMapTool(iface.mapCanvas(), iface.cadDockWidget())
self.digitize_map_tool.digitizingCompleted.connect(self._plan_geom_digitized)
self.feature_digitize_map_tool = None
self.initialize_feature_digitize_map_tool()
def initialize_feature_digitize_map_tool(self, layer: QgsVectorLayer | None = None):
# Get matcing capture mode for given layer
if layer is None:
mode = PlanFeatureDigitizeMapTool.CaptureMode.CaptureNone
elif layer.geometryType() == QgsWkbTypes.PointGeometry:
mode = PlanFeatureDigitizeMapTool.CaptureMode.CapturePoint
elif layer.geometryType() == QgsWkbTypes.LineGeometry:
mode = PlanFeatureDigitizeMapTool.CaptureMode.CaptureLine
elif layer.geometryType() == QgsWkbTypes.PolygonGeometry:
mode = PlanFeatureDigitizeMapTool.CaptureMode.CapturePolygon
# Disconnect signals first to not trigger them unwantedly
if self.feature_digitize_map_tool:
self.feature_digitize_map_tool.digitizingCompleted.disconnect()
self.feature_digitize_map_tool.digitizingFinished.disconnect()
# Reinitialize and reconnect signals
self.feature_digitize_map_tool = PlanFeatureDigitizeMapTool(mode)
self.feature_digitize_map_tool.digitizingCompleted.connect(self._plan_feature_geom_digitized)
self.feature_digitize_map_tool.digitizingFinished.connect(self.new_feature_dock.deactivate_and_clear_selections)
# Set layer if given
if layer:
self.feature_digitize_map_tool.setLayer(layer)
def add_new_plan(self):
"""Initiate the process to add a new plan to the Kaava layer."""
self.previous_map_tool = iface.mapCanvas().mapTool()
self.previous_active_plan_id = get_active_plan_id()
if not handle_unsaved_changes():
return
plan_layer = PlanLayer.get_from_project()
if not plan_layer:
return
self.previously_editable = plan_layer.isEditable()
self.set_active_plan(None)
plan_layer.startEditing()
self.digitize_map_tool.setLayer(plan_layer)
iface.mapCanvas().setMapTool(self.digitize_map_tool)
def add_new_plan_feature(self):
if not handle_unsaved_changes():
return
layer_name = self.new_feature_dock.active_feature_layer
if layer_name is None:
msg = "No plan feature type selected"
iface.messageBar().pushWarning("", msg)
return
layer_class = FEATURE_LAYER_NAME_TO_CLASS_MAP.get(layer_name)
if not layer_class:
msg = f"Could not find plan feature layer class for layer name {layer_name}"
raise ValueError(msg)
layer = layer_class.get_from_project()
layer.startEditing()
self.initialize_feature_digitize_map_tool(layer)
iface.mapCanvas().setMapTool(self.feature_digitize_map_tool)
def _plan_geom_digitized(self, feature: QgsFeature):
"""Callback for when a new feature is added to the Kaava layer."""
plan_layer = PlanLayer.get_from_project()
if not plan_layer:
return
attribute_form = PlanAttributeForm(self.regulation_group_libraries)
if attribute_form.exec_():
plan_attributes = attribute_form.get_plan_attributes()
plan_attributes.geom = feature.geometry()
feature = save_plan(plan_attributes)
plan_to_be_activated = feature["id"]
else:
plan_to_be_activated = self.previous_active_plan_id
self.set_active_plan(plan_to_be_activated)
if self.previously_editable:
plan_layer.startEditing()
iface.mapCanvas().setMapTool(self.previous_map_tool)
def _plan_feature_geom_digitized(self, feature: QgsFeature):
# NOTE: What if user has changed dock selections while digitizng?
if self.new_feature_dock.active_template:
plan_feature = self.new_feature_dock.active_template
title = plan_feature.name
else:
plan_feature = PlanFeature(
layer_name=self.new_feature_dock.active_feature_layer,
)
title = self.new_feature_dock.active_feature_type
plan_feature.geom = feature.geometry()
attribute_form = PlanFeatureForm(
plan_feature, title, [*self.regulation_group_libraries, regulation_group_library_from_active_plan()]
)
if attribute_form.exec_():
save_plan_feature(attribute_form.model)
def set_active_plan(self, plan_id: str | None):
"""Update the project layers based on the selected land use plan.
Layers to be filtered cannot be in edit mode.
This method disables edit mode temporarily if needed.
Therefore if there are unsaved changes, this method will raise an exception.
"""
if check_layer_changes():
raise UnsavedChangesError
plan_layer = PlanLayer.get_from_project()
previously_in_edit_mode = plan_layer.isEditable()
if previously_in_edit_mode:
plan_layer.rollBack()
QgsExpressionContextUtils.setProjectVariable(QgsProject.instance(), "active_plan_id", plan_id)
for layer in plan_layers:
layer.apply_filter(plan_id)
if previously_in_edit_mode:
plan_layer.startEditing()
def load_land_use_plan(self):
"""Load an existing land use plan using a dialog selection."""
connections = get_existing_database_connection_names()
if not connections:
QMessageBox.critical(None, "Error", "No database connections found.")
return
if not handle_unsaved_changes():
return
dialog = LoadPlanDialog(None, connections)
if dialog.exec_() == QDialog.Accepted:
selected_plan_id = dialog.get_selected_plan_id()
self.commit_all_editable_layers()
if not selected_plan_id:
QMessageBox.critical(None, "Error", "No plan was selected.")
return
self.set_active_plan(selected_plan_id)
def commit_all_editable_layers(self):
"""Commit all changes in any editable layers."""
for layer in QgsProject.instance().mapLayers().values():
if isinstance(layer, QgsVectorLayer) and layer.isEditable():
layer.commitChanges()
def get_plan_json(self):
"""Serializes plan and plan outline to JSON"""
dialog = SerializePlan()
if dialog.exec_() == QDialog.Accepted:
self.json_plan_path = str(dialog.plan_file.filePath())
self.json_plan_outline_path = str(dialog.plan_outline_file.filePath())
plan_id = get_active_plan_id()
if not plan_id:
QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa.")
return
self.lambda_service = LambdaService()
self.lambda_service.jsons_received.connect(self.save_plan_jsons)
self.lambda_service.serialize_plan(plan_id)
def save_plan_jsons(self, plan_json, outline_json):
"""This slot saves the plan and outline JSONs to files."""
if plan_json is None or outline_json is None:
QMessageBox.critical(None, "Virhe", "Kaava tai sen ulkoraja ei löytynyt.")
return
# Retrieve paths
if self.json_plan_path is None or self.json_plan_outline_path is None:
QMessageBox.critical(None, "Virhe", "Tiedostopolut eivät ole saatavilla.")
return
# Save the JSONs
with open(self.json_plan_path, "w", encoding="utf-8") as full_file:
json.dump(plan_json, full_file, ensure_ascii=False, indent=2)
with open(self.json_plan_outline_path, "w", encoding="utf-8") as outline_file:
json.dump(outline_json, outline_file, ensure_ascii=False, indent=2)
QMessageBox.information(
None,
"Tallennus onnistui",
"Kaava ja sen ulkoraja tallennettu onnistuneesti.",
)
def create_new_regulation_group(self):
new_regulation_group_form = NewPlanRegulationGroupForm()
if new_regulation_group_form.exec_():
if new_regulation_group_form.save_as_config is True:
save_regulation_group_as_config(new_regulation_group_form.model)
else:
save_regulation_group(new_regulation_group_form.model)
def regulation_group_library_from_active_plan() -> RegulationGroupLibrary:
category_features = list(PlanRegulationGroupTypeLayer.get_features())
category_id_to_name: dict[str, str] = {category["id"]: category["name"]["fin"] for category in category_features}
category_regulation_group_map: dict[str, list[QgsFeature]] = {
feature["name"]["fin"]: [] for feature in category_features
}
for feat in RegulationGroupLayer.get_features():
name = category_id_to_name[feat["type_of_plan_regulation_group_id"]]
category_regulation_group_map[name].append(feat)
return RegulationGroupLibrary(
name="Käytössä olevat kaavamääräysryhmät",
version=None,
description=None,
regulation_group_categories=[
RegulationGroupCategory(
category_code=None,
name=category_name,
regulation_groups=[
RegulationGroupLayer.model_from_feature(feat) for feat in category_regulation_groups
],
)
for category_name, category_regulation_groups in category_regulation_group_map.items()
],
)
def _save_feature(feature: QgsFeature, layer: QgsVectorLayer, id_: int | None, edit_text: str = ""):
if not layer.isEditable():
layer.startEditing()
layer.beginEditCommand(edit_text)
if id_ is None:
layer.addFeature(feature)
else:
layer.updateFeature(feature)
layer.endEditCommand()
layer.commitChanges(stopEditing=False)
def save_plan(plan_data: Plan) -> QgsFeature:
plan_layer = PlanLayer.get_from_project()
in_edit_mode = plan_layer.isEditable()
if not in_edit_mode:
plan_layer.startEditing()
edit_message = "Kaavan lisäys" if plan_data.id_ is None else "Kaavan muokkaus"
plan_layer.beginEditCommand(edit_message)
# plan_data.organisation_id = "99e20d66-9730-4110-815f-5947d3f8abd3"
plan_feature = PlanLayer.feature_from_model(plan_data)
if plan_data.id_ is None:
plan_layer.addFeature(plan_feature)
else:
plan_layer.updateFeature(plan_feature)
plan_layer.endEditCommand()
plan_layer.commitChanges(stopEditing=False)
if plan_data.general_regulations:
for regulation_group in plan_data.general_regulations:
plan_id = plan_feature["id"]
regulation_group_feature = save_regulation_group(regulation_group, plan_id)
save_regulation_group_association(regulation_group_feature["id"], PlanLayer.name, plan_id)
return plan_feature
def save_plan_feature(plan_feature: PlanFeature, plan_id: str | None = None) -> QgsFeature:
if not plan_feature.layer_name:
msg = "Cannot save plan feature without a target layer"
raise ValueError(msg)
layer_class = FEATURE_LAYER_NAME_TO_CLASS_MAP.get(plan_feature.layer_name)
if not layer_class:
msg = f"Could not find plan feature layer class for layer name {plan_feature.layer_name}"
raise ValueError(msg)
feature = layer_class.feature_from_model(plan_feature, plan_id)
layer = layer_class.get_from_project()
_save_feature(
feature=feature,
layer=layer,
id_=plan_feature.id_,
edit_text="Kaavakohteen lisäys" if plan_feature.id_ is None else "Kaavakohteen muokkaus",
)
# Handle regulation groups
if plan_feature.regulation_groups:
for group in plan_feature.regulation_groups:
regulation_group_feature = save_regulation_group(group)
save_regulation_group_association(regulation_group_feature["id"], plan_feature.layer_name, feature["id"])
return feature
def save_regulation_group(regulation_group: RegulationGroup, plan_id: str | None = None) -> QgsFeature:
feature = RegulationGroupLayer.feature_from_model(regulation_group, plan_id)
layer = RegulationGroupLayer.get_from_project()
_save_feature(
feature=feature,
layer=layer,
id_=regulation_group.id_,
edit_text="Kaavamääräysryhmän lisäys" if regulation_group.id_ is None else "Kaavamääräysryhmän muokkaus",
)
# Handle regulations
if regulation_group.regulations:
for regulation in regulation_group.regulations:
regulation.regulation_group_id_ = feature["id"] # Updating regulation group ID
save_regulation(regulation)
return feature
def save_regulation_group_as_config(regulation_group: RegulationGroup):
pass
def save_regulation_group_association(regulation_group_id: str, layer_name: str, feature_id: str) -> QgsFeature:
feature = RegulationGroupAssociationLayer.feature_from(regulation_group_id, layer_name, feature_id)
layer = RegulationGroupAssociationLayer.get_from_project()
_save_feature(feature=feature, layer=layer, id_=None, edit_text="Kaavamääräysryhmän assosiaation lisäys")
return feature
def save_regulation(regulation: Regulation) -> QgsFeature:
feature = PlanRegulationLayer.feature_from_model(regulation)
layer = PlanRegulationLayer.get_from_project()
_save_feature(
feature=feature,
layer=layer,
id_=regulation.id_,
edit_text="Kaavamääräyksen lisäys" if regulation.id_ is None else "Kaavamääräyksen muokkaus",
)
return feature