temporal_relationships.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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_map_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 TemporalTopologyBuilder(object):
  23. """!This class is designed to build the temporal topology
  24. of 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. identical = False
  33. tb = TemporalTopologyBuilder()
  34. tb.build(maps)
  35. dbif, connected = init_dbif(None)
  36. for _map in tb:
  37. _map.select(dbif)
  38. _map.print_info()
  39. # Using the next and previous methods, we can iterate over the
  40. # topological related maps in this way
  41. _first = tb.get_first()
  42. while _first:
  43. _first.print_topology_info()
  44. _first = _first.next()
  45. # Dictionary like accessed
  46. _map = tb["name@mapset"]
  47. @endcode
  48. """
  49. def __init__(self):
  50. self._reset()
  51. # 0001-01-01 00:00:00
  52. self._timeref = datetime(1,1,1)
  53. def _reset(self):
  54. self._store = {}
  55. self._first = None
  56. self._iteratable = False
  57. def _set_first(self, first):
  58. self._first = first
  59. self._insert(first)
  60. def _detect_first(self):
  61. if len(self) > 0:
  62. prev_ = self._store.values()[0]
  63. while prev_ is not None:
  64. self._first = prev_
  65. prev_ = prev_.prev()
  66. def _insert(self, t):
  67. self._store[t.get_id()] = t
  68. def get_first(self):
  69. """!Return the first map with the earliest start time
  70. @return The map with the earliest start time
  71. """
  72. return self._first
  73. def _build_internal_iteratable(self, maps):
  74. """!Build an iteratable temporal topology structure for all maps in
  75. the list and store the maps internally
  76. Basically the "next" and "prev" relations will be set in the
  77. temporal topology structure of each map
  78. The maps will be added to the object, so they can be
  79. accessed using the iterator of this class
  80. @param maps: A sorted (by start_time)list of abstract_dataset
  81. objects with initiated temporal extent
  82. """
  83. self._build_iteratable(maps)
  84. for _map in maps:
  85. self._insert(_map)
  86. # Detect the first map
  87. self._detect_first()
  88. def _build_iteratable(self, maps):
  89. """!Build an iteratable temporal topology structure for
  90. all maps in the list
  91. Basically the "next" and "prev" relations will be set in
  92. the temporal topology structure of each map.
  93. @param maps: A sorted (by start_time)list of abstract_dataset
  94. objects with initiated temporal extent
  95. """
  96. # for i in xrange(len(maps)):
  97. # offset = i + 1
  98. # for j in xrange(offset, len(maps)):
  99. # # Get the temporal relationship
  100. # relation = maps[j].temporal_relation(maps[i])
  101. #
  102. # # Build the next reference
  103. # if relation != "equivalent" and relation != "started":
  104. # maps[i].set_next(maps[j])
  105. # break
  106. # First we need to order the map list chronologically
  107. sorted_maps = sorted(
  108. maps, key=AbstractDatasetComparisonKeyStartTime)
  109. for i in xrange(len(sorted_maps) - 1):
  110. sorted_maps[i].set_next(sorted_maps[i + 1])
  111. for map_ in sorted_maps:
  112. next_ = map_.next()
  113. if next_:
  114. next_.set_prev(map_)
  115. map_.set_topology_build_true()
  116. def _map_to_rect(self, tree, map_):
  117. """Use the temporal extent of a map to create and return a RTree rectange"""
  118. rect = vector.RTreeAllocRect(tree)
  119. start, end = map_.get_valid_time()
  120. if not end:
  121. end = start
  122. if map_.is_time_absolute():
  123. start = time_delta_to_relative_time(start - self._timeref)
  124. end = time_delta_to_relative_time(end - self._timeref)
  125. vector.RTreeSetRect1D(rect, tree, float(start), float(end))
  126. return rect
  127. def _build_1d_rtree(self, maps):
  128. """Build and return the one dimensional R*-Tree"""
  129. tree = vector.RTreeCreateTree(-1, 0, 4)
  130. for i in xrange(len(maps)):
  131. rect = self._map_to_rect(tree, maps[i])
  132. vector.RTreeInsertRect(rect, i + 1, tree)
  133. return tree
  134. def build(self, mapsA, mapsB=None):
  135. """!Build the temporal topology structure between
  136. one or two unordered lists of abstract dataset objects
  137. This method builds the temporal topology from mapsA to
  138. mapsB and vice verse. The temporal topology structure of each map,
  139. defined in class temporal_map_relations,
  140. will be reseted and rebuild for mapsA and mapsB.
  141. After building the temporal topology the modified
  142. map objects of mapsA can be accessed
  143. in the same way as a dictionary using there id.
  144. The implemented iterator assures
  145. the chronological iteration over the mapsA.
  146. @param mapsA: A list of abstract_dataset
  147. objects with initiated temporal extent
  148. @param mapsB: An optional list of abstract_dataset
  149. objects with initiated temporal extent
  150. """
  151. identical = False
  152. if mapsA == mapsB:
  153. identical = True
  154. if mapsB == None:
  155. mapsB = mapsA
  156. idetnical = True
  157. for map_ in mapsA:
  158. map_.reset_topology()
  159. if not identical:
  160. for map_ in mapsB:
  161. map_.reset_topology()
  162. tree = self. _build_1d_rtree(mapsA)
  163. for j in xrange(len(mapsB)):
  164. list_ = gis.ilist()
  165. rect = self._map_to_rect(tree, mapsB[j])
  166. num = vector.RTreeSearch2(tree, rect, byref(list_))
  167. vector.RTreeFreeRect(rect)
  168. for k in xrange(list_.n_values):
  169. i = list_.value[k] - 1
  170. # Get the temporal relationship
  171. relation = mapsB[j].temporal_relation(mapsA[i])
  172. if relation == "equivalent":
  173. if mapsB[j].get_id() != mapsA[i].get_id():
  174. mapsB[j].append_equivalent(mapsA[i])
  175. mapsA[i].append_equivalent(mapsB[j])
  176. elif relation == "follows":
  177. if not mapsB[j].get_follows() or \
  178. (mapsB[j].get_follows() and \
  179. mapsA[i] not in mapsB[j].get_follows()):
  180. mapsB[j].append_follows(mapsA[i])
  181. if not mapsA[i].get_precedes() or \
  182. (mapsA[i].get_precedes() and
  183. mapsB[j] not in mapsA[i].get_precedes()):
  184. mapsA[i].append_precedes(mapsB[j])
  185. elif relation == "precedes":
  186. if not mapsB[j].get_precedes() or \
  187. (mapsB[j].get_precedes() and \
  188. mapsA[i] not in mapsB[j].get_precedes()):
  189. mapsB[j].append_precedes(mapsA[i])
  190. if not mapsA[i].get_follows() or \
  191. (mapsA[i].get_follows() and \
  192. mapsB[j] not in mapsA[i].get_follows()):
  193. mapsA[i].append_follows(mapsB[j])
  194. elif relation == "during" or relation == "starts" or \
  195. relation == "finishes":
  196. if not mapsB[j].get_during() or \
  197. (mapsB[j].get_during() and \
  198. mapsA[i] not in mapsB[j].get_during()):
  199. mapsB[j].append_during(mapsA[i])
  200. if not mapsA[i].get_contains() or \
  201. (mapsA[i].get_contains() and \
  202. mapsB[j] not in mapsA[i].get_contains()):
  203. mapsA[i].append_contains(mapsB[j])
  204. if relation == "starts":
  205. if not mapsB[j].get_starts() or \
  206. (mapsB[j].get_starts() and \
  207. mapsA[i] not in mapsB[j].get_starts()):
  208. mapsB[j].append_starts(mapsA[i])
  209. if not mapsA[i].get_started() or \
  210. (mapsA[i].get_started() and \
  211. mapsB[j] not in mapsA[i].get_started()):
  212. mapsA[i].append_started(mapsB[j])
  213. if relation == "finishes":
  214. if not mapsB[j].get_finishes() or \
  215. (mapsB[j].get_finishes() and \
  216. mapsA[i] not in mapsB[j].get_finishes()):
  217. mapsB[j].append_finishes(mapsA[i])
  218. if not mapsA[i].get_finished() or \
  219. (mapsA[i].get_finished() and \
  220. mapsB[j] not in mapsA[i].get_finished()):
  221. mapsA[i].append_finished(mapsB[j])
  222. elif relation == "contains" or relation == "started" or \
  223. relation == "finished":
  224. if not mapsB[j].get_contains() or \
  225. (mapsB[j].get_contains() and \
  226. mapsA[i] not in mapsB[j].get_contains()):
  227. mapsB[j].append_contains(mapsA[i])
  228. if not mapsA[i].get_during() or \
  229. (mapsA[i].get_during() and \
  230. mapsB[j] not in mapsA[i].get_during()):
  231. mapsA[i].append_during(mapsB[j])
  232. if relation == "started":
  233. if not mapsB[j].get_started() or \
  234. (mapsB[j].get_started() and \
  235. mapsA[i] not in mapsB[j].get_started()):
  236. mapsB[j].append_started(mapsA[i])
  237. if not mapsA[i].get_starts() or \
  238. (mapsA[i].get_starts() and \
  239. mapsB[j] not in mapsA[i].get_starts()):
  240. mapsA[i].append_starts(mapsB[j])
  241. if relation == "finished":
  242. if not mapsB[j].get_finished() or \
  243. (mapsB[j].get_finished() and \
  244. mapsA[i] not in mapsB[j].get_finished()):
  245. mapsB[j].append_finished(mapsA[i])
  246. if not mapsA[i].get_finishes() or \
  247. (mapsA[i].get_finishes() and \
  248. mapsB[j] not in mapsA[i].get_finishes()):
  249. mapsA[i].append_finishes(mapsB[j])
  250. elif relation == "overlaps":
  251. if not mapsB[j].get_overlaps() or \
  252. (mapsB[j].get_overlaps() and \
  253. mapsA[i] not in mapsB[j].get_overlaps()):
  254. mapsB[j].append_overlaps(mapsA[i])
  255. if not mapsA[i].get_overlapped() or \
  256. (mapsA[i].get_overlapped() and \
  257. mapsB[j] not in mapsA[i].get_overlapped()):
  258. mapsA[i].append_overlapped(mapsB[j])
  259. elif relation == "overlapped":
  260. if not mapsB[j].get_overlapped() or \
  261. (mapsB[j].get_overlapped() and \
  262. mapsA[i] not in mapsB[j].get_overlapped()):
  263. mapsB[j].append_overlapped(mapsA[i])
  264. if not mapsA[i].get_overlaps() or \
  265. (mapsA[i].get_overlaps() and \
  266. mapsB[j] not in mapsA[i].get_overlaps()):
  267. mapsA[i].append_overlaps(mapsB[j])
  268. self._build_internal_iteratable(mapsA)
  269. if not identical and mapsB != None:
  270. self._build_iteratable(mapsB)
  271. vector.RTreeDestroyTree(tree)
  272. def __iter__(self):
  273. start_ = self._first
  274. while start_ is not None:
  275. yield start_
  276. start_ = start_.next()
  277. def __getitem__(self, index):
  278. return self._store[index.get_id()]
  279. def __len__(self):
  280. return len(self._store)
  281. def __contains__(self, _map):
  282. return _map in self._store.values()
  283. ###############################################################################
  284. def print_temporal_topology_relationships(maps1, maps2=None, dbif=None):
  285. """!Print the temporal relationships of the
  286. map lists maps1 and maps2 to stdout.
  287. @param maps1: A list of abstract_dataset
  288. objects with initiated temporal extent
  289. @param maps2: An optional list of abstract_dataset
  290. objects with initiated temporal extent
  291. @param dbif: The database interface to be used
  292. """
  293. tb = TemporalTopologyBuilder()
  294. tb.build(maps1, maps2)
  295. dbif, connected = init_dbif(dbif)
  296. for _map in tb:
  297. _map.select(dbif)
  298. _map.print_info()
  299. if connected:
  300. dbif.close()
  301. return
  302. ###############################################################################
  303. def count_temporal_topology_relationships(maps1, maps2=None, dbif=None):
  304. """!Count the temporal relations of a single list of maps or between two lists of maps
  305. @param maps1: A list of abstract_dataset
  306. objects with initiated temporal extent
  307. @param maps2: A list of abstract_dataset
  308. objects with initiated temporal extent
  309. @param dbif: The database interface to be used
  310. @return A dictionary with counted temporal relationships
  311. """
  312. tb = TemporalTopologyBuilder()
  313. tb.build(maps1, maps2)
  314. dbif, connected = init_dbif(dbif)
  315. relations = None
  316. for _map in tb:
  317. if relations != None:
  318. r = _map.get_number_of_relations()
  319. for k in r.keys():
  320. relations[k] += r[k]
  321. else:
  322. relations = _map.get_number_of_relations()
  323. if connected:
  324. dbif.close()
  325. return relations
  326. ###############################################################################
  327. def create_temporal_relation_sql_where_statement(
  328. start, end, use_start=True, use_during=False,
  329. use_overlap=False, use_contain=False, use_equal=False,
  330. use_follows=False, use_precedes=False):
  331. """!Create a SQL WHERE statement for temporal relation selection of maps in space time datasets
  332. @param start: The start time
  333. @param end: The end time
  334. @param use_start: Select maps of which the start time is located in the selection granule
  335. @verbatim
  336. map : s
  337. granule: s-----------------e
  338. map : s--------------------e
  339. granule: s-----------------e
  340. map : s--------e
  341. granule: s-----------------e
  342. @endverbatim
  343. @param use_during: during: Select maps which are temporal during the selection granule
  344. @verbatim
  345. map : s-----------e
  346. granule: s-----------------e
  347. @endverbatim
  348. @param use_overlap: Select maps which temporal overlap the selection granule
  349. @verbatim
  350. map : s-----------e
  351. granule: s-----------------e
  352. map : s-----------e
  353. granule: s----------e
  354. @endverbatim
  355. @param use_contain: Select maps which temporally contain the selection granule
  356. @verbatim
  357. map : s-----------------e
  358. granule: s-----------e
  359. @endverbatim
  360. @param use_equal: Select maps which temporally equal to the selection granule
  361. @verbatim
  362. map : s-----------e
  363. granule: s-----------e
  364. @endverbatim
  365. @param use_follows: Select maps which temporally follow the selection granule
  366. @verbatim
  367. map : s-----------e
  368. granule: s-----------e
  369. @endverbatim
  370. @param use_precedes: Select maps which temporally precedes the selection granule
  371. @verbatim
  372. map : s-----------e
  373. granule: s-----------e
  374. @endverbatim
  375. Usage:
  376. @code
  377. >>> # Relative time
  378. >>> start = 1
  379. >>> end = 2
  380. >>> create_temporal_relation_sql_where_statement(start, end,
  381. ... use_start=False)
  382. >>> create_temporal_relation_sql_where_statement(start, end)
  383. '((start_time >= 1 and start_time < 2) )'
  384. >>> create_temporal_relation_sql_where_statement(start, end,
  385. ... use_start=True)
  386. '((start_time >= 1 and start_time < 2) )'
  387. >>> create_temporal_relation_sql_where_statement(start, end,
  388. ... use_start=False, use_during=True)
  389. '(((start_time > 1 and end_time < 2) OR (start_time >= 1 and end_time < 2) OR (start_time > 1 and end_time <= 2)))'
  390. >>> create_temporal_relation_sql_where_statement(start, end,
  391. ... use_start=False, use_overlap=True)
  392. '(((start_time < 1 and end_time > 1 and end_time < 2) OR (start_time < 2 and start_time > 1 and end_time > 2)))'
  393. >>> create_temporal_relation_sql_where_statement(start, end,
  394. ... use_start=False, use_contain=True)
  395. '(((start_time < 1 and end_time > 2) OR (start_time <= 1 and end_time > 2) OR (start_time < 1 and end_time >= 2)))'
  396. >>> create_temporal_relation_sql_where_statement(start, end,
  397. ... use_start=False, use_equal=True)
  398. '((start_time = 1 and end_time = 2))'
  399. >>> create_temporal_relation_sql_where_statement(start, end,
  400. ... use_start=False, use_follows=True)
  401. '((start_time = 2))'
  402. >>> create_temporal_relation_sql_where_statement(start, end,
  403. ... use_start=False, use_precedes=True)
  404. '((end_time = 1))'
  405. >>> create_temporal_relation_sql_where_statement(start, end,
  406. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  407. ... use_equal=True, use_follows=True, use_precedes=True)
  408. '((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))'
  409. >>> # Absolute time
  410. >>> start = datetime(2001, 1, 1, 12, 30)
  411. >>> end = datetime(2001, 3, 31, 14, 30)
  412. >>> create_temporal_relation_sql_where_statement(start, end,
  413. ... use_start=False)
  414. >>> create_temporal_relation_sql_where_statement(start, end)
  415. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  416. >>> create_temporal_relation_sql_where_statement(start, end,
  417. ... use_start=True)
  418. "((start_time >= '2001-01-01 12:30:00' and start_time < '2001-03-31 14:30:00') )"
  419. >>> create_temporal_relation_sql_where_statement(start, end,
  420. ... use_start=False, use_during=True)
  421. "(((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')))"
  422. >>> create_temporal_relation_sql_where_statement(start, end,
  423. ... use_start=False, use_overlap=True)
  424. "(((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')))"
  425. >>> create_temporal_relation_sql_where_statement(start, end,
  426. ... use_start=False, use_contain=True)
  427. "(((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')))"
  428. >>> create_temporal_relation_sql_where_statement(start, end,
  429. ... use_start=False, use_equal=True)
  430. "((start_time = '2001-01-01 12:30:00' and end_time = '2001-03-31 14:30:00'))"
  431. >>> create_temporal_relation_sql_where_statement(start, end,
  432. ... use_start=False, use_follows=True)
  433. "((start_time = '2001-03-31 14:30:00'))"
  434. >>> create_temporal_relation_sql_where_statement(start, end,
  435. ... use_start=False, use_precedes=True)
  436. "((end_time = '2001-01-01 12:30:00'))"
  437. >>> create_temporal_relation_sql_where_statement(start, end,
  438. ... use_start=True, use_during=True, use_overlap=True, use_contain=True,
  439. ... use_equal=True, use_follows=True, use_precedes=True)
  440. "((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'))"
  441. @endcode
  442. """
  443. where = "("
  444. if use_start:
  445. if isinstance(start, datetime):
  446. where += "(start_time >= '%s' and start_time < '%s') " % (start, end)
  447. else:
  448. where += "(start_time >= %i and start_time < %i) " % (start, end)
  449. if use_during:
  450. if use_start:
  451. where += " OR "
  452. if isinstance(start, datetime):
  453. where += "((start_time > '%s' and end_time < '%s') OR " % (start, end)
  454. where += "(start_time >= '%s' and end_time < '%s') OR " % (start, end)
  455. where += "(start_time > '%s' and end_time <= '%s'))" % (start, end)
  456. else:
  457. where += "((start_time > %i and end_time < %i) OR " % (start, end)
  458. where += "(start_time >= %i and end_time < %i) OR " % (start, end)
  459. where += "(start_time > %i and end_time <= %i))" % (start, end)
  460. if use_overlap:
  461. if use_start or use_during:
  462. where += " OR "
  463. if isinstance(start, datetime):
  464. where += "((start_time < '%s' and end_time > '%s' and end_time < '%s') OR " % (start, start, end)
  465. where += "(start_time < '%s' and start_time > '%s' and end_time > '%s'))" % (end, start, end)
  466. else:
  467. where += "((start_time < %i and end_time > %i and end_time < %i) OR " % (start, start, end)
  468. where += "(start_time < %i and start_time > %i and end_time > %i))" % (end, start, end)
  469. if use_contain:
  470. if use_start or use_during or use_overlap:
  471. where += " OR "
  472. if isinstance(start, datetime):
  473. where += "((start_time < '%s' and end_time > '%s') OR " % (start, end)
  474. where += "(start_time <= '%s' and end_time > '%s') OR " % (start, end)
  475. where += "(start_time < '%s' and end_time >= '%s'))" % (start, end)
  476. else:
  477. where += "((start_time < %i and end_time > %i) OR " % (start, end)
  478. where += "(start_time <= %i and end_time > %i) OR " % (start, end)
  479. where += "(start_time < %i and end_time >= %i))" % (start, end)
  480. if use_equal:
  481. if use_start or use_during or use_overlap or use_contain:
  482. where += " OR "
  483. if isinstance(start, datetime):
  484. where += "(start_time = '%s' and end_time = '%s')" % (start, end)
  485. else:
  486. where += "(start_time = %i and end_time = %i)" % (start, end)
  487. if use_follows:
  488. if use_start or use_during or use_overlap or use_contain or use_equal:
  489. where += " OR "
  490. if isinstance(start, datetime):
  491. where += "(start_time = '%s')" % (end)
  492. else:
  493. where += "(start_time = %i)" % (end)
  494. if use_precedes:
  495. if use_start or use_during or use_overlap or use_contain or use_equal \
  496. or use_follows:
  497. where += " OR "
  498. if isinstance(start, datetime):
  499. where += "(end_time = '%s')" % (start)
  500. else:
  501. where += "(end_time = %i)" % (start)
  502. where += ")"
  503. # Catch empty where statement
  504. if where == "()":
  505. where = None
  506. return where