Skip to content

Commit 4eb96cd

Browse files
committedDec 4, 2024
Fix layer loading
Previously kaava_layer was loaded on plugin load when the layer usually is not yet present.
1 parent 17a49ad commit 4eb96cd

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed
 

‎arho_feature_template/core/plan_manager.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,52 @@ class PlanManager:
1818
def __init__(self):
1919
self.json_plan_path = None
2020
self.json_plan_outline_path = None
21-
self.kaava_layer = get_layer_by_name("Kaava")
2221

2322
def add_new_plan(self):
2423
"""Initiate the process to add a new plan to the Kaava layer."""
2524
if not handle_unsaved_changes():
2625
return
2726

27+
kaava_layer = get_layer_by_name("Kaava")
2828
self.clear_all_filters()
2929

30-
if not self.kaava_layer:
30+
if not kaava_layer:
3131
return
3232

33-
if not self.kaava_layer.isEditable():
34-
self.kaava_layer.startEditing()
33+
if not kaava_layer.isEditable():
34+
kaava_layer.startEditing()
3535

36-
iface.setActiveLayer(self.kaava_layer)
36+
iface.setActiveLayer(kaava_layer)
3737
iface.actionAddFeature().trigger()
3838

3939
# Connect the featureAdded signal to a callback method
40-
self.kaava_layer.featureAdded.connect(self.feature_added)
40+
kaava_layer.featureAdded.connect(self.feature_added)
4141

4242
def feature_added(self):
4343
"""Callback for when a new feature is added to the Kaava layer."""
44-
if not self.kaava_layer:
44+
45+
kaava_layer = get_layer_by_name("Kaava")
46+
if not kaava_layer:
4547
return
4648

4749
# Disconnect the signal to avoid repeated triggers
48-
self.kaava_layer.featureAdded.disconnect()
50+
kaava_layer.featureAdded.disconnect()
4951

50-
feature_ids_before_commit = self.kaava_layer.allFeatureIds()
52+
feature_ids_before_commit = kaava_layer.allFeatureIds()
5153

52-
if self.kaava_layer.isEditable():
53-
if not self.kaava_layer.commitChanges():
54+
if kaava_layer.isEditable():
55+
if not kaava_layer.commitChanges():
5456
iface.messageBar().pushMessage("Error", "Failed to commit changes to the layer.", level=3)
5557
return
5658
else:
5759
iface.messageBar().pushMessage("Error", "Layer is not editable.", level=3)
5860
return
5961

60-
feature_ids_after_commit = self.kaava_layer.allFeatureIds()
62+
feature_ids_after_commit = kaava_layer.allFeatureIds()
6163
new_feature_id = next((fid for fid in feature_ids_after_commit if fid not in feature_ids_before_commit), None)
6264

6365
if new_feature_id is not None:
64-
new_feature = self.kaava_layer.getFeature(new_feature_id)
66+
new_feature = kaava_layer.getFeature(new_feature_id)
6567
if new_feature.isValid():
6668
feature_id_value = new_feature["id"]
6769
update_selected_plan(LandUsePlan(feature_id_value))

0 commit comments

Comments
 (0)