From bbe6821f1033b471b31876cf5a3d480de37f4f66 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Fri, 6 Sep 2019 15:56:48 +0200 Subject: [PATCH] correctly handle NULL values https://nathanw.net/2013/08/31/qgis-2-0-dealing-with-null-values-in-pyqgis/ --- wincan2qgep/core/vsacode.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/wincan2qgep/core/vsacode.py b/wincan2qgep/core/vsacode.py index 05dc0f2..2c8c765 100644 --- a/wincan2qgep/core/vsacode.py +++ b/wincan2qgep/core/vsacode.py @@ -23,7 +23,7 @@ # #--------------------------------------------------------------------- -from qgis.core import QgsProject, QgsFeature, QgsFeatureRequest +from qgis.core import QgsProject, QgsFeature, QgsFeatureRequest, QgsApplication, NULL from wincan2qgep.core.my_settings import MySettings @@ -91,18 +91,20 @@ def structure_condition_2_damage_level(code): """ return damage code to renovation necessity pkey """ - feature = QgsFeature() - layer_id = MySettings().value("vl_wastewater_structure_structure_condition") layer = QgsProject.instance().mapLayer(layer_id) - if layer is not None: - request_text = '"code" = \'{}\''.format(code) - request = QgsFeatureRequest().setFilterExpression(request_text) - feature = next(layer.getFeatures(request), QgsFeature()) - # print request_text, feature.isValid() + + print(code, QgsApplication.nullRepresentation(), code == QgsApplication.nullRepresentation()) + + if layer is None or code == NULL or code is None: + return None + + request_text = '"code" = \'{}\''.format(code) + request = QgsFeatureRequest().setFilterExpression(request_text) + feature = next(layer.getFeatures(request), QgsFeature()) + # print(request_text, feature.isValid()) if feature.isValid(): return feature['value_en'] - else: - return None \ No newline at end of file + return None