temporal_algebra.py 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  1. """!@package grass.temporal
  2. Temporal algebra parser class
  3. (C) 2014 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. @authors Thomas Leppelt and Soeren Gebbert
  8. @code
  9. >>> import grass.temporal as tgis
  10. >>> tgis.init(True)
  11. >>> p = tgis.TemporalAlgebraLexer()
  12. >>> p.build()
  13. >>> p.debug = True
  14. >>> expression = "C = A : B"
  15. >>> p.test(expression)
  16. C = A : B
  17. LexToken(NAME,'C',1,0)
  18. LexToken(EQUALS,'=',1,2)
  19. LexToken(NAME,'A',1,4)
  20. LexToken(T_SELECT,':',1,6)
  21. LexToken(NAME,'B',1,8)
  22. >>> expression = "C = test1 !: test2"
  23. >>> p.test(expression)
  24. C = test1 !: test2
  25. LexToken(NAME,'C',1,0)
  26. LexToken(EQUALS,'=',1,2)
  27. LexToken(NAME,'test1',1,4)
  28. LexToken(T_NOT_SELECT,'!:',1,10)
  29. LexToken(NAME,'test2',1,13)
  30. >>> expression = "C = test1 {equal,:} test2"
  31. >>> p.test(expression)
  32. C = test1 {equal,:} test2
  33. LexToken(NAME,'C',1,0)
  34. LexToken(EQUALS,'=',1,2)
  35. LexToken(NAME,'test1',1,4)
  36. LexToken(T_SELECT_OPERATOR,'{equal,:}',1,10)
  37. LexToken(NAME,'test2',1,20)
  38. >>> expression = "C = test1 {equal,!:} test2"
  39. >>> p.test(expression)
  40. C = test1 {equal,!:} test2
  41. LexToken(NAME,'C',1,0)
  42. LexToken(EQUALS,'=',1,2)
  43. LexToken(NAME,'test1',1,4)
  44. LexToken(T_SELECT_OPERATOR,'{equal,!:}',1,10)
  45. LexToken(NAME,'test2',1,21)
  46. >>> expression = "C = test1 # test2"
  47. >>> p.test(expression)
  48. C = test1 # test2
  49. LexToken(NAME,'C',1,0)
  50. LexToken(EQUALS,'=',1,2)
  51. LexToken(NAME,'test1',1,4)
  52. LexToken(HASH,'#',1,10)
  53. LexToken(NAME,'test2',1,12)
  54. >>> expression = "C = test1 {#} test2"
  55. >>> p.test(expression)
  56. C = test1 {#} test2
  57. LexToken(NAME,'C',1,0)
  58. LexToken(EQUALS,'=',1,2)
  59. LexToken(NAME,'test1',1,4)
  60. LexToken(T_HASH_OPERATOR,'{#}',1,10)
  61. LexToken(NAME,'test2',1,14)
  62. >>> expression = "C = test1 {equal,#} test2"
  63. >>> p.test(expression)
  64. C = test1 {equal,#} test2
  65. LexToken(NAME,'C',1,0)
  66. LexToken(EQUALS,'=',1,2)
  67. LexToken(NAME,'test1',1,4)
  68. LexToken(T_HASH_OPERATOR,'{equal,#}',1,10)
  69. LexToken(NAME,'test2',1,20)
  70. >>> expression = "C = test1 {equal|during,#} test2"
  71. >>> p.test(expression)
  72. C = test1 {equal|during,#} test2
  73. LexToken(NAME,'C',1,0)
  74. LexToken(EQUALS,'=',1,2)
  75. LexToken(NAME,'test1',1,4)
  76. LexToken(T_HASH_OPERATOR,'{equal|during,#}',1,10)
  77. LexToken(NAME,'test2',1,27)
  78. >>> expression = "E = test1 : test2 !: test1"
  79. >>> p.test(expression)
  80. E = test1 : test2 !: test1
  81. LexToken(NAME,'E',1,0)
  82. LexToken(EQUALS,'=',1,2)
  83. LexToken(NAME,'test1',1,4)
  84. LexToken(T_SELECT,':',1,10)
  85. LexToken(NAME,'test2',1,12)
  86. LexToken(T_NOT_SELECT,'!:',1,18)
  87. LexToken(NAME,'test1',1,21)
  88. >>> expression = 'D = buff_t(test1,"10 months")'
  89. >>> p.test(expression)
  90. D = buff_t(test1,"10 months")
  91. LexToken(NAME,'D',1,0)
  92. LexToken(EQUALS,'=',1,2)
  93. LexToken(BUFF_T,'buff_t',1,4)
  94. LexToken(LPAREN,'(',1,10)
  95. LexToken(NAME,'test1',1,11)
  96. LexToken(COMMA,',',1,16)
  97. LexToken(QUOTE,'"',1,17)
  98. LexToken(INT,10,1,18)
  99. LexToken(NAME,'months',1,21)
  100. LexToken(QUOTE,'"',1,27)
  101. LexToken(RPAREN,')',1,28)
  102. >>> expression = 'H = tsnap(test1)'
  103. >>> p.test(expression)
  104. H = tsnap(test1)
  105. LexToken(NAME,'H',1,0)
  106. LexToken(EQUALS,'=',1,2)
  107. LexToken(TSNAP,'tsnap',1,4)
  108. LexToken(LPAREN,'(',1,9)
  109. LexToken(NAME,'test1',1,10)
  110. LexToken(RPAREN,')',1,15)
  111. >>> expression = 'H = tsnap(test2 {during,:} buff_t(test1, "1 days"))'
  112. >>> p.test(expression)
  113. H = tsnap(test2 {during,:} buff_t(test1, "1 days"))
  114. LexToken(NAME,'H',1,0)
  115. LexToken(EQUALS,'=',1,2)
  116. LexToken(TSNAP,'tsnap',1,4)
  117. LexToken(LPAREN,'(',1,9)
  118. LexToken(NAME,'test2',1,10)
  119. LexToken(T_SELECT_OPERATOR,'{during,:}',1,16)
  120. LexToken(BUFF_T,'buff_t',1,27)
  121. LexToken(LPAREN,'(',1,33)
  122. LexToken(NAME,'test1',1,34)
  123. LexToken(COMMA,',',1,39)
  124. LexToken(QUOTE,'"',1,41)
  125. LexToken(INT,1,1,42)
  126. LexToken(NAME,'days',1,44)
  127. LexToken(QUOTE,'"',1,48)
  128. LexToken(RPAREN,')',1,49)
  129. LexToken(RPAREN,')',1,50)
  130. >>> expression = 'H = tshift(test2 {during,:} buff_t(test1, "1 days"), "1 months")'
  131. >>> p.test(expression)
  132. H = tshift(test2 {during,:} buff_t(test1, "1 days"), "1 months")
  133. LexToken(NAME,'H',1,0)
  134. LexToken(EQUALS,'=',1,2)
  135. LexToken(TSHIFT,'tshift',1,4)
  136. LexToken(LPAREN,'(',1,10)
  137. LexToken(NAME,'test2',1,11)
  138. LexToken(T_SELECT_OPERATOR,'{during,:}',1,17)
  139. LexToken(BUFF_T,'buff_t',1,28)
  140. LexToken(LPAREN,'(',1,34)
  141. LexToken(NAME,'test1',1,35)
  142. LexToken(COMMA,',',1,40)
  143. LexToken(QUOTE,'"',1,42)
  144. LexToken(INT,1,1,43)
  145. LexToken(NAME,'days',1,45)
  146. LexToken(QUOTE,'"',1,49)
  147. LexToken(RPAREN,')',1,50)
  148. LexToken(COMMA,',',1,51)
  149. LexToken(QUOTE,'"',1,53)
  150. LexToken(INT,1,1,54)
  151. LexToken(NAME,'months',1,56)
  152. LexToken(QUOTE,'"',1,62)
  153. LexToken(RPAREN,')',1,63)
  154. >>> expression = 'H = tshift(A , 10)'
  155. >>> p.test(expression)
  156. H = tshift(A , 10)
  157. LexToken(NAME,'H',1,0)
  158. LexToken(EQUALS,'=',1,2)
  159. LexToken(TSHIFT,'tshift',1,4)
  160. LexToken(LPAREN,'(',1,10)
  161. LexToken(NAME,'A',1,11)
  162. LexToken(COMMA,',',1,13)
  163. LexToken(INT,10,1,15)
  164. LexToken(RPAREN,')',1,17)
  165. >>> expression = 'H = if(td(A) > 10, A)'
  166. >>> p.test(expression)
  167. H = if(td(A) > 10, A)
  168. LexToken(NAME,'H',1,0)
  169. LexToken(EQUALS,'=',1,2)
  170. LexToken(IF,'if',1,4)
  171. LexToken(LPAREN,'(',1,6)
  172. LexToken(TD,'td',1,7)
  173. LexToken(LPAREN,'(',1,9)
  174. LexToken(NAME,'A',1,10)
  175. LexToken(RPAREN,')',1,11)
  176. LexToken(GREATER,'>',1,13)
  177. LexToken(INT,10,1,15)
  178. LexToken(COMMA,',',1,17)
  179. LexToken(NAME,'A',1,19)
  180. LexToken(RPAREN,')',1,20)
  181. >>> expression = 'H = if(td(A) > 10, A, B)'
  182. >>> p.test(expression)
  183. H = if(td(A) > 10, A, B)
  184. LexToken(NAME,'H',1,0)
  185. LexToken(EQUALS,'=',1,2)
  186. LexToken(IF,'if',1,4)
  187. LexToken(LPAREN,'(',1,6)
  188. LexToken(TD,'td',1,7)
  189. LexToken(LPAREN,'(',1,9)
  190. LexToken(NAME,'A',1,10)
  191. LexToken(RPAREN,')',1,11)
  192. LexToken(GREATER,'>',1,13)
  193. LexToken(INT,10,1,15)
  194. LexToken(COMMA,',',1,17)
  195. LexToken(NAME,'A',1,19)
  196. LexToken(COMMA,',',1,20)
  197. LexToken(NAME,'B',1,22)
  198. LexToken(RPAREN,')',1,23)
  199. >>> expression = 'I = if(equals,td(A) > 10 {equals,||} td(B) < 10, A)'
  200. >>> p.test(expression)
  201. I = if(equals,td(A) > 10 {equals,||} td(B) < 10, A)
  202. LexToken(NAME,'I',1,0)
  203. LexToken(EQUALS,'=',1,2)
  204. LexToken(IF,'if',1,4)
  205. LexToken(LPAREN,'(',1,6)
  206. LexToken(NAME,'equals',1,7)
  207. LexToken(COMMA,',',1,13)
  208. LexToken(TD,'td',1,14)
  209. LexToken(LPAREN,'(',1,16)
  210. LexToken(NAME,'A',1,17)
  211. LexToken(RPAREN,')',1,18)
  212. LexToken(GREATER,'>',1,20)
  213. LexToken(INT,10,1,22)
  214. LexToken(T_COMP_OPERATOR,'{equals,||}',1,25)
  215. LexToken(TD,'td',1,37)
  216. LexToken(LPAREN,'(',1,39)
  217. LexToken(NAME,'B',1,40)
  218. LexToken(RPAREN,')',1,41)
  219. LexToken(LOWER,'<',1,43)
  220. LexToken(INT,10,1,45)
  221. LexToken(COMMA,',',1,47)
  222. LexToken(NAME,'A',1,49)
  223. LexToken(RPAREN,')',1,50)
  224. >>> expression = 'I = if(equals,td(A) > 10 || start_day() < 10, A)'
  225. >>> p.test(expression)
  226. I = if(equals,td(A) > 10 || start_day() < 10, A)
  227. LexToken(NAME,'I',1,0)
  228. LexToken(EQUALS,'=',1,2)
  229. LexToken(IF,'if',1,4)
  230. LexToken(LPAREN,'(',1,6)
  231. LexToken(NAME,'equals',1,7)
  232. LexToken(COMMA,',',1,13)
  233. LexToken(TD,'td',1,14)
  234. LexToken(LPAREN,'(',1,16)
  235. LexToken(NAME,'A',1,17)
  236. LexToken(RPAREN,')',1,18)
  237. LexToken(GREATER,'>',1,20)
  238. LexToken(INT,10,1,22)
  239. LexToken(OR,'|',1,25)
  240. LexToken(OR,'|',1,26)
  241. LexToken(START_DAY,'start_day',1,28)
  242. LexToken(LPAREN,'(',1,37)
  243. LexToken(RPAREN,')',1,38)
  244. LexToken(LOWER,'<',1,40)
  245. LexToken(INT,10,1,42)
  246. LexToken(COMMA,',',1,44)
  247. LexToken(NAME,'A',1,46)
  248. LexToken(RPAREN,')',1,47)
  249. >>> expression = 'E = if({equals},td(A) >= 4 {contain,&&} td(B) == 2, C : D)'
  250. >>> p.test(expression)
  251. E = if({equals},td(A) >= 4 {contain,&&} td(B) == 2, C : D)
  252. LexToken(NAME,'E',1,0)
  253. LexToken(EQUALS,'=',1,2)
  254. LexToken(IF,'if',1,4)
  255. LexToken(LPAREN,'(',1,6)
  256. LexToken(T_REL_OPERATOR,'{equals}',1,7)
  257. LexToken(COMMA,',',1,15)
  258. LexToken(TD,'td',1,16)
  259. LexToken(LPAREN,'(',1,18)
  260. LexToken(NAME,'A',1,19)
  261. LexToken(RPAREN,')',1,20)
  262. LexToken(GREATER_EQUALS,'>=',1,22)
  263. LexToken(INT,4,1,25)
  264. LexToken(T_COMP_OPERATOR,'{contain,&&}',1,27)
  265. LexToken(TD,'td',1,40)
  266. LexToken(LPAREN,'(',1,42)
  267. LexToken(NAME,'B',1,43)
  268. LexToken(RPAREN,')',1,44)
  269. LexToken(CEQUALS,'==',1,46)
  270. LexToken(INT,2,1,49)
  271. LexToken(COMMA,',',1,50)
  272. LexToken(NAME,'C',1,52)
  273. LexToken(T_SELECT,':',1,54)
  274. LexToken(NAME,'D',1,56)
  275. LexToken(RPAREN,')',1,57)
  276. >>> expression = 'F = if({equals},A {equal,#}, B, C : D)'
  277. >>> p.test(expression)
  278. F = if({equals},A {equal,#}, B, C : D)
  279. LexToken(NAME,'F',1,0)
  280. LexToken(EQUALS,'=',1,2)
  281. LexToken(IF,'if',1,4)
  282. LexToken(LPAREN,'(',1,6)
  283. LexToken(T_REL_OPERATOR,'{equals}',1,7)
  284. LexToken(COMMA,',',1,15)
  285. LexToken(NAME,'A',1,16)
  286. LexToken(T_HASH_OPERATOR,'{equal,#}',1,18)
  287. LexToken(COMMA,',',1,27)
  288. LexToken(NAME,'B',1,29)
  289. LexToken(COMMA,',',1,30)
  290. LexToken(NAME,'C',1,32)
  291. LexToken(T_SELECT,':',1,34)
  292. LexToken(NAME,'D',1,36)
  293. LexToken(RPAREN,')',1,37)
  294. >>> p = tgis.TemporalAlgebraParser()
  295. >>> p.run = False
  296. >>> p.debug = True
  297. >>> expression = "D = A : (B !: C)"
  298. >>> p.parse(expression)
  299. B* = B !: C
  300. A* = A : B*
  301. D = A*
  302. >>> expression = "D = A {!:} B {during,:} C"
  303. >>> print(expression)
  304. D = A {!:} B {during,:} C
  305. >>> p.parse(expression)
  306. A* = A {!:} B
  307. A** = A* {during,:} C
  308. D = A**
  309. >>> expression = "D = A {:} B {during,!:} C"
  310. >>> print(expression)
  311. D = A {:} B {during,!:} C
  312. >>> p.parse(expression)
  313. A* = A {:} B
  314. A** = A* {during,!:} C
  315. D = A**
  316. >>> expression = "D = A {:} (B {during,!:} (C : E))"
  317. >>> print(expression)
  318. D = A {:} (B {during,!:} (C : E))
  319. >>> p.parse(expression)
  320. C* = C : E
  321. B* = B {during,!:} C*
  322. A* = A {:} B*
  323. D = A*
  324. >>> p.run = False
  325. >>> p.debug = False
  326. >>> expression = "C = test1 : test2"
  327. >>> print(expression)
  328. C = test1 : test2
  329. >>> p.parse(expression, 'stvds')
  330. >>> expression = 'D = buff_t(test1,"10 months")'
  331. >>> print(expression)
  332. D = buff_t(test1,"10 months")
  333. >>> p.parse(expression, 'stvds')
  334. >>> expression = 'E = test2 {during,:} buff_t(test1,"1 days")'
  335. >>> print(expression)
  336. E = test2 {during,:} buff_t(test1,"1 days")
  337. >>> p.parse(expression, 'stvds')
  338. >>> expression = 'F = test2 {equal,:} buff_t(test1,"1 days")'
  339. >>> print(expression)
  340. F = test2 {equal,:} buff_t(test1,"1 days")
  341. >>> p.parse(expression, 'stvds')
  342. >>> p.debug = True
  343. >>> expression = 'H = tsnap(test2 {during,:} buff_t(test1, "1 days"))'
  344. >>> p.parse(expression, 'stvds')
  345. test1* = buff_t( test1 , " 1 days " )
  346. test2* = test2 {during,:} test1*
  347. test2** = tsnap( test2* )
  348. H = test2**
  349. >>> expression = 'H = tshift(test2 {during,:} test1, "1 days")'
  350. >>> p.parse(expression, 'stvds')
  351. test2* = test2 {during,:} test1
  352. test2** = tshift( test2* , " 1 days " )
  353. H = test2**
  354. >>> expression = 'H = tshift(H, 3)'
  355. >>> p.parse(expression, 'stvds')
  356. H* = tshift( H , 3 )
  357. H = H*
  358. >>> expression = 'C = if(td(A) == 2, A)'
  359. >>> p.parse(expression, 'stvds')
  360. td(A)
  361. td(A) == 2
  362. A* = if condition True then A
  363. C = A*
  364. >>> expression = 'C = if(td(A) == 5, A, B)'
  365. >>> p.parse(expression, 'stvds')
  366. td(A)
  367. td(A) == 5
  368. A* = if condition True then A else B
  369. C = A*
  370. >>> expression = 'C = if(td(A) == 5 || start_date() > "2010-01-01", A, B)'
  371. >>> p.parse(expression, 'stvds')
  372. td(A)
  373. td(A) == 5
  374. start_date > "2010-01-01"
  375. True || True
  376. A* = if condition True then A else B
  377. C = A*
  378. @endcode
  379. """
  380. try:
  381. import ply.lex as lex
  382. import ply.yacc as yacc
  383. except:
  384. pass
  385. import os
  386. import grass.script as grass
  387. from space_time_datasets import *
  388. from factory import *
  389. from open_stds import *
  390. import copy
  391. ##############################################################################
  392. class TemporalAlgebraLexer(object):
  393. """!Lexical analyzer for the GRASS GIS temporal algebra"""
  394. # Functions that defines an if condition, temporal buffering and snapping
  395. conditional_functions = {
  396. 'if' : 'IF',
  397. 'buff_t': 'BUFF_T',
  398. 'tsnap' : 'TSNAP',
  399. 'tshift' : 'TSHIFT',
  400. }
  401. # Variables with date and time strings
  402. datetime_functions = {
  403. 'start_time' : 'START_TIME', # start time as HH::MM:SS
  404. 'start_date' : 'START_DATE', # start date as yyyy-mm-DD
  405. 'start_datetime' : 'START_DATETIME', # start datetime as yyyy-mm-DD HH:MM:SS
  406. 'end_time' : 'END_TIME', # end time as HH:MM:SS
  407. 'end_date' : 'END_DATE', # end date as yyyy-mm-DD
  408. 'end_datetime' : 'END_DATETIME', # end datetime as yyyy-mm-DD HH:MM:SS
  409. }
  410. # Time functions
  411. time_functions = {
  412. 'td' : 'TD', # The size of the current
  413. # sample time interval in days and
  414. # fraction of days for absolute time,
  415. # and in relative units in case of relative time.
  416. #'start_td' : 'START_TD', # The time difference between the start
  417. # time of the sample space time raster
  418. # dataset and the start time of the
  419. # current sample interval or instance.
  420. # The time is measured in days and
  421. # fraction of days for absolute time,
  422. # and in relative units in case of relative time.
  423. #'end_td' : 'END_TD', # The time difference between the
  424. # start time of the sample
  425. # space time raster dataset and the
  426. # end time of the current sample interval.
  427. # The time is measured in days and
  428. # fraction of days for absolute time,
  429. # and in relative units in case of relative time.
  430. # The end_time() will be represented by null() in case of a time instance.
  431. 'start_doy' : 'START_DOY', # Day of year (doy) from the start time [1 - 366]
  432. 'start_dow' : 'START_DOW', # Day of week (dow) from the start time [1 - 7], the start of the week is Monday == 1
  433. 'start_year' : 'START_YEAR', # The year of the start time [0 - 9999]
  434. 'start_month' : 'START_MONTH', # The month of the start time [1 - 12]
  435. 'start_week' : 'START_WEEK', # Week of year of the start time [1 - 54]
  436. 'start_day' : 'START_DAY', # Day of month from the start time [1 - 31]
  437. 'start_hour' : 'START_HOUR', # The hour of the start time [0 - 23]
  438. 'start_minute': 'START_MINUTE', # The minute of the start time [0 - 59]
  439. 'start_second': 'START_SECOND', # The second of the start time [0 - 59]
  440. 'end_doy' : 'END_DOY', # Day of year (doy) from the end time [1 - 366]
  441. 'end_dow' : 'END_DOW', # Day of week (dow) from the end time [1 - 7], the start of the week is Monday == 1
  442. 'end_year' : 'END_YEAR', # The year of the end time [0 - 9999]
  443. 'end_month' : 'END_MONTH', # The month of the end time [1 - 12]
  444. 'end_week' : 'END_WEEK', # Week of year of the end time [1 - 54]
  445. 'end_day' : 'END_DAY', # Day of month from the start time [1 - 31]
  446. 'end_hour' : 'END_HOUR', # The hour of the end time [0 - 23]
  447. 'end_minute' : 'END_MINUTE', # The minute of the end time [0 - 59]
  448. 'end_second' : 'END_SECOND', # The second of the end time [0 - 59]
  449. }
  450. # This is the list of token names.
  451. tokens = (
  452. 'DATETIME',
  453. 'TIME',
  454. 'DATE',
  455. 'INT',
  456. 'FLOAT',
  457. 'LPAREN',
  458. 'RPAREN',
  459. 'COMMA',
  460. 'CEQUALS',
  461. 'EQUALS',
  462. 'UNEQUALS',
  463. 'LOWER',
  464. 'LOWER_EQUALS',
  465. 'GREATER',
  466. 'GREATER_EQUALS',
  467. 'HASH',
  468. 'OR',
  469. 'AND',
  470. 'T_SELECT_OPERATOR',
  471. 'T_HASH_OPERATOR',
  472. 'T_COMP_OPERATOR',
  473. 'T_REL_OPERATOR',
  474. 'T_SELECT',
  475. 'T_NOT_SELECT',
  476. 'NAME',
  477. 'QUOTE',
  478. )
  479. # Build the token list
  480. tokens = tokens + tuple(datetime_functions.values()) \
  481. + tuple(time_functions.values()) \
  482. + tuple(conditional_functions.values())
  483. # Regular expression rules for simple tokens
  484. t_T_SELECT_OPERATOR = r'\{([a-zA-Z\| ]+[,])?([\|&+=]?[!]?[:])\}'
  485. t_T_HASH_OPERATOR = r'\{([a-zA-Z\| ]+[,])?[#]\}'
  486. t_T_COMP_OPERATOR = r'\{([a-zA-Z\| ]+[,])?(\|\||&&)\}'
  487. t_T_REL_OPERATOR = r'\{([a-zA-Z\| ])+\}'
  488. t_T_SELECT = r':'
  489. t_T_NOT_SELECT = r'!:'
  490. t_LPAREN = r'\('
  491. t_RPAREN = r'\)'
  492. t_COMMA = r','
  493. t_CEQUALS = r'=='
  494. t_EQUALS = r'='
  495. t_UNEQUALS = r'!='
  496. t_LOWER = r'<'
  497. t_LOWER_EQUALS = r'<='
  498. t_GREATER = r'>'
  499. t_GREATER_EQUALS = r'>='
  500. t_HASH = r'\#'
  501. t_OR = r'[\|]'
  502. t_AND = r'[&]'
  503. t_QUOTE = r'[\"\']'
  504. # These are the things that should be ignored.
  505. t_ignore = ' \t'
  506. # Read time string and convert it into a date object
  507. def t_DATETIME(self, t):
  508. r'"\d\d\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[ T](0[0-9]|1(0-9)|2[0-4]):(0[0-9]|[1-5][0-9]|60):(0[0-9]|[1-5][0-9]|60)"'
  509. # t.value = int(t.value)
  510. return t
  511. # Read date string and convert it into a date object
  512. def t_DATE(self, t):
  513. r'"\d\d\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])"'
  514. # t.value = int(t.value)
  515. return t
  516. # Read time string and convert it into a date object
  517. def t_TIME(self, t):
  518. r'"(0[0-9]|1[0-9]|2[0-4]):(0[0-9]|[1-5][0-9]|60):(0[0-9]|[1-5][0-9]|60)"'
  519. # t.value = int(t.value)
  520. return t
  521. # Read in a float. This rule has to be done before the int rule.
  522. def t_FLOAT(self, t):
  523. r'-?\d+\.\d*(e-?\d+)?'
  524. t.value = float(t.value)
  525. return t
  526. # Read in an int.
  527. def t_INT(self, t):
  528. r'-?\d+'
  529. t.value = int(t.value)
  530. return t
  531. # Read in a list of maps.
  532. def t_LIST(self, t):
  533. r'[\[][.]*[\]]'
  534. t.value = list(t.value)
  535. return t
  536. # Ignore comments.
  537. # def t_comment(self, t):
  538. # r'^[#][^\n]*'
  539. # pass
  540. # Track line numbers.
  541. def t_newline(self, t):
  542. r'\n+'
  543. t.lineno += len(t.value)
  544. def t_NAME(self, t):
  545. r'[a-zA-Z_][a-zA-Z_0-9]*'
  546. self.temporal_symbol(t)
  547. return t
  548. # Parse symbols
  549. def temporal_symbol(self, t):
  550. # Check for reserved words
  551. if t.value in TemporalAlgebraLexer.time_functions.keys():
  552. t.type = TemporalAlgebraLexer.time_functions.get(t.value)
  553. elif t.value in TemporalAlgebraLexer.datetime_functions.keys():
  554. t.type = TemporalAlgebraLexer.datetime_functions.get(t.value)
  555. elif t.value in TemporalAlgebraLexer.conditional_functions.keys():
  556. t.type = TemporalAlgebraLexer.conditional_functions.get(t.value)
  557. else:
  558. t.type = 'NAME'
  559. return t
  560. # Handle errors.
  561. def t_error(self, t):
  562. raise SyntaxError("syntax error on line %d near '%s'" %
  563. (t.lineno, t.value))
  564. # Build the lexer
  565. def build(self,**kwargs):
  566. self.lexer = lex.lex(module=self, optimize=False, debug=False, **kwargs)
  567. # Just for testing
  568. def test(self,data):
  569. self.name_list = {}
  570. print(data)
  571. self.lexer.input(data)
  572. while True:
  573. tok = self.lexer.token()
  574. if not tok: break
  575. print tok
  576. ###############################################################################
  577. class GlobalTemporalVar(object):
  578. """ This class handles global temporal variable conditional expressions,
  579. like start_doy() == 3.
  580. The three parts of the statement are stored separately in
  581. tfunc (START_DOY), compop (==) and value (3).
  582. But also boolean values, time differences and relation operators for comparison in
  583. if-statements can be stored in this class.
  584. """
  585. def __init__(self):
  586. self.tfunc = None
  587. self.compop = None
  588. self.value = None
  589. self.boolean = None
  590. self.relationop = None
  591. self.topology = []
  592. self.td = None
  593. def get_type(self):
  594. if self.tfunc != None and self.compop != None and self.value != None:
  595. return("global")
  596. elif self.boolean != None:
  597. return("boolean")
  598. elif self.relationop != None and self.topology != []:
  599. return("operator")
  600. elif self.td != None:
  601. return("timediff")
  602. def get_type_value(self):
  603. typename = self.get_type()
  604. valuelist = []
  605. if typename == "global":
  606. valuelist = [self.tfunc, self.compop, self.value]
  607. elif typename == "operator":
  608. valuelist.append(self.topology)
  609. valuelist.append(self.relationop)
  610. elif typename == "boolean":
  611. valuelist = self.boolean
  612. elif typename == "timediff":
  613. valuelist.append(self.td)
  614. return(valuelist)
  615. def __str__(self):
  616. return str(self.tfunc) + str(self.compop) + str(self.value)
  617. ###############################################################################
  618. class TemporalAlgebraParser(object):
  619. """The temporal algebra class"""
  620. # Get the tokens from the lexer class
  621. tokens = TemporalAlgebraLexer.tokens
  622. # Setting equal precedence level for select and hash operations.
  623. precedence = (
  624. ('left', 'T_SELECT_OPERATOR', 'T_SELECT', 'T_NOT_SELECT'), # 1
  625. ('left', 'AND', 'OR', 'T_COMP_OPERATOR'), #2
  626. )
  627. def __init__(self, pid=None, run = True, debug = False, spatial = False, null = False):
  628. self.run = run
  629. self.debug = debug
  630. self.pid = pid
  631. # Intermediate vector map names
  632. self.names = {}
  633. # Count map names
  634. self.spatial = spatial
  635. self.null = null
  636. self.mapset = get_current_mapset()
  637. self.temporaltype = None
  638. self.msgr = get_tgis_message_interface()
  639. self.dbif = SQLDatabaseInterfaceConnection()
  640. self.dbif.connect()
  641. def __del__(self):
  642. if self.dbif.connected:
  643. self.dbif.close()
  644. def parse(self, expression, stdstype = 'strds', basename = None, overwrite=False):
  645. self.lexer = TemporalAlgebraLexer()
  646. self.lexer.build()
  647. self.parser = yacc.yacc(module=self, debug=self.debug)
  648. self.overwrite = overwrite
  649. self.count = 0
  650. self.stdstype = stdstype
  651. self.basename = basename
  652. self.expression = expression
  653. self.parser.parse(expression)
  654. def generate_map_name(self):
  655. """!Generate an unique intermediate vector map name
  656. and register it in the objects map list for later removement.
  657. The vector map names are unique between processes. Do not use the
  658. same object for map name generation in multiple threads.
  659. """
  660. self.count += 1
  661. if self.pid != None:
  662. pid = self.pid
  663. else:
  664. pid = os.getpid()
  665. name = "tmp_map_name_%i_%i"%(pid, self.count)
  666. self.names[name] = name
  667. return name
  668. def generate_new_map(self, base_map, bool_op = 'and', copy = True):
  669. """!Generate a new map using the spatio-temporal extent of the base map
  670. @param base_map This map is used to create the new map
  671. """
  672. # Generate an intermediate name for the result map list.
  673. name = self.generate_map_name()
  674. # Check for mapset in given stds input.
  675. mapname = name + "@" + self.mapset
  676. # Create new map based on the related map list.
  677. map_new = base_map.get_new_instance(mapname)
  678. # Set initial map extend of new vector map.
  679. self.overlay_map_extent(map_new, base_map, bool_op = bool_op, copy = copy)
  680. return map_new
  681. def overlay_map_extent(self, mapA, mapB, bool_op = None, temp_op = '=',
  682. copy = False):
  683. """!Compute the spatio-temporal extent of two topological related maps
  684. @param mapA The first map
  685. @param mapB The second maps
  686. @param bool_op The boolean operator specifying the spatial extent
  687. operation (intersection, union, disjoint union)
  688. @param temp_op The temporal operator specifying the temporal
  689. extent operation (intersection, union, disjoint union)
  690. @param copy Specifies if the temporal extent of mapB should be
  691. copied to mapA
  692. @return 0 if there is no overlay
  693. """
  694. returncode = 1
  695. if copy:
  696. map_extent_temporal = mapB.get_temporal_extent()
  697. map_extent_spatial = mapB.get_spatial_extent()
  698. # Set initial map extend of new vector map.
  699. mapA.set_spatial_extent(map_extent_spatial)
  700. mapA.set_temporal_extent(map_extent_temporal)
  701. if "cmd_list" in dir(mapB):
  702. mapA.cmd_list = mapB.cmd_list
  703. else:
  704. # Calculate spatial extent for different overlay operations.
  705. if bool_op == 'and':
  706. overlay_ext = mapA.spatial_intersection(mapB)
  707. if overlay_ext != None:
  708. mapA.set_spatial_extent(overlay_ext)
  709. else:
  710. returncode = 0
  711. elif bool_op in ['or', 'xor']:
  712. overlay_ext = mapA.spatial_union(mapB)
  713. if overlay_ext != None:
  714. mapA.set_spatial_extent(overlay_ext)
  715. else:
  716. returncode = 0
  717. elif bool_op == 'disor':
  718. overlay_ext = mapA.spatial_disjoint_union(mapB)
  719. if overlay_ext != None:
  720. mapA.set_spatial_extent(overlay_ext)
  721. else:
  722. returncode = 0
  723. # Calculate temporal extent for different temporal operators.
  724. if temp_op == '&':
  725. temp_ext = mapA.temporal_intersection(mapB)
  726. if temp_ext != None:
  727. mapA.set_temporal_extent(temp_ext)
  728. else:
  729. returncode = 0
  730. elif temp_op == '|':
  731. temp_ext = mapA.temporal_union(mapB)
  732. if temp_ext != None:
  733. mapA.set_temporal_extent(temp_ext)
  734. else:
  735. returncode = 0
  736. elif temp_op == '+':
  737. temp_ext = mapA.temporal_disjoint_union(mapB)
  738. if temp_ext != None:
  739. mapA.set_temporal_extent(temp_ext)
  740. else:
  741. returncode = 0
  742. return(returncode)
  743. ######################### Temporal functions ##############################
  744. def check_stds(self, input, clear = False):
  745. """! Check if input space time dataset exist in database and return its map list.
  746. @param input Name of space time data set as string or list of maps.
  747. @param clear Reset the stored conditional values to empty list.
  748. @return List of maps.
  749. """
  750. if not isinstance(input, list):
  751. # Get mapset input.
  752. mapset = get_current_mapset()
  753. # Check for mapset in given stds input.
  754. if input.find("@") >= 0:
  755. id_input = input
  756. else:
  757. id_input = input + "@" + mapset
  758. # Create empty spacetime dataset.
  759. stds = dataset_factory(self.stdstype, id_input)
  760. # Check for occurence of space time dataset.
  761. if stds.is_in_db(dbif=self.dbif) == False:
  762. self.msgr.fatal(_("Space time %s dataset <%s> not found") %
  763. (stds.get_new_map_instance(None).get_type(), id_input))
  764. else:
  765. # Select temporal dataset entry from database.
  766. stds.select(dbif=self.dbif)
  767. maplist = stds.get_registered_maps_as_objects(dbif=self.dbif)
  768. # Create map_value as empty list item.
  769. for map_i in maplist:
  770. if "map_value" not in dir(map_i):
  771. map_i.map_value = []
  772. if "condition_value" not in dir(map_i):
  773. map_i.condition_value = []
  774. # Set and check global temporal type variable and map.
  775. if map_i.is_time_absolute() and self.temporaltype == None:
  776. self.temporaltype = 'absolute'
  777. elif map_i.is_time_relative() and self.temporaltype == None:
  778. self.temporaltype = 'relative'
  779. elif map_i.is_time_absolute() and self.temporaltype == 'relative':
  780. self.msgr.fatal(_("Wrong temporal type of space time dataset <%s> \
  781. <%s> time is required") %
  782. (id_input, self.temporaltype))
  783. elif map_i.is_time_relative() and self.temporaltype == 'absolute':
  784. self.msgr.fatal(_("Wrong temporal type of space time dataset <%s> \
  785. <%s> time is required") %
  786. (id_input, self.temporaltype))
  787. else:
  788. maplist = input
  789. # Create map_value as empty list item.
  790. for map_i in maplist:
  791. if "map_value" not in dir(map_i):
  792. map_i.map_value = []
  793. elif clear:
  794. map_i.map_value = []
  795. if "condition_value" not in dir(map_i):
  796. map_i.condition_value = []
  797. elif clear:
  798. map_i.condition_value = []
  799. return(maplist)
  800. def get_temporal_topo_list(self, maplistA, maplistB = None, topolist = ["EQUAL"],
  801. assign_val = False, count_map = False):
  802. """!Build temporal topology for two space time data sets, copy map objects
  803. for given relation into map list.
  804. @param maplistA List of maps.
  805. @param maplistB List of maps.
  806. @param topolist List of strings of temporal relations.
  807. @param assign_val Boolean for assigning a boolean map value based on
  808. the map_values from the compared map list by
  809. topological relationships.
  810. @param count_map Boolean if the number of topological related maps
  811. should be returned.
  812. @return List of maps from maplistA that fulfil the topological relationships
  813. to maplistB specified in topolist.
  814. @code
  815. # Example with two lists of maps
  816. >>> import grass.temporal as tgis
  817. >>> tgis.init(True)
  818. >>> l = tgis.TemporalAlgebraParser()
  819. >>> # Create two list of maps with equal time stamps
  820. >>> mapsA = []
  821. >>> mapsB = []
  822. >>> for i in range(10):
  823. ... idA = "a%i@B"%(i)
  824. ... mapA = tgis.RasterDataset(idA)
  825. ... idB = "b%i@B"%(i)
  826. ... mapB = tgis.RasterDataset(idB)
  827. ... check = mapA.set_relative_time(i, i + 1, "months")
  828. ... check = mapB.set_relative_time(i, i + 1, "months")
  829. ... mapsA.append(mapA)
  830. ... mapsB.append(mapB)
  831. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['EQUAL'])
  832. >>> for map in resultlist:
  833. ... if map.get_equal():
  834. ... relations = map.get_equal()
  835. ... print "Map %s has equal relation to map %s"%(map.get_name(),
  836. ... relations[0].get_name())
  837. Map a0 has equal relation to map b0
  838. Map a1 has equal relation to map b1
  839. Map a2 has equal relation to map b2
  840. Map a3 has equal relation to map b3
  841. Map a4 has equal relation to map b4
  842. Map a5 has equal relation to map b5
  843. Map a6 has equal relation to map b6
  844. Map a7 has equal relation to map b7
  845. Map a8 has equal relation to map b8
  846. Map a9 has equal relation to map b9
  847. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['DURING'])
  848. >>> print(resultlist)
  849. []
  850. >>> # Create two list of maps with equal time stamps
  851. >>> mapsA = []
  852. >>> mapsB = []
  853. >>> for i in range(10):
  854. ... idA = "a%i@B"%(i)
  855. ... mapA = tgis.RasterDataset(idA)
  856. ... idB = "b%i@B"%(i)
  857. ... mapB = tgis.RasterDataset(idB)
  858. ... check = mapA.set_relative_time(i, i + 1, "months")
  859. ... check = mapB.set_relative_time(i, i + 2, "months")
  860. ... mapsA.append(mapA)
  861. ... mapsB.append(mapB)
  862. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['starts','during'])
  863. >>> for map in resultlist:
  864. ... if map.get_starts():
  865. ... relations = map.get_starts()
  866. ... print "Map %s has start relation to map %s"%(map.get_name(),
  867. ... relations[0].get_name())
  868. Map a0 has start relation to map b0
  869. Map a1 has start relation to map b1
  870. Map a2 has start relation to map b2
  871. Map a3 has start relation to map b3
  872. Map a4 has start relation to map b4
  873. Map a5 has start relation to map b5
  874. Map a6 has start relation to map b6
  875. Map a7 has start relation to map b7
  876. Map a8 has start relation to map b8
  877. Map a9 has start relation to map b9
  878. >>> for map in resultlist:
  879. ... if map.get_during():
  880. ... relations = map.get_during()
  881. ... print "Map %s has during relation to map %s"%(map.get_name(),
  882. ... relations[0].get_name())
  883. Map a0 has during relation to map b0
  884. Map a1 has during relation to map b0
  885. Map a2 has during relation to map b1
  886. Map a3 has during relation to map b2
  887. Map a4 has during relation to map b3
  888. Map a5 has during relation to map b4
  889. Map a6 has during relation to map b5
  890. Map a7 has during relation to map b6
  891. Map a8 has during relation to map b7
  892. Map a9 has during relation to map b8
  893. >>> # Create two list of maps with equal time stamps and map_value method.
  894. >>> mapsA = []
  895. >>> mapsB = []
  896. >>> for i in range(10):
  897. ... idA = "a%i@B"%(i)
  898. ... mapA = tgis.RasterDataset(idA)
  899. ... idB = "b%i@B"%(i)
  900. ... mapB = tgis.RasterDataset(idB)
  901. ... check = mapA.set_relative_time(i, i + 1, "months")
  902. ... check = mapB.set_relative_time(i, i + 1, "months")
  903. ... mapB.map_value = True
  904. ... mapsA.append(mapA)
  905. ... mapsB.append(mapB)
  906. >>> # Create two list of maps with equal time stamps
  907. >>> mapsA = []
  908. >>> mapsB = []
  909. >>> for i in range(10):
  910. ... idA = "a%i@B"%(i)
  911. ... mapA = tgis.RasterDataset(idA)
  912. ... mapA.map_value = True
  913. ... idB = "b%i@B"%(i)
  914. ... mapB = tgis.RasterDataset(idB)
  915. ... mapB.map_value = False
  916. ... check = mapA.set_absolute_time(datetime(2000,1,i+1),
  917. ... datetime(2000,1,i + 2))
  918. ... check = mapB.set_absolute_time(datetime(2000,1,i+6),
  919. ... datetime(2000,1,i + 7))
  920. ... mapsA.append(mapA)
  921. ... mapsB.append(mapB)
  922. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB)
  923. >>> for map in resultlist:
  924. ... print(map.get_id())
  925. a5@B
  926. a6@B
  927. a7@B
  928. a8@B
  929. a9@B
  930. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['during'])
  931. >>> for map in resultlist:
  932. ... print(map.get_id())
  933. @endcode
  934. """
  935. topologylist = ["EQUAL", "FOLLOWS", "PRECEDES", "OVERLAPS", "OVERLAPPED", \
  936. "DURING", "STARTS", "FINISHES", "CONTAINS", "STARTED", \
  937. "FINISHED"]
  938. complementdict = {"EQUAL": "EQUAL", "FOLLOWS" : "PRECEDES",
  939. "PRECEDES" : "FOLLOWS", "OVERLAPS" : "OVERLAPPED",
  940. "OVERLAPPED" : "OVERLAPS", "DURING" : "CONTAINS",
  941. "CONTAINS" : "DURING", "STARTS" : "STARTED",
  942. "STARTED" : "STARTS", "FINISHES" : "FINISHED",
  943. "FINISHED" : "FINISHES"}
  944. resultdict = {}
  945. # Check if given temporal relation are valid.
  946. for topo in topolist:
  947. if topo.upper() not in topologylist:
  948. raise SyntaxError("Unpermitted temporal relation name '" + topo + "'")
  949. # Create temporal topology for maplistA to maplistB.
  950. tb = SpatioTemporalTopologyBuilder()
  951. # Dictionary with different spatial variables used for topology builder.
  952. spatialdict = {'strds' : '2D', 'stvds' : '2D', 'str3ds' : '3D'}
  953. # Build spatial temporal topology
  954. if self.spatial:
  955. tb.build(maplistA, maplistB, spatial = spatialdict[self.stdstype])
  956. else:
  957. tb.build(maplistA, maplistB)
  958. # Iterate through maps in maplistA and search for relationships given
  959. # in topolist.
  960. # TODO: Better implementation with less nesting
  961. for map_i in maplistA:
  962. tbrelations = map_i.get_temporal_relations()
  963. for topo in topolist:
  964. if topo.upper() in tbrelations.keys():
  965. if assign_val:
  966. mapvaluelist = []
  967. if complementdict[topo.upper()] in tbrelations:
  968. relationmaplist = tbrelations[complementdict[topo.upper()]]
  969. for relationmap in relationmaplist:
  970. if "map_value" in dir(relationmap):
  971. for element in relationmap.map_value:
  972. if isinstance(element, GlobalTemporalVar):
  973. if element.get_type() == "boolean":
  974. mapvaluelist.append(element.boolean)
  975. if all(mapvaluelist):
  976. resultbool = True
  977. else:
  978. resultbool = False
  979. if "condition_value" in dir(map_i):
  980. if isinstance(map_i.condition_value, list):
  981. map_i.condition_value.append(resultbool)
  982. if count_map:
  983. relationmaplist = tbrelations[topo.upper()]
  984. gvar = GlobalTemporalVar()
  985. gvar.td = len(relationmaplist)
  986. if "map_value" in dir(map_i):
  987. map_i.map_value.append(gvar)
  988. else:
  989. map_i.map_value = gvar
  990. resultdict[map_i.get_id()] = map_i
  991. resultlist = resultdict.values()
  992. # Sort list of maps chronological.
  993. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  994. return(resultlist)
  995. def eval_toperator(self, operator):
  996. """!This function evaluates a string containing temporal operations.
  997. @param operator String of temporal operations, e.g. {equal|during,=!:}.
  998. @return List of temporal relations (equal, during), the given function
  999. (!:) and the interval/instances (=).
  1000. @code
  1001. >>> import grass.temporal as tgis
  1002. >>> tgis.init()
  1003. >>> p = tgis.TemporalAlgebraParser()
  1004. >>> operator = "{equal,:}"
  1005. >>> p.eval_toperator(operator)
  1006. (['equal'], '=', ':')
  1007. >>> operator = "{equal|during,:}"
  1008. >>> p.eval_toperator(operator)
  1009. (['equal', 'during'], '=', ':')
  1010. >>> operator = "{equal,!:}"
  1011. >>> p.eval_toperator(operator)
  1012. (['equal'], '=', '!:')
  1013. >>> operator = "{equal|during,!:}"
  1014. >>> p.eval_toperator(operator)
  1015. (['equal', 'during'], '=', '!:')
  1016. >>> operator = "{equal|during,=!:}"
  1017. >>> p.eval_toperator(operator)
  1018. (['equal', 'during'], '=', '!:')
  1019. >>> operator = "{equal|during|starts,#}"
  1020. >>> p.eval_toperator(operator)
  1021. (['equal', 'during', 'starts'], '=', '#')
  1022. >>> operator = "{!:}"
  1023. >>> p.eval_toperator(operator)
  1024. (['equal'], '=', '!:')
  1025. >>> operator = "{=:}"
  1026. >>> p.eval_toperator(operator)
  1027. (['equal'], '=', ':')
  1028. >>> operator = "{#}"
  1029. >>> p.eval_toperator(operator)
  1030. (['equal'], '=', '#')
  1031. >>> operator = "{equal|during}"
  1032. >>> p.eval_toperator(operator)
  1033. (['equal', 'during'], '=', '')
  1034. >>> operator = "{equal}"
  1035. >>> p.eval_toperator(operator)
  1036. (['equal'], '=', '')
  1037. >>> operator = "{equal,||}"
  1038. >>> p.eval_toperator(operator)
  1039. (['equal'], '=', '||')
  1040. >>> operator = "{equal|during,&&}"
  1041. >>> p.eval_toperator(operator)
  1042. (['equal', 'during'], '=', '&&')
  1043. @endcode
  1044. """
  1045. topologylist = ["EQUAL", "FOLLOWS", "PRECEDES", "OVERLAPS", "OVERLAPPED", \
  1046. "DURING", "STARTS", "FINISHES", "CONTAINS", "STARTED", \
  1047. "FINISHED"]
  1048. functionlist = [":", "!:", "#"]
  1049. intervallist = ["=", "|", "&", "+"]
  1050. comparelist = ["||", "&&"]
  1051. relations = []
  1052. interval = '='
  1053. function = ''
  1054. op = operator.strip('{}')
  1055. oplist = op.split(',')
  1056. if len(oplist) > 1:
  1057. relationlist = oplist[0].split('|')
  1058. for relation in relationlist:
  1059. if relation.upper() in topologylist and relation not in relations:
  1060. relations.append(relation)
  1061. else:
  1062. raise SyntaxError("invalid syntax")
  1063. opright = oplist[1]
  1064. if opright in comparelist:
  1065. function = opright
  1066. elif opright[0] in intervallist:
  1067. interval = opright[0]
  1068. if opright[1:] in functionlist:
  1069. function = opright[1:]
  1070. else:
  1071. raise SyntaxError("invalid syntax")
  1072. elif opright in functionlist:
  1073. function = opright
  1074. else:
  1075. raise SyntaxError("invalid syntax")
  1076. elif all([rel.upper() in topologylist for rel in oplist[0].split('|')]):
  1077. relations = oplist[0].split('|')
  1078. else:
  1079. relations = ['equal']
  1080. opstr = str(oplist[0])
  1081. if opstr[0] in intervallist:
  1082. interval = opstr[0]
  1083. if opstr[1:] in functionlist:
  1084. function = opstr[1:]
  1085. else:
  1086. raise SyntaxError("invalid syntax")
  1087. elif opstr in functionlist:
  1088. function = opstr
  1089. #else:
  1090. #raise SyntaxError("invalid syntax")
  1091. return(relations, interval, function)
  1092. def perform_temporal_selection(self, maplistA, maplistB, topolist = ["EQUAL"],
  1093. inverse = False, assign_val = False):
  1094. """!This function performs temporal selection operation.
  1095. @param maplistA List of maps representing the left side of a temporal
  1096. expression.
  1097. @param maplistB List of maps representing the right side of a temporal
  1098. expression.
  1099. @param topolist List of strings of temporal relations.
  1100. @param inverse Boolean value that specifies if the selection should be
  1101. inverted.
  1102. @param assign_val Boolean for assigning a boolean map value based on
  1103. the map_values from the compared map list by
  1104. topological relationships.
  1105. @return List of selected maps from maplistA.
  1106. @code
  1107. >>> import grass.temporal as tgis
  1108. >>> tgis.init()
  1109. >>> l = tgis.TemporalAlgebraParser()
  1110. >>> # Example with two lists of maps
  1111. >>> # Create two list of maps with equal time stamps
  1112. >>> mapsA = []
  1113. >>> mapsB = []
  1114. >>> for i in range(10):
  1115. ... idA = "a%i@B"%(i)
  1116. ... mapA = tgis.RasterDataset(idA)
  1117. ... idB = "b%i@B"%(i)
  1118. ... mapB = tgis.RasterDataset(idB)
  1119. ... check = mapA.set_relative_time(i, i + 1, "months")
  1120. ... check = mapB.set_relative_time(i + 5, i + 6, "months")
  1121. ... mapsA.append(mapA)
  1122. ... mapsB.append(mapB)
  1123. >>> resultlist = l.perform_temporal_selection(mapsA, mapsB, ['EQUAL'],
  1124. ... False)
  1125. >>> for map in resultlist:
  1126. ... if map.get_equal():
  1127. ... relations = map.get_equal()
  1128. ... print "Map %s has equal relation to map %s"%(map.get_name(),
  1129. ... relations[0].get_name())
  1130. Map a5 has equal relation to map b0
  1131. Map a6 has equal relation to map b1
  1132. Map a7 has equal relation to map b2
  1133. Map a8 has equal relation to map b3
  1134. Map a9 has equal relation to map b4
  1135. >>> resultlist = l.perform_temporal_selection(mapsA, mapsB, ['EQUAL'],
  1136. ... True)
  1137. >>> for map in resultlist:
  1138. ... if not map.get_equal():
  1139. ... print "Map %s has no equal relation to mapset mapsB"%(map.get_name())
  1140. Map a0 has no equal relation to mapset mapsB
  1141. Map a1 has no equal relation to mapset mapsB
  1142. Map a2 has no equal relation to mapset mapsB
  1143. Map a3 has no equal relation to mapset mapsB
  1144. Map a4 has no equal relation to mapset mapsB
  1145. @endcode
  1146. """
  1147. if not inverse:
  1148. topolist = self.get_temporal_topo_list(maplistA, maplistB, topolist,
  1149. assign_val = assign_val)
  1150. resultlist = topolist
  1151. else:
  1152. topolist = self.get_temporal_topo_list(maplistA, maplistB, topolist,
  1153. assign_val = False)
  1154. resultlist = []
  1155. for map_i in maplistA:
  1156. if map_i not in topolist:
  1157. resultlist.append(map_i)
  1158. if assign_val:
  1159. if "condition_value" in dir(map_i):
  1160. map_i.condition_value.append(False)
  1161. # Sort list of maps chronological.
  1162. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  1163. return(resultlist)
  1164. def set_granularity(self, maplistA, maplistB, toperator = '=', topolist = ["EQUAL"]):
  1165. """!This function sets the temporal extends of a list of maps based on
  1166. another map list.
  1167. @param maplistB List of maps.
  1168. @param maplistB List of maps.
  1169. @param toperator String containing the temporal operator: =, +, &, |.
  1170. @param topolist List of topological relations.
  1171. @return List of maps with the new temporal extends.
  1172. @code
  1173. >>> import grass.temporal as tgis
  1174. >>> tgis.init()
  1175. >>> p = tgis.TemporalAlgebraParser()
  1176. >>> # Create two list of maps with equal time stamps
  1177. >>> mapsA = []
  1178. >>> mapsB = []
  1179. >>> for i in range(10):
  1180. ... idA = "a%i@B"%(i)
  1181. ... mapA = tgis.RasterDataset(idA)
  1182. ... idB = "b%i@B"%(i)
  1183. ... mapB = tgis.RasterDataset(idB)
  1184. ... check = mapA.set_relative_time(i, i + 1, "months")
  1185. ... check = mapB.set_relative_time(i*2, i*2 + 2, "months")
  1186. ... mapsA.append(mapA)
  1187. ... mapsB.append(mapB)
  1188. >>> resultlist = p.set_granularity(mapsA, mapsB, toperator = "|", topolist = ["during"])
  1189. >>> for map in resultlist:
  1190. ... start,end,unit = map.get_relative_time()
  1191. ... print(map.get_id() + ' - start: ' + str(start) + ' end: ' + str(end))
  1192. a1@B - start: 0 end: 2
  1193. a0@B - start: 0 end: 2
  1194. a3@B - start: 2 end: 4
  1195. a2@B - start: 2 end: 4
  1196. a5@B - start: 4 end: 6
  1197. a4@B - start: 4 end: 6
  1198. a7@B - start: 6 end: 8
  1199. a6@B - start: 6 end: 8
  1200. a9@B - start: 8 end: 10
  1201. a8@B - start: 8 end: 10
  1202. @endcode
  1203. """
  1204. topologylist = ["EQUAL", "FOLLOWS", "PRECEDES", "OVERLAPS", "OVERLAPPED", \
  1205. "DURING", "STARTS", "FINISHES", "CONTAINS", "STARTED", \
  1206. "FINISHED"]
  1207. for topo in topolist:
  1208. if topo.upper() not in topologylist:
  1209. raise SyntaxError("Unpermitted temporal relation name '" + topo + "'")
  1210. # Create temporal topology for maplistA to maplistB.
  1211. tb = SpatioTemporalTopologyBuilder()
  1212. # Dictionary with different spatial variables used for topology builder.
  1213. spatialdict = {'strds' : '2D', 'stvds' : '2D', 'str3ds' : '3D'}
  1214. # Build spatial temporal topology for maplistB to maplistB.
  1215. if self.spatial:
  1216. tb.build(maplistA, maplistB, spatial = spatialdict[self.stdstype])
  1217. else:
  1218. tb.build(maplistA, maplistB)
  1219. resultdict = {}
  1220. # Iterate through maps in maplistA and search for relationships given
  1221. # in topolist.
  1222. for map_i in maplistA:
  1223. tbrelations = map_i.get_temporal_relations()
  1224. for topo in topolist:
  1225. if topo.upper() in tbrelations.keys():
  1226. relationmaplist = tbrelations[topo.upper()]
  1227. for relationmap in relationmaplist:
  1228. newextend = None
  1229. if toperator == "&":
  1230. newextend = map_i.temporal_intersection(relationmap)
  1231. elif toperator == "|":
  1232. newextend = map_i.temporal_union(relationmap)
  1233. elif toperator == "+":
  1234. newextend = map_i.temporal_disjoint_union(relationmap)
  1235. elif toperator == "=":
  1236. resultdict[map_i.get_id()] = map_i
  1237. if newextend != None:
  1238. start = newextend.get_start_time()
  1239. end = newextend.get_end_time()
  1240. #print(map_i.get_id() + ' - start: ' + str(start) + ' end: ' + str(end))
  1241. if map_i.is_time_absolute():
  1242. map_i.set_absolute_time(start, end)
  1243. else:
  1244. relunit = map_i.get_relative_time_unit()
  1245. map_i.set_relative_time(int(start), int(end), relunit)
  1246. resultdict[map_i.get_id()] = map_i
  1247. resultlist = resultdict.values()
  1248. # Sort list of maps chronological.
  1249. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  1250. # Get relations to maplistB per map in A.
  1251. # Loop over all relations from list
  1252. # temporal extent = map.temporal_intersection(map)
  1253. # if temporal extend is None = delete map.
  1254. return(resultlist)
  1255. def get_temporal_func_dict(self, map):
  1256. """! This function creates a dictionary containing temporal functions for a
  1257. map dataset with time stamp.
  1258. @param map Map object with time stamps.
  1259. @return Dictionary with temporal functions for given input map.
  1260. @code
  1261. >>> import grass.temporal as tgis
  1262. >>> import datetime
  1263. >>> tgis.init()
  1264. >>> l = tgis.TemporalAlgebraParser()
  1265. >>> # Example with one list of maps
  1266. >>> # Create one list of maps with equal time stamps
  1267. >>> for i in range(1):
  1268. ... idA = "a%i@B"%(i)
  1269. ... mapA = tgis.RasterDataset(idA)
  1270. ... check = mapA.set_absolute_time(datetime.datetime(2000,1,1),
  1271. ... datetime.datetime(2000,10,1))
  1272. ... tfuncdict = l.get_temporal_func_dict(mapA)
  1273. >>> print(tfuncdict["START_YEAR"])
  1274. 2000
  1275. >>> print(tfuncdict["START_TIME"])
  1276. 00:00:00
  1277. >>> print(tfuncdict["START_DATE"])
  1278. 2000-01-01
  1279. >>> print(tfuncdict["START_DATETIME"])
  1280. 2000-01-01 00:00:00
  1281. @endcode
  1282. """
  1283. tvardict = {"START_DOY" : None, "START_DOW" : None, "START_YEAR" : None,
  1284. "START_MONTH" : None, "START_WEEK" : None, "START_DAY" : None,
  1285. "START_HOUR" : None, "START_MINUTE" : None, "START_SECOND" : None,
  1286. "END_DOY" : None, "END_DOW" : None, "END_YEAR" : None,
  1287. "END_MONTH" : None, "END_WEEK" : None, "END_DAY" : None,
  1288. "END_HOUR" : None, "END_MINUTE" : None, "END_SECOND" : None,
  1289. "START_DATE" : None, "START_DATETIME" : None, "START_TIME" : None,
  1290. "END_DATE" : None, "END_DATETIME" : None, "END_TIME" : None}
  1291. # Compute temporal function only for maps with absolute time reference.
  1292. if map.is_time_absolute:
  1293. # Get datetime of map.
  1294. start, end = map.get_absolute_time()
  1295. # Compute DOY via time deltas.
  1296. yearstart = datetime(start.year, 1, 1)
  1297. yearend = datetime(end.year, 1, 1)
  1298. deltastart = start - yearstart
  1299. deltaend = end - yearend
  1300. # Evaluate datetime objects and fill in into dict.
  1301. tvardict["START_DOY"] = deltastart.days + 1
  1302. tvardict["START_DOW"] = start.isoweekday()
  1303. tvardict["START_YEAR"] = start.year
  1304. tvardict["START_MONTH"] = start.month
  1305. tvardict["START_WEEK"] = start.isocalendar()[1]
  1306. tvardict["START_DAY"] = start.day
  1307. tvardict["START_HOUR"] = start.hour
  1308. tvardict["START_MINUTE"] = start.minute
  1309. tvardict["START_SECOND"] = start.second
  1310. tvardict["END_DOY"] = deltaend.days + 1
  1311. tvardict["END_DOW"] = end.isoweekday()
  1312. tvardict["END_YEAR"] = end.year
  1313. tvardict["END_MONTH"] = end.month
  1314. tvardict["END_WEEK"] = end.isocalendar()[1]
  1315. tvardict["END_DAY"] = end.day
  1316. tvardict["END_HOUR"] = end.hour
  1317. tvardict["END_MINUTE"] = end.minute
  1318. tvardict["END_SECOND"] = end.second
  1319. tvardict["START_DATE"] = start.date()
  1320. tvardict["START_DATETIME"] = start
  1321. tvardict["START_TIME"] = start.time()
  1322. tvardict["END_DATE"] = end.date()
  1323. tvardict["END_DATETIME"] = end
  1324. tvardict["END_TIME"] = end.time()
  1325. if not map.is_time_absolute:
  1326. tvardict["START_DATE"] = start.date()
  1327. tvardict["START_DATETIME"] = start
  1328. tvardict["START_TIME"] = start.time()
  1329. tvardict["END_DATE"] = end.date()
  1330. tvardict["END_DATETIME"] = end
  1331. tvardict["END_TIME"] = end.time()
  1332. #core.fatal(_("The temporal functions for map <%s> only supported for absolute"\
  1333. # "time." % (str(map.get_id()))))
  1334. return(tvardict)
  1335. def eval_datetime_str(self, tfuncval, comp, value):
  1336. # Evaluate date object comparison expression.
  1337. if comp == "<":
  1338. boolname = eval(str(tfuncval < value))
  1339. elif comp == ">":
  1340. boolname = eval(str(tfuncval > value))
  1341. elif comp == "==":
  1342. boolname = eval(str(tfuncval == value))
  1343. elif comp == "<=":
  1344. boolname = eval(str(tfuncval <= value))
  1345. elif comp == ">=":
  1346. boolname = eval(str(tfuncval >= value))
  1347. elif comp == "!=":
  1348. boolname = eval(str(tfuncval != value))
  1349. return(boolname)
  1350. def eval_global_var(self, gvar, maplist):
  1351. """! This function evaluates a global variable expression for a map list.
  1352. For example: start_day() > 5 , end_month() == 2.
  1353. @param gvar Object of type GlobalTemporalVar containing temporal.
  1354. @param maplist List of map objects.
  1355. @return List of maps from maplist with added conditional boolean values.
  1356. """
  1357. boollist = []
  1358. # Loop over maps of input map list.
  1359. for map_i in maplist:
  1360. # Get dictionary with temporal variables for the map.
  1361. tfuncdict = self.get_temporal_func_dict(map_i)
  1362. # Get value from global variable.
  1363. value = gvar.value
  1364. # Get comparison operator from global variable, like <, >, <=, >=, ==, !=
  1365. comp_op = gvar.compop
  1366. # Get temporal function name for global variable.
  1367. tfunc = gvar.tfunc.upper()
  1368. # Get value for function name from dictionary.
  1369. tfuncval = tfuncdict[tfunc]
  1370. # Check if value has to be transfered to datetime object for comparison.
  1371. if tfunc in ["START_DATE", "END_DATE"]:
  1372. timeobj = datetime.strptime(value.replace("\"",""), '%Y-%m-%d')
  1373. value = timeobj.date()
  1374. boolname = self.eval_datetime_str(tfuncval, comp_op, value)
  1375. elif tfunc in ["START_TIME", "END_TIME"]:
  1376. timeobj = datetime.strptime(value.replace("\"",""), '%H:%M:%S')
  1377. value = timeobj.time()
  1378. boolname = self.eval_datetime_str(tfuncval, comp_op, value)
  1379. elif tfunc in ["START_DATETIME", "END_DATETIME"]:
  1380. timeobj = datetime.strptime(value.replace("\"",""), '%Y-%m-%d %H:%M:%S')
  1381. value = timeobj
  1382. boolname = self.eval_datetime_str(tfuncval, comp_op, value)
  1383. else:
  1384. boolname = eval(str(tfuncval) + comp_op + str(value))
  1385. # Add conditional boolean value to the map.
  1386. if "condition_value" in dir(map_i):
  1387. map_i.condition_value.append(boolname)
  1388. else:
  1389. map_i.condition_value = boolname
  1390. return(maplist)
  1391. def eval_map_list(self, maplist ,thenlist, topolist = ["EQUAL"]):
  1392. """! This function transfers boolean values from temporal expression
  1393. from one map list to another by their topology. These boolean
  1394. values are added to the maps as condition_value.
  1395. @param maplist List of map objects containing boolean map values.
  1396. @param thenlist List of map objects where the boolean values
  1397. should be added.
  1398. @return List of maps from thenlist with added conditional boolean values.
  1399. """
  1400. # Get topology of then statement map list in relation to the other maplist
  1401. # and assign boolean values of the maplist to the thenlist.
  1402. containlist = self.perform_temporal_selection(thenlist, maplist,
  1403. assign_val = True,
  1404. topolist = topolist)
  1405. # Inverse selection of maps from thenlist and assigning False values.
  1406. excludelist = self.perform_temporal_selection(thenlist, maplist,
  1407. assign_val = True,
  1408. inverse = True,
  1409. topolist = topolist)
  1410. # Combining the selection and inverse selection list.
  1411. resultlist = containlist + excludelist
  1412. return(resultlist)
  1413. def build_condition_list(self, tvarexpr, thenlist, topolist = ["EQUAL"]):
  1414. """! This function evaluates temporal variable expressions of a conditional
  1415. expression related to the map list of the then statement.
  1416. Global variables or map lists with booleans are compared to the topology
  1417. of the conclusion map list and a conditional list will be appended to
  1418. every map. It contain the boolean expressions from these comparisons
  1419. and optional operators to combine several temporal expressions, like
  1420. "&&" or "||".
  1421. For example: td(A) == 1 && start_day() > 5 --> [True || False]
  1422. (for one map.condition_value in a then map list)
  1423. @param tvarexpr List of GlobalTemporalVar objects and map lists.
  1424. The list is constructed by the TemporalAlgebraParser
  1425. in order of expression evaluation in the parser.
  1426. @param thenlist Map list object of the conclusion statement.
  1427. It will be compared and evaluated by the conditions.
  1428. @return Map list with conditional values for all temporal expressions.
  1429. @code
  1430. >>> import grass.temporal as tgis
  1431. >>> tgis.init()
  1432. >>> p = tgis.TemporalAlgebraParser()
  1433. >>> # Example with two lists of maps
  1434. >>> # Create two list of maps with equal time stamps
  1435. >>> mapsA = []
  1436. >>> mapsB = []
  1437. >>> for i in range(10):
  1438. ... idA = "a%i@B"%(i)
  1439. ... mapA = tgis.RasterDataset(idA)
  1440. ... idB = "b%i@B"%(i)
  1441. ... mapB = tgis.RasterDataset(idB)
  1442. ... check = mapA.set_absolute_time(datetime(2000,1,i + 1),
  1443. ... datetime(2000,1,i + 2))
  1444. ... check = mapB.set_absolute_time(datetime(2000,1,i + 6),
  1445. ... datetime(2000,1,i + 7))
  1446. ... mapsA.append(mapA)
  1447. ... mapsB.append(mapB)
  1448. >>> mapsA = p.check_stds(mapsA)
  1449. >>> mapsB = p.check_stds(mapsB)
  1450. >>> # Create global expression object.
  1451. >>> gvarA = tgis.GlobalTemporalVar()
  1452. >>> gvarA.tfunc = "start_day"
  1453. >>> gvarA.compop = ">"
  1454. >>> gvarA.value = 5
  1455. >>> gvarB = tgis.GlobalTemporalVar()
  1456. >>> gvarB.tfunc = "start_day"
  1457. >>> gvarB.compop = "<="
  1458. >>> gvarB.value = 8
  1459. >>> gvarOP = tgis.GlobalTemporalVar()
  1460. >>> gvarOP.relationop = "&&"
  1461. >>> gvarOP.topology.append("EQUAL")
  1462. >>> tvarexpr = gvarA
  1463. >>> result = p.build_condition_list(tvarexpr, mapsA)
  1464. >>> for map_i in result:
  1465. ... print(map_i.get_map_id() + ' ' + str(map_i.condition_value))
  1466. a0@B [False]
  1467. a1@B [False]
  1468. a2@B [False]
  1469. a3@B [False]
  1470. a4@B [False]
  1471. a5@B [True]
  1472. a6@B [True]
  1473. a7@B [True]
  1474. a8@B [True]
  1475. a9@B [True]
  1476. >>> tvarexpr = [gvarA, gvarOP, gvarB]
  1477. >>> result = p.build_condition_list(tvarexpr, mapsB)
  1478. >>> for map_i in result:
  1479. ... print(map_i.get_map_id() + ' ' + str(map_i.condition_value))
  1480. b0@B [True, ['EQUAL'], '&&', True]
  1481. b1@B [True, ['EQUAL'], '&&', True]
  1482. b2@B [True, ['EQUAL'], '&&', True]
  1483. b3@B [True, ['EQUAL'], '&&', False]
  1484. b4@B [True, ['EQUAL'], '&&', False]
  1485. b5@B [True, ['EQUAL'], '&&', False]
  1486. b6@B [True, ['EQUAL'], '&&', False]
  1487. b7@B [True, ['EQUAL'], '&&', False]
  1488. b8@B [True, ['EQUAL'], '&&', False]
  1489. b9@B [True, ['EQUAL'], '&&', False]
  1490. @endcode
  1491. """
  1492. # Check if the input expression is a valid single global variable.
  1493. if not isinstance(tvarexpr, list):
  1494. if isinstance(tvarexpr, GlobalTemporalVar):
  1495. if tvarexpr.get_type() == "global":
  1496. # Use method eval_global_var to evaluate expression.
  1497. resultlist = self.eval_global_var(tvarexpr, thenlist)
  1498. else:
  1499. # Check if a given list is a list of maps.
  1500. if all([issubclass(type(ele), AbstractMapDataset) for ele in tvarexpr]):
  1501. # Use method eval_map_list to evaluate map_list in comparison to thenlist.
  1502. resultlist = self.eval_map_list(tvarexpr, thenlist, topolist)
  1503. # Loop through the list, search for map lists or global variables.
  1504. for expr in tvarexpr:
  1505. if isinstance(expr, list):
  1506. if all([issubclass(type(ele), AbstractMapDataset) for ele in expr]):
  1507. # Use method eval_map_list to evaluate map_list
  1508. resultlist = self.eval_map_list(expr, thenlist, topolist)
  1509. else:
  1510. # Recursive function call to look into nested list elements.
  1511. self.build_condition_list(expr, thenlist)
  1512. elif isinstance(expr, GlobalTemporalVar):
  1513. # Use according functions for different global variable types.
  1514. if expr.get_type() == "operator":
  1515. if all(["condition_value" in dir(map_i) for map_i in thenlist]):
  1516. # Add operator string to the condition list.
  1517. [map_i.condition_value.extend(expr.get_type_value()) for map_i in thenlist]
  1518. if expr.get_type() == "global":
  1519. # Use method eval_global_var to evaluate expression.
  1520. resultlist = self.eval_global_var(expr, thenlist)
  1521. # Sort resulting list of maps chronological.
  1522. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  1523. return(resultlist)
  1524. def eval_condition_list(self, maplist, inverse = False):
  1525. """! This function evaluates conditional values of a map list.
  1526. A recursive function is used to evaluate comparison statements
  1527. from left to right in the given conditional list.
  1528. For example: [True, '||', False, '&&', True] -> True
  1529. [True, '||', False, '&&', False] -> False
  1530. [True, '&&', False, '&&', True] -> False
  1531. [False, '||', True, '||', False] -> True
  1532. [False, '&&', True, '&&', True] -> False
  1533. [True, '&&', True, '&&', True] -> True
  1534. [True, '&&', True] -> True
  1535. [True, '&&', False] -> False
  1536. [False, '||', True] -> True
  1537. @param tvarexpr List of GlobalTemporalVar objects and map lists.
  1538. The list is constructed by the TemporalAlgebraParser
  1539. in order of expression evaluation in the parser.
  1540. @return Map list with conditional values for all temporal expressions.
  1541. @code
  1542. >>> import grass.temporal as tgis
  1543. >>> tgis.init()
  1544. >>> p = tgis.TemporalAlgebraParser()
  1545. >>> # Example with two lists of maps
  1546. >>> # Create two list of maps with equal time stamps
  1547. >>> mapsA = []
  1548. >>> mapsB = []
  1549. >>> for i in range(10):
  1550. ... idA = "a%i@B"%(i)
  1551. ... mapA = tgis.RasterDataset(idA)
  1552. ... idB = "b%i@B"%(i)
  1553. ... mapB = tgis.RasterDataset(idB)
  1554. ... check = mapA.set_absolute_time(datetime(2000,1,i + 1),
  1555. ... datetime(2000,1,i + 2))
  1556. ... check = mapB.set_absolute_time(datetime(2000,1,i + 6),
  1557. ... datetime(2000,1,i + 7))
  1558. ... mapsA.append(mapA)
  1559. ... mapsB.append(mapB)
  1560. >>> mapsA = p.check_stds(mapsA)
  1561. >>> mapsB = p.check_stds(mapsB)
  1562. >>> # Create global expression object.
  1563. >>> gvarA = tgis.GlobalTemporalVar()
  1564. >>> gvarA.tfunc = "start_day"
  1565. >>> gvarA.compop = ">"
  1566. >>> gvarA.value = 5
  1567. >>> gvarB = tgis.GlobalTemporalVar()
  1568. >>> gvarB.tfunc = "start_day"
  1569. >>> gvarB.compop = "<="
  1570. >>> gvarB.value = 8
  1571. >>> gvarOP = tgis.GlobalTemporalVar()
  1572. >>> gvarOP.relationop = "&&"
  1573. >>> gvarOP.topology.append("EQUAL")
  1574. >>> tvarexpr = [mapsA, gvarOP,gvarA]
  1575. @endcode
  1576. """
  1577. def recurse_compare(conditionlist):
  1578. for ele in conditionlist:
  1579. if ele == '||':
  1580. ele_index = conditionlist.index(ele)
  1581. topolist = conditionlist.pop(ele_index -1)
  1582. right = conditionlist.pop(ele_index)
  1583. left = conditionlist.pop(ele_index - 2)
  1584. if any([left, right]):
  1585. result = True
  1586. else:
  1587. result = False
  1588. conditionlist[ele_index - 2] = result
  1589. recurse_compare(conditionlist)
  1590. if ele == '&&':
  1591. ele_index = conditionlist.index(ele)
  1592. topolist = conditionlist.pop(ele_index -1)
  1593. right = conditionlist.pop(ele_index)
  1594. left = conditionlist.pop(ele_index - 2)
  1595. if all([left, right]):
  1596. result = True
  1597. else:
  1598. result = False
  1599. conditionlist[ele_index - 2] = result
  1600. recurse_compare(conditionlist)
  1601. resultlist = conditionlist
  1602. return(resultlist)
  1603. resultlist = []
  1604. inverselist = []
  1605. for map_i in maplist:
  1606. if "condition_value" in dir(map_i):
  1607. # Get condition values from map object.
  1608. conditionlist = map_i.condition_value
  1609. #print(map_i.get_map_id() + ' ' + str(map_i.condition_value))
  1610. # Evaluate conditions in list with recursive function.
  1611. resultbool = recurse_compare(conditionlist)
  1612. # Set conditional value of map to resulting boolean.
  1613. map_i.condition_value = resultbool
  1614. # Add all maps that fulfill the conditions to result list.
  1615. if resultbool[0]:
  1616. resultlist.append(map_i)
  1617. else:
  1618. inverselist.append(map_i)
  1619. #print(map_i.get_map_id() + ' ' + str(map_i.condition_value))
  1620. if inverse:
  1621. return(inverselist)
  1622. else:
  1623. return(resultlist)
  1624. ###########################################################################
  1625. def p_statement_assign(self, t):
  1626. # The expression should always return a list of maps.
  1627. """
  1628. statement : stds EQUALS expr
  1629. """
  1630. if self.run:
  1631. resultstds = open_new_space_time_dataset(t[1], self.stdstype, \
  1632. self.temporaltype, "", "", \
  1633. 'mean', dbif=self.dbif, \
  1634. overwrite = self.overwrite)
  1635. if isinstance(t[3], list):
  1636. num = len(t[3])
  1637. count = 0
  1638. if num > 0:
  1639. dbif, connected = init_dbif(None)
  1640. for map in t[3]:
  1641. map.select(dbif=dbif)
  1642. #map.update()
  1643. resultstds.register_map(map, dbif=dbif)
  1644. count += 1
  1645. if count % 10 == 0:
  1646. self.msgr.percent(count, num, 1)
  1647. resultstds.update_from_registered_maps(dbif=dbif)
  1648. if connected:
  1649. dbif.close()
  1650. t[0] = t[3]
  1651. else:
  1652. t[0] = t[3]
  1653. if self.debug:
  1654. print t[1], "=", t[3]
  1655. def p_stds_1(self, t):
  1656. # Definition of a space time dataset
  1657. """
  1658. stds : NAME
  1659. """
  1660. t[0] = t[1]
  1661. def p_paren_expr(self, t):
  1662. """ expr : LPAREN expr RPAREN"""
  1663. t[0] = t[2]
  1664. def p_number(self,t):
  1665. """number : INT
  1666. | FLOAT
  1667. """
  1668. t[0] = t[1]
  1669. def p_t_hash(self,t):
  1670. """
  1671. t_hash_var : stds HASH stds
  1672. | stds HASH expr
  1673. | expr HASH stds
  1674. | expr HASH expr
  1675. """
  1676. if self.run:
  1677. maplistA = self.check_stds(t[1])
  1678. maplistB = self.check_stds(t[3])
  1679. resultlist = self.get_temporal_topo_list(maplistA, maplistB,
  1680. count_map = True)
  1681. t[0] = resultlist
  1682. def p_t_hash2(self,t):
  1683. """
  1684. t_hash_var : stds T_HASH_OPERATOR stds
  1685. | stds T_HASH_OPERATOR expr
  1686. | expr T_HASH_OPERATOR stds
  1687. | expr T_HASH_OPERATOR expr
  1688. """
  1689. if self.run:
  1690. maplistA = self.check_stds(t[1])
  1691. maplistB = self.check_stds(t[3])
  1692. topolist = self.eval_toperator(t[2])[0]
  1693. resultlist = self.get_temporal_topo_list(maplistA, maplistB, topolist,
  1694. count_map = True)
  1695. t[0] = resultlist
  1696. def p_t_td_var(self, t):
  1697. """
  1698. t_td_var : TD LPAREN stds RPAREN
  1699. | TD LPAREN expr RPAREN
  1700. """
  1701. if self.run:
  1702. maplist = self.check_stds(t[3])
  1703. for map_i in maplist:
  1704. if map_i.is_time_absolute:
  1705. start, end = map_i.get_absolute_time()
  1706. if end != None:
  1707. td = time_delta_to_relative_time(end - start)
  1708. else:
  1709. start, end, unit = current.get_relative_time()
  1710. if end != None:
  1711. td = end - start
  1712. if "map_value" in dir(map_i):
  1713. gvar = GlobalTemporalVar()
  1714. gvar.td = td
  1715. map_i.map_value.append(gvar)
  1716. else:
  1717. map_i.map_value = gvar
  1718. t[0] = maplist
  1719. else:
  1720. t[0] = "td(" + str(t[3]) + ")"
  1721. if self.debug:
  1722. print "td(" + str(t[3]) + ")"
  1723. def p_t_time_var(self, t):
  1724. # Temporal variables that return a double or integer value
  1725. """
  1726. t_var : START_DOY
  1727. | START_DOW
  1728. | START_YEAR
  1729. | START_MONTH
  1730. | START_WEEK
  1731. | START_DAY
  1732. | START_HOUR
  1733. | START_MINUTE
  1734. | START_SECOND
  1735. | END_DOY
  1736. | END_DOW
  1737. | END_YEAR
  1738. | END_MONTH
  1739. | END_WEEK
  1740. | END_DAY
  1741. | END_HOUR
  1742. | END_MINUTE
  1743. | END_SECOND
  1744. """
  1745. t[0] = t[1]
  1746. def p_compare_op(self, t):
  1747. # Compare operators that are supported for temporal expressions
  1748. """
  1749. comp_op : CEQUALS
  1750. | UNEQUALS
  1751. | LOWER
  1752. | LOWER_EQUALS
  1753. | GREATER
  1754. | GREATER_EQUALS
  1755. """
  1756. t[0] = t[1]
  1757. def p_t_var_expr(self, t):
  1758. # Examples:
  1759. # start_month() > 2
  1760. # start_day() < 14
  1761. # start_day() < start_month()
  1762. # td() < 31
  1763. """
  1764. t_var_expr : t_var LPAREN RPAREN comp_op number
  1765. | t_var LPAREN RPAREN comp_op t_var
  1766. | t_td_var comp_op number
  1767. | t_td_var comp_op t_var
  1768. | t_hash_var comp_op number
  1769. | t_hash_var comp_op t_var
  1770. """
  1771. if self.run:
  1772. if len(t) == 4:
  1773. maplist = self.check_stds(t[1])
  1774. comp_op = t[2]
  1775. for map_i in maplist:
  1776. for obj in map_i.map_value:
  1777. if isinstance(obj, GlobalTemporalVar):
  1778. td = obj.td
  1779. boolnum = eval(str(td) + comp_op + str(t[3]))
  1780. gvar = GlobalTemporalVar()
  1781. gvar.boolean = boolnum
  1782. if obj.get_type() == "timediff":
  1783. index = map_i.map_value.index(obj)
  1784. map_i.map_value[index] = gvar
  1785. t[0] = maplist
  1786. if len(t) == 6:
  1787. if isinstance(t[1], GlobalTemporalVar):
  1788. pass
  1789. gvar = GlobalTemporalVar()
  1790. gvar.tfunc = t[1]
  1791. gvar.compop = t[4]
  1792. gvar.value = t[5]
  1793. t[0] = gvar
  1794. else:
  1795. t[0] = True
  1796. if self.debug:
  1797. if len(t) == 6:
  1798. print t[1], t[4], t[5]
  1799. if len(t) == 4:
  1800. print t[1], t[2], t[3]
  1801. def p_t_var_expr_time1(self, t):
  1802. # Examples:
  1803. # start_time() == "12:30:00"
  1804. # start_date() <= "2001-01-01"
  1805. # start_datetime() > "2001-01-01 12:30:00"
  1806. """
  1807. t_var_expr : START_TIME LPAREN RPAREN comp_op TIME
  1808. | START_DATE LPAREN RPAREN comp_op DATE
  1809. | START_DATETIME LPAREN RPAREN comp_op DATETIME
  1810. | END_TIME LPAREN RPAREN comp_op TIME
  1811. | END_DATE LPAREN RPAREN comp_op DATE
  1812. | END_DATETIME LPAREN RPAREN comp_op DATETIME
  1813. """
  1814. if self.run:
  1815. gvar = GlobalTemporalVar()
  1816. gvar.tfunc = t[1]
  1817. gvar.compop = t[4]
  1818. gvar.value = t[5]
  1819. t[0] = gvar
  1820. else:
  1821. t[0] = True
  1822. if self.debug:
  1823. print t[1], t[4], t[5]
  1824. def p_t_var_expr_time2(self, t):
  1825. """
  1826. t_var_expr : TIME comp_op START_TIME LPAREN RPAREN
  1827. | DATE comp_op START_DATE LPAREN RPAREN
  1828. | DATETIME comp_op START_DATETIME LPAREN RPAREN
  1829. | TIME comp_op END_TIME LPAREN RPAREN
  1830. | DATE comp_op END_DATE LPAREN RPAREN
  1831. | DATETIME comp_op END_DATETIME LPAREN RPAREN
  1832. """
  1833. if self.run:
  1834. reverseop = {"<" : ">", ">" : "<", "<=" : ">=", ">=" : "<=",
  1835. "==" : "==", "!=" : "!="}
  1836. gvar = GlobalTemporalVar()
  1837. gvar.tfunc = t[3]
  1838. gvar.compop = reverseop[t[2]]
  1839. gvar.value = t[1]
  1840. t[0] = gvar
  1841. else:
  1842. t[0] = True
  1843. if self.debug:
  1844. print(t[4])
  1845. print t[1], t[4], t[5]
  1846. def p_t_var_expr_comp(self, t):
  1847. """
  1848. t_var_expr : t_var_expr AND AND t_var_expr
  1849. | t_var_expr OR OR t_var_expr
  1850. """
  1851. if self.run:
  1852. tvarexprA = t[1]
  1853. tvarexprB = t[4]
  1854. operator = GlobalTemporalVar()
  1855. operator.relationop = t[2] + t[3]
  1856. operator.topology.append("EQUAL")
  1857. resultlist = []
  1858. resultlist.append(tvarexprA)
  1859. resultlist.append(operator)
  1860. resultlist.append(tvarexprB)
  1861. t[0] = resultlist
  1862. else:
  1863. t[0] = True
  1864. if self.debug:
  1865. print t[1], t[2] + t[3], t[4]
  1866. def p_t_var_expr_comp_op(self, t):
  1867. """
  1868. t_var_expr : t_var_expr T_COMP_OPERATOR t_var_expr
  1869. """
  1870. if self.run:
  1871. tvarexprA = t[1]
  1872. tvarexprB = t[3]
  1873. operator = GlobalTemporalVar()
  1874. toperator = self.eval_toperator(t[2])[0]
  1875. relationop = toperator[2]
  1876. relations = toperator[0]
  1877. operator.relationop = relationop
  1878. operator.topology.extend(relations)
  1879. resultlist = []
  1880. resultlist.append(tvarexprA)
  1881. resultlist.append(operator)
  1882. resultlist.append(tvarexprB)
  1883. t[0] = resultlist
  1884. else:
  1885. t[0] = True
  1886. if self.debug:
  1887. print t[1], t[2], t[3]
  1888. def p_expr_t_select(self, t):
  1889. # Temporal equal selection
  1890. # The temporal topology relation equals is implicit
  1891. # Examples:
  1892. # A : B # Select the part of A that is temporally equal B
  1893. """
  1894. expr : stds T_SELECT stds
  1895. | expr T_SELECT stds
  1896. | stds T_SELECT expr
  1897. | expr T_SELECT expr
  1898. """
  1899. if self.run:
  1900. # Setup database connection.
  1901. # Check input stds.
  1902. maplistA = self.check_stds(t[1])
  1903. maplistB = self.check_stds(t[3])
  1904. # Perform selection.
  1905. selectlist = self.perform_temporal_selection(maplistA, maplistB)
  1906. # Return map list.
  1907. t[0] = selectlist
  1908. else:
  1909. t[0] = t[1] + "*"
  1910. if self.debug:
  1911. print t[1] + "* = ", t[1], t[2], t[3]
  1912. def p_expr_t_not_select(self, t):
  1913. # Temporal equal selection
  1914. # The temporal topology relation equals is implicit
  1915. # Examples:
  1916. # A !: B # Select the part of A that is temporally unequal to B
  1917. """
  1918. expr : stds T_NOT_SELECT stds
  1919. | expr T_NOT_SELECT stds
  1920. | stds T_NOT_SELECT expr
  1921. | expr T_NOT_SELECT expr
  1922. """
  1923. if self.run:
  1924. # Check input stds.
  1925. maplistA = self.check_stds(t[1])
  1926. maplistB = self.check_stds(t[3])
  1927. # Perform negative selection.
  1928. selectlist = self.perform_temporal_selection(maplistA, maplistB,
  1929. inverse = True)
  1930. # Return map list.
  1931. t[0] = selectlist
  1932. else:
  1933. t[0] = t[1] + "*"
  1934. if self.debug:
  1935. print t[1] + "* = ", t[1], t[2], t[3]
  1936. def p_expr_t_select_operator(self, t):
  1937. # Temporal equal selection
  1938. # The temporal topology relation equals is implicit
  1939. # Examples:
  1940. # A {!:} B # Select the part of A that is temporally unequal to B
  1941. # A {:} B # Select the part of A that is temporally equal B
  1942. # A {equals, !:} B # Select the part of A that is temporally unequal to B
  1943. # A {during, !:} B # Select the part of A that is temporally not during B
  1944. # A {overlaps, :} B # Select the part of A that temporally overlaps B
  1945. # A {overlaps|equals, :} B # Select the part of A that temporally overlaps or equals B
  1946. """
  1947. expr : stds T_SELECT_OPERATOR stds
  1948. | expr T_SELECT_OPERATOR stds
  1949. | stds T_SELECT_OPERATOR expr
  1950. | expr T_SELECT_OPERATOR expr
  1951. """
  1952. if self.run:
  1953. # Check input stds.
  1954. maplistA = self.check_stds(t[1])
  1955. maplistB = self.check_stds(t[3])
  1956. # Evaluate temporal operator.
  1957. operators = self.eval_toperator(t[2])
  1958. # Check for negative selection.
  1959. if operators[2] == "!:":
  1960. negation = True
  1961. else:
  1962. negation = False
  1963. # Perform selection.
  1964. selectlist = self.perform_temporal_selection(maplistA, maplistB,
  1965. topolist = operators[0], inverse = negation)
  1966. selectlist = self.set_granularity(selectlist, maplistB, operators[1],
  1967. operators[0])
  1968. # Return map list.
  1969. t[0] = selectlist
  1970. else:
  1971. t[0] = t[1] + "*"
  1972. if self.debug:
  1973. print t[1] + "* = ", t[1], t[2], t[3]
  1974. def p_expr_condition_if(self, t):
  1975. # Examples
  1976. # if( start_date() < "2005-06-01", A:B)
  1977. """
  1978. expr : IF LPAREN t_var_expr COMMA stds RPAREN
  1979. | IF LPAREN t_var_expr COMMA expr RPAREN
  1980. """
  1981. if self.run:
  1982. # Get stds/map list of then statement.
  1983. thenlist = self.check_stds(t[5])
  1984. # Get temporal conditional statement.
  1985. tvarexpr = t[3]
  1986. thencond = self.build_condition_list(tvarexpr, thenlist)
  1987. thenresult = self.eval_condition_list(thencond)
  1988. # Clear the map and conditional values of the map list.
  1989. resultlist = self.check_stds(thenresult, clear = True)
  1990. # Return resulting map list.
  1991. t[0] = resultlist
  1992. else:
  1993. t[0] = t[5] + "*"
  1994. if self.debug:
  1995. print str(t[5]) + "* = ", "if condition", str(t[3]), ' then ', str(t[5])
  1996. def p_expr_condition_if_relation(self, t):
  1997. # Examples
  1998. # if({equal} start_date() < "2005-06-01", A:B)
  1999. """
  2000. expr : IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA stds RPAREN
  2001. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA expr RPAREN
  2002. """
  2003. if self.run:
  2004. # Get stds/map list of then statement.
  2005. thenlist = self.check_stds(t[7])
  2006. # Get temporal conditional statement.
  2007. tvarexpr = t[5]
  2008. topolist = self.eval_toperator(t[3])[0]
  2009. thencond = self.build_condition_list(tvarexpr, thenlist, topolist)
  2010. thenresult = self.eval_condition_list(thencond)
  2011. # Clear the map and conditional values of the map list.
  2012. resultlist = self.check_stds(thenresult, clear = True)
  2013. # Return resulting map list.
  2014. t[0] = resultlist
  2015. else:
  2016. t[0] = t[7] + "*"
  2017. if self.debug:
  2018. print "result* = ", "if ", str(t[3]), "condition", str(t[5]), " then ", str(t[7])
  2019. def p_expr_condition_elif(self, t):
  2020. # Examples
  2021. # if( start_date() < "2005-06-01", if(start_time() < "12:30:00", A:B), A!:B)
  2022. """
  2023. expr : IF LPAREN t_var_expr COMMA stds COMMA stds RPAREN
  2024. | IF LPAREN t_var_expr COMMA stds COMMA expr RPAREN
  2025. | IF LPAREN t_var_expr COMMA expr COMMA stds RPAREN
  2026. | IF LPAREN t_var_expr COMMA expr COMMA expr RPAREN
  2027. """
  2028. if self.run:
  2029. # Get stds/map list of then statement.
  2030. thenlist = self.check_stds(t[5])
  2031. elselist = self.check_stds(t[7])
  2032. # Get temporal conditional statement for then and else expressions.
  2033. tvarexpr = t[3]
  2034. thencond = self.build_condition_list(tvarexpr, thenlist)
  2035. thenresult = self.eval_condition_list(thencond)
  2036. elsecond = self.build_condition_list(tvarexpr, elselist)
  2037. elseresult = self.eval_condition_list(elsecond, inverse = True)
  2038. # Combine and sort else and then statement to result map list.
  2039. combilist = thenresult + elseresult
  2040. resultlist = sorted(combilist, key = AbstractDatasetComparisonKeyStartTime)
  2041. # Clear the map and conditional values of the map list.
  2042. resultlist = self.check_stds(resultlist, clear = True)
  2043. # Return resulting map list.
  2044. t[0] = resultlist
  2045. else:
  2046. t[0] = t[5] + "*"
  2047. if self.debug:
  2048. print str(t[5]) + "* = ", "if condition", str(t[3]), " then ", str(t[5]), ' else ', str(t[7])
  2049. def p_expr_condition_elif_relation(self, t):
  2050. # Examples
  2051. # if({equal}, start_date() < "2005-06-01", if(start_time() < "12:30:00", A:B), A!:B)
  2052. # The then and else statement using the same topological relationships.
  2053. # Feature request: Independent relationships for then and else to conditions.
  2054. """
  2055. expr : IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA stds COMMA stds RPAREN
  2056. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA stds COMMA expr RPAREN
  2057. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA expr COMMA stds RPAREN
  2058. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA expr COMMA expr RPAREN
  2059. """
  2060. if self.run:
  2061. # Get stds/map list of then statement.
  2062. thenlist = self.check_stds(t[7])
  2063. elselist = self.check_stds(t[9])
  2064. # Get temporal conditional statement.
  2065. tvarexpr = t[5]
  2066. topolist = self.eval_toperator(t[3])[0]
  2067. thencond = self.build_condition_list(tvarexpr, thenlist, topolist)
  2068. thenresult = self.eval_condition_list(thencond)
  2069. elsecond = self.build_condition_list(tvarexpr, elselist, topolist)
  2070. elseresult = self.eval_condition_list(elsecond, inverse = True)
  2071. # Combine and sort else and then statement to result map list.
  2072. combilist = thenresult + elseresult
  2073. resultlist = sorted(combilist, key = AbstractDatasetComparisonKeyStartTime)
  2074. # Clear the map and conditional values of the map list.
  2075. resultlist = self.check_stds(resultlist, clear = True)
  2076. # Return resulting map list.
  2077. t[0] = resultlist
  2078. else:
  2079. if t[5]:
  2080. t[0] = str(t[7])
  2081. else:
  2082. t[0] = str(t[9])
  2083. if self.debug:
  2084. if t[5]:
  2085. print str(t[7]), "* = ", "if condition", str(t[5]), " then ", str(t[7]), ' else ', str(t[9])
  2086. else:
  2087. print str(t[9]), "* = ", "if condition", str(t[5]), " then ", str(t[7]), ' else ', str(t[9])
  2088. def p_expr_t_buff(self, t):
  2089. # Examples
  2090. # buff_t(A : B, "10 minutes") # Select the part of A that is temporally
  2091. # equal to B and create a buffer of 10 minutes around
  2092. """
  2093. expr : BUFF_T LPAREN stds COMMA QUOTE number NAME QUOTE RPAREN
  2094. | BUFF_T LPAREN expr COMMA QUOTE number NAME QUOTE RPAREN
  2095. | BUFF_T LPAREN stds COMMA number RPAREN
  2096. | BUFF_T LPAREN expr COMMA number RPAREN
  2097. """
  2098. if self.run:
  2099. # Check input stds.
  2100. bufflist = self.check_stds(t[3])
  2101. for map in bufflist:
  2102. # Get increment format.
  2103. if len(t) == 10:
  2104. increment = str(t[6]) + " " + t[7]
  2105. elif len(t) == 7:
  2106. increment = str(t[5])
  2107. # Perform buffering.
  2108. map.temporal_buffer(increment)
  2109. t[0] = bufflist
  2110. else:
  2111. t[0] = t[3] + "*"
  2112. if self.debug:
  2113. if len(t) == 10:
  2114. print str(t[3]) + "* = buff_t(", str(t[3]), "," , '"', str(t[6]), str(t[7]), '"', ")"
  2115. elif len(t) == 7:
  2116. print str(t[3]) + "* = buff_t(", str(t[3]), ",", str(t[5]), ")"
  2117. def p_expr_t_snap(self, t):
  2118. # Examples
  2119. # tsnap(A : B) # Snap the maps of A temporally.
  2120. """
  2121. expr : TSNAP LPAREN stds RPAREN
  2122. | TSNAP LPAREN expr RPAREN
  2123. """
  2124. if self.run:
  2125. # Check input stds.
  2126. maplist = self.check_stds(t[3])
  2127. # Perform snapping.
  2128. snaplist = AbstractSpaceTimeDataset.snap_map_list(maplist)
  2129. t[0] = snaplist
  2130. else:
  2131. t[0] = t[3] + "*"
  2132. if self.debug:
  2133. print str(t[3]) + "* = tsnap(", str(t[3]), ")"
  2134. def p_expr_t_shift(self, t):
  2135. # Examples
  2136. # tshift(A : B, "10 minutes") # Shift the selection from A temporally
  2137. # by 10 minutes.
  2138. """
  2139. expr : TSHIFT LPAREN stds COMMA QUOTE number NAME QUOTE RPAREN
  2140. | TSHIFT LPAREN expr COMMA QUOTE number NAME QUOTE RPAREN
  2141. | TSHIFT LPAREN stds COMMA number RPAREN
  2142. | TSHIFT LPAREN expr COMMA number RPAREN
  2143. """
  2144. if self.run:
  2145. # Check input stds.
  2146. maplist = self.check_stds(t[3])
  2147. # Get increment format.
  2148. if len(t) == 10:
  2149. increment = str(t[6]) + " " + t[7]
  2150. elif len(t) == 7:
  2151. increment = str(t[5])
  2152. # Perform shifting.
  2153. shiftlist = AbstractSpaceTimeDataset.shift_map_list(maplist, increment)
  2154. t[0] = shiftlist
  2155. else:
  2156. t[0] = t[3] + "*"
  2157. if self.debug:
  2158. if len(t) == 10:
  2159. print str(t[3]) + "* = tshift(", str(t[3]), "," , '"', str(t[6]), str(t[7]), '"', ")"
  2160. elif len(t) == 7:
  2161. print str(t[3]) + "* = tshift(", str(t[3]), ",", str(t[5]), ")"
  2162. # Handle errors.
  2163. def p_error(self, t):
  2164. if t:
  2165. raise SyntaxError("syntax error on line %d, token %s near '%s' expression '%s'" %
  2166. (t.lineno, t.type, t.value, self.expression))
  2167. else:
  2168. raise SyntaxError("Unexpected syntax error")
  2169. ###############################################################################
  2170. if __name__ == "__main__":
  2171. import doctest
  2172. doctest.testmod()