-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
spatio_temporal_relationships.py
1099 lines (931 loc) · 48.1 KB
/
spatio_temporal_relationships.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Class to build the spatio-temporal topology between map lists
Usage:
.. code-block:: python
import grass.temporal as tgis
tgis.print_temporal_relations(maps)
(C) 2012-2013 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
:authors: Soeren Gebbert
"""
from __future__ import print_function
# i18N
import gettext
from datetime import datetime
from .core import init_dbif
from .abstract_dataset import AbstractDatasetComparisonKeyStartTime
from .datetime_math import time_delta_to_relative_time_seconds
import grass.lib.vector as vector
import grass.lib.rtree as rtree
import grass.lib.gis as gis
###############################################################################
class SpatioTemporalTopologyBuilder(object):
"""This class is designed to build the spatio-temporal topology
of spatio-temporally related abstract dataset objects.
The abstract dataset objects must be provided as a single list, or in
two lists.
Example:
.. code-block:: python
# We have a space time raster dataset and build a map list
# from all registered maps ordered by start time
maps = strds.get_registered_maps_as_objects()
# Now lets build the temporal topology of the maps in the list
tb = SpatioTemporalTopologyBuilder()
tb.build(maps)
dbif, connected = init_dbif(None)
for map in tb:
map.select(dbif)
map.print_info()
# Same can be done with the existing map list
# But be aware that this is might not be temporally ordered
for map in maps:
map.select(dbf)
map.print_info()
# Using the next and previous methods, we can iterate over the
# topological related maps in this way
first = tb.get_first()
while first:
first.print_topology_info()
first = first.next()
# Dictionary like accessed
map = tb["name@mapset"]
>>> # Example with two lists of maps
>>> import grass.temporal as tgis
>>> import datetime
>>> # Create two list of maps with equal time stamps
>>> mapsA = []
>>> mapsB = []
>>> for i in range(4):
... idA = "a%i@B"%(i)
... mapA = tgis.RasterDataset(idA)
... idB = "b%i@B"%(i)
... mapB = tgis.RasterDataset(idB)
... check = mapA.set_relative_time(i, i + 1, "months")
... check = mapB.set_relative_time(i, i + 1, "months")
... mapsA.append(mapA)
... mapsB.append(mapB)
>>> # Build the topology between the two map lists
>>> tb = SpatioTemporalTopologyBuilder()
>>> tb.build(mapsA, mapsB, None)
>>> # Check relations of mapsA
>>> for map in mapsA:
... if map.get_equal():
... relations = map.get_equal()
... print("Map %s has equal relation to map %s"%(map.get_name(),
... relations[0].get_name()))
Map a0 has equal relation to map b0
Map a1 has equal relation to map b1
Map a2 has equal relation to map b2
Map a3 has equal relation to map b3
>>> # Check relations of mapsB
>>> for map in mapsB:
... if map.get_equal():
... relations = map.get_equal()
... print("Map %s has equal relation to map %s"%(map.get_name(),
... relations[0].get_name()))
Map b0 has equal relation to map a0
Map b1 has equal relation to map a1
Map b2 has equal relation to map a2
Map b3 has equal relation to map a3
>>> mapsA = []
>>> mapsB = []
>>> for i in range(4):
... idA = "a%i@B"%(i)
... mapA = tgis.RasterDataset(idA)
... idB = "b%i@B"%(i)
... mapB = tgis.RasterDataset(idB)
... check = mapA.set_relative_time(i, i + 1, "months")
... check = mapB.set_relative_time(i + 1, i + 2, "months")
... mapsA.append(mapA)
... mapsB.append(mapB)
>>> # Build the topology between the two map lists
>>> tb = SpatioTemporalTopologyBuilder()
>>> tb.build(mapsA, mapsB, None)
>>> # Check relations of mapsA
>>> for map in mapsA:
... print(map.get_temporal_extent_as_tuple())
... m = map.get_temporal_relations()
... for key in m.keys():
... if key not in ["NEXT", "PREV"]:
... print((key, m[key][0].get_temporal_extent_as_tuple()))
(0, 1)
('PRECEDES', (1, 2))
(1, 2)
('PRECEDES', (2, 3))
('EQUAL', (1, 2))
(2, 3)
('FOLLOWS', (1, 2))
('PRECEDES', (3, 4))
('EQUAL', (2, 3))
(3, 4)
('FOLLOWS', (2, 3))
('EQUAL', (3, 4))
('PRECEDES', (4, 5))
>>> mapsA = []
>>> mapsB = []
>>> for i in range(4):
... idA = "a%i@B"%(i)
... mapA = tgis.RasterDataset(idA)
... idB = "b%i@B"%(i)
... mapB = tgis.RasterDataset(idB)
... start = datetime.datetime(2000 + i, 1, 1)
... end = datetime.datetime(2000 + i + 1, 1, 1)
... check = mapA.set_absolute_time(start, end)
... start = datetime.datetime(2000 + i + 1, 1, 1)
... end = datetime.datetime(2000 + i + 2, 1, 1)
... check = mapB.set_absolute_time(start, end)
... mapsA.append(mapA)
... mapsB.append(mapB)
>>> # Build the topology between the two map lists
>>> tb = SpatioTemporalTopologyBuilder()
>>> tb.build(mapsA, mapsB, None)
>>> # Check relations of mapsA
>>> for map in mapsA:
... print(map.get_temporal_extent_as_tuple())
... m = map.get_temporal_relations()
... for key in m.keys():
... if key not in ["NEXT", "PREV"]:
... print((key, m[key][0].get_temporal_extent_as_tuple()))
(datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2001, 1, 1, 0, 0))
('PRECEDES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0)))
(datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0))
('PRECEDES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('EQUAL', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0)))
(datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0))
('FOLLOWS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0)))
('PRECEDES', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('EQUAL', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
(datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0))
('FOLLOWS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('EQUAL', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('PRECEDES', (datetime.datetime(2004, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
>>> mapsA = []
>>> mapsB = []
>>> for i in range(4):
... idA = "a%i@B"%(i)
... mapA = tgis.RasterDataset(idA)
... idB = "b%i@B"%(i)
... mapB = tgis.RasterDataset(idB)
... start = datetime.datetime(2000 + i, 1, 1)
... end = datetime.datetime(2000 + i + 1, 1, 1)
... check = mapA.set_absolute_time(start, end)
... start = datetime.datetime(2000 + i, 1, 1)
... end = datetime.datetime(2000 + i + 3, 1, 1)
... check = mapB.set_absolute_time(start, end)
... mapsA.append(mapA)
... mapsB.append(mapB)
>>> # Build the topology between the two map lists
>>> tb = SpatioTemporalTopologyBuilder()
>>> tb.build(mapsA, mapsB, None)
>>> # Check relations of mapsA
>>> for map in mapsA:
... print(map.get_temporal_extent_as_tuple())
... m = map.get_temporal_relations()
... for key in m.keys():
... if key not in ["NEXT", "PREV"]:
... print((key, m[key][0].get_temporal_extent_as_tuple()))
(datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2001, 1, 1, 0, 0))
('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('PRECEDES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
(datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0))
('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('PRECEDES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
(datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0))
('PRECEDES', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
('FINISHES', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
(datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0))
('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('DURING', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('FINISHES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
>>> mapsA = []
>>> mapsB = []
>>> for i in range(4):
... idA = "a%i@B"%(i)
... mapA = tgis.RasterDataset(idA)
... idB = "b%i@B"%(i)
... mapB = tgis.RasterDataset(idB)
... start = datetime.datetime(2000 + i, 1, 1)
... end = datetime.datetime(2000 + i + 2, 1, 1)
... check = mapA.set_absolute_time(start, end)
... start = datetime.datetime(2000 + i, 1, 1)
... end = datetime.datetime(2000 + i + 3, 1, 1)
... check = mapB.set_absolute_time(start, end)
... mapsA.append(mapA)
... mapsB.append(mapB)
>>> # Build the topology between the two map lists
>>> tb = SpatioTemporalTopologyBuilder()
>>> tb.build(mapsA, mapsB, None)
>>> # Check relations of mapsA
>>> for map in mapsA:
... print(map.get_temporal_extent_as_tuple())
... m = map.get_temporal_relations()
... for key in m.keys():
... if key not in ["NEXT", "PREV"]:
... print((key, m[key][0].get_temporal_extent_as_tuple()))
(datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0))
('OVERLAPS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('PRECEDES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
(datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0))
('OVERLAPS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
('PRECEDES', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
('FINISHES', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
(datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0))
('OVERLAPS', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
('FINISHES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('DURING', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
(datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0))
('OVERLAPPED', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
('DURING', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
('FINISHES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
('STARTS', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
>>> mapsA = []
>>> mapsB = []
>>> for i in range(4):
... idA = "a%i@B"%(i)
... mapA = tgis.RasterDataset(idA)
... idB = "b%i@B"%(i)
... mapB = tgis.RasterDataset(idB)
... start = datetime.datetime(2000, 1, 1, 0, 0, i)
... end = datetime.datetime(2000, 1, 1, 0, 0, i + 2)
... check = mapA.set_absolute_time(start, end)
... start = datetime.datetime(2000, 1, 1, 0, 0, i + 1)
... end = datetime.datetime(2000, 1, 1, 0, 0, i + 3)
... check = mapB.set_absolute_time(start, end)
... mapsA.append(mapA)
... mapsB.append(mapB)
>>> # Build the topology between the two map lists
>>> tb = SpatioTemporalTopologyBuilder()
>>> tb.build(mapsA, mapsB, None)
>>> # Check relations of mapsA
>>> for map in mapsA:
... print(map.get_temporal_extent_as_tuple())
... m = map.get_temporal_relations()
... for key in m.keys():
... if key not in ["NEXT", "PREV"]:
... print((key, m[key][0].get_temporal_extent_as_tuple()))
(datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2))
('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
(datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3))
('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
('EQUAL', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
(datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4))
('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 4), datetime.datetime(2000, 1, 1, 0, 0, 6)))
('EQUAL', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
(datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5))
('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 4), datetime.datetime(2000, 1, 1, 0, 0, 6)))
('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
('EQUAL', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
>>> mapsA = []
>>> for i in range(4):
... idA = "a%i@B"%(i)
... mapA = tgis.RasterDataset(idA)
... start = datetime.datetime(2000, 1, 1, 0, 0, i)
... end = datetime.datetime(2000, 1, 1, 0, 0, i + 2)
... check = mapA.set_absolute_time(start, end)
... mapsA.append(mapA)
>>> tb = SpatioTemporalTopologyBuilder()
>>> tb.build(mapsA)
>>> # Check relations of mapsA
>>> for map in mapsA:
... print(map.get_temporal_extent_as_tuple())
... m = map.get_temporal_relations()
... for key in m.keys():
... if key not in ["NEXT", "PREV"]:
... print((key, m[key][0].get_temporal_extent_as_tuple()))
(datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2))
('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
(datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3))
('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2)))
('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
(datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4))
('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2)))
('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
(datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5))
('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
"""
def __init__(self):
self._reset()
# 0001-01-01 00:00:00
self._timeref = datetime(1, 1, 1)
def _reset(self):
self._store = {}
self._first = None
self._iteratable = False
def _set_first(self, first):
self._first = first
self._insert(first)
def _detect_first(self):
if len(self) > 0:
prev_ = self._store.values()[0]
while prev_ is not None:
self._first = prev_
prev_ = prev_.prev()
def _insert(self, t):
self._store[t.get_id()] = t
def get_first(self):
"""Return the first map with the earliest start time
:return: The map with the earliest start time
"""
return self._first
def _build_internal_iteratable(self, maps, spatial):
"""Build an iteratable temporal topology structure for all maps in
the list and store the maps internally
Basically the "next" and "prev" relations will be set in the
temporal topology structure of each map
The maps will be added to the object, so they can be
accessed using the iterator of this class
:param maps: A sorted (by start_time)list of abstract_dataset
objects with initiated temporal extent
"""
self._build_iteratable(maps, spatial)
for _map in maps:
self._insert(_map)
# Detect the first map
self._detect_first()
def _build_iteratable(self, maps, spatial):
"""Build an iteratable temporal topology structure for
all maps in the list
Basically the "next" and "prev" relations will be set in
the temporal topology structure of each map.
:param maps: A sorted (by start_time)list of abstract_dataset
objects with initiated temporal extent
"""
# for i in xrange(len(maps)):
# offset = i + 1
# for j in xrange(offset, len(maps)):
# # Get the temporal relationship
# relation = maps[j].temporal_relation(maps[i])
#
# # Build the next reference
# if relation != "equal" and relation != "started":
# maps[i].set_next(maps[j])
# break
# First we need to order the map list chronologically
sorted_maps = sorted(
maps, key=AbstractDatasetComparisonKeyStartTime)
for i in range(len(sorted_maps) - 1):
sorted_maps[i].set_next(sorted_maps[i + 1])
for map_ in sorted_maps:
next_ = map_.next()
if next_:
next_.set_prev(map_)
map_.set_temporal_topology_build_true()
if spatial is not None:
map_.set_spatial_topology_build_true()
def _map_to_rect(self, tree, map_, spatial=None):
"""Use the spatio-temporal extent of a map to create and
return a RTree rectangle
:param spatial: This indicates if the spatial topology is created
as well: spatial can be None (no spatial topology),
"2D" using west, east, south, north or "3D" using
west, east, south, north, bottom, top
"""
rect = rtree.RTreeAllocRect(tree)
start, end = map_.get_temporal_extent_as_tuple()
if not end:
end = start
if map_.is_time_absolute():
start = time_delta_to_relative_time_seconds(start - self._timeref)
end = time_delta_to_relative_time_seconds(end - self._timeref)
if spatial is None:
rtree.RTreeSetRect1D(rect, tree, float(start), float(end))
elif spatial == "2D":
north, south, east, west, top, bottom = map_.get_spatial_extent_as_tuple()
rtree.RTreeSetRect3D(rect, tree, west, east, south, north,
float(start), float(end))
elif spatial == "3D":
north, south, east, west, top, bottom = map_.get_spatial_extent_as_tuple()
rtree.RTreeSetRect4D(rect, tree, west, east, south, north,
bottom, top, float(start), float(end))
return rect
def _build_rtree(self, maps, spatial=None):
"""Build and return the 1-4 dimensional R*-Tree
:param spatial: This indicates if the spatial topology is created
as well: spatial can be None (no spatial topology),
"2D" using west, east, south, north or "3D" using
west, east, south, north, bottom, top
"""
dim = 1
if spatial == "2D":
dim = 3
if spatial == "3D":
dim = 4
tree = rtree.RTreeCreateTree(-1, 0, dim)
for i in range(len(maps)):
rect = self._map_to_rect(tree, maps[i], spatial)
rtree.RTreeInsertRect(rect, i + 1, tree)
return tree
def build(self, mapsA, mapsB=None, spatial=None):
"""Build the spatio-temporal topology structure between
one or two unordered lists of abstract dataset objects
This method builds the temporal or spatio-temporal topology from
mapsA to mapsB and vice verse. The spatio-temporal topology
structure of each map will be reset and rebuild for mapsA and
mapsB.
After building the temporal or spatio-temporal topology the modified
map objects of mapsA can be accessed
in the same way as a dictionary using there id.
The implemented iterator assures
the chronological iteration over the mapsA.
:param mapsA: A list of abstract_dataset
objects with initiated spatio-temporal extent
:param mapsB: An optional list of abstract_dataset
objects with initiated spatio-temporal extent
:param spatial: This indicates if the spatial topology is created
as well: spatial can be None (no spatial topology),
"2D" using west, east, south, north or "3D" using
west, east, south, north, bottom, top
"""
identical = False
if mapsA == mapsB:
identical = True
if mapsB is None:
mapsB = mapsA
identical = True
for map_ in mapsA:
map_.reset_topology()
if not identical:
for map_ in mapsB:
map_.reset_topology()
tree = self. _build_rtree(mapsA, spatial)
list_ = gis.G_new_ilist()
for j in range(len(mapsB)):
rect = self._map_to_rect(tree, mapsB[j], spatial)
vector.RTreeSearch2(tree, rect, list_)
rtree.RTreeFreeRect(rect)
for k in range(list_.contents.n_values):
i = list_.contents.value[k] - 1
# Get the temporal relationship
relation = mapsB[j].temporal_relation(mapsA[i])
A = mapsA[i]
B = mapsB[j]
set_temoral_relationship(A, B, relation)
if spatial is not None:
relation = mapsB[j].spatial_relation(mapsA[i])
set_spatial_relationship(A, B, relation)
self._build_internal_iteratable(mapsA, spatial)
if not identical and mapsB is not None:
self._build_iteratable(mapsB, spatial)
gis.G_free_ilist(list_)
rtree.RTreeDestroyTree(tree)
def __iter__(self):
start_ = self._first
while start_ is not None:
yield start_
start_ = start_.next()
def __getitem__(self, index):
return self._store[index.get_id()]
def __len__(self):
return len(self._store)
def __contains__(self, _map):
return _map in self._store.values()
###############################################################################
def set_temoral_relationship(A, B, relation):
if relation == "equal" or relation == "equals":
if A != B:
if not B.get_equal() or \
(B.get_equal() and \
A not in B.get_equal()):
B.append_equal(A)
if not A.get_equal() or \
(A.get_equal() and \
B not in A.get_equal()):
A.append_equal(B)
elif relation == "follows":
if not B.get_follows() or \
(B.get_follows() and \
A not in B.get_follows()):
B.append_follows(A)
if not A.get_precedes() or \
(A.get_precedes() and
B not in A.get_precedes()):
A.append_precedes(B)
elif relation == "precedes":
if not B.get_precedes() or \
(B.get_precedes() and \
A not in B.get_precedes()):
B.append_precedes(A)
if not A.get_follows() or \
(A.get_follows() and \
B not in A.get_follows()):
A.append_follows(B)
elif relation == "during" or relation == "starts" or \
relation == "finishes":
if not B.get_during() or \
(B.get_during() and \
A not in B.get_during()):
B.append_during(A)
if not A.get_contains() or \
(A.get_contains() and \
B not in A.get_contains()):
A.append_contains(B)
if relation == "starts":
if not B.get_starts() or \
(B.get_starts() and \
A not in B.get_starts()):
B.append_starts(A)
if not A.get_started() or \
(A.get_started() and \
B not in A.get_started()):
A.append_started(B)
if relation == "finishes":
if not B.get_finishes() or \
(B.get_finishes() and \
A not in B.get_finishes()):
B.append_finishes(A)
if not A.get_finished() or \
(A.get_finished() and \
B not in A.get_finished()):
A.append_finished(B)
elif relation == "contains" or relation == "started" or \
relation == "finished":
if not B.get_contains() or \
(B.get_contains() and \
A not in B.get_contains()):
B.append_contains(A)
if not A.get_during() or \
(A.get_during() and \
B not in A.get_during()):
A.append_during(B)
if relation == "started":
if not B.get_started() or \
(B.get_started() and \
A not in B.get_started()):
B.append_started(A)
if not A.get_starts() or \
(A.get_starts() and \
B not in A.get_starts()):
A.append_starts(B)
if relation == "finished":
if not B.get_finished() or \
(B.get_finished() and \
A not in B.get_finished()):
B.append_finished(A)
if not A.get_finishes() or \
(A.get_finishes() and \
B not in A.get_finishes()):
A.append_finishes(B)
elif relation == "overlaps":
if not B.get_overlaps() or \
(B.get_overlaps() and \
A not in B.get_overlaps()):
B.append_overlaps(A)
if not A.get_overlapped() or \
(A.get_overlapped() and \
B not in A.get_overlapped()):
A.append_overlapped(B)
elif relation == "overlapped":
if not B.get_overlapped() or \
(B.get_overlapped() and \
A not in B.get_overlapped()):
B.append_overlapped(A)
if not A.get_overlaps() or \
(A.get_overlaps() and \
B not in A.get_overlaps()):
A.append_overlaps(B)
###############################################################################
def set_spatial_relationship(A, B, relation):
if relation == "equivalent":
if A != B:
if not B.get_equivalent() or \
(B.get_equivalent() and \
A not in B.get_equivalent()):
B.append_equivalent(A)
if not A.get_equivalent() or \
(A.get_equivalent() and \
B not in A.get_equivalent()):
A.append_equivalent(B)
elif relation == "overlap":
if not B.get_overlap() or \
(B.get_overlap() and \
A not in B.get_overlap()):
B.append_overlap(A)
if not A.get_overlap() or \
(A.get_overlap() and
B not in A.get_overlap()):
A.append_overlap(B)
elif relation == "meet":
if not B.get_meet() or \
(B.get_meet() and \
A not in B.get_meet()):
B.append_meet(A)
if not A.get_meet() or \
(A.get_meet() and
B not in A.get_meet()):
A.append_meet(B)
elif relation == "contain":
if not B.get_contain() or \
(B.get_contain() and \
A not in B.get_contain()):
B.append_contain(A)
if not A.get_in() or \
(A.get_in() and \
B not in A.get_in()):
A.append_in(B)
elif relation == "in":
if not B.get_in() or \
(B.get_in() and \
A not in B.get_in()):
B.append_in(A)
if not A.get_contain() or \
(A.get_contain() and \
B not in A.get_contain()):
A.append_contain(B)
elif relation == "cover":
if not B.get_cover() or \
(B.get_cover() and \
A not in B.get_cover()):
B.append_cover(A)
if not A.get_covered() or \
(A.get_covered() and \
B not in A.get_covered()):
A.append_covered(B)
elif relation == "covered":
if not B.get_covered() or \
(B.get_covered() and \
A not in B.get_covered()):
B.append_covered(A)
if not A.get_cover() or \
(A.get_cover() and \
B not in A.get_cover()):
A.append_cover(B)
###############################################################################
def print_temporal_topology_relationships(maps1, maps2=None, dbif=None):
"""Print the temporal relationships of the
map lists maps1 and maps2 to stdout.
:param maps1: A list of abstract_dataset
objects with initiated temporal extent
:param maps2: An optional list of abstract_dataset
objects with initiated temporal extent
:param dbif: The database interface to be used
"""
tb = SpatioTemporalTopologyBuilder()
tb.build(maps1, maps2)
dbif, connected = init_dbif(dbif)
for _map in tb:
_map.select(dbif)
_map.print_info()
if connected:
dbif.close()
return
###############################################################################
def print_spatio_temporal_topology_relationships(maps1, maps2=None,
spatial="2D", dbif=None):
"""Print the temporal relationships of the
map lists maps1 and maps2 to stdout.
:param maps1: A list of abstract_dataset
objects with initiated temporal extent
:param maps2: An optional list of abstract_dataset
objects with initiated temporal extent
:param spatial: The dimension of the spatial extent to be used: "2D"
using west, east, south, north or "3D" using west,
east, south, north, bottom, top
:param dbif: The database interface to be used
"""
tb = SpatioTemporalTopologyBuilder()
tb.build(maps1, maps2, spatial)
dbif, connected = init_dbif(dbif)
for _map in tb:
_map.select(dbif)
_map.print_info()
if connected:
dbif.close()
return
###############################################################################
def count_temporal_topology_relationships(maps1, maps2=None, dbif=None):
"""Count the temporal relations of a single list of maps or between two
lists of maps
:param maps1: A list of abstract_dataset
objects with initiated temporal extent
:param maps2: A list of abstract_dataset
objects with initiated temporal extent
:param dbif: The database interface to be used
:return: A dictionary with counted temporal relationships
"""
tb = SpatioTemporalTopologyBuilder()
tb.build(maps1, maps2)
dbif, connected = init_dbif(dbif)
relations = None
for _map in tb:
if relations is not None:
r = _map.get_number_of_relations()
for k in r.keys():
relations[k] += r[k]
else:
relations = _map.get_number_of_relations()
if connected:
dbif.close()
return relations
###############################################################################
def create_temporal_relation_sql_where_statement(start, end, use_start=True,
use_during=False,
use_overlap=False,
use_contain=False,
use_equal=False,
use_follows=False,
use_precedes=False):
"""Create a SQL WHERE statement for temporal relation selection of maps in
space time datasets
:param start: The start time
:param end: The end time
:param use_start: Select maps of which the start time is located in
the selection granule ::
map : s
granule: s-----------------e
map : s--------------------e
granule: s-----------------e
map : s--------e
granule: s-----------------e
:param use_during: Select maps which are temporal during the selection
granule ::
map : s-----------e
granule: s-----------------e
:param use_overlap: Select maps which temporal overlap the selection
granule ::
map : s-----------e
granule: s-----------------e
map : s-----------e
granule: s----------e
:param use_contain: Select maps which temporally contain the selection
granule ::
map : s-----------------e
granule: s-----------e
:param use_equal: Select maps which temporally equal to the selection
granule ::
map : s-----------e
granule: s-----------e
:param use_follows: Select maps which temporally follow the selection
granule ::
map : s-----------e
granule: s-----------e
:param use_precedes: Select maps which temporally precedes the
selection granule ::
map : s-----------e
granule: s-----------e
Usage:
.. code-block:: python
>>> # Relative time
>>> start = 1
>>> end = 2
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False)
>>> create_temporal_relation_sql_where_statement(start, end)
'((start_time >= 1 and start_time < 2) )'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=True)
'((start_time >= 1 and start_time < 2) )'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_during=True)
'(((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)))'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_overlap=True)
'(((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)))'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_contain=True)
'(((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)))'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_equal=True)
'((start_time = 1 and end_time = 2))'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_follows=True)
'((start_time = 2))'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_precedes=True)
'((end_time = 1))'
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=True, use_during=True, use_overlap=True, use_contain=True,
... use_equal=True, use_follows=True, use_precedes=True)
'((start_time >= 1 and start_time < 2) OR ((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)) OR ((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)) OR ((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)) OR (start_time = 1 and end_time = 2) OR (start_time = 2) OR (end_time = 1))'
>>> # Absolute time
>>> start = datetime(2001, 1, 1, 12, 30)
>>> end = datetime(2001, 3, 31, 14, 30)
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False)
>>> create_temporal_relation_sql_where_statement(start, end)
"((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=True)
"((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_during=True)
"(((start_time > '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time >= '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time > '2001-01-01 12:30:00' and end_time <= '2001-03-31 14:30:00')))"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_overlap=True)
"(((start_time < '2001-01-01 12:30:00' and end_time > '2001-01-01 12:30:00' and end_time < '2001-03-31 14:30:00') OR (start_time < '2001-03-31 14:30:00' and start_time > '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00')))"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_contain=True)
"(((start_time < '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00') OR (start_time <= '2001-01-01 12:30:00' and end_time > '2001-03-31 14:30:00') OR (start_time < '2001-01-01 12:30:00' and end_time >= '2001-03-31 14:30:00')))"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_equal=True)
"((start_time = '2001-01-01 12:30:00' and end_time = '2001-03-31 14:30:00'))"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_follows=True)
"((start_time = '2001-03-31 14:30:00'))"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=False, use_precedes=True)
"((end_time = '2001-01-01 12:30:00'))"
>>> create_temporal_relation_sql_where_statement(start, end,
... use_start=True, use_during=True, use_overlap=True, use_contain=True,
... use_equal=True, use_follows=True, use_precedes=True)