temporal_relationships.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in temporal GIS Python library package.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.print_temporal_relations(maps)
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from abstract_dataset import *
  17. from datetime_math import *
  18. def print_temporal_relations(maps1, maps2):
  19. """!Print the temporal relation matrix of the temporal ordered map lists maps1 and maps2
  20. to stdout.
  21. @param maps1: a ordered by start_time list of map objects
  22. @param maps2: a ordered by start_time list of map objects
  23. @param dbif: The database interface to be used
  24. """
  25. identical = False
  26. use_id = True
  27. if maps1 == maps2:
  28. identical = True
  29. use_id = False
  30. for i in range(len(maps1)):
  31. if identical == True:
  32. start = i + 1
  33. else:
  34. start = 0
  35. for j in range(start, len(maps2)):
  36. relation = maps1[j].temporal_relation(maps2[i])
  37. if use_id == False:
  38. print maps2[j].base.get_name(), relation, maps1[i].base.get_name()
  39. else:
  40. print maps2[j].base.get_id(), relation, maps1[i].base.get_id()
  41. # Break if the last map follows
  42. if relation == "follows":
  43. if j < len(maps1) - 1:
  44. relation = maps1[j + 1].temporal_relation(maps2[i])
  45. if relation == "after":
  46. break
  47. # Break if the the next map is after
  48. if relation == "after":
  49. break
  50. def get_temporal_relation_matrix(maps1, maps2):
  51. """!Return the temporal relation matrix of all registered maps as list of lists
  52. The map list must be ordered by start time
  53. The temporal relation matrix includes the temporal relations between
  54. all registered maps. Returned is a nested dict representing
  55. a sparse (upper right side in case maps1 == maps2) relationship matrix.
  56. @param maps: a ordered by start_time list of map objects
  57. @param dbif: The database interface to be used
  58. """
  59. matrix = {}
  60. identical = False
  61. if maps1 == maps2:
  62. identical = True
  63. for i in range(len(maps1)):
  64. if identical == True:
  65. start = i + 1
  66. else:
  67. start = 0
  68. row = {}
  69. for j in range(start, len(maps2)):
  70. relation = maps1[j].temporal_relation(maps2[i])
  71. row[maps2[j].base.get_id()] = relation
  72. # Break if the last map follows
  73. if relation == "follows":
  74. if j < len(maps1) - 1:
  75. relation = maps1[j + 1].temporal_relation(maps2[i])
  76. if relation == "after":
  77. break
  78. # Break if the the next map is after
  79. if relation == "after":
  80. break
  81. matrix[maps1[i].base.get_id()] = row
  82. return matrix
  83. def count_temporal_relations(maps1, maps2):
  84. """!Count the temporal relations between the registered maps.
  85. The map lists must be ordered by start time. Temporal relations are counted
  86. by analyzing the sparse (upper right side in case maps1 == maps2) temporal relationships matrix.
  87. @param maps: A sorted (start_time) list of abstract_dataset objects
  88. @param dbif: The database interface to be used
  89. @return A dictionary with counted temporal relationships
  90. """
  91. tcount = {}
  92. identical = False
  93. if maps1 == maps2:
  94. identical = True
  95. for i in range(len(maps1)):
  96. if identical == True:
  97. start = i + 1
  98. else:
  99. start = 0
  100. for j in range(start, len(maps2)):
  101. relation = maps1[j].temporal_relation(maps2[i])
  102. if tcount.has_key(relation):
  103. tcount[relation] = tcount[relation] + 1
  104. else:
  105. tcount[relation] = 1
  106. # Break if the last map follows
  107. if relation == "follows":
  108. if j < len(maps1) - 1:
  109. relation = maps1[j + 1].temporal_relation(maps2[i])
  110. if relation == "after":
  111. break
  112. # Break if the the next map is after
  113. if relation == "after":
  114. break
  115. return tcount