|
11 | 11 | __copyright__ = 'Copyright 2019, The QGIS Project'
|
12 | 12 |
|
13 | 13 | import qgis # NOQA
|
| 14 | +import random |
14 | 15 |
|
15 | 16 | from qgis.PyQt.QtCore import QLocale
|
16 | 17 | from qgis.testing import unittest, start_app
|
17 |
| -from qgis.core import QgsClassificationMethod, QgsClassificationLogarithmic, QgsFeature, QgsVectorLayer, QgsPointXY, \ |
| 18 | +from qgis.core import QgsClassificationMethod, QgsClassificationLogarithmic, QgsClassificationJenks, QgsFeature, QgsVectorLayer, QgsPointXY, \ |
18 | 19 | QgsGeometry
|
19 | 20 |
|
20 | 21 | start_app()
|
@@ -85,6 +86,34 @@ def testQgsClassificationLogarithmic_FilterZeroNeg(self):
|
85 | 86 | self.assertEqual(r[0].label(), '-2 - 10^0')
|
86 | 87 | self.assertEqual(QgsClassificationMethod.rangesToBreaks(r), [1.0, 10.0, 100.0, 1000.0, 10000.0])
|
87 | 88 |
|
| 89 | + def testQgsClassificationJenksSimple(self): |
| 90 | + # This is a simple Natural Breaks Jenks test checking if simple calculation can be done |
| 91 | + # when number of values is below 3000 (value hardcoded in qgsclassificationjenks.h) |
| 92 | + # And if returned values are consistent (the same as at the time of creation of this test) |
| 93 | + values = [-33, -41, -43, 16, 29, 9, -35, 57, 26, -30] |
| 94 | + vl = createMemoryLayer(values) |
| 95 | + m = QgsClassificationJenks() |
| 96 | + |
| 97 | + r = m.classes(vl, 'value', 4) |
| 98 | + self.assertEqual(len(r), 4) |
| 99 | + self.assertEqual(QgsClassificationMethod.rangesToBreaks(r), |
| 100 | + [-30.0, 16.0, 29.0, 57.0]) |
| 101 | + |
| 102 | + def testQgsClassificationJenksHighNumber(self): |
| 103 | + # This test checks if Jenkis classification does not crash when number of |
| 104 | + # values in a set is higher than 3000 (value hardcoded in qgsclassificationjenks.h) |
| 105 | + # And if returned values are consistent (the same as at the time of creation of this test) |
| 106 | + random.seed(42*42); |
| 107 | + values = [random.randint(-1000, 1000) for _ in range(5000)] |
| 108 | + vl = createMemoryLayer(values) |
| 109 | + m = QgsClassificationJenks() |
| 110 | + |
| 111 | + r = m.classes(vl, 'value', 4) |
| 112 | + print('result high=',QgsClassificationMethod.rangesToBreaks(r)) |
| 113 | + self.assertEqual(len(r), 4) |
| 114 | + self.assertEqual(QgsClassificationMethod.rangesToBreaks(r), |
| 115 | + [-506.0, -4.0, 499.0, 1000.0]) |
| 116 | + |
88 | 117 |
|
89 | 118 | if __name__ == "__main__":
|
90 | 119 | unittest.main()
|
0 commit comments