-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_polygonizeInC2.py
205 lines (183 loc) · 9.17 KB
/
test_polygonizeInC2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- coding: utf-8 -*-
"""
/***************************************************************************
QSWAT
A QGIS plugin
Create SWAT inputs
-------------------
begin : 2014-07-18
copyright : (C) 2014 by Chris George
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from qgis.core import QgsPointXY
from QSWATPlus.QSWATUtils import QSWATUtils, fileWriter
import unittest
import random
import numpy as np
import os
from os.path import expanduser
from QSWATPlus import polygonizeInC2 as Polygonize # @UnresolvedImport
class TestPoly(unittest.TestCase):
"""Test cases for polygonize."""
def setUp(self):
"""Initialise tests."""
## debugging aid
self.report = QSWATUtils.join(expanduser('~'), 'testing.txt')
## debugging file writer
self.fw = fileWriter(self.report)
def tearDown(self):
self.fw.close()
def test1(self):
"""Simplest polygon with a hole."""
shapes = Polygonize.Polygonize(True, 3, -1, QgsPointXY(0,0), 1, 1, self.fw)
shapes.addRow(np.array([1,1,1]), 0)
shapes.addRow(np.array([1,2,1]), 1)
shapes.addRow(np.array([1,1,1]), 2)
shapes.finish()
self.check(shapes, 3, 'Test1', True)
def test2(self):
"""Nested polygons. Note this fails geometry validation (nested polygons) if the central square is 1."""
shapes = Polygonize.Polygonize(True, 5, -1, QgsPointXY(0,0), 1, 1, self.fw)
shapes.addRow(np.array([1,1,1,1,1]), 0)
shapes.addRow(np.array([1,2,2,2,1]), 1)
shapes.addRow(np.array([1,2,3,2,1]), 2)
shapes.addRow(np.array([1,2,2,2,1]), 3)
shapes.addRow(np.array([1,1,1,1,1]), 4)
shapes.finish()
self.check(shapes, 5, 'Test2', True)
def test3(self):
"""Multiple holes. Checks for holes after main polygon."""
shapes = Polygonize.Polygonize(True, 5, -1, QgsPointXY(0,0), 1, 1, self.fw)
shapes.addRow(np.array([1,1,1,1,1]), 0)
shapes.addRow(np.array([1,2,1,2,1]), 1)
shapes.addRow(np.array([1,1,1,1,1]), 2)
shapes.addRow(np.array([1,2,1,2,1]), 3)
shapes.addRow(np.array([1,1,1,1,1]), 4)
shapes.finish()
self.check(shapes, 5, 'Test3', True)
def test4(self):
"""Single complex hole."""
shapes = Polygonize.Polygonize(True, 5, -1, QgsPointXY(0,0), 1, 1, self.fw)
shapes.addRow(np.array([1,1,1,1,1]), 0)
shapes.addRow(np.array([1,2,1,2,1]), 1)
shapes.addRow(np.array([1,1,2,1,1]), 2)
shapes.addRow(np.array([1,2,1,2,1]), 3)
shapes.addRow(np.array([1,1,1,1,1]), 4)
shapes.finish()
self.check(shapes, 5, 'Test4', True)
def test10(self):
"""Example of not properly connected when using connected8: produces geometry error."""
shapes = Polygonize.Polygonize(False, 10, -1, QgsPointXY(0,0), 1, 1, self.fw)
shapes.addRow(np.array([2, 1, 2, 1, 2, 1, 1, 1, 1, 1]), 0)
shapes.addRow(np.array([1, 2, 2, 1, 1, 1, 1, 1, 2, 1]), 1)
shapes.addRow(np.array([1, 1, 1, 1, 1, 2, 1, 1, 1, 2]), 2)
shapes.addRow(np.array([1, 1, 2, 1, 2, 1, 1, 1, 1, 2]), 3)
shapes.addRow(np.array([1, 1, 1, 1, 1, 1, 2, 2, 1, 1]), 4)
shapes.addRow(np.array([1, 2, 1, 1, 1, 2, 1, 2, 1, 1]), 5)
shapes.addRow(np.array([2, 1, 1, 2, 1, 1, 1, 1, 1, 1]), 6)
shapes.addRow(np.array([2, 1, 1, 1, 1, 2, 1, 1, 1, 2]), 7)
shapes.addRow(np.array([1, 1, 1, 2, 1, 1, 1, 1, 1, 1]), 8)
shapes.addRow(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 9)
shapes.finish()
self.check(shapes, 10, 'Test10', False)
def test11(self):
"""Problem for connected8."""
shapes = Polygonize.Polygonize(False, 4, -1, QgsPointXY(0,0), 1, 1, self.fw)
shapes.addRow(np.array([1,2,1,1]), 0)
shapes.addRow(np.array([1,1,2,1]), 1)
shapes.addRow(np.array([2,2,1,2]), 2)
shapes.addRow(np.array([1,2,1,2]), 3)
shapes.finish()
self.check(shapes, 4, 'Test11', False)
def test0(self):
"""Example of 1 inside 2 inside 1, which is classed as a geometry error."""
shapes = Polygonize.Polygonize(True, 10, -1, QgsPointXY(0,0), 1, 1, self.fw)
shapes.addRow(np.array([1, 1, 2, 1, 1, 1, 1, 1, 1, 1]), 0)
shapes.addRow(np.array([1, 1, 1, 2, 1, 1, 2, 2, 1, 2]), 1)
shapes.addRow(np.array([2, 2, 1, 2, 1, 2, 1, 1, 2, 1]), 2)
shapes.addRow(np.array([1, 1, 2, 1, 2, 1, 1, 1, 2, 1]), 3)
shapes.addRow(np.array([2, 1, 1, 1, 1, 2, 1, 1, 1, 2]), 4)
shapes.addRow(np.array([1, 1, 2, 2, 2, 1, 1, 1, 2, 1]), 5)
shapes.addRow(np.array([1, 1, 2, 1, 2, 1, 2, 1, 2, 1]), 6)
shapes.addRow(np.array([1, 1, 2, 2, 2, 1, 1, 1, 1, 1]), 7)
shapes.addRow(np.array([1, 1, 2, 1, 2, 1, 1, 1, 1, 2]), 8)
shapes.addRow(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 9)
shapes.finish()
self.check(shapes, 10, 'Test0', True)
def test5(self):
"""Random 10x10 grid of 1s and 2s. Probability of 1 set at 70% to encourage holes.
Run with connectedness4 and then 8"""
size = 10
for isConnected4 in [True, False]:
for _ in range(10000):
shapes = Polygonize.Polygonize(isConnected4, size, -1, QgsPointXY(0,0), 1, 1, self.fw)
rows = []
for r in range(size):
row = []
for _ in range(size):
val = 1 if random.random() <= 0.7 else 2
row.append(val)
shapes.addRow(np.array(row), r)
rows.append(row)
shapes.finish()
arrayString = 'Connected4' if isConnected4 else 'Connected8'
arrayString += os.linesep
for r in range(size):
arrayString += str(rows[r])
arrayString += os.linesep
self.check(shapes, size, arrayString, isConnected4)
def check(self, shapes, size, arrayString, isConnected4):
"""Print string for shapes; check shapes for closure, no complementary pairs, and for geometric validity."""
#output = shapes.makeString()
#print(output)
# check cell counts: assuming no nodata
cellCount = 0
for shape in shapes.shapes.values():
cellCount += shape.cellCount
self.assertEqual(size * size, cellCount, 'Cell count is {0} when it should be {1}'.format(cellCount, size * size))
for hru, shape in shapes.shapes.items():
for poly in shape.polygons.values():
ring = poly.rings[0]
self.assertTrue(Polygonize.isClockwise(ring),
'Outer polygon {0} of {1} is not clockwise'.format(Polygonize.ringToString(ring), str(poly)))
for ring in poly.rings:
self.assertTrue(Polygonize.isClosed(ring),
'Polygon {0} is not closed'.format(Polygonize.ringToString(ring)))
geometry = shapes.getGeometry(hru)
self.assertIsNotNone(geometry, 'No geometry for hru {0!s}'.format(hru))
errors = TestPoly.stripErrors(geometry.validateGeometry())
for error in errors:
if error.hasWhere():
self.fail('{0} Geometry error at {1}: {2} for shapes{3}{4}'. \
format(arrayString, error.where().toString(), error.what(), os.linesep, shapes.shapesToString()))
else:
self.fail('{0} Geometry error: {1} for shapes{2}{3}'. \
format(arrayString, error.what(), os.linesep, shapes.shapesToString()))
@staticmethod
def stripErrors(errors):
"""A geometry error is generated if there is double nesting: see test0 above.
We ignore these by removing them."""
outErrors = []
insideErrorFound = False
num = len(errors)
for i in range(num):
if i == num-1 and insideErrorFound:
# ignore final message with error count
return outErrors
error = errors[i]
if not error.hasWhere() and ' inside polygon ' in error.what():
insideErrorFound = True
else:
outErrors.append(error)
return outErrors
if __name__ == '__main__':
unittest.main()