spatio_temporal_relationships.py 48 KB

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