|
18 | 18 | from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
|
19 | 19 | from arho_feature_template.gui.plan_attribure_form import PlanAttributeForm
|
20 | 20 | from arho_feature_template.gui.serialize_plan import SerializePlan
|
21 |
| -from arho_feature_template.project.layers.plan_layers import PlanLayer, RegulationGroupLayer, plan_layers |
| 21 | +from arho_feature_template.project.layers.plan_layers import ( |
| 22 | + LandUseAreaLayer, |
| 23 | + LandUsePointLayer, |
| 24 | + LineLayer, |
| 25 | + OtherAreaLayer, |
| 26 | + OtherPointLayer, |
| 27 | + PlanFeatureLayer, |
| 28 | + PlanLayer, |
| 29 | + PlanRegulationLayer, |
| 30 | + RegulationGroupLayer, |
| 31 | + plan_layers, |
| 32 | +) |
22 | 33 | from arho_feature_template.utils.db_utils import get_existing_database_connection_names
|
23 | 34 | from arho_feature_template.utils.misc_utils import (
|
24 | 35 | check_layer_changes,
|
|
29 | 40 | if TYPE_CHECKING:
|
30 | 41 | from qgis.core import QgsFeature
|
31 | 42 |
|
32 |
| - from arho_feature_template.core.models import Plan, RegulationGroup |
| 43 | + from arho_feature_template.core.models import Plan, PlanFeature, Regulation, RegulationGroup |
| 44 | + |
33 | 45 | logger = logging.getLogger(__name__)
|
34 | 46 |
|
35 | 47 |
|
@@ -179,6 +191,20 @@ def save_plan_jsons(self, plan_json, outline_json):
|
179 | 191 | )
|
180 | 192 |
|
181 | 193 |
|
| 194 | +def _save_feature(feature: QgsFeature, layer: QgsVectorLayer, id_: int | None, edit_text: str = ""): |
| 195 | + if not layer.isEditable(): |
| 196 | + layer.startEditing() |
| 197 | + layer.beginEditCommand(edit_text) |
| 198 | + |
| 199 | + if id_ is None: |
| 200 | + layer.addFeature(feature) |
| 201 | + else: |
| 202 | + layer.updateFeature(feature) |
| 203 | + |
| 204 | + layer.endEditCommand() |
| 205 | + layer.commitChanges(stopEditing=False) |
| 206 | + |
| 207 | + |
182 | 208 | def save_plan(plan_data: Plan) -> QgsFeature:
|
183 | 209 | plan_layer = PlanLayer.get_from_project()
|
184 | 210 | in_edit_mode = plan_layer.isEditable()
|
@@ -208,11 +234,76 @@ def save_plan(plan_data: Plan) -> QgsFeature:
|
208 | 234 | return plan_feature
|
209 | 235 |
|
210 | 236 |
|
211 |
| -def save_regulation_group(regulation_group: RegulationGroup, plan_id: str) -> QgsFeature: |
212 |
| - feature = RegulationGroupLayer.feature_from_model(regulation_group) |
213 |
| - feature["plan_id"] = plan_id |
214 |
| - return feature |
| 237 | +def save_plan_feature(plan_feature: PlanFeature, plan_id: str | None = None): |
| 238 | + layer_name_to_class_map: dict[str, type[PlanFeatureLayer]] = { |
| 239 | + LandUsePointLayer.name: LandUsePointLayer, |
| 240 | + OtherAreaLayer.name: OtherAreaLayer, |
| 241 | + OtherPointLayer.name: OtherPointLayer, |
| 242 | + LandUseAreaLayer.name: LandUseAreaLayer, |
| 243 | + LineLayer.name: LineLayer, |
| 244 | + } |
| 245 | + |
| 246 | + if not plan_feature.layer_name: |
| 247 | + msg = "Cannot save plan feature without a target layer" |
| 248 | + raise ValueError(msg) |
| 249 | + layer_class = layer_name_to_class_map.get(plan_feature.layer_name) |
| 250 | + if not layer_class: |
| 251 | + msg = f"Could not find plan feature layer class for layer name {plan_feature.layer_name}" |
| 252 | + raise ValueError(msg) |
| 253 | + feature = layer_class.feature_from_model(plan_feature) |
| 254 | + layer = layer_class.get_from_project() |
| 255 | + |
| 256 | + if plan_id: |
| 257 | + feature["plan_id"] = plan_id |
| 258 | + |
| 259 | + _save_feature( |
| 260 | + feature=feature, |
| 261 | + layer=layer, |
| 262 | + id_=plan_feature.id_, |
| 263 | + edit_text="Kaavakohteen lisäys" if plan_feature.id_ is None else "Kaavakohteen muokkaus", |
| 264 | + ) |
| 265 | + |
| 266 | + # Handle regulation groups |
| 267 | + if plan_feature.regulation_groups: |
| 268 | + for group in plan_feature.regulation_groups: |
| 269 | + save_regulation_group(group) |
| 270 | + |
| 271 | + |
| 272 | +# def save_regulation_group(regulation_group: RegulationGroup, plan_id: str) -> QgsFeature: |
| 273 | +# feature = RegulationGroupLayer.feature_from_model(regulation_group) |
| 274 | +# feature["plan_id"] = plan_id |
| 275 | +# return feature |
| 276 | + |
| 277 | + |
| 278 | +def save_regulation_group(regulation_group: RegulationGroup, plan_id: str | None = None): |
| 279 | + feature = RegulationGroupLayer.feature_from_model(regulation_group, plan_id) |
| 280 | + layer = RegulationGroupLayer.get_from_project() |
| 281 | + |
| 282 | + _save_feature( |
| 283 | + feature=feature, |
| 284 | + layer=layer, |
| 285 | + id_=regulation_group.id_, |
| 286 | + edit_text="Kaavamääräysryhmän lisäys" if regulation_group.id_ is None else "Kaavamääräysryhmän muokkaus", |
| 287 | + ) |
| 288 | + |
| 289 | + # Handle regulations |
| 290 | + if regulation_group.regulations: |
| 291 | + for regulation in regulation_group.regulations: |
| 292 | + regulation.regulation_group_id_ = feature["id"] # Updating regulation group ID |
| 293 | + save_regulation(regulation) |
215 | 294 |
|
216 | 295 |
|
217 | 296 | def save_regulation_grop_assosiation(plan_id: str, regulation_group_id: str):
|
218 | 297 | pass
|
| 298 | + |
| 299 | + |
| 300 | +def save_regulation(regulation: Regulation): |
| 301 | + feature = PlanRegulationLayer.feature_from_model(regulation) |
| 302 | + layer = PlanRegulationLayer.get_from_project() |
| 303 | + |
| 304 | + _save_feature( |
| 305 | + feature=feature, |
| 306 | + layer=layer, |
| 307 | + id_=regulation.id_, |
| 308 | + edit_text="Kaavamääräyksen lisäys" if regulation.id_ is None else "Kaavamääräyksen muokkaus", |
| 309 | + ) |
0 commit comments