spatio_temporal_relationships.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. import grass.lib.vector as vector
  19. import grass.lib.gis as gis
  20. from ctypes import *
  21. ###############################################################################
  22. class SpatioTemporalTopologyBuilder(object):
  23. """!This class is designed to build the spatio-temporal topology
  24. of spatio-temporally related abstract dataset objects.
  25. The abstract dataset objects must be provided as a single list, or in two lists.
  26. Example:
  27. @code
  28. # We have a space time raster dataset and build a map list
  29. # from all registered maps ordered by start time
  30. maps = strds.get_registered_maps_as_objects()
  31. # Now lets build the temporal topology of the maps in the list
  32. tb = SpatioTemporalTopologyBuilder()
  33. tb.build(maps)
  34. dbif, connected = init_dbif(None)
  35. for map in tb:
  36. map.select(dbif)
  37. map.print_info()
  38. # Same can be done with the existing map list
  39. # But be aware that this is might not be temporally ordered
  40. for map in maps:
  41. map.select(dbf)
  42. map.print_info()
  43. # Using the next and previous methods, we can iterate over the
  44. # topological related maps in this way
  45. first = tb.get_first()
  46. while first:
  47. first.print_topology_info()
  48. first = first.next()
  49. # Dictionary like accessed
  50. map = tb["name@mapset"]
  51. >>> # Example with two lists of maps
  52. >>> import grass.temporal as tgis
  53. >>> import datetime
  54. >>> # Create two list of maps with equal time stamps
  55. >>> mapsA = []
  56. >>> mapsB = []
  57. >>> for i in range(4):
  58. ... idA = "a%i@B"%(i)
  59. ... mapA = tgis.RasterDataset(idA)
  60. ... idB = "b%i@B"%(i)
  61. ... mapB = tgis.RasterDataset(idB)
  62. ... check = mapA.set_relative_time(i, i + 1, "months")
  63. ... check = mapB.set_relative_time(i, i + 1, "months")
  64. ... mapsA.append(mapA)
  65. ... mapsB.append(mapB)
  66. >>> # Build the topology between the two map lists
  67. >>> tb = SpatioTemporalTopologyBuilder()
  68. >>> tb.build(mapsA, mapsB, None)
  69. >>> # Check relations of mapsA
  70. >>> for map in mapsA:
  71. ... if map.get_equal():
  72. ... relations = map.get_equal()
  73. ... print "Map %s has equal relation to map %s"%(map.get_name(),
  74. ... relations[0].get_name())
  75. Map a0 has equal relation to map b0
  76. Map a1 has equal relation to map b1
  77. Map a2 has equal relation to map b2
  78. Map a3 has equal relation to map b3
  79. >>> # Check relations of mapsB
  80. >>> for map in mapsB:
  81. ... if map.get_equal():
  82. ... relations = map.get_equal()
  83. ... print "Map %s has equal relation to map %s"%(map.get_name(),
  84. ... relations[0].get_name())
  85. Map b0 has equal relation to map a0
  86. Map b1 has equal relation to map a1
  87. Map b2 has equal relation to map a2
  88. Map b3 has equal relation to map a3
  89. >>> mapsA = []
  90. >>> mapsB = []
  91. >>> for i in range(4):
  92. ... idA = "a%i@B"%(i)
  93. ... mapA = tgis.RasterDataset(idA)
  94. ... idB = "b%i@B"%(i)
  95. ... mapB = tgis.RasterDataset(idB)
  96. ... check = mapA.set_relative_time(i, i + 1, "months")
  97. ... check = mapB.set_relative_time(i + 1, i + 2, "months")
  98. ... mapsA.append(mapA)
  99. ... mapsB.append(mapB)
  100. >>> # Build the topology between the two map lists
  101. >>> tb = SpatioTemporalTopologyBuilder()
  102. >>> tb.build(mapsA, mapsB, None)
  103. >>> # Check relations of mapsA
  104. >>> for map in mapsA:
  105. ... print(map.get_temporal_extent_as_tuple())
  106. ... m = map.get_temporal_relations()
  107. ... for key in m.keys():
  108. ... if key not in ["NEXT", "PREV"]:
  109. ... print(key, m[key][0].get_temporal_extent_as_tuple())
  110. (0, 1)
  111. ('PRECEDES', (1, 2))
  112. (1, 2)
  113. ('PRECEDES', (2, 3))
  114. ('EQUAL', (1, 2))
  115. (2, 3)
  116. ('FOLLOWS', (1, 2))
  117. ('PRECEDES', (3, 4))
  118. ('EQUAL', (2, 3))
  119. (3, 4)
  120. ('FOLLOWS', (2, 3))
  121. ('EQUAL', (3, 4))
  122. ('PRECEDES', (4, 5))
  123. >>> mapsA = []
  124. >>> mapsB = []
  125. >>> for i in range(4):
  126. ... idA = "a%i@B"%(i)
  127. ... mapA = tgis.RasterDataset(idA)
  128. ... idB = "b%i@B"%(i)
  129. ... mapB = tgis.RasterDataset(idB)
  130. ... start = datetime.datetime(2000 + i, 1, 1)
  131. ... end = datetime.datetime(2000 + i + 1, 1, 1)
  132. ... check = mapA.set_absolute_time(start, end)
  133. ... start = datetime.datetime(2000 + i + 1, 1, 1)
  134. ... end = datetime.datetime(2000 + i + 2, 1, 1)
  135. ... check = mapB.set_absolute_time(start, end)
  136. ... mapsA.append(mapA)
  137. ... mapsB.append(mapB)
  138. >>> # Build the topology between the two map lists
  139. >>> tb = SpatioTemporalTopologyBuilder()
  140. >>> tb.build(mapsA, mapsB, None)
  141. >>> # Check relations of mapsA
  142. >>> for map in mapsA:
  143. ... print(map.get_temporal_extent_as_tuple())
  144. ... m = map.get_temporal_relations()
  145. ... for key in m.keys():
  146. ... if key not in ["NEXT", "PREV"]:
  147. ... print(key, m[key][0].get_temporal_extent_as_tuple())
  148. (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2001, 1, 1, 0, 0))
  149. ('PRECEDES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0)))
  150. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0))
  151. ('PRECEDES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  152. ('EQUAL', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0)))
  153. (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0))
  154. ('FOLLOWS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0)))
  155. ('PRECEDES', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  156. ('EQUAL', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  157. (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0))
  158. ('FOLLOWS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  159. ('EQUAL', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  160. ('PRECEDES', (datetime.datetime(2004, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  161. >>> mapsA = []
  162. >>> mapsB = []
  163. >>> for i in range(4):
  164. ... idA = "a%i@B"%(i)
  165. ... mapA = tgis.RasterDataset(idA)
  166. ... idB = "b%i@B"%(i)
  167. ... mapB = tgis.RasterDataset(idB)
  168. ... start = datetime.datetime(2000 + i, 1, 1)
  169. ... end = datetime.datetime(2000 + i + 1, 1, 1)
  170. ... check = mapA.set_absolute_time(start, end)
  171. ... start = datetime.datetime(2000 + i, 1, 1)
  172. ... end = datetime.datetime(2000 + i + 3, 1, 1)
  173. ... check = mapB.set_absolute_time(start, end)
  174. ... mapsA.append(mapA)
  175. ... mapsB.append(mapB)
  176. >>> # Build the topology between the two map lists
  177. >>> tb = SpatioTemporalTopologyBuilder()
  178. >>> tb.build(mapsA, mapsB, None)
  179. >>> # Check relations of mapsA
  180. >>> for map in mapsA:
  181. ... print(map.get_temporal_extent_as_tuple())
  182. ... m = map.get_temporal_relations()
  183. ... for key in m.keys():
  184. ... if key not in ["NEXT", "PREV"]:
  185. ... print(key, m[key][0].get_temporal_extent_as_tuple())
  186. (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2001, 1, 1, 0, 0))
  187. ('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  188. ('STARTS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  189. ('PRECEDES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  190. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0))
  191. ('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  192. ('STARTS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  193. ('PRECEDES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  194. (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0))
  195. ('PRECEDES', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
  196. ('FINISHES', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  197. ('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  198. ('STARTS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  199. (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0))
  200. ('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  201. ('DURING', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  202. ('FINISHES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  203. ('STARTS', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
  204. >>> mapsA = []
  205. >>> mapsB = []
  206. >>> for i in range(4):
  207. ... idA = "a%i@B"%(i)
  208. ... mapA = tgis.RasterDataset(idA)
  209. ... idB = "b%i@B"%(i)
  210. ... mapB = tgis.RasterDataset(idB)
  211. ... start = datetime.datetime(2000 + i, 1, 1)
  212. ... end = datetime.datetime(2000 + i + 2, 1, 1)
  213. ... check = mapA.set_absolute_time(start, end)
  214. ... start = datetime.datetime(2000 + i, 1, 1)
  215. ... end = datetime.datetime(2000 + i + 3, 1, 1)
  216. ... check = mapB.set_absolute_time(start, end)
  217. ... mapsA.append(mapA)
  218. ... mapsB.append(mapB)
  219. >>> # Build the topology between the two map lists
  220. >>> tb = SpatioTemporalTopologyBuilder()
  221. >>> tb.build(mapsA, mapsB, None)
  222. >>> # Check relations of mapsA
  223. >>> for map in mapsA:
  224. ... print(map.get_temporal_extent_as_tuple())
  225. ... m = map.get_temporal_relations()
  226. ... for key in m.keys():
  227. ... if key not in ["NEXT", "PREV"]:
  228. ... print(key, m[key][0].get_temporal_extent_as_tuple())
  229. (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2002, 1, 1, 0, 0))
  230. ('OVERLAPS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  231. ('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  232. ('STARTS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  233. ('PRECEDES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  234. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0))
  235. ('OVERLAPS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  236. ('PRECEDES', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
  237. ('FINISHES', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  238. ('DURING', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  239. ('STARTS', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  240. (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0))
  241. ('OVERLAPS', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
  242. ('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  243. ('FINISHES', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  244. ('DURING', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  245. ('STARTS', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  246. (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0))
  247. ('OVERLAPPED', (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2004, 1, 1, 0, 0)))
  248. ('DURING', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  249. ('FINISHES', (datetime.datetime(2002, 1, 1, 0, 0), datetime.datetime(2005, 1, 1, 0, 0)))
  250. ('STARTS', (datetime.datetime(2003, 1, 1, 0, 0), datetime.datetime(2006, 1, 1, 0, 0)))
  251. ('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2003, 1, 1, 0, 0)))
  252. >>> mapsA = []
  253. >>> mapsB = []
  254. >>> for i in range(4):
  255. ... idA = "a%i@B"%(i)
  256. ... mapA = tgis.RasterDataset(idA)
  257. ... idB = "b%i@B"%(i)
  258. ... mapB = tgis.RasterDataset(idB)
  259. ... start = datetime.datetime(2000, 1, 1, 0, 0, i)
  260. ... end = datetime.datetime(2000, 1, 1, 0, 0, i + 2)
  261. ... check = mapA.set_absolute_time(start, end)
  262. ... start = datetime.datetime(2000, 1, 1, 0, 0, i + 1)
  263. ... end = datetime.datetime(2000, 1, 1, 0, 0, i + 3)
  264. ... check = mapB.set_absolute_time(start, end)
  265. ... mapsA.append(mapA)
  266. ... mapsB.append(mapB)
  267. >>> # Build the topology between the two map lists
  268. >>> tb = SpatioTemporalTopologyBuilder()
  269. >>> tb.build(mapsA, mapsB, None)
  270. >>> # Check relations of mapsA
  271. >>> for map in mapsA:
  272. ... print(map.get_temporal_extent_as_tuple())
  273. ... m = map.get_temporal_relations()
  274. ... for key in m.keys():
  275. ... if key not in ["NEXT", "PREV"]:
  276. ... print(key, m[key][0].get_temporal_extent_as_tuple())
  277. (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2))
  278. ('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
  279. ('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
  280. (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3))
  281. ('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
  282. ('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
  283. ('EQUAL', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
  284. (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4))
  285. ('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
  286. ('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
  287. ('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 4), datetime.datetime(2000, 1, 1, 0, 0, 6)))
  288. ('EQUAL', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
  289. (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5))
  290. ('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 4), datetime.datetime(2000, 1, 1, 0, 0, 6)))
  291. ('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
  292. ('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
  293. ('EQUAL', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
  294. >>> mapsA = []
  295. >>> for i in range(4):
  296. ... idA = "a%i@B"%(i)
  297. ... mapA = tgis.RasterDataset(idA)
  298. ... start = datetime.datetime(2000, 1, 1, 0, 0, i)
  299. ... end = datetime.datetime(2000, 1, 1, 0, 0, i + 2)
  300. ... check = mapA.set_absolute_time(start, end)
  301. ... mapsA.append(mapA)
  302. >>> tb = SpatioTemporalTopologyBuilder()
  303. >>> tb.build(mapsA)
  304. >>> # Check relations of mapsA
  305. >>> for map in mapsA:
  306. ... print(map.get_temporal_extent_as_tuple())
  307. ... m = map.get_temporal_relations()
  308. ... for key in m.keys():
  309. ... if key not in ["NEXT", "PREV"]:
  310. ... print(key, m[key][0].get_temporal_extent_as_tuple())
  311. (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2))
  312. ('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
  313. ('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
  314. (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3))
  315. ('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
  316. ('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2)))
  317. ('PRECEDES', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
  318. (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4))
  319. ('OVERLAPS', (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5)))
  320. ('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 1, 1, 0, 0, 2)))
  321. ('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
  322. (datetime.datetime(2000, 1, 1, 0, 0, 3), datetime.datetime(2000, 1, 1, 0, 0, 5))
  323. ('FOLLOWS', (datetime.datetime(2000, 1, 1, 0, 0, 1), datetime.datetime(2000, 1, 1, 0, 0, 3)))
  324. ('OVERLAPPED', (datetime.datetime(2000, 1, 1, 0, 0, 2), datetime.datetime(2000, 1, 1, 0, 0, 4)))
  325. @endcode
  326. """
  327. def __init__(self):
  328. self._reset()
  329. # 0001-01-01 00:00:00
  330. self._timeref = datetime(1,1,1)
  331. def _reset(self):
  332. self._store = {}
  333. self._first = None
  334. self._iteratable = False
  335. def _set_first(self, first):
  336. self._first = first
  337. self._insert(first)
  338. def _detect_first(self):
  339. if len(self) > 0:
  340. prev_ = self._store.values()[0]
  341. while prev_ is not None:
  342. self._first = prev_
  343. prev_ = prev_.prev()
  344. def _insert(self, t):
  345. self._store[t.get_id()] = t
  346. def get_first(self):
  347. """!Return the first map with the earliest start time
  348. @return The map with the earliest start time
  349. """
  350. return self._first
  351. def _build_internal_iteratable(self, maps, spatial):
  352. """!Build an iteratable temporal topology structure for all maps in
  353. the list and store the maps internally
  354. Basically the "next" and "prev" relations will be set in the
  355. temporal topology structure of each map
  356. The maps will be added to the object, so they can be
  357. accessed using the iterator of this class
  358. @param maps A sorted (by start_time)list of abstract_dataset
  359. objects with initiated temporal extent
  360. """
  361. self._build_iteratable(maps, spatial)
  362. for _map in maps:
  363. self._insert(_map)
  364. # Detect the first map
  365. self._detect_first()
  366. def _build_iteratable(self, maps, spatial):
  367. """!Build an iteratable temporal topology structure for
  368. all maps in the list
  369. Basically the "next" and "prev" relations will be set in
  370. the temporal topology structure of each map.
  371. @param maps A sorted (by start_time)list of abstract_dataset
  372. objects with initiated temporal extent
  373. """
  374. # for i in xrange(len(maps)):
  375. # offset = i + 1
  376. # for j in xrange(offset, len(maps)):
  377. # # Get the temporal relationship
  378. # relation = maps[j].temporal_relation(maps[i])
  379. #
  380. # # Build the next reference
  381. # if relation != "equal" and relation != "started":
  382. # maps[i].set_next(maps[j])
  383. # break
  384. # First we need to order the map list chronologically
  385. sorted_maps = sorted(
  386. maps, key=AbstractDatasetComparisonKeyStartTime)
  387. for i in xrange(len(sorted_maps) - 1):
  388. sorted_maps[i].set_next(sorted_maps[i + 1])
  389. for map_ in sorted_maps:
  390. next_ = map_.next()
  391. if next_:
  392. next_.set_prev(map_)
  393. map_.set_temporal_topology_build_true()
  394. if spatial is not None:
  395. map_.set_spatial_topology_build_true()
  396. def _map_to_rect(self, tree, map_, spatial=None):
  397. """!Use the spatio-temporal extent of a map to create and
  398. return a RTree rectange
  399. @param spatial This indicates if the spatial topology is created as well:
  400. spatial can be None (no spatial topology), "2D" using west, east,
  401. #south, north or "3D" using west, east, south, north, bottom, top
  402. """
  403. rect = vector.RTreeAllocRect(tree)
  404. start, end = map_.get_temporal_extent_as_tuple()
  405. if not end:
  406. end = start
  407. if map_.is_time_absolute():
  408. start = time_delta_to_relative_time_seconds(start - self._timeref)
  409. end = time_delta_to_relative_time_seconds(end - self._timeref)
  410. if spatial is None:
  411. vector.RTreeSetRect1D(rect, tree, float(start), float(end))
  412. elif spatial == "2D":
  413. north, south, east, west, top, bottom = map_.get_spatial_extent_as_tuple()
  414. vector.RTreeSetRect3D(rect, tree, west, east, south, north,
  415. float(start), float(end))
  416. elif spatial == "3D":
  417. north, south, east, west, top, bottom = map_.get_spatial_extent_as_tuple()
  418. vector.RTreeSetRect4D(rect, tree, west, east, south, north,
  419. bottom, top, float(start), float(end))
  420. return rect
  421. def _build_rtree(self, maps, spatial=None):
  422. """!Build and return the 1-4 dimensional R*-Tree
  423. @param spatial This indicates if the spatial topology is created as well:
  424. spatial can be None (no spatial topology), "2D" using west, east,
  425. south, north or "3D" using west, east, south, north, bottom, top
  426. """
  427. dim = 1
  428. if spatial == "2D":
  429. dim = 3
  430. if spatial == "3D":
  431. dim = 4
  432. tree = vector.RTreeCreateTree(-1, 0, dim)
  433. for i in xrange(len(maps)):
  434. rect = self._map_to_rect(tree, maps[i], spatial)
  435. vector.RTreeInsertRect(rect, i + 1, tree)
  436. return tree
  437. def build(self, mapsA, mapsB=None, spatial=None):
  438. """!Build the spatio-temporal topology structure between
  439. one or two unordered lists of abstract dataset objects
  440. This method builds the temporal or spatio-temporal topology from mapsA to
  441. mapsB and vice verse. The spatio-temporal topology structure of each map
  442. will be reseted and rebuild for mapsA and mapsB.
  443. After building the temporal or spatio-temporal topology the modified
  444. map objects of mapsA can be accessed
  445. in the same way as a dictionary using there id.
  446. The implemented iterator assures
  447. the chronological iteration over the mapsA.
  448. @param mapsA A list of abstract_dataset
  449. objects with initiated spatio-temporal extent
  450. @param mapsB An optional list of abstract_dataset
  451. objects with initiated spatio-temporal extent
  452. @param spatial This indicates if the spatial topology is created as well:
  453. spatial can be None (no spatial topology), "2D" using west, east,
  454. south, north or "3D" using west, east, south, north, bottom, top
  455. """
  456. identical = False
  457. if mapsA == mapsB:
  458. identical = True
  459. if mapsB == None:
  460. mapsB = mapsA
  461. identical = True
  462. for map_ in mapsA:
  463. map_.reset_topology()
  464. if not identical:
  465. for map_ in mapsB:
  466. map_.reset_topology()
  467. tree = self. _build_rtree(mapsA, spatial)
  468. list_ = gis.G_new_ilist()
  469. for j in xrange(len(mapsB)):
  470. rect = self._map_to_rect(tree, mapsB[j], spatial)
  471. vector.RTreeSearch2(tree, rect, list_)
  472. vector.RTreeFreeRect(rect)
  473. for k in xrange(list_.contents.n_values):
  474. i = list_.contents.value[k] - 1
  475. # Get the temporal relationship
  476. relation = mapsB[j].temporal_relation(mapsA[i])
  477. A = mapsA[i]
  478. B = mapsB[j]
  479. set_temoral_relationship(A, B, relation)
  480. if spatial is not None:
  481. relation = mapsB[j].spatial_relation(mapsA[i])
  482. set_spatial_relationship(A, B, relation)
  483. self._build_internal_iteratable(mapsA, spatial)
  484. if not identical and mapsB != None:
  485. self._build_iteratable(mapsB, spatial)
  486. gis.G_free_ilist(list_)
  487. vector.RTreeDestroyTree(tree)
  488. def __iter__(self):
  489. start_ = self._first
  490. while start_ is not None:
  491. yield start_
  492. start_ = start_.next()
  493. def __getitem__(self, index):
  494. return self._store[index.get_id()]
  495. def __len__(self):
  496. return len(self._store)
  497. def __contains__(self, _map):
  498. return _map in self._store.values()
  499. ###############################################################################
  500. def set_temoral_relationship(A, B, relation):
  501. if relation == "equal" or relation == "equals":
  502. if A != B:
  503. if not B.get_equal() or \
  504. (B.get_equal() and \
  505. A not in B.get_equal()):
  506. B.append_equal(A)
  507. if not A.get_equal() or \
  508. (A.get_equal() and \
  509. B not in A.get_equal()):
  510. A.append_equal(B)
  511. elif relation == "follows":
  512. if not B.get_follows() or \
  513. (B.get_follows() and \
  514. A not in B.get_follows()):
  515. B.append_follows(A)
  516. if not A.get_precedes() or \
  517. (A.get_precedes() and
  518. B not in A.get_precedes()):
  519. A.append_precedes(B)
  520. elif relation == "precedes":
  521. if not B.get_precedes() or \
  522. (B.get_precedes() and \
  523. A not in B.get_precedes()):
  524. B.append_precedes(A)
  525. if not A.get_follows() or \
  526. (A.get_follows() and \
  527. B not in A.get_follows()):
  528. A.append_follows(B)
  529. elif relation == "during" or relation == "starts" or \
  530. relation == "finishes":
  531. if not B.get_during() or \
  532. (B.get_during() and \
  533. A not in B.get_during()):
  534. B.append_during(A)
  535. if not A.get_contains() or \
  536. (A.get_contains() and \
  537. B not in A.get_contains()):
  538. A.append_contains(B)
  539. if relation == "starts":
  540. if not B.get_starts() or \
  541. (B.get_starts() and \
  542. A not in B.get_starts()):
  543. B.append_starts(A)
  544. if not A.get_started() or \
  545. (A.get_started() and \
  546. B not in A.get_started()):
  547. A.append_started(B)
  548. if relation == "finishes":
  549. if not B.get_finishes() or \
  550. (B.get_finishes() and \
  551. A not in B.get_finishes()):
  552. B.append_finishes(A)
  553. if not A.get_finished() or \
  554. (A.get_finished() and \
  555. B not in A.get_finished()):
  556. A.append_finished(B)
  557. elif relation == "contains" or relation == "started" or \
  558. relation == "finished":
  559. if not B.get_contains() or \
  560. (B.get_contains() and \
  561. A not in B.get_contains()):
  562. B.append_contains(A)
  563. if not A.get_during() or \
  564. (A.get_during() and \
  565. B not in A.get_during()):
  566. A.append_during(B)
  567. if relation == "started":
  568. if not B.get_started() or \
  569. (B.get_started() and \
  570. A not in B.get_started()):
  571. B.append_started(A)
  572. if not A.get_starts() or \
  573. (A.get_starts() and \
  574. B not in A.get_starts()):
  575. A.append_starts(B)
  576. if relation == "finished":
  577. if not B.get_finished() or \
  578. (B.get_finished() and \
  579. A not in B.get_finished()):
  580. B.append_finished(A)
  581. if not A.get_finishes() or \
  582. (A.get_finishes() and \
  583. B not in A.get_finishes()):
  584. A.append_finishes(B)
  585. elif relation == "overlaps":
  586. if not B.get_overlaps() or \
  587. (B.get_overlaps() and \
  588. A not in B.get_overlaps()):
  589. B.append_overlaps(A)
  590. if not A.get_overlapped() or \
  591. (A.get_overlapped() and \
  592. B not in A.get_overlapped()):
  593. A.append_overlapped(B)
  594. elif relation == "overlapped":
  595. if not B.get_overlapped() or \
  596. (B.get_overlapped() and \
  597. A not in B.get_overlapped()):
  598. B.append_overlapped(A)
  599. if not A.get_overlaps() or \
  600. (A.get_overlaps() and \
  601. B not in A.get_overlaps()):
  602. A.append_overlaps(B)
  603. ###############################################################################
  604. def set_spatial_relationship(A, B, relation):
  605. if relation == "equivalent":
  606. if not B.get_equivalent() or \
  607. (B.get_equivalent() and \
  608. A not in B.get_equivalent()):
  609. B.append_equivalent(A)
  610. if not A.get_equivalent() or \
  611. (A.get_equivalent() and \
  612. B not in A.get_equivalent()):
  613. A.append_equivalent(B)
  614. elif relation == "overlap":
  615. if not B.get_overlap() or \
  616. (B.get_overlap() and \
  617. A not in B.get_overlap()):
  618. B.append_overlap(A)
  619. if not A.get_overlap() or \
  620. (A.get_overlap() and
  621. B not in A.get_overlap()):
  622. A.append_overlap(B)
  623. elif relation == "meet":
  624. if not B.get_meet() or \
  625. (B.get_meet() and \
  626. A not in B.get_meet()):
  627. B.append_meet(A)
  628. if not A.get_meet() or \
  629. (A.get_meet() and
  630. B not in A.get_meet()):
  631. A.append_meet(B)
  632. elif relation == "contain":
  633. if not B.get_contain() or \
  634. (B.get_contain() and \
  635. A not in B.get_contain()):
  636. B.append_contain(A)
  637. if not A.get_in() or \
  638. (A.get_in() and \
  639. B not in A.get_in()):
  640. A.append_in(B)
  641. elif relation == "in":
  642. if not B.get_in() or \
  643. (B.get_in() and \
  644. A not in B.get_in()):
  645. B.append_in(A)
  646. if not A.get_contain() or \
  647. (A.get_contain() and \
  648. B not in A.get_contain()):
  649. A.append_contain(B)
  650. elif relation == "cover":
  651. if not B.get_cover() or \
  652. (B.get_cover() and \
  653. A not in B.get_cover()):
  654. B.append_cover(A)
  655. if not A.get_covered() or \
  656. (A.get_covered() and \
  657. B not in A.get_covered()):
  658. A.append_covered(B)
  659. elif relation == "covered":
  660. if not B.get_covered() or \
  661. (B.get_covered() and \
  662. A not in B.get_covered()):
  663. B.append_covered(A)
  664. if not A.get_cover() or \
  665. (A.get_cover() and \
  666. B not in A.get_cover()):
  667. A.append_cover(B)
  668. ###############################################################################
  669. def print_temporal_topology_relationships(maps1, maps2=None, dbif=None):
  670. """!Print the temporal relationships of the
  671. map lists maps1 and maps2 to stdout.
  672. @param maps1 A list of abstract_dataset
  673. objects with initiated temporal extent
  674. @param maps2 An optional list of abstract_dataset
  675. objects with initiated temporal extent
  676. @param dbif The database interface to be used
  677. """
  678. tb = SpatioTemporalTopologyBuilder()
  679. tb.build(maps1, maps2)
  680. dbif, connected = init_dbif(dbif)
  681. for _map in tb:
  682. _map.select(dbif)
  683. _map.print_info()
  684. if connected:
  685. dbif.close()
  686. return
  687. ###############################################################################
  688. def print_spatio_temporal_topology_relationships(maps1, maps2=None, spatial="2D", dbif=None):
  689. """!Print the temporal relationships of the
  690. map lists maps1 and maps2 to stdout.
  691. @param maps1 A list of abstract_dataset
  692. objects with initiated temporal extent
  693. @param maps2 An optional list of abstract_dataset
  694. objects with initiated temporal extent
  695. @param spatial The dimension of the spatial extent to be used: "2D" using west, east,
  696. south, north or "3D" using west, east, south, north, bottom, top
  697. @param dbif The database interface to be used
  698. """
  699. tb = SpatioTemporalTopologyBuilder()
  700. tb.build(maps1, maps2, spatial)
  701. dbif, connected = init_dbif(dbif)
  702. for _map in tb:
  703. _map.select(dbif)
  704. _map.print_info()
  705. if connected:
  706. dbif.close()
  707. return
  708. ###############################################################################
  709. def count_temporal_topology_relationships(maps1, maps2=None, dbif=None):
  710. """!Count the temporal relations of a single list of maps or between two lists of maps
  711. @param maps1 A list of abstract_dataset
  712. objects with initiated temporal extent
  713. @param maps2 A list of abstract_dataset
  714. objects with initiated temporal extent
  715. @param dbif The database interface to be used
  716. @return A dictionary with counted temporal relationships
  717. """
  718. tb = SpatioTemporalTopologyBuilder()
  719. tb.build(maps1, maps2)
  720. dbif, connected = init_dbif(dbif)
  721. relations = None
  722. for _map in tb:
  723. if relations != None:
  724. r = _map.get_number_of_relations()
  725. for k in r.keys():
  726. relations[k] += r[k]
  727. else:
  728. relations = _map.get_number_of_relations()
  729. if connected:
  730. dbif.close()
  731. return relations
  732. ###############################################################################
  733. def create_temporal_relation_sql_where_statement(
  734. start, end, use_start=True, use_during=False,
  735. use_overlap=False, use_contain=False, use_equal=False,
  736. use_follows=False, use_precedes=False):
  737. """!Create a SQL WHERE statement for temporal relation selection of maps in space time datasets
  738. @param start The start time
  739. @param end The end time
  740. @param use_start Select maps of which the start time is located in the selection granule
  741. @verbatim
  742. map : s
  743. granule: s-----------------e
  744. map : s--------------------e
  745. granule: s-----------------e
  746. map : s--------e
  747. granule: s-----------------e
  748. @endverbatim
  749. @param use_during Select maps which are temporal during the selection granule
  750. @verbatim
  751. map : s-----------e
  752. granule: s-----------------e
  753. @endverbatim
  754. @param use_overlap Select maps which temporal overlap the selection granule
  755. @verbatim
  756. map : s-----------e
  757. granule: s-----------------e
  758. map : s-----------e
  759. granule: s----------e
  760. @endverbatim
  761. @param use_contain Select maps which temporally contain the selection granule
  762. @verbatim
  763. map : s-----------------e
  764. granule: s-----------e
  765. @endverbatim
  766. @param use_equal Select maps which temporally equal to the selection granule
  767. @verbatim
  768. map : s-----------e
  769. granule: s-----------e
  770. @endverbatim
  771. @param use_follows Select maps which temporally follow the selection granule
  772. @verbatim
  773. map : s-----------e
  774. granule: s-----------e
  775. @endverbatim
  776. @param use_precedes Select maps which temporally precedes the selection granule
  777. @verbatim
  778. map : s-----------e
  779. granule: s-----------e
  780. @endverbatim
  781. Usage:
  782. @code
  783. >>> # Relative time
  784. >>> start = 1
  785. >>> end = 2
  786. >>> create_temporal_relation_sql_where_statement(start, end,
  787. ... use_start=False)
  788. >>> create_temporal_relation_sql_where_statement(start, end)
  789. '((start_time >= 1 and start_time < 2) )'
  790. >>> create_temporal_relation_sql_where_statement(start, end,
  791. ... use_start=True)
  792. '((start_time >= 1 and start_time < 2) )'
  793. >>> create_temporal_relation_sql_where_statement(start, end,
  794. ... use_start=False, use_during=True)
  795. '(((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)))'
  796. >>> create_temporal_relation_sql_where_statement(start, end,
  797. ... use_start=False, use_overlap=True)
  798. '(((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)))'
  799. >>> create_temporal_relation_sql_where_statement(start, end,
  800. ... use_start=False, use_contain=True)
  801. '(((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)))'
  802. >>> create_temporal_relation_sql_where_statement(start, end,
  803. ... use_start=False, use_equal=True)
  804. '((start_time = 1 and end_time = 2))'
  805. >>> create_temporal_relation_sql_where_statement(start, end,
  806. ... use_start=False, use_follows=True)
  807. '((start_time = 2))'
  808. >>> create_temporal_relation_sql_where_statement(start, end,
  809. ... use_start=False, use_precedes=True)
  810. '((end_time = 1))'
  811. >>> create_temporal_relation_sql_where_statement(start, end,
  812. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  813. ... use_equal=True, use_follows=True, use_precedes=True)
  814. '((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))'
  815. >>> # Absolute time
  816. >>> start = datetime(2001, 1, 1, 12, 30)
  817. >>> end = datetime(2001, 3, 31, 14, 30)
  818. >>> create_temporal_relation_sql_where_statement(start, end,
  819. ... use_start=False)
  820. >>> create_temporal_relation_sql_where_statement(start, end)
  821. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  822. >>> create_temporal_relation_sql_where_statement(start, end,
  823. ... use_start=True)
  824. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  825. >>> create_temporal_relation_sql_where_statement(start, end,
  826. ... use_start=False, use_during=True)
  827. "(((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')))"
  828. >>> create_temporal_relation_sql_where_statement(start, end,
  829. ... use_start=False, use_overlap=True)
  830. "(((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')))"
  831. >>> create_temporal_relation_sql_where_statement(start, end,
  832. ... use_start=False, use_contain=True)
  833. "(((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')))"
  834. >>> create_temporal_relation_sql_where_statement(start, end,
  835. ... use_start=False, use_equal=True)
  836. "((start_time = '2001-01-01 12:30:00' and end_time = '2001-03-31 14:30:00'))"
  837. >>> create_temporal_relation_sql_where_statement(start, end,
  838. ... use_start=False, use_follows=True)
  839. "((start_time = '2001-03-31 14:30:00'))"
  840. >>> create_temporal_relation_sql_where_statement(start, end,
  841. ... use_start=False, use_precedes=True)
  842. "((end_time = '2001-01-01 12:30:00'))"
  843. >>> create_temporal_relation_sql_where_statement(start, end,
  844. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  845. ... use_equal=True, use_follows=True, use_precedes=True)
  846. "((start_time >= '2001-01-01 12:30:00' and start_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') 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-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')) 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') 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') OR (start_time = '2001-03-31 14:30:00') OR (end_time = '2001-01-01 12:30:00'))"
  847. @endcode
  848. """
  849. where = "("
  850. if use_start:
  851. if isinstance(start, datetime):
  852. where += "(start_time >= '%s' and start_time < '%s') " % (start, end)
  853. else:
  854. where += "(start_time >= %i and start_time < %i) " % (start, end)
  855. if use_during:
  856. if use_start:
  857. where += " OR "
  858. if isinstance(start, datetime):
  859. where += "((start_time > '%s' and end_time < '%s') OR " % (start, end)
  860. where += "(start_time >= '%s' and end_time < '%s') OR " % (start, end)
  861. where += "(start_time > '%s' and end_time <= '%s'))" % (start, end)
  862. else:
  863. where += "((start_time > %i and end_time < %i) OR " % (start, end)
  864. where += "(start_time >= %i and end_time < %i) OR " % (start, end)
  865. where += "(start_time > %i and end_time <= %i))" % (start, end)
  866. if use_overlap:
  867. if use_start or use_during:
  868. where += " OR "
  869. if isinstance(start, datetime):
  870. where += "((start_time < '%s' and end_time > '%s' and end_time < '%s') OR " % (start, start, end)
  871. where += "(start_time < '%s' and start_time > '%s' and end_time > '%s'))" % (end, start, end)
  872. else:
  873. where += "((start_time < %i and end_time > %i and end_time < %i) OR " % (start, start, end)
  874. where += "(start_time < %i and start_time > %i and end_time > %i))" % (end, start, end)
  875. if use_contain:
  876. if use_start or use_during or use_overlap:
  877. where += " OR "
  878. if isinstance(start, datetime):
  879. where += "((start_time < '%s' and end_time > '%s') OR " % (start, end)
  880. where += "(start_time <= '%s' and end_time > '%s') OR " % (start, end)
  881. where += "(start_time < '%s' and end_time >= '%s'))" % (start, end)
  882. else:
  883. where += "((start_time < %i and end_time > %i) OR " % (start, end)
  884. where += "(start_time <= %i and end_time > %i) OR " % (start, end)
  885. where += "(start_time < %i and end_time >= %i))" % (start, end)
  886. if use_equal:
  887. if use_start or use_during or use_overlap or use_contain:
  888. where += " OR "
  889. if isinstance(start, datetime):
  890. where += "(start_time = '%s' and end_time = '%s')" % (start, end)
  891. else:
  892. where += "(start_time = %i and end_time = %i)" % (start, end)
  893. if use_follows:
  894. if use_start or use_during or use_overlap or use_contain or use_equal:
  895. where += " OR "
  896. if isinstance(start, datetime):
  897. where += "(start_time = '%s')" % (end)
  898. else:
  899. where += "(start_time = %i)" % (end)
  900. if use_precedes:
  901. if use_start or use_during or use_overlap or use_contain or use_equal \
  902. or use_follows:
  903. where += " OR "
  904. if isinstance(start, datetime):
  905. where += "(end_time = '%s')" % (start)
  906. else:
  907. where += "(end_time = %i)" % (start)
  908. where += ")"
  909. # Catch empty where statement
  910. if where == "()":
  911. where = None
  912. return where
  913. ###############################################################################
  914. if __name__ == "__main__":
  915. import doctest
  916. doctest.testmod()