@@ -45,7 +45,14 @@ def __init__(self, iface):
4545 self .app = AppInterface ()
4646 self .geometry = GeometryUtils ()
4747
48- def build_boundary (self , db ):
48+ def build_boundaries (self , db , skip_selection = False ):
49+ """
50+ Builds the boundaries correctly and update boundary layer
51+
52+ :param db: db connection instance
53+ :param skip_selection: Boolean True if we omit the boundaries selected by the user, False if we validate the user's selection.
54+ :return:
55+ """
4956 QgsProject .instance ().setAutoTransaction (False )
5057 use_selection = True
5158
@@ -58,6 +65,9 @@ def build_boundary(self, db):
5865 }
5966 self .app .core .get_layers (db , layers , load = True )
6067
68+ if skip_selection :
69+ layers [db .names .LC_BOUNDARY_T ].selectAll ()
70+
6171 if layers [db .names .LC_BOUNDARY_T ].selectedFeatureCount () == 0 :
6272 reply = QMessageBox .question (None ,
6373 QCoreApplication .translate ("ToolBar" , "Continue?" ),
@@ -72,12 +82,16 @@ def build_boundary(self, db):
7282
7383 boundary_t_ids = list ()
7484 if use_selection :
85+ copy_boundary_layer = processing .run ("native:saveselectedfeatures" ,{'INPUT' : layers [db .names .LC_BOUNDARY_T ], 'OUTPUT' : 'memory:' })['OUTPUT' ]
7586 boundary_t_ids = QgsVectorLayerUtils .getValues (layers [db .names .LC_BOUNDARY_T ], db .names .T_ID_F , selectedOnly = True )[0 ]
76- num_boundaries = layers [db .names .LC_BOUNDARY_T ].selectedFeatureCount ()
7787 else :
88+ copy_boundary_layer = self .app .core .get_layer_copy (layers [db .names .LC_BOUNDARY_T ])
7889 boundary_t_ids = QgsVectorLayerUtils .getValues (layers [db .names .LC_BOUNDARY_T ], db .names .T_ID_F )[0 ]
7990 layers [db .names .LC_BOUNDARY_T ].selectAll ()
80- num_boundaries = layers [db .names .LC_BOUNDARY_T ].featureCount ()
91+
92+ boundaries_count = layers [db .names .LC_BOUNDARY_T ].featureCount ()
93+ selected_boundaries_count = layers [db .names .LC_BOUNDARY_T ].selectedFeatureCount ()
94+ boundary_t_ili_tids = QgsVectorLayerUtils .getValues (layers [db .names .LC_BOUNDARY_T ], db .names .T_ILI_TID_F )[0 ]
8195
8296 if boundary_t_ids :
8397 boundary_topology_relation = {
@@ -113,15 +127,41 @@ def build_boundary(self, db):
113127 selected_boundaries_layer = processing .run ("native:saveselectedfeatures" , {'INPUT' : layers [db .names .LC_BOUNDARY_T ], 'OUTPUT' : 'TEMPORARY_OUTPUT' })['OUTPUT' ]
114128 build_boundaries_layer = self .geometry .build_boundaries (selected_boundaries_layer )
115129
116- with edit (layers [db .names .LC_BOUNDARY_T ]):
117- # Delete selected features as they will be imported again from a newly created layer after processed
118- layers [db .names .LC_BOUNDARY_T ].deleteSelectedFeatures ()
130+ build_boundaries_count = build_boundaries_layer .featureCount ()
131+ expected_boundaries_count = boundaries_count - selected_boundaries_count + build_boundaries_count
132+
133+ # Build boundaries should have generated at least one boundary.
134+ if build_boundaries_count > 0 :
135+ with edit (layers [db .names .LC_BOUNDARY_T ]):
136+ # Delete selected features as they will be imported again from a newly created layer after processed
137+ layers [db .names .LC_BOUNDARY_T ].deleteSelectedFeatures ()
138+
139+ # Bring back the features we deleted before, but this time, with the boundaries fixed
140+ self .app .core .run_etl_model_in_backgroud_mode (db , build_boundaries_layer , db .names .LC_BOUNDARY_T )
141+
142+ # check if features were inserted successfully
143+ if layers [db .names .LC_BOUNDARY_T ].featureCount () == expected_boundaries_count :
144+ self .logger .info_msg (__name__ , QCoreApplication .translate ("ToolBar" ,
145+ "{} feature(s) was(were) analyzed generating {} boundary(ies)!" ).format (selected_boundaries_count , build_boundaries_layer .featureCount ()))
119146
120- # Bring back the features we deleted before, but this time, with the boundaries fixed
121- self .app .core .run_etl_model_in_backgroud_mode (db , build_boundaries_layer , db .names .LC_BOUNDARY_T , False )
147+ else :
148+ if layers [db .names .LC_BOUNDARY_T ].featureCount () != boundaries_count - selected_boundaries_count :
149+ # Clean layer because wrong data could have been inserted previously
150+ expr = "{} NOT IN ('{}')" .format (db .names .T_ILI_TID_F , "','" .join ([t_ili_tid for t_ili_tid in boundary_t_ili_tids ]))
151+ layers [db .names .LC_BOUNDARY_T ].selectByExpression (expr )
152+
153+ with edit (layers [db .names .LC_BOUNDARY_T ]):
154+ layers [db .names .LC_BOUNDARY_T ].deleteSelectedFeatures ()
155+
156+ # the previously deleted boundaries are restored because an error occurred when trying to insert the building boundaries
157+ self .app .core .run_etl_model_in_backgroud_mode (db , copy_boundary_layer , db .names .LC_BOUNDARY_T )
158+
159+ self .logger .warning_msg (__name__ , QCoreApplication .translate ("ToolBar" ,
160+ "An error occurred when trying to build the boundary(ies). No changes are made!" ))
161+ else :
162+ self .logger .warning_msg (__name__ , QCoreApplication .translate ("ToolBar" ,
163+ "An error occurred when trying to build the boundary(ies). No changes are made!" ))
122164
123- self .logger .info_msg (__name__ , QCoreApplication .translate ("ToolBar" ,
124- "{} feature(s) was(were) analyzed generating {} boundary(ies)!" ).format (num_boundaries , build_boundaries_layer .featureCount ()))
125165 self .iface .mapCanvas ().refresh ()
126166
127167 # topology tables are recalculated with the new boundaries
0 commit comments