From 1b84700532d9a2821b9ba2c02729ef54968e6e76 Mon Sep 17 00:00:00 2001 From: Samweli Date: Wed, 4 Dec 2024 14:05:05 +0300 Subject: [PATCH] added geometry check for imported project layers --- src/qgis_gea_plugin/gui/attribute_form.py | 24 +++++++++++++++------ src/qgis_gea_plugin/gui/qgis_gea.py | 26 ++++++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/qgis_gea_plugin/gui/attribute_form.py b/src/qgis_gea_plugin/gui/attribute_form.py index 411df6e..28caee0 100644 --- a/src/qgis_gea_plugin/gui/attribute_form.py +++ b/src/qgis_gea_plugin/gui/attribute_form.py @@ -4,8 +4,6 @@ """ import os -from datetime import datetime - from qgis.PyQt import QtCore, QtGui, QtWidgets @@ -50,6 +48,7 @@ def __init__( QtCore.Qt.WindowMinimizeButtonHint | QtCore.Qt.WindowCloseButtonHint ) self.setupUi(self) + self.parent = parent self.layer = layer self.project_cmb_box.addItems(PROJECT_AREAS) @@ -123,9 +122,19 @@ def accept(self): # Set attribute values feature_area = "-" geom = feature.geometry() - if geom is not None and geom.isGeosValid(): - area = geom.area() / 10000 - feature_area = f"{area:,.2f}" + + if geom is None or not geom.isGeosValid(): + self.parent.show_message( + tr( + "Skipped a feature with " + "an invalid geometry." + ) + ) + feature = next(features, None) + continue + + area = geom.area() / 10000 + feature_area = f"{area:,.2f}" feature.setAttribute( "author", @@ -135,7 +144,10 @@ def accept(self): "project", self.project_cmb_box.currentText() ) - feature.setAttribute("area (ha)", feature_area) + feature.setAttribute( + "area (ha)", + feature_area + ) self.layer.updateFeature(feature) # Retrieve the next feature diff --git a/src/qgis_gea_plugin/gui/qgis_gea.py b/src/qgis_gea_plugin/gui/qgis_gea.py index ee1c108..be3bd08 100644 --- a/src/qgis_gea_plugin/gui/qgis_gea.py +++ b/src/qgis_gea_plugin/gui/qgis_gea.py @@ -29,7 +29,6 @@ QgsPalLayerSettings, QgsProject, QgsTask, - QgsTextBackgroundSettings, QgsTextFormat, QgsTemporalNavigationObject, QgsUnitTypes, @@ -37,7 +36,7 @@ QgsVectorLayer, QgsVectorLayerEditUtils, QgsVectorLayerSimpleLabeling, - + QgsWkbTypes ) from qgis.gui import QgsLayerTreeView, QgsMessageBar @@ -325,7 +324,28 @@ def project_instances_changed(self): self.load_attribute_form(layer) def load_attribute_form(self, layer): - attribute_form = AttributeForm(layer) + + wkb_type = layer.wkbType() + + if Qgis.QGIS_VERSION_INT < 33000: + geometry_type = QgsWkbTypes.geometryType(wkb_type) + layer_type = geometry_type == QgsWkbTypes.PolygonGeometry + else: + layer_type = QgsWkbTypes.flatType(wkb_type) in ( + QgsWkbTypes.Polygon, + QgsWkbTypes.MultiPolygon + ) + + if not layer_type: + self.show_message( + tr( + "Selected layer doesn't " + "have a polygon geometry type." + ) + ) + return + + attribute_form = AttributeForm(layer, parent=self) attribute_form.exec_() def project_folder_changed(self):