spatio_temporal_relationships.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. @endcode
  295. """
  296. def __init__(self):
  297. self._reset()
  298. # 0001-01-01 00:00:00
  299. self._timeref = datetime(1,1,1)
  300. def _reset(self):
  301. self._store = {}
  302. self._first = None
  303. self._iteratable = False
  304. def _set_first(self, first):
  305. self._first = first
  306. self._insert(first)
  307. def _detect_first(self):
  308. if len(self) > 0:
  309. prev_ = self._store.values()[0]
  310. while prev_ is not None:
  311. self._first = prev_
  312. prev_ = prev_.prev()
  313. def _insert(self, t):
  314. self._store[t.get_id()] = t
  315. def get_first(self):
  316. """!Return the first map with the earliest start time
  317. @return The map with the earliest start time
  318. """
  319. return self._first
  320. def _build_internal_iteratable(self, maps, spatial):
  321. """!Build an iteratable temporal topology structure for all maps in
  322. the list and store the maps internally
  323. Basically the "next" and "prev" relations will be set in the
  324. temporal topology structure of each map
  325. The maps will be added to the object, so they can be
  326. accessed using the iterator of this class
  327. @param maps A sorted (by start_time)list of abstract_dataset
  328. objects with initiated temporal extent
  329. """
  330. self._build_iteratable(maps, spatial)
  331. for _map in maps:
  332. self._insert(_map)
  333. # Detect the first map
  334. self._detect_first()
  335. def _build_iteratable(self, maps, spatial):
  336. """!Build an iteratable temporal topology structure for
  337. all maps in the list
  338. Basically the "next" and "prev" relations will be set in
  339. the temporal topology structure of each map.
  340. @param maps A sorted (by start_time)list of abstract_dataset
  341. objects with initiated temporal extent
  342. """
  343. # for i in xrange(len(maps)):
  344. # offset = i + 1
  345. # for j in xrange(offset, len(maps)):
  346. # # Get the temporal relationship
  347. # relation = maps[j].temporal_relation(maps[i])
  348. #
  349. # # Build the next reference
  350. # if relation != "equal" and relation != "started":
  351. # maps[i].set_next(maps[j])
  352. # break
  353. # First we need to order the map list chronologically
  354. sorted_maps = sorted(
  355. maps, key=AbstractDatasetComparisonKeyStartTime)
  356. for i in xrange(len(sorted_maps) - 1):
  357. sorted_maps[i].set_next(sorted_maps[i + 1])
  358. for map_ in sorted_maps:
  359. next_ = map_.next()
  360. if next_:
  361. next_.set_prev(map_)
  362. map_.set_temporal_topology_build_true()
  363. if spatial is not None:
  364. map_.set_spatial_topology_build_true()
  365. def _map_to_rect(self, tree, map_, spatial=None):
  366. """!Use the spatio-temporal extent of a map to create and
  367. return a RTree rectange
  368. @param spatial This indicates if the spatial topology is created as well:
  369. spatial can be None (no spatial topology), "2D" using west, east,
  370. #south, north or "3D" using west, east, south, north, bottom, top
  371. """
  372. rect = vector.RTreeAllocRect(tree)
  373. start, end = map_.get_temporal_extent_as_tuple()
  374. if not end:
  375. end = start
  376. if map_.is_time_absolute():
  377. start = time_delta_to_relative_time_seconds(start - self._timeref)
  378. end = time_delta_to_relative_time_seconds(end - self._timeref)
  379. if spatial is None:
  380. vector.RTreeSetRect1D(rect, tree, float(start), float(end))
  381. elif spatial == "2D":
  382. north, south, east, west, top, bottom = map_.get_spatial_extent_as_tuple()
  383. vector.RTreeSetRect3D(rect, tree, west, east, south, north,
  384. float(start), float(end))
  385. elif spatial == "3D":
  386. north, south, east, west, top, bottom = map_.get_spatial_extent_as_tuple()
  387. vector.RTreeSetRect4D(rect, tree, west, east, south, north,
  388. bottom, top, float(start), float(end))
  389. return rect
  390. def _build_rtree(self, maps, spatial=None):
  391. """!Build and return the 1-4 dimensional R*-Tree
  392. @param spatial This indicates if the spatial topology is created as well:
  393. spatial can be None (no spatial topology), "2D" using west, east,
  394. south, north or "3D" using west, east, south, north, bottom, top
  395. """
  396. dim = 1
  397. if spatial == "2D":
  398. dim = 3
  399. if spatial == "3D":
  400. dim = 4
  401. tree = vector.RTreeCreateTree(-1, 0, dim)
  402. for i in xrange(len(maps)):
  403. rect = self._map_to_rect(tree, maps[i], spatial)
  404. vector.RTreeInsertRect(rect, i + 1, tree)
  405. return tree
  406. def build(self, mapsA, mapsB=None, spatial=None):
  407. """!Build the spatio-temporal topology structure between
  408. one or two unordered lists of abstract dataset objects
  409. This method builds the temporal or spatio-temporal topology from mapsA to
  410. mapsB and vice verse. The spatio-temporal topology structure of each map
  411. will be reseted and rebuild for mapsA and mapsB.
  412. After building the temporal or spatio-temporal topology the modified
  413. map objects of mapsA can be accessed
  414. in the same way as a dictionary using there id.
  415. The implemented iterator assures
  416. the chronological iteration over the mapsA.
  417. @param mapsA A list of abstract_dataset
  418. objects with initiated spatio-temporal extent
  419. @param mapsB An optional list of abstract_dataset
  420. objects with initiated spatio-temporal extent
  421. @param spatial This indicates if the spatial topology is created as well:
  422. spatial can be None (no spatial topology), "2D" using west, east,
  423. south, north or "3D" using west, east, south, north, bottom, top
  424. """
  425. identical = False
  426. if mapsA == mapsB:
  427. identical = True
  428. if mapsB == None:
  429. mapsB = mapsA
  430. idetnical = True
  431. for map_ in mapsA:
  432. map_.reset_topology()
  433. if not identical:
  434. for map_ in mapsB:
  435. map_.reset_topology()
  436. tree = self. _build_rtree(mapsA, spatial)
  437. for j in xrange(len(mapsB)):
  438. list_ = gis.ilist()
  439. rect = self._map_to_rect(tree, mapsB[j], spatial)
  440. vector.RTreeSearch2(tree, rect, byref(list_))
  441. vector.RTreeFreeRect(rect)
  442. for k in xrange(list_.n_values):
  443. i = list_.value[k] - 1
  444. # Get the temporal relationship
  445. relation = mapsB[j].temporal_relation(mapsA[i])
  446. A = mapsA[i]
  447. B = mapsB[j]
  448. set_temoral_relationship(A, B, relation)
  449. if spatial is not None:
  450. relation = mapsB[j].spatial_relation(mapsA[i])
  451. set_spatial_relationship(A, B, relation)
  452. self._build_internal_iteratable(mapsA, spatial)
  453. if not identical and mapsB != None:
  454. self._build_iteratable(mapsB, spatial)
  455. vector.RTreeDestroyTree(tree)
  456. def __iter__(self):
  457. start_ = self._first
  458. while start_ is not None:
  459. yield start_
  460. start_ = start_.next()
  461. def __getitem__(self, index):
  462. return self._store[index.get_id()]
  463. def __len__(self):
  464. return len(self._store)
  465. def __contains__(self, _map):
  466. return _map in self._store.values()
  467. ###############################################################################
  468. def set_temoral_relationship(A, B, relation):
  469. if relation == "equal":
  470. if B != A:
  471. if not B.get_equal() or \
  472. (B.get_equal() and \
  473. A not in B.get_equal()):
  474. B.append_equal(A)
  475. if not A.get_equal() or \
  476. (A.get_equal() and \
  477. B not in A.get_equal()):
  478. A.append_equal(B)
  479. elif relation == "follows":
  480. if not B.get_follows() or \
  481. (B.get_follows() and \
  482. A not in B.get_follows()):
  483. B.append_follows(A)
  484. if not A.get_precedes() or \
  485. (A.get_precedes() and
  486. B not in A.get_precedes()):
  487. A.append_precedes(B)
  488. elif relation == "precedes":
  489. if not B.get_precedes() or \
  490. (B.get_precedes() and \
  491. A not in B.get_precedes()):
  492. B.append_precedes(A)
  493. if not A.get_follows() or \
  494. (A.get_follows() and \
  495. B not in A.get_follows()):
  496. A.append_follows(B)
  497. elif relation == "during" or relation == "starts" or \
  498. relation == "finishes":
  499. if not B.get_during() or \
  500. (B.get_during() and \
  501. A not in B.get_during()):
  502. B.append_during(A)
  503. if not A.get_contains() or \
  504. (A.get_contains() and \
  505. B not in A.get_contains()):
  506. A.append_contains(B)
  507. if relation == "starts":
  508. if not B.get_starts() or \
  509. (B.get_starts() and \
  510. A not in B.get_starts()):
  511. B.append_starts(A)
  512. if not A.get_started() or \
  513. (A.get_started() and \
  514. B not in A.get_started()):
  515. A.append_started(B)
  516. if relation == "finishes":
  517. if not B.get_finishes() or \
  518. (B.get_finishes() and \
  519. A not in B.get_finishes()):
  520. B.append_finishes(A)
  521. if not A.get_finished() or \
  522. (A.get_finished() and \
  523. B not in A.get_finished()):
  524. A.append_finished(B)
  525. elif relation == "contains" or relation == "started" or \
  526. relation == "finished":
  527. if not B.get_contains() or \
  528. (B.get_contains() and \
  529. A not in B.get_contains()):
  530. B.append_contains(A)
  531. if not A.get_during() or \
  532. (A.get_during() and \
  533. B not in A.get_during()):
  534. A.append_during(B)
  535. if relation == "started":
  536. if not B.get_started() or \
  537. (B.get_started() and \
  538. A not in B.get_started()):
  539. B.append_started(A)
  540. if not A.get_starts() or \
  541. (A.get_starts() and \
  542. B not in A.get_starts()):
  543. A.append_starts(B)
  544. if relation == "finished":
  545. if not B.get_finished() or \
  546. (B.get_finished() and \
  547. A not in B.get_finished()):
  548. B.append_finished(A)
  549. if not A.get_finishes() or \
  550. (A.get_finishes() and \
  551. B not in A.get_finishes()):
  552. A.append_finishes(B)
  553. elif relation == "overlaps":
  554. if not B.get_overlaps() or \
  555. (B.get_overlaps() and \
  556. A not in B.get_overlaps()):
  557. B.append_overlaps(A)
  558. if not A.get_overlapped() or \
  559. (A.get_overlapped() and \
  560. B not in A.get_overlapped()):
  561. A.append_overlapped(B)
  562. elif relation == "overlapped":
  563. if not B.get_overlapped() or \
  564. (B.get_overlapped() and \
  565. A not in B.get_overlapped()):
  566. B.append_overlapped(A)
  567. if not A.get_overlaps() or \
  568. (A.get_overlaps() and \
  569. B not in A.get_overlaps()):
  570. A.append_overlaps(B)
  571. ###############################################################################
  572. def set_spatial_relationship(A, B, relation):
  573. if relation == "equivalent":
  574. if B != A:
  575. if not B.get_equivalent() or \
  576. (B.get_equivalent() and \
  577. A not in B.get_equivalent()):
  578. B.append_equivalent(A)
  579. if not A.get_equivalent() or \
  580. (A.get_equivalent() and \
  581. B not in A.get_equivalent()):
  582. A.append_equivalent(B)
  583. elif relation == "overlap":
  584. if not B.get_overlap() or \
  585. (B.get_overlap() and \
  586. A not in B.get_overlap()):
  587. B.append_overlap(A)
  588. if not A.get_overlap() or \
  589. (A.get_overlap() and
  590. B not in A.get_overlap()):
  591. A.append_overlap(B)
  592. elif relation == "meet":
  593. if not B.get_meet() or \
  594. (B.get_meet() and \
  595. A not in B.get_meet()):
  596. B.append_meet(A)
  597. if not A.get_meet() or \
  598. (A.get_meet() and
  599. B not in A.get_meet()):
  600. A.append_meet(B)
  601. elif relation == "contain":
  602. if not B.get_contain() or \
  603. (B.get_contain() and \
  604. A not in B.get_contain()):
  605. B.append_contain(A)
  606. if not A.get_in() or \
  607. (A.get_in() and \
  608. B not in A.get_in()):
  609. A.append_in(B)
  610. elif relation == "in":
  611. if not B.get_in() or \
  612. (B.get_in() and \
  613. A not in B.get_in()):
  614. B.append_in(A)
  615. if not A.get_contain() or \
  616. (A.get_contain() and \
  617. B not in A.get_contain()):
  618. A.append_contain(B)
  619. elif relation == "cover":
  620. if not B.get_cover() or \
  621. (B.get_cover() and \
  622. A not in B.get_cover()):
  623. B.append_cover(A)
  624. if not A.get_covered() or \
  625. (A.get_covered() and \
  626. B not in A.get_covered()):
  627. A.append_covered(B)
  628. elif relation == "covered":
  629. if not B.get_covered() or \
  630. (B.get_covered() and \
  631. A not in B.get_covered()):
  632. B.append_covered(A)
  633. if not A.get_cover() or \
  634. (A.get_cover() and \
  635. B not in A.get_cover()):
  636. A.append_cover(B)
  637. ###############################################################################
  638. def print_temporal_topology_relationships(maps1, maps2=None, dbif=None):
  639. """!Print the temporal relationships of the
  640. map lists maps1 and maps2 to stdout.
  641. @param maps1 A list of abstract_dataset
  642. objects with initiated temporal extent
  643. @param maps2 An optional list of abstract_dataset
  644. objects with initiated temporal extent
  645. @param dbif The database interface to be used
  646. """
  647. tb = SpatioTemporalTopologyBuilder()
  648. tb.build(maps1, maps2)
  649. dbif, connected = init_dbif(dbif)
  650. for _map in tb:
  651. _map.select(dbif)
  652. _map.print_info()
  653. if connected:
  654. dbif.close()
  655. return
  656. ###############################################################################
  657. def print_spatio_temporal_topology_relationships(maps1, maps2=None, spatial="2D", dbif=None):
  658. """!Print the temporal relationships of the
  659. map lists maps1 and maps2 to stdout.
  660. @param maps1 A list of abstract_dataset
  661. objects with initiated temporal extent
  662. @param maps2 An optional list of abstract_dataset
  663. objects with initiated temporal extent
  664. @param spatial The dimension of the spatial extent to be used: "2D" using west, east,
  665. south, north or "3D" using west, east, south, north, bottom, top
  666. @param dbif The database interface to be used
  667. """
  668. tb = SpatioTemporalTopologyBuilder()
  669. tb.build(maps1, maps2, spatial)
  670. dbif, connected = init_dbif(dbif)
  671. for _map in tb:
  672. _map.select(dbif)
  673. _map.print_info()
  674. if connected:
  675. dbif.close()
  676. return
  677. ###############################################################################
  678. def count_temporal_topology_relationships(maps1, maps2=None, dbif=None):
  679. """!Count the temporal relations of a single list of maps or between two lists of maps
  680. @param maps1 A list of abstract_dataset
  681. objects with initiated temporal extent
  682. @param maps2 A list of abstract_dataset
  683. objects with initiated temporal extent
  684. @param dbif The database interface to be used
  685. @return A dictionary with counted temporal relationships
  686. """
  687. tb = SpatioTemporalTopologyBuilder()
  688. tb.build(maps1, maps2)
  689. dbif, connected = init_dbif(dbif)
  690. relations = None
  691. for _map in tb:
  692. if relations != None:
  693. r = _map.get_number_of_relations()
  694. for k in r.keys():
  695. relations[k] += r[k]
  696. else:
  697. relations = _map.get_number_of_relations()
  698. if connected:
  699. dbif.close()
  700. return relations
  701. ###############################################################################
  702. def create_temporal_relation_sql_where_statement(
  703. start, end, use_start=True, use_during=False,
  704. use_overlap=False, use_contain=False, use_equal=False,
  705. use_follows=False, use_precedes=False):
  706. """!Create a SQL WHERE statement for temporal relation selection of maps in space time datasets
  707. @param start The start time
  708. @param end The end time
  709. @param use_start Select maps of which the start time is located in the selection granule
  710. @verbatim
  711. map : s
  712. granule: s-----------------e
  713. map : s--------------------e
  714. granule: s-----------------e
  715. map : s--------e
  716. granule: s-----------------e
  717. @endverbatim
  718. @param use_during Select maps which are temporal during the selection granule
  719. @verbatim
  720. map : s-----------e
  721. granule: s-----------------e
  722. @endverbatim
  723. @param use_overlap Select maps which temporal overlap the selection granule
  724. @verbatim
  725. map : s-----------e
  726. granule: s-----------------e
  727. map : s-----------e
  728. granule: s----------e
  729. @endverbatim
  730. @param use_contain Select maps which temporally contain the selection granule
  731. @verbatim
  732. map : s-----------------e
  733. granule: s-----------e
  734. @endverbatim
  735. @param use_equal Select maps which temporally equal to the selection granule
  736. @verbatim
  737. map : s-----------e
  738. granule: s-----------e
  739. @endverbatim
  740. @param use_follows Select maps which temporally follow the selection granule
  741. @verbatim
  742. map : s-----------e
  743. granule: s-----------e
  744. @endverbatim
  745. @param use_precedes Select maps which temporally precedes the selection granule
  746. @verbatim
  747. map : s-----------e
  748. granule: s-----------e
  749. @endverbatim
  750. Usage:
  751. @code
  752. >>> # Relative time
  753. >>> start = 1
  754. >>> end = 2
  755. >>> create_temporal_relation_sql_where_statement(start, end,
  756. ... use_start=False)
  757. >>> create_temporal_relation_sql_where_statement(start, end)
  758. '((start_time >= 1 and start_time < 2) )'
  759. >>> create_temporal_relation_sql_where_statement(start, end,
  760. ... use_start=True)
  761. '((start_time >= 1 and start_time < 2) )'
  762. >>> create_temporal_relation_sql_where_statement(start, end,
  763. ... use_start=False, use_during=True)
  764. '(((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)))'
  765. >>> create_temporal_relation_sql_where_statement(start, end,
  766. ... use_start=False, use_overlap=True)
  767. '(((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)))'
  768. >>> create_temporal_relation_sql_where_statement(start, end,
  769. ... use_start=False, use_contain=True)
  770. '(((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)))'
  771. >>> create_temporal_relation_sql_where_statement(start, end,
  772. ... use_start=False, use_equal=True)
  773. '((start_time = 1 and end_time = 2))'
  774. >>> create_temporal_relation_sql_where_statement(start, end,
  775. ... use_start=False, use_follows=True)
  776. '((start_time = 2))'
  777. >>> create_temporal_relation_sql_where_statement(start, end,
  778. ... use_start=False, use_precedes=True)
  779. '((end_time = 1))'
  780. >>> create_temporal_relation_sql_where_statement(start, end,
  781. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  782. ... use_equal=True, use_follows=True, use_precedes=True)
  783. '((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))'
  784. >>> # Absolute time
  785. >>> start = datetime(2001, 1, 1, 12, 30)
  786. >>> end = datetime(2001, 3, 31, 14, 30)
  787. >>> create_temporal_relation_sql_where_statement(start, end,
  788. ... use_start=False)
  789. >>> create_temporal_relation_sql_where_statement(start, end)
  790. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  791. >>> create_temporal_relation_sql_where_statement(start, end,
  792. ... use_start=True)
  793. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  794. >>> create_temporal_relation_sql_where_statement(start, end,
  795. ... use_start=False, use_during=True)
  796. "(((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')))"
  797. >>> create_temporal_relation_sql_where_statement(start, end,
  798. ... use_start=False, use_overlap=True)
  799. "(((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')))"
  800. >>> create_temporal_relation_sql_where_statement(start, end,
  801. ... use_start=False, use_contain=True)
  802. "(((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')))"
  803. >>> create_temporal_relation_sql_where_statement(start, end,
  804. ... use_start=False, use_equal=True)
  805. "((start_time = '2001-01-01 12:30:00' and end_time = '2001-03-31 14:30:00'))"
  806. >>> create_temporal_relation_sql_where_statement(start, end,
  807. ... use_start=False, use_follows=True)
  808. "((start_time = '2001-03-31 14:30:00'))"
  809. >>> create_temporal_relation_sql_where_statement(start, end,
  810. ... use_start=False, use_precedes=True)
  811. "((end_time = '2001-01-01 12:30:00'))"
  812. >>> create_temporal_relation_sql_where_statement(start, end,
  813. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  814. ... use_equal=True, use_follows=True, use_precedes=True)
  815. "((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'))"
  816. @endcode
  817. """
  818. where = "("
  819. if use_start:
  820. if isinstance(start, datetime):
  821. where += "(start_time >= '%s' and start_time < '%s') " % (start, end)
  822. else:
  823. where += "(start_time >= %i and start_time < %i) " % (start, end)
  824. if use_during:
  825. if use_start:
  826. where += " OR "
  827. if isinstance(start, datetime):
  828. where += "((start_time > '%s' and end_time < '%s') OR " % (start, end)
  829. where += "(start_time >= '%s' and end_time < '%s') OR " % (start, end)
  830. where += "(start_time > '%s' and end_time <= '%s'))" % (start, end)
  831. else:
  832. where += "((start_time > %i and end_time < %i) OR " % (start, end)
  833. where += "(start_time >= %i and end_time < %i) OR " % (start, end)
  834. where += "(start_time > %i and end_time <= %i))" % (start, end)
  835. if use_overlap:
  836. if use_start or use_during:
  837. where += " OR "
  838. if isinstance(start, datetime):
  839. where += "((start_time < '%s' and end_time > '%s' and end_time < '%s') OR " % (start, start, end)
  840. where += "(start_time < '%s' and start_time > '%s' and end_time > '%s'))" % (end, start, end)
  841. else:
  842. where += "((start_time < %i and end_time > %i and end_time < %i) OR " % (start, start, end)
  843. where += "(start_time < %i and start_time > %i and end_time > %i))" % (end, start, end)
  844. if use_contain:
  845. if use_start or use_during or use_overlap:
  846. where += " OR "
  847. if isinstance(start, datetime):
  848. where += "((start_time < '%s' and end_time > '%s') OR " % (start, end)
  849. where += "(start_time <= '%s' and end_time > '%s') OR " % (start, end)
  850. where += "(start_time < '%s' and end_time >= '%s'))" % (start, end)
  851. else:
  852. where += "((start_time < %i and end_time > %i) OR " % (start, end)
  853. where += "(start_time <= %i and end_time > %i) OR " % (start, end)
  854. where += "(start_time < %i and end_time >= %i))" % (start, end)
  855. if use_equal:
  856. if use_start or use_during or use_overlap or use_contain:
  857. where += " OR "
  858. if isinstance(start, datetime):
  859. where += "(start_time = '%s' and end_time = '%s')" % (start, end)
  860. else:
  861. where += "(start_time = %i and end_time = %i)" % (start, end)
  862. if use_follows:
  863. if use_start or use_during or use_overlap or use_contain or use_equal:
  864. where += " OR "
  865. if isinstance(start, datetime):
  866. where += "(start_time = '%s')" % (end)
  867. else:
  868. where += "(start_time = %i)" % (end)
  869. if use_precedes:
  870. if use_start or use_during or use_overlap or use_contain or use_equal \
  871. or use_follows:
  872. where += " OR "
  873. if isinstance(start, datetime):
  874. where += "(end_time = '%s')" % (start)
  875. else:
  876. where += "(end_time = %i)" % (start)
  877. where += ")"
  878. # Catch empty where statement
  879. if where == "()":
  880. where = None
  881. return where
  882. ###############################################################################
  883. if __name__ == "__main__":
  884. import doctest
  885. doctest.testmod()