spatio_temporal_relationships.py 46 KB

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