| 1 |
"""Test t.rast.contour |
| 2 |
|
| 3 |
(C) 2015 by the GRASS Development Team |
| 4 |
This program is free software under the GNU General Public |
| 5 |
License (>=v2). Read the file COPYING that comes with GRASS |
| 6 |
for details. |
| 7 |
|
| 8 |
@author Soeren Gebbert |
| 9 |
""" |
| 10 |
|
| 11 |
from grass.gunittest.case import TestCase |
| 12 |
from grass.gunittest.gmodules import SimpleModule |
| 13 |
|
| 14 |
|
| 15 |
class TestRasterContour(TestCase): |
| 16 |
@classmethod |
| 17 |
def setUpClass(cls): |
| 18 |
"""Initiate the temporal GIS and set the region""" |
| 19 |
cls.use_temp_region() |
| 20 |
cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10) |
| 21 |
|
| 22 |
cls.runModule("r.mapcalc", expression="a_1 = row()", overwrite=True) |
| 23 |
cls.runModule("r.mapcalc", expression="a_2 = col()", overwrite=True) |
| 24 |
cls.runModule("r.mapcalc", expression="a_3 = row()", overwrite=True) |
| 25 |
cls.runModule("r.mapcalc", expression="a_4 = col()", overwrite=True) |
| 26 |
cls.runModule("r.mapcalc", expression="a_5 = null()", overwrite=True) |
| 27 |
|
| 28 |
cls.runModule( |
| 29 |
"t.create", |
| 30 |
type="strds", |
| 31 |
temporaltype="absolute", |
| 32 |
output="A", |
| 33 |
title="A test", |
| 34 |
description="A test", |
| 35 |
overwrite=True, |
| 36 |
) |
| 37 |
cls.runModule( |
| 38 |
"t.register", |
| 39 |
flags="i", |
| 40 |
type="raster", |
| 41 |
input="A", |
| 42 |
maps="a_1,a_2,a_3,a_4,a_5", |
| 43 |
start="2001-01-01", |
| 44 |
increment="3 months", |
| 45 |
overwrite=True, |
| 46 |
) |
| 47 |
|
| 48 |
@classmethod |
| 49 |
def tearDownClass(cls): |
| 50 |
"""Remove the temporary region""" |
| 51 |
cls.runModule("t.remove", flags="df", type="strds", inputs="A") |
| 52 |
cls.del_temp_region() |
| 53 |
|
| 54 |
def tearDown(self): |
| 55 |
"""Remove generated data""" |
| 56 |
self.runModule("t.remove", flags="df", type="stvds", inputs="result") |
| 57 |
|
| 58 |
def test_register_empty_maps(self): |
| 59 |
self.assertModule( |
| 60 |
"t.rast.contour", |
| 61 |
input="A", |
| 62 |
output="result", |
| 63 |
levels=(1, 2, 3, 4, 5, 6, 7, 8), |
| 64 |
flags="n", |
| 65 |
basename="test", |
| 66 |
nprocs=1, |
| 67 |
overwrite=True, |
| 68 |
verbose=True, |
| 69 |
) |
| 70 |
|
| 71 |
# self.assertModule("t.info", type="stvds", flags="g", input="result") |
| 72 |
|
| 73 |
tinfo_string = """start_time='2001-01-01 00:00:00' |
| 74 |
end_time='2002-04-01 00:00:00' |
| 75 |
granularity='3 months' |
| 76 |
map_time=interval |
| 77 |
number_of_maps=5 |
| 78 |
lines=28 |
| 79 |
primitives=28 |
| 80 |
nodes=56""" |
| 81 |
|
| 82 |
info = SimpleModule("t.info", flags="g", type="stvds", input="result") |
| 83 |
self.assertModuleKeyValue( |
| 84 |
module=info, reference=tinfo_string, precision=2, sep="=" |
| 85 |
) |
| 86 |
|
| 87 |
def test_simple(self): |
| 88 |
"""Do not register empty maps""" |
| 89 |
self.assertModule( |
| 90 |
"t.rast.contour", |
| 91 |
input="A", |
| 92 |
output="result", |
| 93 |
levels=(1, 2, 3, 4, 5, 6, 7, 8), |
| 94 |
basename="test", |
| 95 |
nprocs=1, |
| 96 |
overwrite=True, |
| 97 |
verbose=True, |
| 98 |
) |
| 99 |
|
| 100 |
# self.assertModule("t.info", type="stvds", flags="g", input="result") |
| 101 |
|
| 102 |
tinfo_string = """start_time='2001-01-01 00:00:00' |
| 103 |
end_time='2002-01-01 00:00:00' |
| 104 |
granularity='3 months' |
| 105 |
map_time=interval |
| 106 |
number_of_maps=4 |
| 107 |
lines=28 |
| 108 |
primitives=28 |
| 109 |
nodes=56""" |
| 110 |
|
| 111 |
info = SimpleModule("t.info", flags="g", type="stvds", input="result") |
| 112 |
self.assertModuleKeyValue( |
| 113 |
module=info, reference=tinfo_string, precision=2, sep="=" |
| 114 |
) |
| 115 |
|
| 116 |
def test_where(self): |
| 117 |
"""Use where statement and do not register empty maps""" |
| 118 |
self.assertModule( |
| 119 |
"t.rast.contour", |
| 120 |
input="A", |
| 121 |
output="result", |
| 122 |
levels=(1, 2, 3, 4, 5, 6, 7, 8), |
| 123 |
basename="test", |
| 124 |
where="start_time > '2001-02-01'", |
| 125 |
nprocs=1, |
| 126 |
overwrite=True, |
| 127 |
verbose=True, |
| 128 |
) |
| 129 |
|
| 130 |
# self.assertModule("t.info", type="stvds", flags="g", input="result") |
| 131 |
|
| 132 |
tinfo_string = """start_time='2001-04-01 00:00:00' |
| 133 |
end_time='2002-01-01 00:00:00' |
| 134 |
granularity='3 months' |
| 135 |
map_time=interval |
| 136 |
number_of_maps=3 |
| 137 |
lines=21 |
| 138 |
primitives=21 |
| 139 |
nodes=42""" |
| 140 |
|
| 141 |
info = SimpleModule("t.info", flags="g", type="stvds", input="result") |
| 142 |
self.assertModuleKeyValue( |
| 143 |
module=info, reference=tinfo_string, precision=2, sep="=" |
| 144 |
) |
| 145 |
|
| 146 |
def test_parallel(self): |
| 147 |
"""Run 4 contour processes do not create attribute tables""" |
| 148 |
self.assertModule( |
| 149 |
"t.rast.contour", |
| 150 |
input="A", |
| 151 |
output="result", |
| 152 |
levels=(1, 2, 3, 4, 5, 6, 7, 8), |
| 153 |
basename="test", |
| 154 |
flags="t", |
| 155 |
nprocs=4, |
| 156 |
overwrite=True, |
| 157 |
verbose=True, |
| 158 |
) |
| 159 |
|
| 160 |
# self.assertModule("t.info", type="stvds", flags="g", input="result") |
| 161 |
|
| 162 |
tinfo_string = """start_time='2001-01-01 00:00:00' |
| 163 |
end_time='2002-01-01 00:00:00' |
| 164 |
granularity='3 months' |
| 165 |
map_time=interval |
| 166 |
number_of_maps=4 |
| 167 |
lines=28 |
| 168 |
primitives=28 |
| 169 |
nodes=56""" |
| 170 |
|
| 171 |
info = SimpleModule("t.info", flags="g", type="stvds", input="result") |
| 172 |
self.assertModuleKeyValue( |
| 173 |
module=info, reference=tinfo_string, precision=2, sep="=" |
| 174 |
) |
| 175 |
|
| 176 |
def test_parallel_cut(self): |
| 177 |
"""Do not register empty maps""" |
| 178 |
self.assertModule( |
| 179 |
"t.rast.contour", |
| 180 |
input="A", |
| 181 |
output="result", |
| 182 |
levels=(1, 2, 3, 4, 5, 6, 7, 8), |
| 183 |
cut=2, |
| 184 |
basename="test", |
| 185 |
flags="t", |
| 186 |
nprocs=4, |
| 187 |
overwrite=True, |
| 188 |
verbose=True, |
| 189 |
) |
| 190 |
|
| 191 |
# self.assertModule("t.info", type="stvds", flags="g", input="result") |
| 192 |
|
| 193 |
tinfo_string = """start_time='2001-01-01 00:00:00' |
| 194 |
end_time='2002-01-01 00:00:00' |
| 195 |
granularity='3 months' |
| 196 |
map_time=interval |
| 197 |
number_of_maps=4 |
| 198 |
lines=28 |
| 199 |
primitives=28 |
| 200 |
nodes=56""" |
| 201 |
|
| 202 |
info = SimpleModule("t.info", flags="g", type="stvds", input="result") |
| 203 |
self.assertModuleKeyValue( |
| 204 |
module=info, reference=tinfo_string, precision=2, sep="=" |
| 205 |
) |
| 206 |
|
| 207 |
def test_where_step(self): |
| 208 |
"""Use where statement and do not register empty maps""" |
| 209 |
self.assertModule( |
| 210 |
"t.rast.contour", |
| 211 |
input="A", |
| 212 |
output="result", |
| 213 |
step=1, |
| 214 |
minlevel=1, |
| 215 |
maxlevel=8, |
| 216 |
basename="test", |
| 217 |
where="start_time > '2001-02-01'", |
| 218 |
nprocs=1, |
| 219 |
overwrite=True, |
| 220 |
verbose=True, |
| 221 |
) |
| 222 |
|
| 223 |
# self.assertModule("t.info", type="stvds", flags="g", input="result") |
| 224 |
|
| 225 |
tinfo_string = """start_time='2001-04-01 00:00:00' |
| 226 |
end_time='2002-01-01 00:00:00' |
| 227 |
granularity='3 months' |
| 228 |
map_time=interval |
| 229 |
number_of_maps=3 |
| 230 |
lines=21 |
| 231 |
primitives=21 |
| 232 |
nodes=42""" |
| 233 |
|
| 234 |
info = SimpleModule("t.info", flags="g", type="stvds", input="result") |
| 235 |
self.assertModuleKeyValue( |
| 236 |
module=info, reference=tinfo_string, precision=2, sep="=" |
| 237 |
) |
| 238 |
|
| 239 |
def test_suffix_num(self): |
| 240 |
"""Test the -s flag""" |
| 241 |
self.assertModule( |
| 242 |
"t.rast.contour", |
| 243 |
input="A", |
| 244 |
output="result", |
| 245 |
step=1, |
| 246 |
minlevel=1, |
| 247 |
maxlevel=8, |
| 248 |
basename="time", |
| 249 |
suffix="num%03", |
| 250 |
where="start_time > '2001-02-01'", |
| 251 |
nprocs=1, |
| 252 |
overwrite=True, |
| 253 |
verbose=True, |
| 254 |
) |
| 255 |
|
| 256 |
self.assertVectorExists("time_001") |
| 257 |
self.assertVectorDoesNotExist("time_00005") |
| 258 |
self.assertVectorDoesNotExist("time_2001_07") |
| 259 |
|
| 260 |
def test_suffix_time(self): |
| 261 |
"""Test the -s flag""" |
| 262 |
self.assertModule( |
| 263 |
"t.rast.contour", |
| 264 |
input="A", |
| 265 |
output="result", |
| 266 |
step=1, |
| 267 |
minlevel=1, |
| 268 |
maxlevel=8, |
| 269 |
basename="time", |
| 270 |
suffix="time", |
| 271 |
where="start_time > '2001-02-01'", |
| 272 |
nprocs=1, |
| 273 |
overwrite=True, |
| 274 |
verbose=True, |
| 275 |
) |
| 276 |
|
| 277 |
self.assertVectorExists("time_2001_07_01T00_00_00") |
| 278 |
self.assertVectorDoesNotExist("time_00005") |
| 279 |
self.assertVectorDoesNotExist("time_2001_07") |
| 280 |
|
| 281 |
|
| 282 |
class TestRasterContourFails(TestCase): |
| 283 |
@classmethod |
| 284 |
def setUpClass(cls): |
| 285 |
"""Initiate the temporal GIS and set the region""" |
| 286 |
cls.use_temp_region() |
| 287 |
cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10) |
| 288 |
|
| 289 |
cls.runModule("r.mapcalc", expression="a_1 = 100", overwrite=True) |
| 290 |
|
| 291 |
cls.runModule( |
| 292 |
"t.create", |
| 293 |
type="strds", |
| 294 |
temporaltype="absolute", |
| 295 |
output="A", |
| 296 |
title="A test", |
| 297 |
description="A test", |
| 298 |
overwrite=True, |
| 299 |
) |
| 300 |
cls.runModule( |
| 301 |
"t.register", |
| 302 |
flags="i", |
| 303 |
type="raster", |
| 304 |
input="A", |
| 305 |
maps="a_1", |
| 306 |
start="2001-01-01", |
| 307 |
increment="3 months", |
| 308 |
overwrite=True, |
| 309 |
) |
| 310 |
|
| 311 |
@classmethod |
| 312 |
def tearDownClass(cls): |
| 313 |
"""Remove the temporary region""" |
| 314 |
cls.runModule("t.remove", flags="df", type="strds", inputs="A") |
| 315 |
cls.del_temp_region() |
| 316 |
|
| 317 |
def test_error_handling(self): |
| 318 |
# No basename |
| 319 |
self.assertModuleFail( |
| 320 |
"t.rast.contour", |
| 321 |
input="A", |
| 322 |
output="result", |
| 323 |
step=1, |
| 324 |
minlevel=1, |
| 325 |
maxlevel=8, |
| 326 |
where="start_time > '2001-02-01'", |
| 327 |
nprocs=1, |
| 328 |
overwrite=True, |
| 329 |
verbose=True, |
| 330 |
) |
| 331 |
|
| 332 |
def test_empty_strds(self): |
| 333 |
"""Test for empty strds""" |
| 334 |
self.assertModule( |
| 335 |
"t.rast.contour", |
| 336 |
input="A", |
| 337 |
output="result", |
| 338 |
basename="test", |
| 339 |
step=1, |
| 340 |
minlevel=1, |
| 341 |
maxlevel=8, |
| 342 |
where="start_time > '2010-01-01'", |
| 343 |
nprocs=1, |
| 344 |
overwrite=True, |
| 345 |
verbose=True, |
| 346 |
) |
| 347 |
|
| 348 |
|
| 349 |
if __name__ == "__main__": |
| 350 |
from grass.gunittest.main import test |
| 351 |
|
| 352 |
test() |