Skip to content

Commit 009efe3

Browse files
committed
fix reading project layers too early
The fix is not very scalable, might need to address this more thoroughly in the future
1 parent 640a822 commit 009efe3

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

arho_feature_template/core/plan_manager.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ def __init__(self):
8181
self.feature_template_libraries = [
8282
FeatureTemplateLibrary.from_config_file(file) for file in feature_template_library_config_files()
8383
]
84-
self.regulation_group_libraries = [
85-
RegulationGroupLibrary.from_config_file(file) for file in regulation_group_library_config_files()
86-
]
84+
self.regulation_group_libraries = None
8785

8886
# Initialize new feature dock
8987
self.new_feature_dock = NewFeatureDock(self.feature_template_libraries)
@@ -107,6 +105,14 @@ def __init__(self):
107105
self.lambda_service = LambdaService()
108106
self.lambda_service.jsons_received.connect(self.save_plan_jsons)
109107

108+
def get_regulation_group_libraries(self):
109+
# Lazy initialization of regulation_group_libraries to avoid reading regulation code layer too early
110+
if self.regulation_group_libraries is None:
111+
self.regulation_group_libraries = [
112+
RegulationGroupLibrary.from_config_file(file) for file in regulation_group_library_config_files()
113+
]
114+
return self.regulation_group_libraries
115+
110116
def toggle_identify_plan_features(self, activate: bool): # noqa: FBT001
111117
if activate:
112118
self.previous_map_tool = iface.mapCanvas().mapTool()
@@ -171,7 +177,7 @@ def edit_plan(self):
171177
return
172178
plan_model = PlanLayer.model_from_feature(feature)
173179

174-
attribute_form = PlanAttributeForm(plan_model, self.regulation_group_libraries)
180+
attribute_form = PlanAttributeForm(plan_model, self.get_regulation_group_libraries())
175181
if attribute_form.exec_():
176182
feature = save_plan(attribute_form.model)
177183

@@ -202,7 +208,7 @@ def _plan_geom_digitized(self, feature: QgsFeature):
202208
return
203209

204210
plan_model = Plan(geom=feature.geometry())
205-
attribute_form = PlanAttributeForm(plan_model, self.regulation_group_libraries)
211+
attribute_form = PlanAttributeForm(plan_model, self.get_regulation_group_libraries())
206212
if attribute_form.exec_():
207213
feature = save_plan(attribute_form.model)
208214
plan_to_be_activated = feature["id"]
@@ -229,7 +235,7 @@ def _plan_feature_geom_digitized(self, feature: QgsFeature):
229235

230236
plan_feature.geom = feature.geometry()
231237
attribute_form = PlanFeatureForm(
232-
plan_feature, title, [*self.regulation_group_libraries, regulation_group_library_from_active_plan()]
238+
plan_feature, title, [*self.get_regulation_group_libraries(), regulation_group_library_from_active_plan()]
233239
)
234240
if attribute_form.exec_():
235241
save_plan_feature(attribute_form.model)
@@ -241,7 +247,7 @@ def edit_plan_feature(self, feature: QgsFeature, layer_name: str):
241247
# Geom editing handled with basic QGIS vertex editing?
242248
title = plan_feature.name if plan_feature.name else layer_name
243249
attribute_form = PlanFeatureForm(
244-
plan_feature, title, [*self.regulation_group_libraries, regulation_group_library_from_active_plan()]
250+
plan_feature, title, [*self.get_regulation_group_libraries(), regulation_group_library_from_active_plan()]
245251
)
246252
if attribute_form.exec_():
247253
save_plan_feature(attribute_form.model)

arho_feature_template/gui/tools/inspect_plan_features_tool.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, canvas: QgsMapCanvas, layer_classes: list[PlanFeatureLayer]):
2323
self.canvas = canvas
2424

2525
self.layer_classes = layer_classes
26-
self.layers = [cls.get_from_project() for cls in layer_classes]
26+
self.layers: list[QgsVectorLayer] | None = None
2727

2828
self.highlighted: tuple[QgsFeature, str] | None = None
2929
self.highlight_rubber_band: QgsRubberBand | None = None
@@ -70,6 +70,11 @@ def query_nearby_features(self, point: QgsPointXY) -> dict[QgsVectorLayer, list[
7070
request = QgsFeatureRequest()
7171
request.setFilterRect(tolerance_geom)
7272
request.setFlags(QgsFeatureRequest.Flag.ExactIntersect)
73+
74+
# Lazy reading of feature layers to avoid reading them too early
75+
if self.layers is None:
76+
self.layers = [cls.get_from_project() for cls in self.layer_classes]
77+
7378
with OverrideCursor(Qt.WaitCursor):
7479
for layer in self.layers:
7580
features = list(layer.getFeatures(request))

0 commit comments

Comments
 (0)