1"""
2Depricazed unittests
3
4(C) 2008-2011 by the GRASS Development Team
5This program is free software under the GNU General Public
6License (>=v2). Read the file COPYING that comes with GRASS
7for details.
8
9:authors: Soeren Gebbert
10"""
11from __future__ import print_function
12
13import copy
14from datetime import datetime
15import grass.script.core as core
16from .temporal_granularity import *
17from .datetime_math import *
18from .space_time_datasets import *
19
20import grass.lib.vector as vector
21import grass.lib.rtree as rtree
22import grass.lib.gis as gis
23from ctypes import *
24
25# Uncomment this to detect the error
26core.set_raise_on_error(True)
27
28###############################################################################
29
30
31def test_increment_datetime_by_string():
32
33    # First test
34    print("# Test 1")
35    dt = datetime(2001, 9, 1, 0, 0, 0)
36    string = "60 seconds, 4 minutes, 12 hours, 10 days, 1 weeks, 5 months, 1 years"
37
38    dt1 = datetime(2003, 2, 18, 12, 5, 0)
39    dt2 = increment_datetime_by_string(dt, string)
40
41    print(dt)
42    print(dt2)
43
44    delta = dt1 - dt2
45
46    if delta.days != 0 or delta.seconds != 0:
47        core.fatal("increment computation is wrong %s" % (delta))
48
49    # Second test
50    print("# Test 2")
51    dt = datetime(2001, 11, 1, 0, 0, 0)
52    string = "1 months"
53
54    dt1 = datetime(2001, 12, 1)
55    dt2 = increment_datetime_by_string(dt, string)
56
57    print(dt)
58    print(dt2)
59
60    delta = dt1 - dt2
61
62    if delta.days != 0 or delta.seconds != 0:
63        core.fatal("increment computation is wrong %s" % (delta))
64
65    # Third test
66    print("# Test 3")
67    dt = datetime(2001, 11, 1, 0, 0, 0)
68    string = "13 months"
69
70    dt1 = datetime(2002, 12, 1)
71    dt2 = increment_datetime_by_string(dt, string)
72
73    print(dt)
74    print(dt2)
75
76    delta = dt1 - dt2
77
78    if delta.days != 0 or delta.seconds != 0:
79        core.fatal("increment computation is wrong %s" % (delta))
80
81    # 4. test
82    print("# Test 4")
83    dt = datetime(2001, 1, 1, 0, 0, 0)
84    string = "72 months"
85
86    dt1 = datetime(2007, 1, 1)
87    dt2 = increment_datetime_by_string(dt, string)
88
89    print(dt)
90    print(dt2)
91
92    delta = dt1 - dt2
93
94    if delta.days != 0 or delta.seconds != 0:
95        core.fatal("increment computation is wrong %s" % (delta))
96
97###############################################################################
98
99
100def test_adjust_datetime_to_granularity():
101
102    # First test
103    print("Test 1")
104    dt = datetime(2001, 8, 8, 12, 30, 30)
105    result = adjust_datetime_to_granularity(dt, "5 seconds")
106    correct = datetime(2001, 8, 8, 12, 30, 30)
107
108    delta = correct - result
109
110    if delta.days != 0 or delta.seconds != 0:
111        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
112
113    # Second test
114    print("Test 2")
115    result = adjust_datetime_to_granularity(dt, "20 minutes")
116    correct = datetime(2001, 8, 8, 12, 30, 0)
117
118    delta = correct - result
119
120    if delta.days != 0 or delta.seconds != 0:
121        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
122
123    # Third test
124    print("Test 2")
125    result = adjust_datetime_to_granularity(dt, "20 minutes")
126    correct = datetime(2001, 8, 8, 12, 30, 0)
127
128    delta = correct - result
129
130    if delta.days != 0 or delta.seconds != 0:
131        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
132
133    # 4. test
134    print("Test 4")
135    result = adjust_datetime_to_granularity(dt, "3 hours")
136    correct = datetime(2001, 8, 8, 12, 0, 0)
137
138    delta = correct - result
139
140    if delta.days != 0 or delta.seconds != 0:
141        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
142
143    # 5. test
144    print("Test 5")
145    result = adjust_datetime_to_granularity(dt, "5 days")
146    correct = datetime(2001, 8, 8, 0, 0, 0)
147
148    delta = correct - result
149
150    if delta.days != 0 or delta.seconds != 0:
151        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
152
153    # 6. test
154    print("Test 6")
155    result = adjust_datetime_to_granularity(dt, "2 weeks")
156    correct = datetime(2001, 8, 6, 0, 0, 0)
157
158    delta = correct - result
159
160    if delta.days != 0 or delta.seconds != 0:
161        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
162
163    # 7. test
164    print("Test 7")
165    result = adjust_datetime_to_granularity(dt, "6 months")
166    correct = datetime(2001, 8, 1, 0, 0, 0)
167
168    delta = correct - result
169
170    if delta.days != 0 or delta.seconds != 0:
171        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
172
173    # 8. test
174    print("Test 8")
175    result = adjust_datetime_to_granularity(dt, "2 years")
176    correct = datetime(2001, 1, 1, 0, 0, 0)
177
178    delta = correct - result
179
180    if delta.days != 0 or delta.seconds != 0:
181        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
182
183    # 9. test
184    print("Test 9")
185    result = adjust_datetime_to_granularity(
186        dt, "2 years, 3 months, 5 days, 3 hours, 3 minutes, 2 seconds")
187    correct = datetime(2001, 8, 8, 12, 30, 30)
188
189    delta = correct - result
190
191    if delta.days != 0 or delta.seconds != 0:
192        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
193
194    # 10. test
195    print("Test 10")
196    result = adjust_datetime_to_granularity(dt, "3 months, 5 days, 3 minutes")
197    correct = datetime(2001, 8, 8, 12, 30, 0)
198
199    delta = correct - result
200
201    if delta.days != 0 or delta.seconds != 0:
202        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
203
204    # 11. test
205    print("Test 11")
206    result = adjust_datetime_to_granularity(dt, "3 weeks, 5 days")
207    correct = datetime(2001, 8, 8, 0, 0, 0)
208
209    delta = correct - result
210
211    if delta.days != 0 or delta.seconds != 0:
212        core.fatal("Granularity adjustment computation is wrong %s" % (delta))
213
214###############################################################################
215
216
217def test_compute_datetime_delta():
218
219    print("Test 1")
220    start = datetime(2001, 1, 1, 0, 0, 0)
221    end = datetime(2001, 1, 1, 0, 0, 0)
222
223    comp = compute_datetime_delta(start, end)
224
225    result = comp["second"]
226    correct = 0
227
228    delta = correct - result
229
230    if delta != 0:
231        core.fatal("Compute datetime delta is wrong %s" % (delta))
232
233    print("Test 2")
234    start = datetime(2001, 1, 1, 0, 0, 14)
235    end = datetime(2001, 1, 1, 0, 0, 44)
236
237    comp = compute_datetime_delta(start, end)
238
239    result = comp["second"]
240    correct = 30
241
242    delta = correct - result
243
244    if delta != 0:
245        core.fatal("Compute datetime delta is wrong %s" % (delta))
246
247    print("Test 3")
248    start = datetime(2001, 1, 1, 0, 0, 44)
249    end = datetime(2001, 1, 1, 0, 1, 14)
250
251    comp = compute_datetime_delta(start, end)
252
253    result = comp["second"]
254    correct = 30
255
256    delta = correct - result
257
258    if delta != 0:
259        core.fatal("Compute datetime delta is wrong %s" % (delta))
260
261    print("Test 4")
262    start = datetime(2001, 1, 1, 0, 0, 30)
263    end = datetime(2001, 1, 1, 0, 5, 30)
264
265    comp = compute_datetime_delta(start, end)
266
267    result = comp["second"]
268    correct = 300
269
270    delta = correct - result
271
272    if delta != 0:
273        core.fatal("Compute datetime delta is wrong %s" % (delta))
274
275    print("Test 5")
276    start = datetime(2001, 1, 1, 0, 0, 0)
277    end = datetime(2001, 1, 1, 0, 1, 0)
278
279    comp = compute_datetime_delta(start, end)
280
281    result = comp["minute"]
282    correct = 1
283
284    delta = correct - result
285
286    if delta != 0:
287        core.fatal("Compute datetime delta is wrong %s" % (delta))
288
289    print("Test 6")
290    start = datetime(2011, 10, 31, 0, 45, 0)
291    end = datetime(2011, 10, 31, 1, 45, 0)
292
293    comp = compute_datetime_delta(start, end)
294
295    result = comp["minute"]
296    correct = 60
297
298    delta = correct - result
299
300    if delta != 0:
301        core.fatal("Compute datetime delta is wrong %s" % (delta))
302
303    print("Test 7")
304    start = datetime(2011, 10, 31, 0, 45, 0)
305    end = datetime(2011, 10, 31, 1, 15, 0)
306
307    comp = compute_datetime_delta(start, end)
308
309    result = comp["minute"]
310    correct = 30
311
312    delta = correct - result
313
314    if delta != 0:
315        core.fatal("Compute datetime delta is wrong %s" % (delta))
316
317    print("Test 8")
318    start = datetime(2011, 10, 31, 0, 45, 0)
319    end = datetime(2011, 10, 31, 12, 15, 0)
320
321    comp = compute_datetime_delta(start, end)
322
323    result = comp["minute"]
324    correct = 690
325
326    delta = correct - result
327
328    if delta != 0:
329        core.fatal("Compute datetime delta is wrong %s" % (delta))
330
331    print("Test 9")
332    start = datetime(2011, 10, 31, 0, 0, 0)
333    end = datetime(2011, 10, 31, 1, 0, 0)
334
335    comp = compute_datetime_delta(start, end)
336
337    result = comp["hour"]
338    correct = 1
339
340    delta = correct - result
341
342    if delta != 0:
343        core.fatal("Compute datetime delta is wrong %s" % (delta))
344
345    print("Test 10")
346    start = datetime(2011, 10, 31, 0, 0, 0)
347    end = datetime(2011, 11, 1, 1, 0, 0)
348
349    comp = compute_datetime_delta(start, end)
350
351    result = comp["hour"]
352    correct = 25
353
354    delta = correct - result
355
356    if delta != 0:
357        core.fatal("Compute datetime delta is wrong %s" % (delta))
358
359    print("Test 11")
360    start = datetime(2011, 10, 31, 12, 0, 0)
361    end = datetime(2011, 11, 1, 6, 0, 0)
362
363    comp = compute_datetime_delta(start, end)
364
365    result = comp["hour"]
366    correct = 18
367
368    delta = correct - result
369
370    if delta != 0:
371        core.fatal("Compute datetime delta is wrong %s" % (delta))
372
373    print("Test 12")
374    start = datetime(2011, 11, 1, 0, 0, 0)
375    end = datetime(2011, 12, 1, 1, 0, 0)
376
377    comp = compute_datetime_delta(start, end)
378
379    result = comp["hour"]
380    correct = 30 * 24 + 1
381
382    delta = correct - result
383
384    if delta != 0:
385        core.fatal("Compute datetime delta is wrong %s" % (delta))
386
387    print("Test 13")
388    start = datetime(2011, 11, 1, 0, 0, 0)
389    end = datetime(2011, 11, 5, 0, 0, 0)
390
391    comp = compute_datetime_delta(start, end)
392
393    result = comp["day"]
394    correct = 4
395
396    delta = correct - result
397
398    if delta != 0:
399        core.fatal("Compute datetime delta is wrong %s" % (delta))
400
401    print("Test 14")
402    start = datetime(2011, 10, 6, 0, 0, 0)
403    end = datetime(2011, 11, 5, 0, 0, 0)
404
405    comp = compute_datetime_delta(start, end)
406
407    result = comp["day"]
408    correct = 30
409
410    delta = correct - result
411
412    if delta != 0:
413        core.fatal("Compute datetime delta is wrong %s" % (delta))
414
415    print("Test 15")
416    start = datetime(2011, 12, 2, 0, 0, 0)
417    end = datetime(2012, 1, 1, 0, 0, 0)
418
419    comp = compute_datetime_delta(start, end)
420
421    result = comp["day"]
422    correct = 30
423
424    delta = correct - result
425
426    if delta != 0:
427        core.fatal("Compute datetime delta is wrong %s" % (delta))
428
429    print("Test 16")
430    start = datetime(2011, 1, 1, 0, 0, 0)
431    end = datetime(2011, 2, 1, 0, 0, 0)
432
433    comp = compute_datetime_delta(start, end)
434
435    result = comp["month"]
436    correct = 1
437
438    delta = correct - result
439
440    if delta != 0:
441        core.fatal("Compute datetime delta is wrong %s" % (delta))
442
443    print("Test 17")
444    start = datetime(2011, 12, 1, 0, 0, 0)
445    end = datetime(2012, 1, 1, 0, 0, 0)
446
447    comp = compute_datetime_delta(start, end)
448
449    result = comp["month"]
450    correct = 1
451
452    delta = correct - result
453
454    if delta != 0:
455        core.fatal("Compute datetime delta is wrong %s" % (delta))
456
457    print("Test 18")
458    start = datetime(2011, 12, 1, 0, 0, 0)
459    end = datetime(2012, 6, 1, 0, 0, 0)
460
461    comp = compute_datetime_delta(start, end)
462
463    result = comp["month"]
464    correct = 6
465
466    delta = correct - result
467
468    if delta != 0:
469        core.fatal("Compute datetime delta is wrong %s" % (delta))
470
471    print("Test 19")
472    start = datetime(2011, 6, 1, 0, 0, 0)
473    end = datetime(2021, 6, 1, 0, 0, 0)
474
475    comp = compute_datetime_delta(start, end)
476
477    result = comp["year"]
478    correct = 10
479
480    delta = correct - result
481
482    if delta != 0:
483        core.fatal("Compute datetime delta is wrong %s" % (delta))
484
485    print("Test 20")
486    start = datetime(2011, 6, 1, 0, 0, 0)
487    end = datetime(2012, 6, 1, 12, 0, 0)
488
489    comp = compute_datetime_delta(start, end)
490
491    result = comp["hour"]
492    d = end - start
493    correct = 12 + d.days * 24
494
495    delta = correct - result
496
497    if delta != 0:
498        core.fatal("Compute datetime delta is wrong %s" % (delta))
499
500    print("Test 21")
501    start = datetime(2011, 6, 1, 0, 0, 0)
502    end = datetime(2012, 6, 1, 12, 30, 0)
503
504    comp = compute_datetime_delta(start, end)
505
506    result = comp["minute"]
507    d = end - start
508    correct = d.days * 24 * 60 + 12 * 60 + 30
509
510    delta = correct - result
511
512    if delta != 0:
513        core.fatal("Compute datetime delta is wrong %s" % (delta))
514
515    print("Test 22")
516    start = datetime(2011, 6, 1, 0, 0, 0)
517    end = datetime(2012, 6, 1, 12, 0, 5)
518
519    comp = compute_datetime_delta(start, end)
520
521    result = comp["second"]
522    d = end - start
523    correct = 5 + 60 * 60 * 12 + d.days * 24 * 60 * 60
524
525    delta = correct - result
526
527    if delta != 0:
528        core.fatal("Compute datetime delta is wrong %s" % (delta))
529
530    print("Test 23")
531    start = datetime(2011, 6, 1, 0, 0, 0)
532    end = datetime(2012, 6, 1, 0, 30, 0)
533
534    comp = compute_datetime_delta(start, end)
535
536    result = comp["minute"]
537    d = end - start
538    correct = 30 + d.days * 24 * 60
539
540    delta = correct - result
541
542    if delta != 0:
543        core.fatal("Compute datetime delta is wrong %s" % (delta))
544
545    print("Test 24")
546    start = datetime(2011, 6, 1, 0, 0, 0)
547    end = datetime(2012, 6, 1, 0, 0, 5)
548
549    comp = compute_datetime_delta(start, end)
550
551    result = comp["second"]
552    d = end - start
553    correct = 5 + d.days * 24 * 60 * 60
554
555    delta = correct - result
556
557    if delta != 0:
558        core.fatal("Compute datetime delta is wrong %s" % (delta))
559
560
561def test_compute_absolute_time_granularity():
562
563    # First we test intervals
564    print("Test 1")
565    maps = []
566    a = datetime(2001, 1, 1)
567    increment = "1 year"
568    for i in range(10):
569        start = increment_datetime_by_string(a, increment, i)
570        end = increment_datetime_by_string(a, increment, i + 1)
571        map = RasterDataset(None)
572        map.set_absolute_time(start, end)
573        maps.append(map)
574
575    gran = compute_absolute_time_granularity(maps)
576    if increment != gran:
577        core.fatal("Wrong granularity reference %s != gran %s" % (
578            increment, gran))
579
580    print("Test 2")
581    maps = []
582    a = datetime(2001, 1, 1)
583    increment = "3 years"
584    for i in range(10):
585        start = increment_datetime_by_string(a, increment, i)
586        end = increment_datetime_by_string(a, increment, i + 1)
587        map = RasterDataset(None)
588        map.set_absolute_time(start, end)
589        maps.append(map)
590
591    gran = compute_absolute_time_granularity(maps)
592    if increment != gran:
593        core.fatal("Wrong granularity reference %s != gran %s" % (
594            increment, gran))
595
596    print("Test 3")
597    maps = []
598    a = datetime(2001, 5, 1)
599    increment = "1 month"
600    for i in range(20):
601        start = increment_datetime_by_string(a, increment, i)
602        end = increment_datetime_by_string(a, increment, i + 1)
603        map = RasterDataset(None)
604        map.set_absolute_time(start, end)
605        maps.append(map)
606
607    gran = compute_absolute_time_granularity(maps)
608    if increment != gran:
609        core.fatal("Wrong granularity reference %s != gran %s" % (
610            increment, gran))
611
612    print("Test 4")
613    maps = []
614    a = datetime(2001, 1, 1)
615    increment = "3 months"
616    for i in range(20):
617        start = increment_datetime_by_string(a, increment, i)
618        end = increment_datetime_by_string(a, increment, i + 1)
619        map = RasterDataset(None)
620        map.set_absolute_time(start, end)
621        maps.append(map)
622
623    gran = compute_absolute_time_granularity(maps)
624    if increment != gran:
625        core.fatal("Wrong granularity reference %s != gran %s" % (
626            increment, gran))
627
628    print("Test 3")
629    maps = []
630    a = datetime(2001, 1, 1)
631    increment = "1 day"
632    for i in range(6):
633        start = increment_datetime_by_string(a, increment, i)
634        end = increment_datetime_by_string(a, increment, i + 1)
635        map = RasterDataset(None)
636        map.set_absolute_time(start, end)
637        maps.append(map)
638
639    gran = compute_absolute_time_granularity(maps)
640    if increment != gran:
641        core.fatal("Wrong granularity reference %s != gran %s" % (
642            increment, gran))
643
644    print("Test 4")
645    maps = []
646    a = datetime(2001, 1, 14)
647    increment = "14 days"
648    for i in range(6):
649        start = increment_datetime_by_string(a, increment, i)
650        end = increment_datetime_by_string(a, increment, i + 1)
651        map = RasterDataset(None)
652        map.set_absolute_time(start, end)
653        maps.append(map)
654
655    gran = compute_absolute_time_granularity(maps)
656    if increment != gran:
657        core.fatal("Wrong granularity reference %s != gran %s" % (
658            increment, gran))
659
660    print("Test 5")
661    maps = []
662    a = datetime(2001, 3, 1)
663    increment = "1 month, 4 days"
664    for i in range(20):
665        start = increment_datetime_by_string(a, increment, i)
666        end = increment_datetime_by_string(a, increment, i + 1)
667        map = RasterDataset(None)
668        map.set_absolute_time(start, end)
669        maps.append(map)
670
671    increment = "1 day"
672    gran = compute_absolute_time_granularity(maps)
673    if increment != gran:
674        core.fatal("Wrong granularity reference %s != gran %s" % (
675            increment, gran))
676
677    print("Test 6")
678    maps = []
679    a = datetime(2001, 2, 11)
680    increment = "1 days, 1 hours"
681    for i in range(20):
682        start = increment_datetime_by_string(a, increment, i)
683        end = increment_datetime_by_string(a, increment, i + 1)
684        map = RasterDataset(None)
685        map.set_absolute_time(start, end)
686        maps.append(map)
687
688    increment = "25 hours"
689    gran = compute_absolute_time_granularity(maps)
690    if increment != gran:
691        core.fatal("Wrong granularity reference %s != gran %s" % (
692            increment, gran))
693
694    print("Test 7")
695    maps = []
696    a = datetime(2001, 6, 12)
697    increment = "6 hours"
698    for i in range(20):
699        start = increment_datetime_by_string(a, increment, i)
700        end = increment_datetime_by_string(a, increment, i + 1)
701        map = RasterDataset(None)
702        map.set_absolute_time(start, end)
703        maps.append(map)
704
705    gran = compute_absolute_time_granularity(maps)
706    if increment != gran:
707        core.fatal("Wrong granularity reference %s != gran %s" % (
708            increment, gran))
709
710    print("Test 8")
711    maps = []
712    a = datetime(2001, 1, 1)
713    increment = "20 minutes"
714    for i in range(20):
715        start = increment_datetime_by_string(a, increment, i)
716        end = increment_datetime_by_string(a, increment, i + 1)
717        map = RasterDataset(None)
718        map.set_absolute_time(start, end)
719        maps.append(map)
720
721    gran = compute_absolute_time_granularity(maps)
722    if increment != gran:
723        core.fatal("Wrong granularity reference %s != gran %s" % (
724            increment, gran))
725
726    print("Test 9")
727    maps = []
728    a = datetime(2001, 1, 1)
729    increment = "5 hours, 25 minutes"
730    for i in range(20):
731        start = increment_datetime_by_string(a, increment, i)
732        end = increment_datetime_by_string(a, increment, i + 1)
733        map = RasterDataset(None)
734        map.set_absolute_time(start, end)
735        maps.append(map)
736
737    increment = "325 minutes"
738    gran = compute_absolute_time_granularity(maps)
739    if increment != gran:
740        core.fatal("Wrong granularity reference %s != gran %s" % (
741            increment, gran))
742
743    print("Test 10")
744    maps = []
745    a = datetime(2001, 1, 1)
746    increment = "5 minutes, 30 seconds"
747    for i in range(20):
748        start = increment_datetime_by_string(a, increment, i)
749        end = increment_datetime_by_string(a, increment, i + 1)
750        map = RasterDataset(None)
751        map.set_absolute_time(start, end)
752        maps.append(map)
753
754    increment = "330 seconds"
755    gran = compute_absolute_time_granularity(maps)
756    if increment != gran:
757        core.fatal("Wrong granularity reference %s != gran %s" % (
758            increment, gran))
759
760    print("Test 11")
761    maps = []
762    a = datetime(2001, 12, 31)
763    increment = "60 minutes, 30 seconds"
764    for i in range(24):
765        start = increment_datetime_by_string(a, increment, i)
766        end = increment_datetime_by_string(a, increment, i + 1)
767        map = RasterDataset(None)
768        map.set_absolute_time(start, end)
769        maps.append(map)
770
771    increment = "3630 seconds"
772    gran = compute_absolute_time_granularity(maps)
773    if increment != gran:
774        core.fatal("Wrong granularity reference %s != gran %s" % (
775            increment, gran))
776
777    print("Test 12")
778    maps = []
779    a = datetime(2001, 12, 31, 12, 30, 30)
780    increment = "3600 seconds"
781    for i in range(24):
782        start = increment_datetime_by_string(a, increment, i)
783        end = increment_datetime_by_string(a, increment, i + 1)
784        print(start)
785        print(end)
786        map = RasterDataset(None)
787        map.set_absolute_time(start, end)
788        maps.append(map)
789
790    gran = compute_absolute_time_granularity(maps)
791    if increment != gran:
792        core.fatal("Wrong granularity reference %s != gran %s" % (
793            increment, gran))
794
795    # Test absolute time points
796
797    print("Test 13")
798    maps = []
799    a = datetime(2001, 12, 31, 12, 30, 30)
800    increment = "3600 seconds"
801    for i in range(24):
802        start = increment_datetime_by_string(a, increment, i)
803        end = None
804        map = RasterDataset(None)
805        map.set_absolute_time(start, end)
806        maps.append(map)
807
808    gran = compute_absolute_time_granularity(maps)
809    if increment != gran:
810        core.fatal("Wrong granularity reference %s != gran %s" % (
811            increment, gran))
812
813    print("Test 14")
814    maps = []
815    a = datetime(2001, 12, 31, 0, 0, 0)
816    increment = "20 days"
817    for i in range(24):
818        start = increment_datetime_by_string(a, increment, i)
819        end = None
820        map = RasterDataset(None)
821        map.set_absolute_time(start, end)
822        maps.append(map)
823
824    gran = compute_absolute_time_granularity(maps)
825    if increment != gran:
826        core.fatal("Wrong granularity reference %s != gran %s" % (
827            increment, gran))
828
829    print("Test 15")
830    maps = []
831    a = datetime(2001, 12, 1, 0, 0, 0)
832    increment = "5 months"
833    for i in range(24):
834        start = increment_datetime_by_string(a, increment, i)
835        end = None
836        map = RasterDataset(None)
837        map.set_absolute_time(start, end)
838        maps.append(map)
839
840    gran = compute_absolute_time_granularity(maps)
841    if increment != gran:
842        core.fatal("Wrong granularity reference %s != gran %s" % (
843            increment, gran))
844
845    # Test absolute time interval and points
846
847    print("Test 16")
848    maps = []
849    a = datetime(2001, 12, 31, 12, 30, 30)
850    increment = "3600 seconds"
851
852    for i in range(24):
853        start = increment_datetime_by_string(a, increment, i)
854        end = increment_datetime_by_string(a, increment, i + 1)
855        map = RasterDataset(None)
856        map.set_absolute_time(start, end)
857        maps.append(map)
858
859    a = datetime(2002, 2, 1, 12, 30, 30)
860    for i in range(24):
861        start = increment_datetime_by_string(a, increment, i)
862        end = None
863        map = RasterDataset(None)
864        map.set_absolute_time(start, end)
865        maps.append(map)
866
867    gran = compute_absolute_time_granularity(maps)
868    if increment != gran:
869        core.fatal("Wrong granularity reference %s != gran %s" % (
870            increment, gran))
871
872    print("Test 17")
873    maps = []
874    a = datetime(2001, 1, 1)
875    increment = "2 days"
876
877    for i in range(8):
878        start = increment_datetime_by_string(a, increment, i)
879        end = increment_datetime_by_string(a, increment, i + 1)
880        map = RasterDataset(None)
881        map.set_absolute_time(start, end)
882        maps.append(map)
883
884    a = datetime(2001, 2, 2)
885    for i in range(8):
886        start = increment_datetime_by_string(a, increment, i)
887        end = None
888        map = RasterDataset(None)
889        map.set_absolute_time(start, end)
890        maps.append(map)
891
892    gran = compute_absolute_time_granularity(maps)
893    if increment != gran:
894        core.fatal("Wrong granularity reference %s != gran %s" % (
895            increment, gran))
896
897###############################################################################
898
899
900def test_spatial_extent_intersection():
901    # Generate the extents
902
903    A = SpatialExtent(
904        north=80, south=20, east=60, west=10, bottom=-50, top=50)
905    A.print_info()
906    B = SpatialExtent(
907        north=80, south=20, east=60, west=10, bottom=-50, top=50)
908    B.print_info()
909    C = A.intersect(B)
910    C.print_info()
911
912    if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
913        C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
914        C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
915        core.fatal("Wrong intersection computation")
916
917    B = SpatialExtent(
918        north=40, south=30, east=60, west=10, bottom=-50, top=50)
919    B.print_info()
920    C = A.intersect(B)
921    C.print_info()
922
923    if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
924       C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
925       C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
926        core.fatal("Wrong intersection computation")
927
928    B = SpatialExtent(
929        north=40, south=30, east=60, west=30, bottom=-50, top=50)
930    B.print_info()
931    C = A.intersect(B)
932    C.print_info()
933
934    if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
935       C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
936       C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
937        core.fatal("Wrong intersection computation")
938
939    B = SpatialExtent(
940        north=40, south=30, east=60, west=30, bottom=-30, top=50)
941    B.print_info()
942    C = A.intersect(B)
943    C.print_info()
944
945    if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
946       C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
947       C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
948        core.fatal("Wrong intersection computation")
949
950    B = SpatialExtent(
951        north=40, south=30, east=60, west=30, bottom=-30, top=30)
952    B.print_info()
953    C = A.intersect(B)
954    C.print_info()
955
956    if C.get_north() != B.get_north() or C.get_south() != B.get_south() or \
957       C.get_west() != B.get_west() or C.get_east() != B.get_east() or \
958       C.get_bottom() != B.get_bottom() or C.get_top() != B.get_top():
959        core.fatal("Wrong intersection computation")
960
961###############################################################################
962
963
964def test_spatial_relations():
965    # Generate the extents
966
967    A = SpatialExtent(
968        north=80, south=20, east=60, west=10, bottom=-50, top=50)
969    A.print_info()
970    B = SpatialExtent(
971        north=80, south=20, east=60, west=10, bottom=-50, top=50)
972    B.print_info()
973
974    relation = A.spatial_relation(B)
975    print(relation)
976    if relation != "equivalent":
977        core.fatal("Wrong spatial relation: %s" % (relation))
978
979    B = SpatialExtent(
980        north=70, south=20, east=60, west=10, bottom=-50, top=50)
981    B.print_info()
982
983    relation = A.spatial_relation_2d(B)
984    print(relation)
985    if relation != "cover":
986        core.fatal("Wrong spatial relation: %s" % (relation))
987
988    relation = A.spatial_relation(B)
989    print(relation)
990    if relation != "cover":
991        core.fatal("Wrong spatial relation: %s" % (relation))
992
993    B = SpatialExtent(
994        north=70, south=30, east=60, west=10, bottom=-50, top=50)
995    B.print_info()
996
997    relation = A.spatial_relation_2d(B)
998    print(relation)
999    if relation != "cover":
1000        core.fatal("Wrong spatial relation: %s" % (relation))
1001
1002    relation = A.spatial_relation(B)
1003    print(relation)
1004    if relation != "cover":
1005        core.fatal("Wrong spatial relation: %s" % (relation))
1006
1007    relation = B.spatial_relation_2d(A)
1008    print(relation)
1009    if relation != "covered":
1010        core.fatal("Wrong spatial relation: %s" % (relation))
1011
1012    relation = B.spatial_relation(A)
1013    print(relation)
1014    if relation != "covered":
1015        core.fatal("Wrong spatial relation: %s" % (relation))
1016
1017    B = SpatialExtent(
1018        north=70, south=30, east=50, west=10, bottom=-50, top=50)
1019    B.print_info()
1020
1021    relation = A.spatial_relation_2d(B)
1022    print(relation)
1023    if relation != "cover":
1024        core.fatal("Wrong spatial relation: %s" % (relation))
1025
1026    relation = B.spatial_relation_2d(A)
1027    print(relation)
1028    if relation != "covered":
1029        core.fatal("Wrong spatial relation: %s" % (relation))
1030
1031    relation = A.spatial_relation(B)
1032    print(relation)
1033    if relation != "cover":
1034        core.fatal("Wrong spatial relation: %s" % (relation))
1035
1036    B = SpatialExtent(
1037        north=70, south=30, east=50, west=20, bottom=-50, top=50)
1038
1039    relation = B.spatial_relation(A)
1040    print(relation)
1041    if relation != "covered":
1042        core.fatal("Wrong spatial relation: %s" % (relation))
1043
1044    B = SpatialExtent(
1045        north=70, south=30, east=50, west=20, bottom=-50, top=50)
1046    B.print_info()
1047
1048    relation = A.spatial_relation_2d(B)
1049    print(relation)
1050    if relation != "contain":
1051        core.fatal("Wrong spatial relation: %s" % (relation))
1052
1053    relation = A.spatial_relation(B)
1054    print(relation)
1055    if relation != "cover":
1056        core.fatal("Wrong spatial relation: %s" % (relation))
1057
1058    B = SpatialExtent(
1059        north=70, south=30, east=50, west=20, bottom=-40, top=50)
1060    B.print_info()
1061
1062    relation = A.spatial_relation(B)
1063    print(relation)
1064    if relation != "cover":
1065        core.fatal("Wrong spatial relation: %s" % (relation))
1066
1067    B = SpatialExtent(
1068        north=70, south=30, east=50, west=20, bottom=-40, top=40)
1069    B.print_info()
1070
1071    relation = A.spatial_relation(B)
1072    print(relation)
1073    if relation != "contain":
1074        core.fatal("Wrong spatial relation: %s" % (relation))
1075
1076    relation = B.spatial_relation(A)
1077    print(relation)
1078    if relation != "in":
1079        core.fatal("Wrong spatial relation: %s" % (relation))
1080
1081    B = SpatialExtent(
1082        north=90, south=30, east=50, west=20, bottom=-40, top=40)
1083    B.print_info()
1084
1085    relation = A.spatial_relation_2d(B)
1086    print(relation)
1087    if relation != "overlap":
1088        core.fatal("Wrong spatial relation: %s" % (relation))
1089
1090    relation = A.spatial_relation(B)
1091    print(relation)
1092    if relation != "overlap":
1093        core.fatal("Wrong spatial relation: %s" % (relation))
1094
1095    B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=40)
1096    A.print_info()
1097    B.print_info()
1098
1099    relation = A.spatial_relation_2d(B)
1100    print(relation)
1101    if relation != "in":
1102        core.fatal("Wrong spatial relation: %s" % (relation))
1103
1104    relation = A.spatial_relation(B)
1105    print(relation)
1106    if relation != "overlap":
1107        core.fatal("Wrong spatial relation: %s" % (relation))
1108
1109    B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-40, top=60)
1110    A.print_info()
1111    B.print_info()
1112
1113    relation = A.spatial_relation(B)
1114    print(relation)
1115    if relation != "overlap":
1116        core.fatal("Wrong spatial relation: %s" % (relation))
1117
1118    B = SpatialExtent(north=90, south=5, east=70, west=5, bottom=-60, top=60)
1119    A.print_info()
1120    B.print_info()
1121
1122    relation = A.spatial_relation(B)
1123    print(relation)
1124    if relation != "in":
1125        core.fatal("Wrong spatial relation: %s" % (relation))
1126
1127    A = SpatialExtent(
1128        north=80, south=60, east=60, west=10, bottom=-50, top=50)
1129    A.print_info()
1130    B = SpatialExtent(
1131        north=60, south=20, east=60, west=10, bottom=-50, top=50)
1132    B.print_info()
1133
1134    relation = A.spatial_relation_2d(B)
1135    print(relation)
1136    if relation != "meet":
1137        core.fatal("Wrong spatial relation: %s" % (relation))
1138
1139    relation = A.spatial_relation(B)
1140    print(relation)
1141    if relation != "meet":
1142        core.fatal("Wrong spatial relation: %s" % (relation))
1143
1144    A = SpatialExtent(
1145        north=60, south=40, east=60, west=10, bottom=-50, top=50)
1146    A.print_info()
1147    B = SpatialExtent(
1148        north=80, south=60, east=60, west=10, bottom=-50, top=50)
1149    B.print_info()
1150
1151    relation = A.spatial_relation_2d(B)
1152    print(relation)
1153    if relation != "meet":
1154        core.fatal("Wrong spatial relation: %s" % (relation))
1155
1156    relation = A.spatial_relation(B)
1157    print(relation)
1158    if relation != "meet":
1159        core.fatal("Wrong spatial relation: %s" % (relation))
1160
1161    A = SpatialExtent(
1162        north=80, south=40, east=60, west=40, bottom=-50, top=50)
1163    A.print_info()
1164    B = SpatialExtent(
1165        north=80, south=40, east=40, west=20, bottom=-50, top=50)
1166    B.print_info()
1167
1168    relation = A.spatial_relation_2d(B)
1169    print(relation)
1170    if relation != "meet":
1171        core.fatal("Wrong spatial relation: %s" % (relation))
1172
1173    relation = A.spatial_relation(B)
1174    print(relation)
1175    if relation != "meet":
1176        core.fatal("Wrong spatial relation: %s" % (relation))
1177
1178    A = SpatialExtent(
1179        north=80, south=40, east=40, west=20, bottom=-50, top=50)
1180    A.print_info()
1181    B = SpatialExtent(
1182        north=90, south=30, east=60, west=40, bottom=-50, top=50)
1183    B.print_info()
1184
1185    relation = A.spatial_relation_2d(B)
1186    print(relation)
1187    if relation != "meet":
1188        core.fatal("Wrong spatial relation: %s" % (relation))
1189
1190    relation = A.spatial_relation(B)
1191    print(relation)
1192    if relation != "meet":
1193        core.fatal("Wrong spatial relation: %s" % (relation))
1194
1195    A = SpatialExtent(
1196        north=80, south=40, east=40, west=20, bottom=-50, top=50)
1197    A.print_info()
1198    B = SpatialExtent(
1199        north=70, south=50, east=60, west=40, bottom=-50, top=50)
1200    B.print_info()
1201
1202    relation = A.spatial_relation_2d(B)
1203    print(relation)
1204    if relation != "meet":
1205        core.fatal("Wrong spatial relation: %s" % (relation))
1206
1207    relation = A.spatial_relation(B)
1208    print(relation)
1209    if relation != "meet":
1210        core.fatal("Wrong spatial relation: %s" % (relation))
1211
1212    A = SpatialExtent(
1213        north=80, south=40, east=40, west=20, bottom=-50, top=50)
1214    A.print_info()
1215    B = SpatialExtent(
1216        north=60, south=20, east=60, west=40, bottom=-50, top=50)
1217    B.print_info()
1218
1219    relation = A.spatial_relation_2d(B)
1220    print(relation)
1221    if relation != "meet":
1222        core.fatal("Wrong spatial relation: %s" % (relation))
1223
1224    relation = A.spatial_relation(B)
1225    print(relation)
1226    if relation != "meet":
1227        core.fatal("Wrong spatial relation: %s" % (relation))
1228
1229    A = SpatialExtent(
1230        north=80, south=40, east=40, west=20, bottom=-50, top=50)
1231    A.print_info()
1232    B = SpatialExtent(
1233        north=40, south=20, east=60, west=40, bottom=-50, top=50)
1234    B.print_info()
1235
1236    relation = A.spatial_relation_2d(B)
1237    print(relation)
1238    if relation != "disjoint":
1239        core.fatal("Wrong spatial relation: %s" % (relation))
1240
1241    relation = A.spatial_relation(B)
1242    print(relation)
1243    if relation != "disjoint":
1244        core.fatal("Wrong spatial relation: %s" % (relation))
1245
1246    A = SpatialExtent(
1247        north=80, south=40, east=40, west=20, bottom=-50, top=50)
1248    A.print_info()
1249    B = SpatialExtent(
1250        north=60, south=20, east=60, west=40, bottom=-60, top=60)
1251    B.print_info()
1252
1253    relation = A.spatial_relation(B)
1254    print(relation)
1255    if relation != "meet":
1256        core.fatal("Wrong spatial relation: %s" % (relation))
1257
1258    A = SpatialExtent(
1259        north=80, south=40, east=40, west=20, bottom=-50, top=50)
1260    A.print_info()
1261    B = SpatialExtent(
1262        north=90, south=30, east=60, west=40, bottom=-40, top=40)
1263    B.print_info()
1264
1265    relation = A.spatial_relation(B)
1266    print(relation)
1267    if relation != "meet":
1268        core.fatal("Wrong spatial relation: %s" % (relation))
1269
1270    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
1271    A.print_info()
1272    B = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
1273    B.print_info()
1274
1275    relation = A.spatial_relation(B)
1276    print(relation)
1277    if relation != "meet":
1278        core.fatal("Wrong spatial relation: %s" % (relation))
1279
1280    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
1281    A.print_info()
1282    B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=-50, top=0)
1283    B.print_info()
1284
1285    relation = A.spatial_relation(B)
1286    print(relation)
1287    if relation != "meet":
1288        core.fatal("Wrong spatial relation: %s" % (relation))
1289
1290    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
1291    A.print_info()
1292    B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=-50, top=0)
1293    B.print_info()
1294
1295    relation = A.spatial_relation(B)
1296    print(relation)
1297    if relation != "meet":
1298        core.fatal("Wrong spatial relation: %s" % (relation))
1299
1300    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
1301    A.print_info()
1302    B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=-50, top=0)
1303    B.print_info()
1304
1305    relation = A.spatial_relation(B)
1306    print(relation)
1307    if relation != "meet":
1308        core.fatal("Wrong spatial relation: %s" % (relation))
1309
1310    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
1311    A.print_info()
1312    B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=-50, top=0)
1313    B.print_info()
1314
1315    relation = A.spatial_relation(B)
1316    print(relation)
1317    if relation != "meet":
1318        core.fatal("Wrong spatial relation: %s" % (relation))
1319
1320    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
1321    A.print_info()
1322    B = SpatialExtent(north=80, south=40, east=60, west=20, bottom=0, top=50)
1323    B.print_info()
1324
1325    relation = A.spatial_relation(B)
1326    print(relation)
1327    if relation != "meet":
1328        core.fatal("Wrong spatial relation: %s" % (relation))
1329
1330    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
1331    A.print_info()
1332    B = SpatialExtent(north=80, south=50, east=60, west=30, bottom=0, top=50)
1333    B.print_info()
1334
1335    relation = A.spatial_relation(B)
1336    print(relation)
1337    if relation != "meet":
1338        core.fatal("Wrong spatial relation: %s" % (relation))
1339
1340    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
1341    A.print_info()
1342    B = SpatialExtent(north=70, south=50, east=50, west=30, bottom=0, top=50)
1343    B.print_info()
1344
1345    relation = A.spatial_relation(B)
1346    print(relation)
1347    if relation != "meet":
1348        core.fatal("Wrong spatial relation: %s" % (relation))
1349
1350    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
1351    A.print_info()
1352    B = SpatialExtent(north=90, south=30, east=70, west=10, bottom=0, top=50)
1353    B.print_info()
1354
1355    relation = A.spatial_relation(B)
1356    print(relation)
1357    if relation != "meet":
1358        core.fatal("Wrong spatial relation: %s" % (relation))
1359
1360    A = SpatialExtent(north=80, south=40, east=60, west=20, bottom=-50, top=0)
1361    A.print_info()
1362    B = SpatialExtent(north=70, south=30, east=50, west=10, bottom=0, top=50)
1363    B.print_info()
1364
1365    relation = A.spatial_relation(B)
1366    print(relation)
1367    if relation != "meet":
1368        core.fatal("Wrong spatial relation: %s" % (relation))
1369
1370###############################################################################
1371
1372
1373def test_temporal_topology_builder():
1374    map_listA = []
1375
1376    _map = RasterDataset(ident="1@a")
1377    _map.set_absolute_time(datetime(2001, 1, 1), datetime(2001, 2, 1))
1378    map_listA.append(copy.copy(_map))
1379    _map = RasterDataset(ident="2@a")
1380    _map.set_absolute_time(datetime(2001, 2, 1), datetime(2001, 3, 1))
1381    map_listA.append(copy.copy(_map))
1382    _map = RasterDataset(ident="3@a")
1383    _map.set_absolute_time(datetime(2001, 3, 1), datetime(2001, 4, 1))
1384    map_listA.append(copy.copy(_map))
1385    _map = RasterDataset(ident="4@a")
1386    _map.set_absolute_time(datetime(2001, 4, 1), datetime(2001, 5, 1))
1387    map_listA.append(copy.copy(_map))
1388    _map = RasterDataset(ident="5@a")
1389    _map.set_absolute_time(datetime(2001, 5, 1), datetime(2001, 6, 1))
1390    map_listA.append(copy.copy(_map))
1391
1392    tb = SpatioTemporalTopologyBuilder()
1393    tb.build(map_listA)
1394
1395    count = 0
1396    for _map in tb:
1397        print("[%s]" % (_map.get_name()))
1398        _map.print_topology_info()
1399        if _map.get_id() != map_listA[count].get_id():
1400            core.fatal("Error building temporal topology <%s> != <%s>" %
1401                       (_map.get_id(), map_listA[count].get_id()))
1402        count += 1
1403
1404    map_listB = []
1405
1406    _map = RasterDataset(ident="1@b")
1407    _map.set_absolute_time(datetime(2001, 1, 14), datetime(2001, 3, 14))
1408    map_listB.append(copy.copy(_map))
1409    _map = RasterDataset(ident="2@b")
1410    _map.set_absolute_time(datetime(2001, 2, 1), datetime(2001, 4, 1))
1411    map_listB.append(copy.copy(_map))
1412    _map = RasterDataset(ident="3@b")
1413    _map.set_absolute_time(datetime(2001, 2, 14), datetime(2001, 4, 30))
1414    map_listB.append(copy.copy(_map))
1415    _map = RasterDataset(ident="4@b")
1416    _map.set_absolute_time(datetime(2001, 4, 2), datetime(2001, 4, 30))
1417    map_listB.append(copy.copy(_map))
1418    _map = RasterDataset(ident="5@b")
1419    _map.set_absolute_time(datetime(2001, 5, 1), datetime(2001, 5, 14))
1420    map_listB.append(copy.copy(_map))
1421
1422    tb = SpatioTemporalTopologyBuilder()
1423    tb.build(map_listB)
1424
1425    # Probing some relations
1426    if map_listB[0].get_overlapped()[0] != map_listB[1]:
1427        core.fatal("Error building temporal topology")
1428    if map_listB[0].get_overlapped()[1] != map_listB[2]:
1429        core.fatal("Error building temporal topology")
1430    if map_listB[2].get_contains()[0] != map_listB[3]:
1431        core.fatal("Error building temporal topology")
1432    if map_listB[3].get_during()[0] != map_listB[2]:
1433        core.fatal("Error building temporal topology")
1434
1435    count = 0
1436    for _map in tb:
1437        print("[%s]" % (_map.get_map_id()))
1438        _map.print_topology_shell_info()
1439        if _map.get_id() != map_listB[count].get_id():
1440            core.fatal("Error building temporal topology <%s> != <%s>" %
1441                       (_map.get_id(), map_listB[count].get_id()))
1442        count += 1
1443
1444    tb = SpatioTemporalTopologyBuilder()
1445    tb.build(map_listA, map_listB)
1446
1447    count = 0
1448    for _map in tb:
1449        print("[%s]" % (_map.get_map_id()))
1450        _map.print_topology_shell_info()
1451        if _map.get_id() != map_listA[count].get_id():
1452            core.fatal("Error building temporal topology <%s> != <%s>" %
1453                       (_map.get_id(), map_listA[count].get_id()))
1454        count += 1
1455
1456    count = 0
1457    for _map in map_listB:
1458        print("[%s]" % (_map.get_map_id()))
1459        _map.print_topology_shell_info()
1460
1461    # Probing some relations
1462    if map_listA[3].get_follows()[0] != map_listB[1]:
1463        core.fatal("Error building temporal topology")
1464    if map_listA[3].get_precedes()[0] != map_listB[4]:
1465        core.fatal("Error building temporal topology")
1466    if map_listA[3].get_overlaps()[0] != map_listB[2]:
1467        core.fatal("Error building temporal topology")
1468    if map_listA[3].get_contains()[0] != map_listB[3]:
1469        core.fatal("Error building temporal topology")
1470
1471    if map_listA[2].get_during()[0] != map_listB[1]:
1472        core.fatal("Error building temporal topology")
1473    if map_listA[2].get_during()[1] != map_listB[2]:
1474        core.fatal("Error building temporal topology")
1475
1476###############################################################################
1477
1478
1479def test_map_list_sorting():
1480
1481    map_list = []
1482
1483    _map = RasterDataset(ident="1@a")
1484    _map.set_absolute_time(datetime(2001, 2, 1), datetime(2001, 3, 1))
1485    map_list.append(copy.copy(_map))
1486    _map = RasterDataset(ident="2@a")
1487    _map.set_absolute_time(datetime(2001, 1, 1), datetime(2001, 2, 1))
1488    map_list.append(copy.copy(_map))
1489    _map = RasterDataset(ident="3@a")
1490    _map.set_absolute_time(datetime(2001, 3, 1), datetime(2001, 4, 1))
1491    map_list.append(copy.copy(_map))
1492
1493    print("Original")
1494    for _map in map_list:
1495        print(_map.get_temporal_extent_as_tuple()[0], _map.get_temporal_extent_as_tuple()[1])
1496    print("Sorted by start time")
1497    new_list = sorted(map_list, key=AbstractDatasetComparisonKeyStartTime)
1498    for _map in new_list:
1499        print(_map.get_temporal_extent_as_tuple()[0], _map.get_temporal_extent_as_tuple()[1])
1500
1501    if new_list[0] != map_list[1]:
1502        core.fatal("Sorting by start time failed")
1503    if new_list[1] != map_list[0]:
1504        core.fatal("Sorting by start time failed")
1505    if new_list[2] != map_list[2]:
1506        core.fatal("Sorting by start time failed")
1507
1508    print("Sorted by end time")
1509    new_list = sorted(map_list, key=AbstractDatasetComparisonKeyEndTime)
1510    for _map in new_list:
1511        print(_map.get_temporal_extent_as_tuple()[0], _map.get_temporal_extent_as_tuple()[1])
1512
1513    if new_list[0] != map_list[1]:
1514        core.fatal("Sorting by end time failed")
1515    if new_list[1] != map_list[0]:
1516        core.fatal("Sorting by end time failed")
1517    if new_list[2] != map_list[2]:
1518        core.fatal("Sorting by end time failed")
1519
1520###############################################################################
1521
1522
1523def test_1d_rtree():
1524    """Testing the rtree ctypes wrapper"""
1525
1526    tree = rtree.RTreeCreateTree(-1, 0, 1)
1527
1528    for i in range(10):
1529
1530        rect = rtree.RTreeAllocRect(tree)
1531        rtree.RTreeSetRect1D(rect, tree, float(i - 2), float(i + 2))
1532        rtree.RTreeInsertRect(rect, i + 1, tree)
1533
1534    rect = rtree.RTreeAllocRect(tree)
1535    rtree.RTreeSetRect1D(rect, tree, 2.0, 7.0)
1536
1537    list_ = gis.ilist()
1538
1539    num = vector.RTreeSearch2(tree, rect, byref(list_))
1540
1541    rtree.RTreeFreeRect(rect)
1542
1543    # print rectangle ids
1544    print("Number of overlapping rectangles", num)
1545    for i in range(list_.n_values):
1546        print("id", list_.value[i])
1547
1548    rtree.RTreeDestroyTree(tree)
1549
1550###############################################################################
1551
1552
1553def test_2d_rtree():
1554    """Testing the rtree ctypes wrapper"""
1555
1556    tree = rtree.RTreeCreateTree(-1, 0, 2)
1557
1558    for i in range(10):
1559
1560        rect = rtree.RTreeAllocRect(tree)
1561
1562        rtree.RTreeSetRect2D(rect, tree,
1563                             float(i - 2), float(i + 2),
1564                             float(i - 2), float(i + 2))
1565        rtree.RTreeInsertRect(rect, i + 1, tree)
1566
1567    rect = rtree.RTreeAllocRect(tree)
1568    rtree.RTreeSetRect2D(rect, tree, 2.0, 7.0, 2.0, 7.0)
1569
1570    list_ = gis.ilist()
1571
1572    num = vector.RTreeSearch2(tree, rect, byref(list_))
1573    rtree.RTreeFreeRect(rect)
1574
1575    # print rectangle ids
1576    print("Number of overlapping rectangles", num)
1577    for i in range(list_.n_values):
1578        print("id", list_.value[i])
1579
1580    rtree.RTreeDestroyTree(tree)
1581
1582###############################################################################
1583
1584
1585def test_3d_rtree():
1586    """Testing the rtree ctypes wrapper"""
1587
1588    tree = rtree.RTreeCreateTree(-1, 0, 3)
1589
1590    for i in range(10):
1591
1592        rect = rtree.RTreeAllocRect(tree)
1593        rtree.RTreeSetRect3D(rect, tree,
1594                             float(i - 2), float(i + 2),
1595                             float(i - 2), float(i + 2),
1596                             float(i - 2), float(i + 2))
1597        rtree.RTreeInsertRect(rect, i + 1, tree)
1598        print(i + 1)
1599        rtree.RTreePrintRect(rect, 1, tree)
1600
1601    rect = rtree.RTreeAllocRect(tree)
1602    rtree.RTreeSetRect3D(rect, tree, 2.0, 7.0, 2.0, 7.0, 2.0, 7.0)
1603    print("Select")
1604    rtree.RTreePrintRect(rect, 1, tree)
1605
1606    list_ = gis.ilist()
1607
1608    num = vector.RTreeSearch2(tree, rect, byref(list_))
1609    rtree.RTreeFreeRect(rect)
1610
1611    # print rectangle ids
1612    print("Number of overlapping rectangles", num)
1613    for i in range(list_.n_values):
1614        print("id", list_.value[i])
1615
1616    rtree.RTreeDestroyTree(tree)
1617
1618###############################################################################
1619
1620
1621def test_4d_rtree():
1622    """Testing the rtree ctypes wrapper"""
1623
1624    tree = rtree.RTreeCreateTree(-1, 0, 4)
1625
1626    for i in range(10):
1627
1628        # Allocate the boundary
1629        rect = rtree.RTreeAllocRect(tree)
1630        rtree.RTreeSetRect4D(rect, tree,
1631                             float(i - 2), float(i + 2),
1632                             float(i - 2), float(i + 2),
1633                             float(i - 2), float(i + 2),
1634                             float(i - 2), float(i + 2))
1635        rtree.RTreeInsertRect(rect, i + 1, tree)
1636
1637    rect = rtree.RTreeAllocRect(tree)
1638    rtree.RTreeSetRect4D(rect, tree, 2.0, 7.0, 2.0,
1639                         7.0, 2.0, 7.0, 2.0, 7.0)
1640
1641    list_ = gis.ilist()
1642
1643    num = vector.RTreeSearch2(tree, rect, byref(list_))
1644
1645    rtree.RTreeFreeRect(rect)
1646
1647    # print rectangle ids
1648    print("Number of overlapping rectangles", num)
1649    for i in range(list_.n_values):
1650        print("id", list_.value[i])
1651
1652    rtree.RTreeDestroyTree(tree)
1653
1654###############################################################################
1655
1656if __name__ == "__main__":
1657    init()
1658    test_increment_datetime_by_string()
1659    test_adjust_datetime_to_granularity()
1660    test_spatial_extent_intersection()
1661    test_compute_absolute_time_granularity()
1662    test_compute_datetime_delta()
1663    test_spatial_extent_intersection()
1664    test_spatial_relations()
1665    test_temporal_topology_builder()
1666    test_map_list_sorting()
1667    test_1d_rtree()
1668    test_2d_rtree()
1669    test_3d_rtree()
1670    test_4d_rtree()
1671