|
2 | 2 |
|
3 | 3 | import json
|
4 | 4 | import logging
|
5 |
| - |
6 |
| -from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +from qgis.core import ( |
| 8 | + QgsExpressionContextUtils, |
| 9 | + QgsProject, |
| 10 | + QgsVectorLayer, |
| 11 | +) |
| 12 | +from qgis.gui import QgsMapToolDigitizeFeature |
7 | 13 | from qgis.PyQt.QtWidgets import QDialog, QMessageBox
|
8 | 14 | from qgis.utils import iface
|
9 | 15 |
|
10 | 16 | from arho_feature_template.core.lambda_service import LambdaService
|
11 | 17 | from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
|
| 18 | +from arho_feature_template.gui.plan_attribure_form import PlanAttributeForm |
12 | 19 | from arho_feature_template.gui.serialize_plan import SerializePlan
|
13 |
| -from arho_feature_template.project.layers.plan_layers import PlanLayer, plan_layers |
| 20 | +from arho_feature_template.project.layers.plan_layers import PlanLayer, RegulationGroupLayer, plan_layers |
14 | 21 | from arho_feature_template.utils.db_utils import get_existing_database_connection_names
|
15 |
| -from arho_feature_template.utils.misc_utils import get_active_plan_id, handle_unsaved_changes |
| 22 | +from arho_feature_template.utils.misc_utils import ( |
| 23 | + get_active_plan_id, |
| 24 | + handle_unsaved_changes, |
| 25 | +) |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + from qgis.core import QgsFeature |
16 | 29 |
|
| 30 | + from arho_feature_template.core.models import Plan, RegulationGroup |
17 | 31 | logger = logging.getLogger(__name__)
|
18 | 32 |
|
19 | 33 |
|
| 34 | +class PlanDigitizeMapTool(QgsMapToolDigitizeFeature): ... |
| 35 | + |
| 36 | + |
20 | 37 | class PlanManager:
|
21 | 38 | def __init__(self):
|
22 | 39 | self.json_plan_path = None
|
23 | 40 | self.json_plan_outline_path = None
|
24 | 41 |
|
| 42 | + self.digitize_map_tool = PlanDigitizeMapTool(iface.mapCanvas(), iface.cadDockWidget()) |
| 43 | + self.digitize_map_tool.digitizingCompleted.connect(self._plan_geom_digitized) |
| 44 | + |
25 | 45 | def add_new_plan(self):
|
26 | 46 | """Initiate the process to add a new plan to the Kaava layer."""
|
| 47 | + |
| 48 | + self.previous_map_tool = iface.mapCanvas().mapTool() |
| 49 | + self.previous_active_plan_id = get_active_plan_id() |
| 50 | + |
27 | 51 | if not handle_unsaved_changes():
|
28 | 52 | return
|
29 | 53 |
|
30 | 54 | plan_layer = PlanLayer.get_from_project()
|
31 |
| - |
32 | 55 | if not plan_layer:
|
33 | 56 | return
|
| 57 | + |
34 | 58 | self.set_active_plan(None)
|
35 | 59 |
|
| 60 | + self.previously_editable = plan_layer.isEditable() |
36 | 61 | if not plan_layer.isEditable():
|
37 | 62 | plan_layer.startEditing()
|
38 | 63 |
|
39 |
| - iface.setActiveLayer(plan_layer) |
40 |
| - iface.actionAddFeature().trigger() |
41 |
| - |
42 |
| - # Connect the featureAdded signal to a callback method |
43 |
| - plan_layer.featureAdded.connect(self._feature_added) |
| 64 | + self.digitize_map_tool.setLayer(plan_layer) |
| 65 | + iface.mapCanvas().setMapTool(self.digitize_map_tool) |
44 | 66 |
|
45 |
| - def _feature_added(self): |
| 67 | + def _plan_geom_digitized(self, feature: QgsFeature): |
46 | 68 | """Callback for when a new feature is added to the Kaava layer."""
|
47 | 69 | plan_layer = PlanLayer.get_from_project()
|
48 | 70 | if not plan_layer:
|
49 | 71 | return
|
50 | 72 |
|
51 |
| - # Disconnect the signal to avoid repeated triggers |
52 |
| - plan_layer.featureAdded.disconnect(self._feature_added) |
53 |
| - |
54 |
| - feature_ids_before_commit = plan_layer.allFeatureIds() |
55 |
| - |
56 |
| - if plan_layer.isEditable(): |
57 |
| - if not plan_layer.commitChanges(): |
58 |
| - iface.messageBar().pushMessage("Error", "Failed to commit changes to the layer.", level=3) |
59 |
| - return |
60 |
| - else: |
61 |
| - iface.messageBar().pushMessage("Error", "Layer is not editable.", level=3) |
62 |
| - return |
| 73 | + attribute_form = PlanAttributeForm() |
| 74 | + if attribute_form.exec_(): |
| 75 | + plan_attributes = attribute_form.get_plan_attributes() |
| 76 | + plan_attributes.geom = feature.geometry() |
| 77 | + feature = save_plan(plan_attributes) |
| 78 | + plan_layer = PlanLayer.get_from_project() |
| 79 | + if plan_layer.isEditable(): |
| 80 | + plan_layer.commitChanges() |
| 81 | + self.set_active_plan(feature["id"]) |
| 82 | + if self.previously_editable: |
| 83 | + plan_layer.startEditing() |
63 | 84 |
|
64 |
| - feature_ids_after_commit = plan_layer.allFeatureIds() |
65 |
| - new_feature_id = next( |
66 |
| - (fid for fid in feature_ids_after_commit if fid not in feature_ids_before_commit), |
67 |
| - None, |
68 |
| - ) |
69 |
| - |
70 |
| - if new_feature_id is not None: |
71 |
| - new_feature = plan_layer.getFeature(new_feature_id) |
72 |
| - if new_feature.isValid(): |
73 |
| - feature_id_value = new_feature["id"] |
74 |
| - self.set_active_plan(feature_id_value) |
75 |
| - else: |
76 |
| - iface.messageBar().pushMessage("Error", "Invalid feature retrieved.", level=3) |
77 |
| - else: |
78 |
| - iface.messageBar().pushMessage("Error", "No new feature was added.", level=3) |
| 85 | + iface.mapCanvas().setMapTool(self.previous_map_tool) |
79 | 86 |
|
80 | 87 | def set_active_plan(self, plan_id: str | None):
|
81 | 88 | """Update the project layers based on the selected land use plan."""
|
@@ -147,4 +154,47 @@ def save_plan_jsons(self, plan_json, outline_json):
|
147 | 154 | with open(self.json_plan_outline_path, "w", encoding="utf-8") as outline_file:
|
148 | 155 | json.dump(outline_json, outline_file, ensure_ascii=False, indent=2)
|
149 | 156 |
|
150 |
| - QMessageBox.information(None, "Tallennus onnistui", "Kaava ja sen ulkoraja tallennettu onnistuneesti.") |
| 157 | + QMessageBox.information( |
| 158 | + None, |
| 159 | + "Tallennus onnistui", |
| 160 | + "Kaava ja sen ulkoraja tallennettu onnistuneesti.", |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +def save_plan(plan_data: Plan) -> QgsFeature: |
| 165 | + plan_layer = PlanLayer.get_from_project() |
| 166 | + in_edit_mode = plan_layer.isEditable() |
| 167 | + if not in_edit_mode: |
| 168 | + plan_layer.startEditing() |
| 169 | + |
| 170 | + edit_message = "Kaavan lisäys" if plan_data.id_ is None else "Kaavan muokkaus" |
| 171 | + plan_layer.beginEditCommand(edit_message) |
| 172 | + |
| 173 | + plan_data.organisation_id = "99e20d66-9730-4110-815f-5947d3f8abd3" |
| 174 | + plan_feature = PlanLayer.feature_from_model(plan_data) |
| 175 | + |
| 176 | + if plan_data.id_ is None: |
| 177 | + plan_layer.addFeature(plan_feature) |
| 178 | + else: |
| 179 | + plan_layer.updateFeature(plan_feature) |
| 180 | + |
| 181 | + plan_layer.endEditCommand() |
| 182 | + plan_layer.commitChanges(stopEditing=False) |
| 183 | + |
| 184 | + if plan_data.general_regulations: |
| 185 | + for regulation_group in plan_data.general_regulations: |
| 186 | + plan_id = plan_feature["id"] |
| 187 | + regulation_group_feature = save_regulation_group(regulation_group, plan_id) |
| 188 | + save_regulation_grop_assosiation(plan_id, regulation_group_feature["id"]) |
| 189 | + |
| 190 | + return plan_feature |
| 191 | + |
| 192 | + |
| 193 | +def save_regulation_group(regulation_group: RegulationGroup, plan_id: str) -> QgsFeature: |
| 194 | + feature = RegulationGroupLayer.feature_from_model(regulation_group) |
| 195 | + feature["plan_id"] = plan_id |
| 196 | + return feature |
| 197 | + |
| 198 | + |
| 199 | +def save_regulation_grop_assosiation(plan_id: str, regulation_group_id: str): |
| 200 | + pass |
0 commit comments