|
42 | 42 | from qtalsim_soil_dialog import SoilPreprocessingDialog |
43 | 43 | from qtalsim_landuse_dialog import LanduseAssignmentDialog |
44 | 44 | import os.path |
45 | | -from qgis.core import QgsProject, QgsField, QgsVectorLayer, QgsRasterLayer, QgsFeature, QgsGeometry, QgsSpatialIndex, Qgis, QgsMessageLog, QgsLayerTreeGroup, QgsLayerTreeLayer, QgsProcessingFeedback, QgsWkbTypes, QgsFeatureRequest, QgsMapLayer, QgsFields, QgsTask, QgsTaskManager, QgsApplication, QgsExpression |
| 45 | +from qgis.core import QgsProject, QgsField, QgsVectorLayer, QgsRasterLayer, QgsFeature, QgsGeometry, QgsSpatialIndex, Qgis, QgsMessageLog, QgsLayerTreeGroup, QgsLayerTreeLayer, QgsProcessingFeedback, QgsWkbTypes, QgsFeatureRequest, QgsMapLayer, QgsFields, QgsMapLayerProxyModel |
46 | 46 | from qgis.analysis import QgsGeometrySnapper |
47 | 47 | import processing |
48 | 48 | import pandas as pd |
@@ -386,40 +386,37 @@ def fillPolygonsCombobox(self): |
386 | 386 | ''' |
387 | 387 | #Get Layers |
388 | 388 | layers, rasterLayers = self.getAllLayers(QgsProject.instance().layerTreeRoot()) |
389 | | - current_text = self.dlg.comboboxEZGLayer.currentText() |
390 | | - self.dlg.comboboxEZGLayer.clear() #clear combobox EZG from previous runs |
391 | | - self.dlg.comboboxEZGLayer.addItem(self.noLayerSelected) |
392 | | - self.dlg.comboboxEZGLayer.addItems([layer.name() for layer in layers]) |
393 | | - index = self.dlg.comboboxEZGLayer.findText(current_text) |
394 | | - if index != -1: |
395 | | - self.dlg.comboboxEZGLayer.setCurrentIndex(index) |
396 | 389 |
|
397 | | - self.safeConnect(self.dlg.comboboxEZGLayer.currentIndexChanged, self.on_ezg_changed) |
| 390 | + # Sub-basin layer |
| 391 | + self.dlg.comboboxSubBasinLayer.setFilters( |
| 392 | + QgsMapLayerProxyModel.PolygonLayer |
| 393 | + ) |
| 394 | + self.dlg.comboboxSubBasinLayer.layerChanged.connect(self.on_ezg_changed) |
398 | 395 |
|
399 | 396 | #Soil Layer |
400 | | - current_text = self.dlg.comboboxSoilLayer.currentText() |
401 | | - self.dlg.comboboxSoilLayer.clear() #clear combobox soil from previous runs |
402 | | - self.dlg.comboboxSoilLayer.addItems([layer.name() for layer in layers]) |
403 | | - index = self.dlg.comboboxSoilLayer.findText(current_text) |
404 | | - if index != -1: |
405 | | - self.dlg.comboboxSoilLayer.setCurrentIndex(index) |
| 397 | + self.dlg.comboboxSoilLayer.setFilters( |
| 398 | + QgsMapLayerProxyModel.PolygonLayer |
| 399 | + ) |
406 | 400 |
|
407 | 401 | #Land use layer |
408 | | - current_text = self.dlg.comboboxLanduseLayer.currentText() |
409 | | - self.dlg.comboboxLanduseLayer.clear() #clear combobox Landuse from previous runs |
410 | | - self.dlg.comboboxLanduseLayer.addItems([layer.name() for layer in layers]) |
411 | | - index = self.dlg.comboboxLanduseLayer.findText(current_text) |
412 | | - if index != -1: |
413 | | - self.dlg.comboboxLanduseLayer.setCurrentIndex(index) |
| 402 | + self.dlg.comboboxLanduseLayer.setFilters( |
| 403 | + QgsMapLayerProxyModel.PolygonLayer |
| 404 | + ) |
| 405 | + |
414 | 406 |
|
415 | 407 | #DEM Layer |
416 | | - current_text = self.dlg.comboboxDEMLayer.currentText() |
417 | | - self.dlg.comboboxDEMLayer.clear() #clear combobox Landuse from previous runs |
418 | | - self.dlg.comboboxDEMLayer.addItem("Optional: Upload DEM Layer") |
419 | | - self.dlg.comboboxDEMLayer.addItems([layer.name() for layer in rasterLayers]) |
420 | | - index = self.dlg.comboboxDEMLayer.findText(current_text) |
421 | | - if index != -1: |
422 | | - self.dlg.comboboxDEMLayer.setCurrentIndex(index) |
| 408 | + self.dlg.comboboxDEMLayer.setAllowEmptyLayer(True) |
| 409 | + self.dlg.comboboxDEMLayer.setPlaceholderText("Optional: Upload DEM Layer") |
| 410 | + self.dlg.comboboxDEMLayer.setFilters( |
| 411 | + QgsMapLayerProxyModel.RasterLayer |
| 412 | + ) |
| 413 | + # current_text = self.dlg.comboboxDEMLayer.currentText() |
| 414 | + # self.dlg.comboboxDEMLayer.clear() #clear combobox Landuse from previous runs |
| 415 | + # self.dlg.comboboxDEMLayer.addItem("Optional: Upload DEM Layer") |
| 416 | + # self.dlg.comboboxDEMLayer.addItems([layer.name() for layer in rasterLayers]) |
| 417 | + # index = self.dlg.comboboxDEMLayer.findText(current_text) |
| 418 | + # if index != -1: |
| 419 | + # self.dlg.comboboxDEMLayer.setCurrentIndex(index) |
423 | 420 |
|
424 | 421 | def update_layer_name(self, layer_name, function): |
425 | 422 | ''' |
@@ -1083,31 +1080,23 @@ def on_ezg_changed(self): |
1083 | 1080 | If the user changes the sub-basin's layer, the field comboboxes to select UI and slope field are updated. |
1084 | 1081 | ''' |
1085 | 1082 | try: |
1086 | | - current_text = self.dlg.comboboxEZGLayer.currentText() |
1087 | | - |
| 1083 | + self.ezgLayerCombobox = self.dlg.comboboxSubBasinLayer.currentLayer() |
1088 | 1084 | current_text_comboboxui = self.dlg.comboboxUICatchment.currentText() #if the current field name exists in new layer - leave this field |
1089 | 1085 | index_ui = -1 |
1090 | | - |
1091 | | - layers = QgsProject.instance().mapLayersByName(current_text) |
1092 | | - |
1093 | | - if not layers: #if the layer does not exist |
1094 | | - return |
1095 | | - |
1096 | | - if current_text is not None and current_text != self.noLayerSelected: |
1097 | | - index = self.dlg.comboboxEZGLayer.findText(current_text) |
1098 | | - if index == -1: |
1099 | | - return |
1100 | | - self.dlg.comboboxEZGLayer.setCurrentIndex(index) |
1101 | | - self.ezgLayerCombobox = QgsProject.instance().mapLayersByName(current_text)[0] |
1102 | | - |
1103 | | - self.dlg.comboboxUICatchment.clear() #clear combobox EZG from previous runs |
1104 | | - self.dlg.comboboxUICatchment.addItems([field.name() for field in self.ezgLayerCombobox.fields()]) |
1105 | | - if current_text_comboboxui is not None: |
1106 | | - index_ui = self.dlg.comboboxUICatchment.findText(current_text_comboboxui) |
1107 | | - if index_ui != -1: |
1108 | | - self.dlg.comboboxUICatchment.setCurrentIndex(index_ui) |
| 1086 | + |
| 1087 | + self.dlg.comboboxUICatchment.clear() |
1109 | 1088 |
|
1110 | | - except: |
| 1089 | + # Add new fields |
| 1090 | + self.dlg.comboboxUICatchment.addItems( |
| 1091 | + [field.name() for field in self.ezgLayerCombobox.fields()] |
| 1092 | + ) |
| 1093 | + # Restore previously selected field if it still exists |
| 1094 | + index_ui = self.dlg.comboboxUICatchment.findText(current_text_comboboxui) |
| 1095 | + if index_ui != -1: |
| 1096 | + self.dlg.comboboxUICatchment.setCurrentIndex(index_ui) |
| 1097 | + |
| 1098 | + except Exception as e: |
| 1099 | + self.log_to_qtalsim_tab(f"Error updating sub-basin layer fields: {e}", Qgis.Critical) |
1111 | 1100 | return |
1112 | 1101 |
|
1113 | 1102 | def on_input_db_changed(self): |
@@ -1246,18 +1235,18 @@ def selectSoil(self): |
1246 | 1235 | #self.dlg.progressbar.setValue(0) |
1247 | 1236 | self.log_to_qtalsim_tab(f"Starting the clipping process of the Soil Layer.", Qgis.Info) |
1248 | 1237 | if self.clippingEZG is None: |
1249 | | - selected_layer_soil = None |
| 1238 | + self.soilLayer = None |
1250 | 1239 | raise Exception("User has not selected a sub-basins layer.") |
1251 | 1240 |
|
1252 | 1241 | #Select Layer |
1253 | | - selected_layer_soil = self.dlg.comboboxSoilLayer.currentText() |
1254 | | - self.soilLayer = QgsProject.instance().mapLayersByName(selected_layer_soil)[0] |
1255 | | - |
| 1242 | + self.soilLayer = None |
| 1243 | + self.soilLayer = self.dlg.comboboxSoilLayer.currentLayer() |
| 1244 | + |
1256 | 1245 | #Create field with the feature-id |
1257 | 1246 | self.soilFieldInputID = 'fid_qta' |
1258 | 1247 | existing_field_names = [field.name() for field in self.soilLayer.fields()] |
1259 | 1248 | if self.soilFieldInputID in existing_field_names: |
1260 | | - self.log_to_qtalsim_tab(f"Please rename field {self.soilFieldInputID} of layer {selected_layer_soil} or delete the field.", Qgis.Critical) |
| 1249 | + self.log_to_qtalsim_tab(f"Please rename field {self.soilFieldInputID} of layer {self.soilLayer.name()} or delete the field.", Qgis.Critical) |
1261 | 1250 | return |
1262 | 1251 | #self.dlg.progressbar.setValue(10) |
1263 | 1252 | self.log_to_qtalsim_tab(f"Progress: 10.00% done", Qgis.Info) |
@@ -1299,9 +1288,9 @@ def selectSoil(self): |
1299 | 1288 | #self.dlg.progressbar.setValue(0) |
1300 | 1289 |
|
1301 | 1290 | finally: |
1302 | | - if selected_layer_soil: |
| 1291 | + if self.soilLayer: |
1303 | 1292 | #Remove the created soil-ID-field from the input layer |
1304 | | - layer = QgsProject.instance().mapLayersByName(selected_layer_soil)[0] |
| 1293 | + layer = self.soilLayer |
1305 | 1294 | layer.startEditing() |
1306 | 1295 | field_index = layer.fields().indexFromName(self.soilFieldInputID) |
1307 | 1296 |
|
@@ -1874,8 +1863,8 @@ def selectLanduse(self): |
1874 | 1863 | if self.clippingEZG is None: |
1875 | 1864 | selected_layer_name = None |
1876 | 1865 | raise Exception("User has not selected a sub-basins layer.") |
1877 | | - selected_layer_name = self.dlg.comboboxLanduseLayer.currentText() |
1878 | | - self.landuseLayer = QgsProject.instance().mapLayersByName(selected_layer_name)[0] |
| 1866 | + |
| 1867 | + self.landuseLayer = self.dlg.comboboxLanduseLayer.currentLayer() |
1879 | 1868 |
|
1880 | 1869 | #Create field with the feature-id |
1881 | 1870 | self.landuseFieldInputID = 'fid_qta' |
@@ -2630,8 +2619,9 @@ def calculateSlopeHRUs(self, hruLayer): |
2630 | 2619 | ''' |
2631 | 2620 |
|
2632 | 2621 | #Get DEM Layer |
2633 | | - selected_layer_name = self.dlg.comboboxDEMLayer.currentText() |
2634 | | - self.demLayer = QgsProject.instance().mapLayersByName(selected_layer_name)[0] |
| 2622 | + self.demLayer = self.dlg.comboboxDEMLayer.currentLayer() |
| 2623 | + #selected_layer_name = self.dlg.comboboxDEMLayer.currentText() |
| 2624 | + #self.demLayer = QgsProject.instance().mapLayersByName(selected_layer_name)[0] |
2635 | 2625 |
|
2636 | 2626 | #Calculate Slope Layer |
2637 | 2627 | slope_layer_path = processing.run("native:slope", {'INPUT':self.demLayer, 'Z_FACTOR':1,'OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT'] |
@@ -3203,7 +3193,7 @@ def performIntersect(self): |
3203 | 3193 | self.eflLayer = processing.run("native:dissolve", {'INPUT': self.eflLayer,'FIELD': eflFieldList, 'SEPARATE_DISJOINT':False,'OUTPUT':'TEMPORARY_OUTPUT'}, feedback=None)['OUTPUT'] |
3204 | 3194 |
|
3205 | 3195 | #Add Fields |
3206 | | - if self.dlg.comboboxDEMLayer.currentText() != "Optional: Upload DEM Layer": |
| 3196 | + if self.dlg.comboboxDEMLayer.currentLayer(): |
3207 | 3197 | self.log_to_qtalsim_tab(f"Calculating Slope...", Qgis.Info) |
3208 | 3198 | self.eflLayer = self.calculateSlopeHRUs(self.eflLayer) |
3209 | 3199 | else: |
@@ -4356,7 +4346,26 @@ def run(self): |
4356 | 4346 | #Open help - documentation |
4357 | 4347 | self.connectButtontoFunction(self.dlg.finalButtonBox.button(QDialogButtonBox.Help), self.openDocumentation) |
4358 | 4348 |
|
4359 | | - self.fillPolygonsCombobox() |
| 4349 | + # Layer comboboxes |
| 4350 | + # Sub-basin layer |
| 4351 | + self.dlg.comboboxSubBasinLayer.setFilters( |
| 4352 | + QgsMapLayerProxyModel.PolygonLayer |
| 4353 | + ) |
| 4354 | + self.dlg.comboboxSubBasinLayer.layerChanged.connect(self.on_ezg_changed) |
| 4355 | + #Soil Layer |
| 4356 | + self.dlg.comboboxSoilLayer.setFilters( |
| 4357 | + QgsMapLayerProxyModel.PolygonLayer |
| 4358 | + ) |
| 4359 | + #Land use layer |
| 4360 | + self.dlg.comboboxLanduseLayer.setFilters( |
| 4361 | + QgsMapLayerProxyModel.PolygonLayer |
| 4362 | + ) |
| 4363 | + #DEM Layer |
| 4364 | + self.dlg.comboboxDEMLayer.setAllowEmptyLayer(True) |
| 4365 | + self.dlg.comboboxDEMLayer.setPlaceholderText("Optional: Upload DEM Layer") |
| 4366 | + self.dlg.comboboxDEMLayer.setFilters( |
| 4367 | + QgsMapLayerProxyModel.RasterLayer |
| 4368 | + ) |
4360 | 4369 |
|
4361 | 4370 | self.feedback = self.CustomFeedback(self.log_to_qtalsim_tab) |
4362 | 4371 |
|
@@ -4396,8 +4405,8 @@ def run(self): |
4396 | 4405 | #self.dlg.groupboxIntersect.setVisible(False) |
4397 | 4406 | self.connectButtontoFunction(self.dlg.onPerformIntersect, self.performIntersect) |
4398 | 4407 |
|
4399 | | - QgsProject.instance().layersAdded.connect(self.layersAddedHandler) |
4400 | | - QgsProject.instance().layersRemoved.connect(self.layersAddedHandler) |
| 4408 | + #QgsProject.instance().layersAdded.connect(self.layersAddedHandler) |
| 4409 | + #QgsProject.instance().layersRemoved.connect(self.layersAddedHandler) |
4401 | 4410 |
|
4402 | 4411 | self.dlg.comboboxEliminateModes.clear() |
4403 | 4412 | self.dlg.comboboxEliminateModes.addItems(['Largest Area', 'Smallest Area','Largest Common Boundary']) |
|
0 commit comments