Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[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 16ac437
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/BarPlot.py
Expand Up @@ -29,7 +29,8 @@
import plotly.graph_objs as go


from qgis.core import (QgsProcessingParameterFeatureSource,
from qgis.core import (QgsFeatureRequest,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingException,
QgsProcessingParameterFileDestination)
Expand Down Expand Up @@ -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])]
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/BoxPlot.py
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion python/plugins/processing/tests/ToolsTest.py
Expand Up @@ -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
Expand Down Expand Up @@ -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()
14 changes: 11 additions & 3 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -25,9 +25,7 @@

__revision__ = '$Format:%H$'

import csv

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


Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 16ac437

Please sign in to comment.