temporal_relationships.py 27 KB

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