Skip to content
Permalink
Browse files

[processing] Fix bar and box plot exception when category field has N…

…ULL values
  • Loading branch information
nyalldawson committed Jun 19, 2018
1 parent 31a8f3f commit 16ac43778074ab860577765bf89ca3cf6a6a4353
@@ -29,7 +29,8 @@
import plotly.graph_objs as go


from qgis.core import (QgsProcessingParameterFeatureSource,
from qgis.core import (QgsFeatureRequest,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingException,
QgsProcessingParameterFileDestination)
@@ -83,7 +84,7 @@ def processAlgorithm(self, parameters, context, feedback):

values = vector.values(source, valuefieldname)

x_var = [i[namefieldname] for i in source.getFeatures()]
x_var = vector.convert_nulls([i[namefieldname] for i in source.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry))], '<NULL>')

data = [go.Bar(x=x_var,
y=values[valuefieldname])]
@@ -96,7 +96,7 @@ def processAlgorithm(self, parameters, context, feedback):
values = vector.values(source, valuefieldname)

x_index = source.fields().lookupField(namefieldname)
x_var = [i[namefieldname] for i in source.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([x_index]))]
x_var = vector.convert_nulls([i[namefieldname] for i in source.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([x_index]))], '<NULL>')

msdIndex = self.parameterAsEnum(parameters, self.MSD, context)
msd = True
@@ -28,7 +28,7 @@
import os
import shutil

from qgis.core import QgsVectorLayer
from qgis.core import NULL, QgsVectorLayer
from qgis.testing import start_app, unittest

from processing.tests.TestData import points
@@ -77,6 +77,17 @@ def testValues(self):
self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

def testConvertNulls(self):
self.assertEqual(vector.convert_nulls([]), [])
self.assertEqual(vector.convert_nulls([], '_'), [])
self.assertEqual(vector.convert_nulls([NULL]), [None])
self.assertEqual(vector.convert_nulls([NULL], '_'), ['_'])
self.assertEqual(vector.convert_nulls([NULL], -1), [-1])
self.assertEqual(vector.convert_nulls([1, 2, 3]), [1, 2, 3])
self.assertEqual(vector.convert_nulls([1, None, 3]), [1, None, 3])
self.assertEqual(vector.convert_nulls([1, NULL, 3, NULL]), [1, None, 3, None])
self.assertEqual(vector.convert_nulls([1, NULL, 3, NULL], '_'), [1, '_', 3, '_'])


if __name__ == '__main__':
unittest.main()
@@ -25,9 +25,7 @@

__revision__ = '$Format:%H$'

import csv

from qgis.core import (QgsWkbTypes,
from qgis.core import (NULL,
QgsFeatureRequest)


@@ -90,6 +88,16 @@ def values(source, *attributes):
return ret


def convert_nulls(values, replacement=None):
"""
Converts NULL items in a list of values to a replacement value (usually None)
:param values: list of values
:param replacement: value to use in place of NULL
:return: converted list
"""
return [i if i != NULL else replacement for i in values]


def checkMinDistance(point, index, distance, points):
"""Check if distance from given point to all other points is greater
than given value.

0 comments on commit 16ac437

Please sign in to comment.
You can’t perform that action at this time.