5
5
from numbers import Number
6
6
from string import Template
7
7
from textwrap import dedent
8
- from typing import Any , ClassVar
8
+ from typing import Any , ClassVar , Generator
9
9
10
10
from qgis .core import NULL , QgsExpressionContextUtils , QgsFeature , QgsProject , QgsVectorLayerUtils
11
11
from qgis .utils import iface
12
12
13
13
from arho_feature_template .core .models import Plan , PlanFeature , Regulation , RegulationGroup , RegulationLibrary
14
- from arho_feature_template .exceptions import FeatureNotFoundError , LayerEditableError
14
+ from arho_feature_template .exceptions import FeatureNotFoundError , LayerEditableError , LayerNotFoundError
15
15
from arho_feature_template .project .layers import AbstractLayer
16
16
from arho_feature_template .project .layers .code_layers import PlanRegulationTypeLayer
17
17
@@ -48,7 +48,7 @@ def feature_from_model(cls, model: Any) -> QgsFeature:
48
48
@classmethod
49
49
def initialize_feature_from_model (cls , model : Any ) -> QgsFeature :
50
50
if model .id_ is not None : # Expects all plan layer models to have 'id_' attribute
51
- feature = cls .get_feature_by_id (model .id_ )
51
+ feature = cls .get_feature_by_id (model .id_ , no_geometries = False )
52
52
if not feature :
53
53
raise FeatureNotFoundError (model .id_ , cls .name )
54
54
else :
@@ -82,6 +82,10 @@ def feature_from_model(cls, model: Plan) -> QgsFeature:
82
82
83
83
@classmethod
84
84
def model_from_feature (cls , feature : QgsFeature ) -> Plan :
85
+ general_regulation_features = [
86
+ RegulationGroupLayer .get_feature_by_id (group_id )
87
+ for group_id in RegulationGroupAssociationLayer .get_group_ids_for_feature (feature ["id" ], cls .name )
88
+ ]
85
89
return Plan (
86
90
geom = feature .geometry (),
87
91
name = feature ["name" ]["fin" ],
@@ -94,8 +98,9 @@ def model_from_feature(cls, feature: QgsFeature) -> Plan:
94
98
lifecycle_status_id = feature ["lifecycle_status_id" ],
95
99
organisation_id = feature ["organisation_id" ],
96
100
general_regulations = [
97
- RegulationGroupLayer .model_from_feature (RegulationGroupLayer .get_feature_by_id (group_id ))
98
- for group_id in RegulationGroupAssociationLayer .get_group_ids_for_feature (feature ["id" ], cls .name )
101
+ RegulationGroupLayer .model_from_feature (feat )
102
+ for feat in general_regulation_features
103
+ if feat is not None
99
104
],
100
105
id_ = feature ["id" ],
101
106
)
@@ -123,15 +128,18 @@ def feature_from_model(cls, model: PlanFeature, plan_id: str | None = None) -> Q
123
128
124
129
@classmethod
125
130
def model_from_feature (cls , feature : QgsFeature ) -> PlanFeature :
131
+ regulation_group_features = [
132
+ RegulationGroupLayer .get_feature_by_id (group_id )
133
+ for group_id in RegulationGroupAssociationLayer .get_group_ids_for_feature (feature ["id" ], cls .name )
134
+ ]
126
135
return PlanFeature (
127
136
geom = feature .geometry (),
128
137
type_of_underground_id = feature ["type_of_underground_id" ],
129
138
layer_name = cls .get_from_project ().name (),
130
139
name = feature ["name" ]["fin" ],
131
140
description = feature ["description" ]["fin" ],
132
141
regulation_groups = [
133
- RegulationGroupLayer .model_from_feature (RegulationGroupLayer .get_feature_by_id (group_id ))
134
- for group_id in RegulationGroupAssociationLayer .get_group_ids_for_feature (feature ["id" ], cls .name )
142
+ RegulationGroupLayer .model_from_feature (feat ) for feat in regulation_group_features if feat is not None
135
143
],
136
144
plan_id = feature ["plan_id" ],
137
145
id_ = feature ["id" ],
@@ -228,9 +236,12 @@ def feature_from(cls, regulation_group_id: str, layer_name: str, feature_id: str
228
236
attribute = cls .layer_name_to_attribute_map .get (layer_name )
229
237
230
238
# Check if association exists to avoid duplicate assocations
231
- for feature in layer . getFeatures ( ):
232
- if feature ["plan_regulation_group_id" ] == regulation_group_id and feature [ attribute ] == feature_id :
239
+ for feature in cls . get_features_by_attribute_value ( "plan_regulation_group_id" , regulation_group_id ):
240
+ if feature [attribute ] == feature_id :
233
241
return None
242
+ # for feature in layer.getFeatures():
243
+ # if feature["plan_regulation_group_id"] == regulation_group_id and feature[attribute] == feature_id:
244
+ # return None
234
245
235
246
feature = QgsVectorLayerUtils .createFeature (layer )
236
247
feature ["plan_regulation_group_id" ] = regulation_group_id
@@ -243,16 +254,18 @@ def feature_from(cls, regulation_group_id: str, layer_name: str, feature_id: str
243
254
return feature
244
255
245
256
@classmethod
246
- def get_associations_for_feature (cls , feature_id : str , layer_name : str ) -> list [QgsFeature ]:
257
+ def get_associations_for_feature (cls , feature_id : str , layer_name : str ) -> Generator [QgsFeature ]:
247
258
attribute = cls .layer_name_to_attribute_map .get (layer_name )
248
- return [feature for feature in cls .get_features () if feature [attribute ] == feature_id ]
259
+ if not attribute :
260
+ raise LayerNotFoundError (layer_name )
261
+ return cls .get_features_by_attribute_value (attribute , feature_id )
249
262
250
263
@classmethod
251
- def get_group_ids_for_feature (cls , feature_id : str , layer_name : str ) -> list [str ]:
264
+ def get_group_ids_for_feature (cls , feature_id : str , layer_name : str ) -> Generator [str ]:
252
265
attribute = cls .layer_name_to_attribute_map .get (layer_name )
253
- return [
254
- feature [ "plan_regulation_group_id" ] for feature in cls . get_features () if feature [ attribute ] == feature_id
255
- ]
266
+ if not attribute :
267
+ raise LayerNotFoundError ( layer_name )
268
+ return cls . get_attribute_values_by_another_attribute_value ( "plan_regulation_group_id" , attribute , feature_id )
256
269
257
270
@classmethod
258
271
def get_dangling_associations (
@@ -318,8 +331,8 @@ def model_from_feature(cls, feature: QgsFeature) -> Regulation:
318
331
)
319
332
320
333
@classmethod
321
- def regulations_with_group_id (cls , group_id : str ) -> list [QgsFeature ]:
322
- return [ feat for feat in cls .get_features () if feat [ "plan_regulation_group_id" ] == group_id ]
334
+ def regulations_with_group_id (cls , group_id : str ) -> Generator [QgsFeature ]:
335
+ return cls .get_features_by_attribute_value ( "plan_regulation_group_id" , group_id )
323
336
324
337
325
338
class PlanPropositionLayer (AbstractPlanLayer ):
0 commit comments