temporal_algebra.py 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  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-block:: python
  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 {:,during} C"
  298. >>> print(expression)
  299. D = A {!:} B {:,during} C
  300. >>> p.parse(expression)
  301. A* = A {!:} B
  302. A** = A* {:,during} C
  303. D = A**
  304. >>> expression = "D = A {:} B {!:,during} C"
  305. >>> print(expression)
  306. D = A {:} B {!:,during} C
  307. >>> p.parse(expression)
  308. A* = A {:} B
  309. A** = A* {!:,during} C
  310. D = A**
  311. >>> p.run = False
  312. >>> p.debug = False
  313. >>> expression = "C = test1 : test2"
  314. >>> print(expression)
  315. C = test1 : test2
  316. >>> p.parse(expression, 'stvds')
  317. >>> expression = 'D = buff_t(test1,"10 months")'
  318. >>> print(expression)
  319. D = buff_t(test1,"10 months")
  320. >>> p.parse(expression, 'stvds')
  321. >>> expression = 'E = test2 {:,during} buff_t(test1,"1 days")'
  322. >>> print(expression)
  323. E = test2 {:,during} buff_t(test1,"1 days")
  324. >>> p.parse(expression, 'stvds')
  325. >>> expression = 'F = test2 {:,equal} buff_t(test1,"1 days")'
  326. >>> print(expression)
  327. F = test2 {:,equal} buff_t(test1,"1 days")
  328. >>> p.parse(expression, 'stvds')
  329. >>> p.debug = True
  330. >>> expression = 'H = tsnap(test2 {:,during} buff_t(test1, "1 days"))'
  331. >>> p.parse(expression, 'stvds')
  332. test1* = buff_t( test1 , " 1 days " )
  333. test2* = test2 {:,during} test1*
  334. test2** = tsnap( test2* )
  335. H = test2**
  336. >>> expression = 'H = tshift(test2 {:,during} test1, "1 days")'
  337. >>> p.parse(expression, 'stvds')
  338. test2* = test2 {:,during} test1
  339. test2** = tshift( test2* , " 1 days " )
  340. H = test2**
  341. >>> expression = 'H = tshift(H, 3)'
  342. >>> p.parse(expression, 'stvds')
  343. H* = tshift( H , 3 )
  344. H = H*
  345. >>> expression = 'C = if(td(A) == 2, A)'
  346. >>> p.parse(expression, 'stvds')
  347. td(A)
  348. td(A) == 2
  349. A* = if condition None then A
  350. C = A*
  351. >>> expression = 'C = if(td(A) == 5, A, B)'
  352. >>> p.parse(expression, 'stvds')
  353. td(A)
  354. td(A) == 5
  355. A* = if condition None then A else B
  356. C = A*
  357. >>> expression = 'C = if(td(A) == 5 || start_date(A) > "2010-01-01", A, B)'
  358. >>> p.parse(expression, 'stvds')
  359. td(A)
  360. td(A) == 5
  361. start_date A > "2010-01-01"
  362. None || None
  363. A* = if condition None then A else B
  364. C = A*
  365. """
  366. try:
  367. import ply.lex as lex
  368. import ply.yacc as yacc
  369. except:
  370. pass
  371. import os
  372. import copy
  373. import grass.pygrass.modules as pymod
  374. from space_time_datasets import *
  375. from factory import *
  376. from open_stds import *
  377. from temporal_operator import *
  378. ##############################################################################
  379. class TemporalAlgebraLexer(object):
  380. """Lexical analyzer for the GRASS GIS temporal algebra"""
  381. # Functions that defines an if condition, temporal buffering, snapping and
  382. # selection of maps with temporal extent.
  383. conditional_functions = {
  384. 'if' : 'IF',
  385. 'buff_t': 'BUFF_T',
  386. 'tsnap' : 'TSNAP',
  387. 'tshift' : 'TSHIFT',
  388. 'tmap' : 'TMAP',
  389. }
  390. # Variables with date and time strings
  391. datetime_functions = {
  392. 'start_time' : 'START_TIME', # start time as HH::MM:SS
  393. 'start_date' : 'START_DATE', # start date as yyyy-mm-DD
  394. 'start_datetime' : 'START_DATETIME', # start datetime as yyyy-mm-DD HH:MM:SS
  395. 'end_time' : 'END_TIME', # end time as HH:MM:SS
  396. 'end_date' : 'END_DATE', # end date as yyyy-mm-DD
  397. 'end_datetime' : 'END_DATETIME', # end datetime as yyyy-mm-DD HH:MM:SS
  398. }
  399. # Time functions
  400. time_functions = {
  401. 'td' : 'TD', # The size of the current
  402. # sample time interval in days and
  403. # fraction of days for absolute time,
  404. # and in relative units in case of relative time.
  405. #'start_td' : 'START_TD', # The time difference between the start
  406. # time of the sample space time raster
  407. # dataset and the start time of the
  408. # current sample interval or instance.
  409. # The time is measured in days and
  410. # fraction of days for absolute time,
  411. # and in relative units in case of relative time.
  412. #'end_td' : 'END_TD', # The time difference between the
  413. # start time of the sample
  414. # space time raster dataset and the
  415. # end time of the current sample interval.
  416. # The time is measured in days and
  417. # fraction of days for absolute time,
  418. # and in relative units in case of relative time.
  419. # The end_time() will be represented by null() in case of a time instance.
  420. 'start_doy' : 'START_DOY', # Day of year (doy) from the start time [1 - 366]
  421. 'start_dow' : 'START_DOW', # Day of week (dow) from the start time [1 - 7], the start of the week is Monday == 1
  422. 'start_year' : 'START_YEAR', # The year of the start time [0 - 9999]
  423. 'start_month' : 'START_MONTH', # The month of the start time [1 - 12]
  424. 'start_week' : 'START_WEEK', # Week of year of the start time [1 - 54]
  425. 'start_day' : 'START_DAY', # Day of month from the start time [1 - 31]
  426. 'start_hour' : 'START_HOUR', # The hour of the start time [0 - 23]
  427. 'start_minute': 'START_MINUTE', # The minute of the start time [0 - 59]
  428. 'start_second': 'START_SECOND', # The second of the start time [0 - 59]
  429. 'end_doy' : 'END_DOY', # Day of year (doy) from the end time [1 - 366]
  430. 'end_dow' : 'END_DOW', # Day of week (dow) from the end time [1 - 7], the start of the week is Monday == 1
  431. 'end_year' : 'END_YEAR', # The year of the end time [0 - 9999]
  432. 'end_month' : 'END_MONTH', # The month of the end time [1 - 12]
  433. 'end_week' : 'END_WEEK', # Week of year of the end time [1 - 54]
  434. 'end_day' : 'END_DAY', # Day of month from the start time [1 - 31]
  435. 'end_hour' : 'END_HOUR', # The hour of the end time [0 - 23]
  436. 'end_minute' : 'END_MINUTE', # The minute of the end time [0 - 59]
  437. 'end_second' : 'END_SECOND', # The second of the end time [0 - 59]
  438. }
  439. # This is the list of token names.
  440. tokens = (
  441. 'DATETIME',
  442. 'TIME',
  443. 'DATE',
  444. 'INT',
  445. 'FLOAT',
  446. 'LPAREN',
  447. 'RPAREN',
  448. 'COMMA',
  449. 'CEQUALS',
  450. 'EQUALS',
  451. 'UNEQUALS',
  452. 'LOWER',
  453. 'LOWER_EQUALS',
  454. 'GREATER',
  455. 'GREATER_EQUALS',
  456. 'HASH',
  457. 'OR',
  458. 'AND',
  459. 'T_SELECT_OPERATOR',
  460. 'T_HASH_OPERATOR',
  461. 'T_COMP_OPERATOR',
  462. 'T_REL_OPERATOR',
  463. 'T_SELECT',
  464. 'T_NOT_SELECT',
  465. 'NAME',
  466. 'QUOTE',
  467. )
  468. # Build the token list
  469. tokens = tokens + tuple(datetime_functions.values()) \
  470. + tuple(time_functions.values()) \
  471. + tuple(conditional_functions.values())
  472. # Regular expression rules for simple tokens
  473. t_T_SELECT_OPERATOR = r'\{[!]?[:][,]?[a-zA-Z\| ]*([,])?([lrudi]|left|right|union|disjoint|intersect)?\}'
  474. t_T_HASH_OPERATOR = r'\{[#][,]?[a-zA-Z\| ]*([,])?([lrudi]|left|right|union|disjoint|intersect)?\}'
  475. t_T_COMP_OPERATOR = r'\{(\|\||&&)[,][a-zA-Z\| ]*[,]?[\|&]?([,])?([lrudi]|left|right|union|disjoint|intersect)?\}'
  476. t_T_REL_OPERATOR = r'\{([a-zA-Z\| ])+\}'
  477. t_T_SELECT = r':'
  478. t_T_NOT_SELECT = r'!:'
  479. t_LPAREN = r'\('
  480. t_RPAREN = r'\)'
  481. t_COMMA = r','
  482. t_CEQUALS = r'=='
  483. t_EQUALS = r'='
  484. t_UNEQUALS = r'!='
  485. t_LOWER = r'<'
  486. t_LOWER_EQUALS = r'<='
  487. t_GREATER = r'>'
  488. t_GREATER_EQUALS = r'>='
  489. t_HASH = r'\#'
  490. t_OR = r'[\|]'
  491. t_AND = r'[&]'
  492. t_QUOTE = r'[\"\']'
  493. # These are the things that should be ignored.
  494. t_ignore = ' \t'
  495. # Read time string and convert it into a date object
  496. def t_DATETIME(self, t):
  497. 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)"'
  498. # t.value = int(t.value)
  499. return t
  500. # Read date string and convert it into a date object
  501. def t_DATE(self, t):
  502. r'"\d\d\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])"'
  503. # t.value = int(t.value)
  504. return t
  505. # Read time string and convert it into a date object
  506. def t_TIME(self, t):
  507. 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)"'
  508. # t.value = int(t.value)
  509. return t
  510. # Read in a float. This rule has to be done before the int rule.
  511. def t_FLOAT(self, t):
  512. r'-?\d+\.\d*(e-?\d+)?'
  513. t.value = float(t.value)
  514. return t
  515. # Read in an int.
  516. def t_INT(self, t):
  517. r'-?\d+'
  518. t.value = int(t.value)
  519. return t
  520. # Read in a list of maps.
  521. def t_LIST(self, t):
  522. r'[\[][.]*[\]]'
  523. t.value = list(t.value)
  524. return t
  525. # Ignore comments.
  526. # def t_comment(self, t):
  527. # r'^[#][^\n]*'
  528. # pass
  529. # Track line numbers.
  530. def t_newline(self, t):
  531. r'\n+'
  532. t.lineno += len(t.value)
  533. def t_NAME(self, t):
  534. r'[a-zA-Z_][a-zA-Z_0-9]*'
  535. self.temporal_symbol(t)
  536. return t
  537. # Parse symbols
  538. def temporal_symbol(self, t):
  539. # Check for reserved words
  540. if t.value in TemporalAlgebraLexer.time_functions.keys():
  541. t.type = TemporalAlgebraLexer.time_functions.get(t.value)
  542. elif t.value in TemporalAlgebraLexer.datetime_functions.keys():
  543. t.type = TemporalAlgebraLexer.datetime_functions.get(t.value)
  544. elif t.value in TemporalAlgebraLexer.conditional_functions.keys():
  545. t.type = TemporalAlgebraLexer.conditional_functions.get(t.value)
  546. else:
  547. t.type = 'NAME'
  548. return t
  549. # Handle errors.
  550. def t_error(self, t):
  551. raise SyntaxError("syntax error on line %d near '%s'" %
  552. (t.lineno, t.value))
  553. # Build the lexer
  554. def build(self,**kwargs):
  555. self.lexer = lex.lex(module=self, optimize=False, debug=False, **kwargs)
  556. # Just for testing
  557. def test(self,data):
  558. self.name_list = {}
  559. print(data)
  560. self.lexer.input(data)
  561. while True:
  562. tok = self.lexer.token()
  563. if not tok: break
  564. print tok
  565. ###############################################################################
  566. class GlobalTemporalVar(object):
  567. """ This class handles global temporal variable conditional expressions,
  568. like start_doy() == 3.
  569. The three parts of the statement are stored separately in
  570. tfunc (START_DOY), compop (==) and value (3).
  571. But also boolean values, time differences and relation operators for comparison in
  572. if-statements can be stored in this class.
  573. """
  574. def __init__(self):
  575. self.tfunc = None
  576. self.compop = None
  577. self.value = None
  578. self.boolean = None
  579. self.relationop = None
  580. self.topology = []
  581. self.td = None
  582. def get_type(self):
  583. if self.tfunc != None and self.compop != None and self.value != None:
  584. return("global")
  585. elif self.boolean != None:
  586. return("boolean")
  587. elif self.relationop != None and self.topology != []:
  588. return("operator")
  589. elif self.td != None:
  590. return("timediff")
  591. def get_type_value(self):
  592. typename = self.get_type()
  593. valuelist = []
  594. if typename == "global":
  595. valuelist = [self.tfunc, self.compop, self.value]
  596. elif typename == "operator":
  597. valuelist.append(self.topology)
  598. valuelist.append(self.relationop)
  599. elif typename == "boolean":
  600. valuelist = self.boolean
  601. elif typename == "timediff":
  602. valuelist.append(self.td)
  603. return(valuelist)
  604. def __str__(self):
  605. return str(self.tfunc) + str(self.compop) + str(self.value)
  606. ###############################################################################
  607. class FatalError(Exception):
  608. def __init__(self, msg):
  609. self.value = msg
  610. def __str__(self):
  611. return self.value
  612. ###############################################################################
  613. class TemporalAlgebraParser(object):
  614. """The temporal algebra class"""
  615. # Get the tokens from the lexer class
  616. tokens = TemporalAlgebraLexer.tokens
  617. # Setting equal precedence level for select and hash operations.
  618. precedence = (
  619. ('left', 'T_SELECT_OPERATOR', 'T_SELECT', 'T_NOT_SELECT'), # 1
  620. ('left', 'AND', 'OR', 'T_COMP_OPERATOR'), #2
  621. )
  622. def __init__(self, pid=None, run = True, debug = False, spatial = False,
  623. null = False, register_null = False, nprocs = 1):
  624. self.run = run
  625. self.debug = debug
  626. self.pid = pid
  627. # Intermediate vector map names
  628. self.names = {}
  629. # Count map names
  630. self.spatial = spatial
  631. self.null = null
  632. self.mapset = get_current_mapset()
  633. self.temporaltype = None
  634. self.msgr = get_tgis_message_interface()
  635. self.dbif = SQLDatabaseInterfaceConnection()
  636. self.dbif.connect()
  637. self.register_null = register_null
  638. self.removable_maps = {}
  639. self.m_mremove = pymod.Module('g.remove')
  640. self.m_copy = pymod.Module('g.copy')
  641. self.nprocs = nprocs
  642. self.use_granularity = False
  643. def __del__(self):
  644. if self.dbif.connected:
  645. self.dbif.close()
  646. def setup_common_granularity(self, expression, stdstype = 'strds', lexer = None):
  647. """Configure the temporal algebra to use the common granularity of all
  648. space time datasets in the expression to generate the map lists.
  649. This function will analyze the expression to detect space time datasets
  650. and computes the common granularity from all granularities.
  651. This granularity is then be used to generate the map lists. Hence, all
  652. maps from all STDS will have equidistant temporal extents. The only meaningful
  653. temporal relation is "equal".
  654. :param expression: The algebra expression to analyze
  655. :param lexer: The temporal algebra lexer (select, raster, voxel, vector) that should be used to
  656. parse the expression, default is TemporalAlgebraLexer
  657. :return: True if successful, False otherwise
  658. """
  659. l = lexer
  660. # Split the expression to ignore the left part
  661. expressions = expression.split("=")[1:]
  662. expression = " ".join(expressions)
  663. # Check if spatio-temporal operators are present in the expression
  664. if "{" in expression or "}" in expression:
  665. self.msgr.error(_("Spatio temporal operators are not supported in granularity algebra mode"))
  666. return False
  667. # detect all STDS
  668. if l is None:
  669. l = TemporalAlgebraLexer()
  670. l.build()
  671. l.lexer.input(expression)
  672. name_list = []
  673. tokens = []
  674. count = 0
  675. while True:
  676. tok = l.lexer.token()
  677. if not tok: break
  678. # Ignore map layer
  679. tokens.append(tok.type)
  680. ignore = False
  681. if count > 1:
  682. if tokens[count - 2] == "MAP" or tokens[count - 2] == "TMAP":
  683. ignore = True
  684. if tok.type == "NAME" and ignore == False:
  685. name_list.append(tok.value)
  686. count += 1
  687. grans = []
  688. ttypes = {}
  689. dbif, connected = init_dbif(self.dbif)
  690. for name in name_list:
  691. stds = open_old_stds(name, stdstype, dbif)
  692. # We need valid temporal topology
  693. if stds.check_temporal_topology() is False:
  694. self.msgr.error(_("All input space time datasets must have a valid temporal topology."))
  695. return False
  696. grans.append(stds.get_granularity())
  697. ttypes[stds.get_temporal_type()] = stds.get_temporal_type()
  698. # Only one temporal type is allowed
  699. if len(ttypes) > 1:
  700. self.msgr.error(_("All input space time datasets must have the same temporal type."))
  701. return False
  702. # Compute the common granularity
  703. if "absolute" in ttypes.keys():
  704. self.granularity = compute_common_absolute_time_granularity(grans)
  705. else:
  706. self.granularity = compute_common_relative_time_granularity(grans)
  707. self.use_granularity = True
  708. return True
  709. def parse(self, expression, stdstype = 'strds', maptype = 'rast', mapclass = RasterDataset,
  710. basename = None, overwrite=False):
  711. self.lexer = TemporalAlgebraLexer()
  712. self.lexer.build()
  713. self.parser = yacc.yacc(module=self, debug=self.debug)
  714. self.overwrite = overwrite
  715. self.count = 0
  716. self.stdstype = stdstype
  717. self.maptype = maptype
  718. self.mapclass = mapclass
  719. self.basename = basename
  720. self.expression = expression
  721. self.parser.parse(expression)
  722. def generate_map_name(self):
  723. """Generate an unique map name and register it in the objects map list
  724. The map names are unique between processes. Do not use the
  725. same object for map name generation in multiple threads.
  726. """
  727. self.count += 1
  728. if self.pid != None:
  729. pid = self.pid
  730. else:
  731. pid = os.getpid()
  732. name = "tmp_map_name_%i_%i"%(pid, self.count)
  733. self.names[name] = name
  734. return name
  735. def generate_new_map(self, base_map, bool_op = 'and', copy = True, rename = True,
  736. remove = False):
  737. """Generate a new map using the spatio-temporal extent of the base map
  738. :param base_map: This map is used to create the new map
  739. :param bool_op: The boolean operator specifying the spatial extent
  740. operation (intersection, union, disjoint union)
  741. :param copy: Specifies if the temporal extent of mapB should be
  742. copied to mapA
  743. :param rename: Specifies if the generated map get a random name or get
  744. the id from the base map.
  745. :param remove: Set this True if this map is an intermediate or empty map that should be removed
  746. :return: Map object
  747. """
  748. # Generate an intermediate name for the result map list.
  749. name = self.generate_map_name()
  750. # Check for mapset in given stds input.
  751. mapname = name + "@" + self.mapset
  752. # Create new map based on the related map list.
  753. map_new = base_map.get_new_instance(mapname)
  754. # Set initial map extend of new vector map.
  755. self.overlay_map_extent(map_new, base_map, bool_op = bool_op, copy = copy)
  756. if not rename:
  757. name = base_map.get_id()
  758. map_new.set_id(name)
  759. if remove is True:
  760. self.removable_maps[name] = map_new
  761. # Make sure to set the uid that is used in several dictionaries
  762. map_new.uid = name
  763. return map_new
  764. def overlay_map_extent(self, mapA, mapB, bool_op = None, temp_op = 'l',
  765. copy = False):
  766. """Compute the spatio-temporal extent of two topological related maps
  767. :param mapA: The first map
  768. :param mapB: The second maps
  769. :param bool_op: The boolean operator specifying the spatial extent
  770. operation (intersection, union, disjoint union)
  771. :param temp_op: The temporal operator specifying the temporal
  772. extent operation (intersection, union, disjoint union, right reference)
  773. Left reference is the default temporal extent behaviour.
  774. :param copy: Specifies if the temporal extent of mapB should be
  775. copied to mapA
  776. :return: 0 if there is no overlay
  777. """
  778. returncode = 1
  779. if copy:
  780. map_extent_temporal = mapB.get_temporal_extent()
  781. map_extent_spatial = mapB.get_spatial_extent()
  782. # Set initial map extend of new vector map.
  783. mapA.set_spatial_extent(map_extent_spatial)
  784. mapA.set_temporal_extent(map_extent_temporal)
  785. if "cmd_list" in dir(mapB):
  786. mapA.cmd_list = mapB.cmd_list
  787. if "condition_value" in dir(mapB):
  788. mapA.condition_value = mapB.condition_value
  789. else:
  790. # Calculate spatial extent for different overlay operations.
  791. if bool_op == 'and':
  792. overlay_ext = mapA.spatial_intersection(mapB)
  793. if overlay_ext != None:
  794. mapA.set_spatial_extent(overlay_ext)
  795. else:
  796. returncode = 0
  797. elif bool_op in ['or', 'xor']:
  798. overlay_ext = mapA.spatial_union(mapB)
  799. if overlay_ext != None:
  800. mapA.set_spatial_extent(overlay_ext)
  801. else:
  802. returncode = 0
  803. elif bool_op == 'disor':
  804. overlay_ext = mapA.spatial_disjoint_union(mapB)
  805. if overlay_ext != None:
  806. mapA.set_spatial_extent(overlay_ext)
  807. else:
  808. returncode = 0
  809. # Calculate temporal extent for different temporal operators.
  810. if temp_op == 'i':
  811. temp_ext = mapA.temporal_intersection(mapB)
  812. if temp_ext != None:
  813. mapA.set_temporal_extent(temp_ext)
  814. else:
  815. returncode = 0
  816. elif temp_op == 'u':
  817. temp_ext = mapA.temporal_union(mapB)
  818. if temp_ext != None:
  819. mapA.set_temporal_extent(temp_ext)
  820. else:
  821. returncode = 0
  822. elif temp_op == 'd':
  823. temp_ext = mapA.temporal_disjoint_union(mapB)
  824. if temp_ext != None:
  825. mapA.set_temporal_extent(temp_ext)
  826. else:
  827. returncode = 0
  828. elif temp_op == 'r':
  829. temp_ext = mapB.get_temporal_extent()
  830. if temp_ext != None:
  831. mapA.set_temporal_extent(temp_ext)
  832. else:
  833. returncode = 0
  834. return(returncode)
  835. def set_temporal_extent_list(self, maplist, topolist = ["EQUAL"], temporal = 'l' ):
  836. """ Change temporal extent of map list based on temporal relations to
  837. other map list and given temporal operator.
  838. :param maplist: List of map objects for which relations has been build
  839. correctely.
  840. :param topolist: List of strings of temporal relations.
  841. :param temporal: The temporal operator specifying the temporal
  842. extent operation (intersection, union, disjoint
  843. union, right reference, left reference).
  844. :return: Map list with specified temporal extent.
  845. """
  846. resultdict = {}
  847. for map_i in maplist:
  848. # Loop over temporal related maps and create overlay modules.
  849. tbrelations = map_i.get_temporal_relations()
  850. # Generate an intermediate map for the result map list.
  851. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and',
  852. copy = True, rename = True)
  853. # Combine temporal and spatial extents of intermediate map with related maps.
  854. for topo in topolist:
  855. if topo in tbrelations.keys():
  856. for map_j in (tbrelations[topo]):
  857. if temporal == 'r':
  858. # Generate an intermediate map for the result map list.
  859. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and',
  860. copy = True, rename = True)
  861. # Create overlayed map extent.
  862. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  863. temp_op = temporal)
  864. # Stop the loop if no temporal or spatial relationship exist.
  865. if returncode == 0:
  866. break
  867. # Append map to result map list.
  868. elif returncode == 1:
  869. print(map_new.get_id() + " " + str(map_new.get_temporal_extent_as_tuple()))
  870. print(map_new.condition_value)
  871. # print(map_new.cmd_list)
  872. # resultlist.append(map_new)
  873. resultdict[map_new.get_id()] = map_new
  874. # Create r.mapcalc expression string for the operation.
  875. #cmdstring = self.build_command_string(s_expr_a = map_new,
  876. # s_expr_b = map_j,
  877. # operator = function)
  878. # Conditional append of module command.
  879. #map_new.cmd_list = cmdstring
  880. if returncode == 0:
  881. break
  882. # Append map to result map list.
  883. #if returncode == 1:
  884. # resultlist.append(map_new)
  885. # Get sorted map objects as values from result dictionoary.
  886. resultlist = resultdict.values()
  887. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  888. return(resultlist)
  889. ######################### Temporal functions ##############################
  890. def remove_maps(self):
  891. """Removes empty or intermediate maps of different type.
  892. """
  893. map_names = {}
  894. map_names["raster"] = []
  895. map_names["raster3d"] = []
  896. map_names["vector"] = []
  897. if self.removable_maps:
  898. for map in self.removable_maps.values():
  899. map_names[map.get_type()].append(map.get_name())
  900. for key in map_names.keys():
  901. if map_names[key]:
  902. self.msgr.message(_("Removing un-needed or empty %s maps"%(key)))
  903. self._remove_maps(map_names[key], key)
  904. def _remove_maps(self, namelist, map_type):
  905. """Remove maps of specific type
  906. :param namelist: List of map names to be removed
  907. :param map_type: The type of the maps (rast, rast3d or vect)
  908. """
  909. max = 100
  910. chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
  911. for chunk in chunklist:
  912. stringlist = ",".join(chunk)
  913. # Transform the map type
  914. if map_type == "raster":
  915. map_type = "rast"
  916. if map_type == "raster3d":
  917. map_type = "rast3d"
  918. if map_type == "vector":
  919. map_type = "vect"
  920. if self.run:
  921. m = copy.deepcopy(self.m_mremove)
  922. m.inputs["type"].value = map_type
  923. m.inputs["name"].value = stringlist
  924. m.flags["f"].value = True
  925. print m.get_bash()
  926. m.run()
  927. def check_stds(self, input, clear = False, stds_type = None):
  928. """ Check if input space time dataset exist in database and return its map list.
  929. :param input: Name of space time data set as string or list of maps.
  930. :param clear: Reset the stored conditional values to empty list.
  931. :param stds_type: The type of the space time dataset to be opened, if not provided
  932. then self.stdstype will be used
  933. :return: List of maps.
  934. """
  935. if isinstance(input, str):
  936. # Check for mapset in given stds input.
  937. if input.find("@") >= 0:
  938. id_input = input
  939. else:
  940. id_input = input + "@" + self.mapset
  941. # Create empty spacetime dataset.
  942. if stds_type:
  943. stds = dataset_factory(stds_type, id_input)
  944. else:
  945. stds = dataset_factory(self.stdstype, id_input)
  946. # Check for occurence of space time dataset.
  947. if stds.is_in_db(dbif=self.dbif) == False:
  948. raise FatalError(_("Space time %s dataset <%s> not found") %
  949. (stds.get_new_map_instance(None).get_type(), id_input))
  950. else:
  951. # Select temporal dataset entry from database.
  952. stds.select(dbif=self.dbif)
  953. if self.use_granularity:
  954. # We create the maplist out of the map array from none-gap objects
  955. maplist = []
  956. map_array = stds.get_registered_maps_as_objects_by_granularity(gran=self.granularity, dbif=self.dbif)
  957. for entry in map_array:
  958. # Ignore gap objects
  959. if entry[0].get_id() is not None:
  960. maplist.append(entry[0])
  961. else:
  962. maplist = stds.get_registered_maps_as_objects(dbif=self.dbif)
  963. # Create map_value as empty list item.
  964. for map_i in maplist:
  965. if "map_value" not in dir(map_i):
  966. map_i.map_value = []
  967. if "condition_value" not in dir(map_i):
  968. map_i.condition_value = []
  969. # Set and check global temporal type variable and map.
  970. if map_i.is_time_absolute() and self.temporaltype == None:
  971. self.temporaltype = 'absolute'
  972. elif map_i.is_time_relative() and self.temporaltype == None:
  973. self.temporaltype = 'relative'
  974. elif map_i.is_time_absolute() and self.temporaltype == 'relative':
  975. self.msgr.fatal(_("Wrong temporal type of space time dataset <%s> \
  976. <%s> time is required") %
  977. (id_input, self.temporaltype))
  978. elif map_i.is_time_relative() and self.temporaltype == 'absolute':
  979. self.msgr.fatal(_("Wrong temporal type of space time dataset <%s> \
  980. <%s> time is required") %
  981. (id_input, self.temporaltype))
  982. elif isinstance(input, self.mapclass):
  983. # Check if the input is a single map and return it as list with one entry.
  984. maplist = [input]
  985. elif isinstance(input, list):
  986. maplist = input
  987. # Create map_value as empty list item.
  988. for map_i in maplist:
  989. if "map_value" not in dir(map_i):
  990. map_i.map_value = []
  991. elif clear:
  992. map_i.map_value = []
  993. if "condition_value" not in dir(map_i):
  994. map_i.condition_value = []
  995. elif clear:
  996. map_i.condition_value = []
  997. else:
  998. self.msgr.fatal(_("Wrong type of input"))
  999. # We generate a unique map id that will be used
  1000. # in the topology analysis, since the maplist can
  1001. # contain maps with equal map ids
  1002. for map in maplist:
  1003. map.uid = self.generate_map_name()
  1004. if self.debug:
  1005. print map.get_name(), map.uid, map.get_temporal_extent_as_tuple()
  1006. return(maplist)
  1007. def get_temporal_topo_list(self, maplistA, maplistB = None, topolist = ["EQUAL"],
  1008. assign_val = False, count_map = False, compare_bool = False,
  1009. compop = None, aggregate = None):
  1010. """Build temporal topology for two space time data sets, copy map objects
  1011. for given relation into map list.
  1012. :param maplistA: List of maps.
  1013. :param maplistB: List of maps.
  1014. :param topolist: List of strings of temporal relations.
  1015. :param assign_val: Boolean for assigning a boolean map value based on
  1016. the map_values from the compared map list by
  1017. topological relationships.
  1018. :param count_map: Boolean if the number of topological related maps
  1019. should be returned.
  1020. :param compare_bool: Boolean for comparing boolean map values based on
  1021. related map list and compariosn operator.
  1022. :param compop: Comparison operator, && or ||.
  1023. :param aggregate: Aggregation operator for relation map list, & or |.
  1024. :return: List of maps from maplistA that fulfil the topological relationships
  1025. to maplistB specified in topolist.
  1026. .. code-block:: python
  1027. # Example with two lists of maps
  1028. >>> import grass.temporal as tgis
  1029. >>> tgis.init(True)
  1030. >>> l = tgis.TemporalAlgebraParser()
  1031. >>> # Create two list of maps with equal time stamps
  1032. >>> mapsA = []
  1033. >>> mapsB = []
  1034. >>> for i in range(10):
  1035. ... idA = "a%i@B"%(i)
  1036. ... mapA = tgis.RasterDataset(idA)
  1037. ... mapA.uid = idA
  1038. ... idB = "b%i@B"%(i)
  1039. ... mapB = tgis.RasterDataset(idB)
  1040. ... mapB.uid = idB
  1041. ... check = mapA.set_relative_time(i, i + 1, "months")
  1042. ... check = mapB.set_relative_time(i, i + 1, "months")
  1043. ... mapsA.append(mapA)
  1044. ... mapsB.append(mapB)
  1045. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['EQUAL'])
  1046. >>> for map in resultlist:
  1047. ... if map.get_equal():
  1048. ... relations = map.get_equal()
  1049. ... print "Map %s has equal relation to map %s"%(map.get_name(),
  1050. ... relations[0].get_name())
  1051. Map a0 has equal relation to map b0
  1052. Map a1 has equal relation to map b1
  1053. Map a2 has equal relation to map b2
  1054. Map a3 has equal relation to map b3
  1055. Map a4 has equal relation to map b4
  1056. Map a5 has equal relation to map b5
  1057. Map a6 has equal relation to map b6
  1058. Map a7 has equal relation to map b7
  1059. Map a8 has equal relation to map b8
  1060. Map a9 has equal relation to map b9
  1061. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['DURING'])
  1062. >>> print(resultlist)
  1063. []
  1064. >>> # Create two list of maps with equal time stamps
  1065. >>> mapsA = []
  1066. >>> mapsB = []
  1067. >>> for i in range(10):
  1068. ... idA = "a%i@B"%(i)
  1069. ... mapA = tgis.RasterDataset(idA)
  1070. ... mapA.uid = idA
  1071. ... idB = "b%i@B"%(i)
  1072. ... mapB = tgis.RasterDataset(idB)
  1073. ... mapB.uid = idB
  1074. ... check = mapA.set_relative_time(i, i + 1, "months")
  1075. ... check = mapB.set_relative_time(i, i + 2, "months")
  1076. ... mapsA.append(mapA)
  1077. ... mapsB.append(mapB)
  1078. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['starts','during'])
  1079. >>> for map in resultlist:
  1080. ... if map.get_starts():
  1081. ... relations = map.get_starts()
  1082. ... print "Map %s has start relation to map %s"%(map.get_name(),
  1083. ... relations[0].get_name())
  1084. Map a0 has start relation to map b0
  1085. Map a1 has start relation to map b1
  1086. Map a2 has start relation to map b2
  1087. Map a3 has start relation to map b3
  1088. Map a4 has start relation to map b4
  1089. Map a5 has start relation to map b5
  1090. Map a6 has start relation to map b6
  1091. Map a7 has start relation to map b7
  1092. Map a8 has start relation to map b8
  1093. Map a9 has start relation to map b9
  1094. >>> for map in resultlist:
  1095. ... if map.get_during():
  1096. ... relations = map.get_during()
  1097. ... print "Map %s has during relation to map %s"%(map.get_name(),
  1098. ... relations[0].get_name())
  1099. Map a0 has during relation to map b0
  1100. Map a1 has during relation to map b0
  1101. Map a2 has during relation to map b1
  1102. Map a3 has during relation to map b2
  1103. Map a4 has during relation to map b3
  1104. Map a5 has during relation to map b4
  1105. Map a6 has during relation to map b5
  1106. Map a7 has during relation to map b6
  1107. Map a8 has during relation to map b7
  1108. Map a9 has during relation to map b8
  1109. >>> # Create two list of maps with equal time stamps and map_value method.
  1110. >>> mapsA = []
  1111. >>> mapsB = []
  1112. >>> for i in range(10):
  1113. ... idA = "a%i@B"%(i)
  1114. ... mapA = tgis.RasterDataset(idA)
  1115. ... mapA.uid = idA
  1116. ... idB = "b%i@B"%(i)
  1117. ... mapB = tgis.RasterDataset(idB)
  1118. ... mapB.uid = idB
  1119. ... check = mapA.set_relative_time(i, i + 1, "months")
  1120. ... check = mapB.set_relative_time(i, i + 1, "months")
  1121. ... mapB.map_value = True
  1122. ... mapsA.append(mapA)
  1123. ... mapsB.append(mapB)
  1124. >>> # Create two list of maps with equal time stamps
  1125. >>> mapsA = []
  1126. >>> mapsB = []
  1127. >>> for i in range(10):
  1128. ... idA = "a%i@B"%(i)
  1129. ... mapA = tgis.RasterDataset(idA)
  1130. ... mapA.uid = idA
  1131. ... mapA.map_value = True
  1132. ... idB = "b%i@B"%(i)
  1133. ... mapB = tgis.RasterDataset(idB)
  1134. ... mapB.uid = idB
  1135. ... mapB.map_value = False
  1136. ... check = mapA.set_absolute_time(datetime(2000,1,i+1),
  1137. ... datetime(2000,1,i + 2))
  1138. ... check = mapB.set_absolute_time(datetime(2000,1,i+6),
  1139. ... datetime(2000,1,i + 7))
  1140. ... mapsA.append(mapA)
  1141. ... mapsB.append(mapB)
  1142. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB)
  1143. >>> for map in resultlist:
  1144. ... print(map.get_id())
  1145. a5@B
  1146. a6@B
  1147. a7@B
  1148. a8@B
  1149. a9@B
  1150. >>> resultlist = l.get_temporal_topo_list(mapsA, mapsB, ['during'])
  1151. >>> for map in resultlist:
  1152. ... print(map.get_id())
  1153. """
  1154. topologylist = ["EQUAL", "FOLLOWS", "PRECEDES", "OVERLAPS", "OVERLAPPED", \
  1155. "DURING", "STARTS", "FINISHES", "CONTAINS", "STARTED", \
  1156. "FINISHED"]
  1157. complementdict = {"EQUAL": "EQUAL", "FOLLOWS" : "PRECEDES",
  1158. "PRECEDES" : "FOLLOWS", "OVERLAPS" : "OVERLAPPED",
  1159. "OVERLAPPED" : "OVERLAPS", "DURING" : "CONTAINS",
  1160. "CONTAINS" : "DURING", "STARTS" : "STARTED",
  1161. "STARTED" : "STARTS", "FINISHES" : "FINISHED",
  1162. "FINISHED" : "FINISHES"}
  1163. resultdict = {}
  1164. # Check if given temporal relation are valid.
  1165. for topo in topolist:
  1166. if topo.upper() not in topologylist:
  1167. raise SyntaxError("Unpermitted temporal relation name '" + topo + "'")
  1168. # Create temporal topology for maplistA to maplistB.
  1169. tb = SpatioTemporalTopologyBuilder()
  1170. # Dictionary with different spatial variables used for topology builder.
  1171. spatialdict = {'strds' : '2D', 'stvds' : '2D', 'str3ds' : '3D'}
  1172. # Build spatial temporal topology
  1173. if self.spatial:
  1174. tb.build(maplistA, maplistB, spatial = spatialdict[self.stdstype])
  1175. else:
  1176. tb.build(maplistA, maplistB)
  1177. # Iterate through maps in maplistA and search for relationships given
  1178. # in topolist.
  1179. for map_i in maplistA:
  1180. tbrelations = map_i.get_temporal_relations()
  1181. if assign_val:
  1182. self.assign_bool_value(map_i, tbrelations, topolist)
  1183. elif compare_bool:
  1184. self.compare_bool_value(map_i, tbrelations, compop, aggregate, topolist)
  1185. for topo in topolist:
  1186. if topo.upper() in tbrelations.keys():
  1187. if count_map:
  1188. relationmaplist = tbrelations[topo.upper()]
  1189. gvar = GlobalTemporalVar()
  1190. gvar.td = len(relationmaplist)
  1191. if "map_value" in dir(map_i):
  1192. map_i.map_value.append(gvar)
  1193. else:
  1194. map_i.map_value = gvar
  1195. # Use unique identifier, since map names may be equal
  1196. resultdict[map_i.uid] = map_i
  1197. resultlist = resultdict.values()
  1198. # Sort list of maps chronological.
  1199. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  1200. return(resultlist)
  1201. def assign_bool_value(self, map_i, tbrelations, topolist = ["EQUAL"]):
  1202. """ Function to assign boolean map value based on the map_values from the
  1203. compared map list by topological relationships.
  1204. :param map_i: Map object with temporal extent.
  1205. :param tbrelations: List of temporal relation to map_i.
  1206. :param topolist: List of strings for given temporal relations.
  1207. :return: Map object with conditional value that has been assigned by
  1208. relation maps that fulfil the topological relationships to
  1209. maplistB specified in topolist.
  1210. """
  1211. condition_value_list = []
  1212. for topo in topolist:
  1213. if topo.upper() in tbrelations.keys():
  1214. #relationmaplist = tbrelations[complementdict[topo.upper()]]
  1215. relationmaplist = tbrelations[topo.upper()]
  1216. for relationmap in relationmaplist:
  1217. for boolean in relationmap.condition_value:
  1218. if isinstance(boolean, bool):
  1219. condition_value_list.append(boolean)
  1220. if self.debug:
  1221. print(str(relationmap.get_temporal_extent_as_tuple()) + str(boolean))
  1222. if all(condition_value_list):
  1223. resultbool = True
  1224. else:
  1225. resultbool = False
  1226. map_i.condition_value = [resultbool]
  1227. return(resultbool)
  1228. def compare_bool_value(self, map_i, tbrelations, compop, aggregate, topolist = ["EQUAL"]):
  1229. """ Function to evaluate two map lists with boolean values by boolean
  1230. comparison operator.
  1231. :param map_i: Map object with temporal extent.
  1232. :param tbrelations: List of temporal relation to map_i.
  1233. :param topolist: List of strings for given temporal relations.
  1234. :param compop: Comparison operator, && or ||.
  1235. :param aggregate: Aggregation operator for relation map list, & or |.
  1236. :return: Map object with conditional value that has been evaluated by
  1237. comparison operators.
  1238. """
  1239. # Build conditional list with elements from related maps and given relation operator.
  1240. leftbool = map_i.condition_value[0]
  1241. condition_value_list = [leftbool]
  1242. count = 0
  1243. for topo in topolist:
  1244. if topo.upper() in tbrelations.keys():
  1245. relationmaplist = tbrelations[topo.upper()]
  1246. if count == 0:
  1247. condition_value_list.append(compop[0])
  1248. condition_value_list.append('(')
  1249. for relationmap in relationmaplist:
  1250. for boolean in relationmap.condition_value:
  1251. if isinstance(boolean, bool):
  1252. if count > 0:
  1253. condition_value_list.append(aggregate)
  1254. condition_value_list.append(boolean)
  1255. count = count + 1
  1256. if count > 0:
  1257. condition_value_list.append(')')
  1258. # Convert conditional list to concatenated string and evaluate booleans.
  1259. condition_value_str = ''.join(map(str, condition_value_list))
  1260. if self.debug:
  1261. print(condition_value_str)
  1262. resultbool = eval(condition_value_str)
  1263. if self.debug:
  1264. print(resultbool)
  1265. # Add boolean value to result list.
  1266. map_i.condition_value = [resultbool]
  1267. return(resultbool)
  1268. def eval_toperator(self, operator, optype = 'relation'):
  1269. """This function evaluates a string containing temporal operations.
  1270. :param operator: String of temporal operations, e.g. {!=,equal|during,l}.
  1271. :param optype: String to define operator type.
  1272. :return :List of temporal relations (equal, during), the given function
  1273. (!:) and the interval/instances (l).
  1274. .. code-block:: python
  1275. >>> import grass.temporal as tgis
  1276. >>> tgis.init()
  1277. >>> p = tgis.TemporalOperatorParser()
  1278. >>> operator = "{+, during}"
  1279. >>> p.parse(operator, optype = 'raster')
  1280. >>> print(p.relations, p.temporal, p.function)
  1281. (['during'], 'l', '+')
  1282. """
  1283. p = TemporalOperatorParser()
  1284. p.parse(operator, optype)
  1285. p.relations = [rel.upper() for rel in p.relations]
  1286. return(p.relations, p.temporal, p.function, p.aggregate)
  1287. def perform_temporal_selection(self, maplistA, maplistB, topolist = ["EQUAL"],
  1288. inverse = False, assign_val = False):
  1289. """This function performs temporal selection operation.
  1290. :param maplistA: List of maps representing the left side of a temporal
  1291. expression.
  1292. :param maplistB: List of maps representing the right side of a temporal
  1293. expression.
  1294. :param topolist: List of strings of temporal relations.
  1295. :param inverse: Boolean value that specifies if the selection should be
  1296. inverted.
  1297. :param assign_val: Boolean for assigning a boolean map value based on
  1298. the map_values from the compared map list by
  1299. topological relationships.
  1300. :return: List of selected maps from maplistA.
  1301. .. code-block:: python
  1302. >>> import grass.temporal as tgis
  1303. >>> tgis.init()
  1304. >>> l = tgis.TemporalAlgebraParser()
  1305. >>> # Example with two lists of maps
  1306. >>> # Create two list of maps with equal time stamps
  1307. >>> mapsA = []
  1308. >>> mapsB = []
  1309. >>> for i in range(10):
  1310. ... idA = "a%i@B"%(i)
  1311. ... mapA = tgis.RasterDataset(idA)
  1312. ... mapA.uid = idA
  1313. ... idB = "b%i@B"%(i)
  1314. ... mapB = tgis.RasterDataset(idB)
  1315. ... mapB.uid = idB
  1316. ... check = mapA.set_relative_time(i, i + 1, "months")
  1317. ... check = mapB.set_relative_time(i + 5, i + 6, "months")
  1318. ... mapsA.append(mapA)
  1319. ... mapsB.append(mapB)
  1320. >>> resultlist = l.perform_temporal_selection(mapsA, mapsB, ['EQUAL'],
  1321. ... False)
  1322. >>> for map in resultlist:
  1323. ... if map.get_equal():
  1324. ... relations = map.get_equal()
  1325. ... print "Map %s has equal relation to map %s"%(map.get_name(),
  1326. ... relations[0].get_name())
  1327. Map a5 has equal relation to map b0
  1328. Map a6 has equal relation to map b1
  1329. Map a7 has equal relation to map b2
  1330. Map a8 has equal relation to map b3
  1331. Map a9 has equal relation to map b4
  1332. >>> resultlist = l.perform_temporal_selection(mapsA, mapsB, ['EQUAL'],
  1333. ... True)
  1334. >>> for map in resultlist:
  1335. ... if not map.get_equal():
  1336. ... print "Map %s has no equal relation to mapset mapsB"%(map.get_name())
  1337. Map a0 has no equal relation to mapset mapsB
  1338. Map a1 has no equal relation to mapset mapsB
  1339. Map a2 has no equal relation to mapset mapsB
  1340. Map a3 has no equal relation to mapset mapsB
  1341. Map a4 has no equal relation to mapset mapsB
  1342. """
  1343. if not inverse:
  1344. topolist = self.get_temporal_topo_list(maplistA, maplistB, topolist,
  1345. assign_val = assign_val)
  1346. resultlist = topolist
  1347. else:
  1348. topolist = self.get_temporal_topo_list(maplistA, maplistB, topolist,
  1349. assign_val = assign_val)
  1350. resultlist = []
  1351. for map_i in maplistA:
  1352. if map_i not in topolist:
  1353. resultlist.append(map_i)
  1354. #if assign_val:
  1355. # if "condition_value" in dir(map_i):
  1356. # map_i.condition_value.append(False)
  1357. # Sort list of maps chronological.
  1358. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  1359. return(resultlist)
  1360. def set_granularity(self, maplistA, maplistB, toperator = 'l', topolist = ["EQUAL"]):
  1361. """This function sets the temporal extends of a list of maps based on
  1362. another map list.
  1363. :param maplistB: List of maps.
  1364. :param maplistB: List of maps.
  1365. :param toperator: String containing the temporal operator: l, r, d, i, u.
  1366. :param topolist: List of topological relations.
  1367. :return: List of maps with the new temporal extends.
  1368. .. code-block:: python
  1369. >>> import grass.temporal as tgis
  1370. >>> tgis.init()
  1371. >>> p = tgis.TemporalAlgebraParser()
  1372. >>> # Create two list of maps with equal time stamps
  1373. >>> mapsA = []
  1374. >>> mapsB = []
  1375. >>> for i in range(10):
  1376. ... idA = "a%i@B"%(i)
  1377. ... mapA = tgis.RasterDataset(idA)
  1378. ... mapA.uid = idA
  1379. ... idB = "b%i@B"%(i)
  1380. ... mapB = tgis.RasterDataset(idB)
  1381. ... mapB.uid = idB
  1382. ... check = mapA.set_relative_time(i, i + 1, "months")
  1383. ... check = mapB.set_relative_time(i*2, i*2 + 2, "months")
  1384. ... mapsA.append(mapA)
  1385. ... mapsB.append(mapB)
  1386. >>> resultlist = p.set_granularity(mapsA, mapsB, toperator = "u", topolist = ["during"])
  1387. >>> for map in resultlist:
  1388. ... start,end,unit = map.get_relative_time()
  1389. ... print(map.get_id() + ' - start: ' + str(start) + ' end: ' + str(end))
  1390. a1@B - start: 0 end: 2
  1391. a0@B - start: 0 end: 2
  1392. a3@B - start: 2 end: 4
  1393. a2@B - start: 2 end: 4
  1394. a5@B - start: 4 end: 6
  1395. a4@B - start: 4 end: 6
  1396. a7@B - start: 6 end: 8
  1397. a6@B - start: 6 end: 8
  1398. a9@B - start: 8 end: 10
  1399. a8@B - start: 8 end: 10
  1400. """
  1401. topologylist = ["EQUAL", "FOLLOWS", "PRECEDES", "OVERLAPS", "OVERLAPPED", \
  1402. "DURING", "STARTS", "FINISHES", "CONTAINS", "STARTED", \
  1403. "FINISHED"]
  1404. for topo in topolist:
  1405. if topo.upper() not in topologylist:
  1406. raise SyntaxError("Unpermitted temporal relation name '" + topo + "'")
  1407. # Create temporal topology for maplistA to maplistB.
  1408. tb = SpatioTemporalTopologyBuilder()
  1409. # Dictionary with different spatial variables used for topology builder.
  1410. spatialdict = {'strds' : '2D', 'stvds' : '2D', 'str3ds' : '3D'}
  1411. # Build spatial temporal topology for maplistB to maplistB.
  1412. if self.spatial:
  1413. tb.build(maplistA, maplistB, spatial = spatialdict[self.stdstype])
  1414. else:
  1415. tb.build(maplistA, maplistB)
  1416. resultdict = {}
  1417. # Iterate through maps in maplistA and search for relationships given
  1418. # in topolist.
  1419. for map_i in maplistA:
  1420. tbrelations = map_i.get_temporal_relations()
  1421. map_extent = map_i.get_temporal_extent()
  1422. map_start = map_extent.get_start_time()
  1423. map_end = map_extent.get_end_time()
  1424. unchanged = True
  1425. for topo in topolist:
  1426. if topo.upper() in tbrelations.keys():
  1427. relationmaplist = tbrelations[topo.upper()]
  1428. for relationmap in relationmaplist:
  1429. newextent = None
  1430. if toperator == "i":
  1431. newextent = map_i.temporal_intersection(relationmap)
  1432. elif toperator == "u":
  1433. newextent = map_i.temporal_union(relationmap)
  1434. elif toperator == "d":
  1435. newextent = map_i.temporal_disjoint_union(relationmap)
  1436. elif toperator == "l":
  1437. newextent = map_i.get_temporal_extent()
  1438. elif toperator == "r":
  1439. newextent = relationmap.get_temporal_extent()
  1440. if newextent != None:
  1441. start = newextent.get_start_time()
  1442. end = newextent.get_end_time()
  1443. #print(map_i.get_id() + ' - start: ' + str(start) + ' end: ' + str(end))
  1444. # Track changes in temporal extents of maps.
  1445. if map_start != start or map_end != end :
  1446. unchanged = False
  1447. if map_i.is_time_absolute():
  1448. map_i.set_absolute_time(start, end)
  1449. else:
  1450. relunit = map_i.get_relative_time_unit()
  1451. map_i.set_relative_time(int(start), int(end), relunit)
  1452. resultdict[map_i.get_id()] = map_i
  1453. else:
  1454. if self.debug:
  1455. print('Topologic relation: ' + topo.upper() + ' not found.')
  1456. resultdict[map_i.get_id()] = map_i
  1457. if unchanged == True:
  1458. if self.debug:
  1459. print('Leave temporal extend of result map: ' + map_i.get_map_id() + ' unchanged.')
  1460. resultlist = resultdict.values()
  1461. # Sort list of maps chronological.
  1462. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  1463. # Get relations to maplistB per map in A.
  1464. # Loop over all relations from list
  1465. # temporal extent = map.temporal_intersection(map)
  1466. # if temporal extend is None = delete map.
  1467. return(resultlist)
  1468. def get_temporal_func_dict(self, map):
  1469. """ This function creates a dictionary containing temporal functions for a
  1470. map dataset with time stamp.
  1471. :param map: Map object with time stamps.
  1472. :return: Dictionary with temporal functions for given input map.
  1473. .. code-block:: python
  1474. >>> import grass.temporal as tgis
  1475. >>> import datetime
  1476. >>> tgis.init()
  1477. >>> l = tgis.TemporalAlgebraParser()
  1478. >>> # Example with one list of maps
  1479. >>> # Create one list of maps with equal time stamps
  1480. >>> for i in range(1):
  1481. ... idA = "a%i@B"%(i)
  1482. ... mapA = tgis.RasterDataset(idA)
  1483. ... mapA.uid = idA
  1484. ... check = mapA.set_absolute_time(datetime.datetime(2000,1,1),
  1485. ... datetime.datetime(2000,10,1))
  1486. ... tfuncdict = l.get_temporal_func_dict(mapA)
  1487. >>> print(tfuncdict["START_YEAR"])
  1488. 2000
  1489. >>> print(tfuncdict["START_TIME"])
  1490. 00:00:00
  1491. >>> print(tfuncdict["START_DATE"])
  1492. 2000-01-01
  1493. >>> print(tfuncdict["START_DATETIME"])
  1494. 2000-01-01 00:00:00
  1495. """
  1496. tvardict = {"START_DOY" : None, "START_DOW" : None, "START_YEAR" : None,
  1497. "START_MONTH" : None, "START_WEEK" : None, "START_DAY" : None,
  1498. "START_HOUR" : None, "START_MINUTE" : None, "START_SECOND" : None,
  1499. "END_DOY" : None, "END_DOW" : None, "END_YEAR" : None,
  1500. "END_MONTH" : None, "END_WEEK" : None, "END_DAY" : None,
  1501. "END_HOUR" : None, "END_MINUTE" : None, "END_SECOND" : None,
  1502. "START_DATE" : None, "START_DATETIME" : None, "START_TIME" : None,
  1503. "END_DATE" : None, "END_DATETIME" : None, "END_TIME" : None}
  1504. # Compute temporal function only for maps with absolute time reference.
  1505. if map.is_time_absolute:
  1506. # Get datetime of map.
  1507. start, end = map.get_absolute_time()
  1508. # Compute DOY via time deltas.
  1509. yearstart = datetime(start.year, 1, 1)
  1510. yearend = datetime(end.year, 1, 1)
  1511. deltastart = start - yearstart
  1512. deltaend = end - yearend
  1513. # Evaluate datetime objects and fill in into dict.
  1514. tvardict["START_DOY"] = deltastart.days + 1
  1515. tvardict["START_DOW"] = start.isoweekday()
  1516. tvardict["START_YEAR"] = start.year
  1517. tvardict["START_MONTH"] = start.month
  1518. tvardict["START_WEEK"] = start.isocalendar()[1]
  1519. tvardict["START_DAY"] = start.day
  1520. tvardict["START_HOUR"] = start.hour
  1521. tvardict["START_MINUTE"] = start.minute
  1522. tvardict["START_SECOND"] = start.second
  1523. tvardict["END_DOY"] = deltaend.days + 1
  1524. tvardict["END_DOW"] = end.isoweekday()
  1525. tvardict["END_YEAR"] = end.year
  1526. tvardict["END_MONTH"] = end.month
  1527. tvardict["END_WEEK"] = end.isocalendar()[1]
  1528. tvardict["END_DAY"] = end.day
  1529. tvardict["END_HOUR"] = end.hour
  1530. tvardict["END_MINUTE"] = end.minute
  1531. tvardict["END_SECOND"] = end.second
  1532. tvardict["START_DATE"] = start.date()
  1533. tvardict["START_DATETIME"] = start
  1534. tvardict["START_TIME"] = start.time()
  1535. tvardict["END_DATE"] = end.date()
  1536. tvardict["END_DATETIME"] = end
  1537. tvardict["END_TIME"] = end.time()
  1538. if not map.is_time_absolute:
  1539. tvardict["START_DATE"] = start.date()
  1540. tvardict["START_DATETIME"] = start
  1541. tvardict["START_TIME"] = start.time()
  1542. tvardict["END_DATE"] = end.date()
  1543. tvardict["END_DATETIME"] = end
  1544. tvardict["END_TIME"] = end.time()
  1545. #core.fatal(_("The temporal functions for map <%s> only supported for absolute"\
  1546. # "time." % (str(map.get_id()))))
  1547. return(tvardict)
  1548. def eval_datetime_str(self, tfuncval, comp, value):
  1549. # Evaluate date object comparison expression.
  1550. if comp == "<":
  1551. boolname = eval(str(tfuncval < value))
  1552. elif comp == ">":
  1553. boolname = eval(str(tfuncval > value))
  1554. elif comp == "==":
  1555. boolname = eval(str(tfuncval == value))
  1556. elif comp == "<=":
  1557. boolname = eval(str(tfuncval <= value))
  1558. elif comp == ">=":
  1559. boolname = eval(str(tfuncval >= value))
  1560. elif comp == "!=":
  1561. boolname = eval(str(tfuncval != value))
  1562. return(boolname)
  1563. def eval_global_var(self, gvar, maplist):
  1564. """ This function evaluates a global variable expression for a map list.
  1565. For example: start_day() > 5 , end_month() == 2.
  1566. :param gvar: Object of type GlobalTemporalVar containing temporal.
  1567. :param maplist: List of map objects.
  1568. :return: List of maps from maplist with added conditional boolean values.
  1569. """
  1570. boollist = []
  1571. # Loop over maps of input map list.
  1572. for map_i in maplist:
  1573. # Get dictionary with temporal variables for the map.
  1574. tfuncdict = self.get_temporal_func_dict(map_i)
  1575. # Get value from global variable.
  1576. value = gvar.value
  1577. # Get comparison operator from global variable, like <, >, <=, >=, ==, !=
  1578. comp_op = gvar.compop
  1579. # Get temporal function name for global variable.
  1580. tfunc = gvar.tfunc.upper()
  1581. # Get value for function name from dictionary.
  1582. tfuncval = tfuncdict[tfunc]
  1583. # Check if value has to be transfered to datetime object for comparison.
  1584. if tfunc in ["START_DATE", "END_DATE"]:
  1585. timeobj = datetime.strptime(value.replace("\"",""), '%Y-%m-%d')
  1586. value = timeobj.date()
  1587. boolname = self.eval_datetime_str(tfuncval, comp_op, value)
  1588. elif tfunc in ["START_TIME", "END_TIME"]:
  1589. timeobj = datetime.strptime(value.replace("\"",""), '%H:%M:%S')
  1590. value = timeobj.time()
  1591. boolname = self.eval_datetime_str(tfuncval, comp_op, value)
  1592. elif tfunc in ["START_DATETIME", "END_DATETIME"]:
  1593. timeobj = datetime.strptime(value.replace("\"",""), '%Y-%m-%d %H:%M:%S')
  1594. value = timeobj
  1595. boolname = self.eval_datetime_str(tfuncval, comp_op, value)
  1596. else:
  1597. boolname = eval(str(tfuncval) + comp_op + str(value))
  1598. # Add conditional boolean value to the map.
  1599. if "condition_value" in dir(map_i):
  1600. map_i.condition_value.append(boolname)
  1601. else:
  1602. map_i.condition_value = boolname
  1603. return(maplist)
  1604. def eval_map_list(self, maplist ,thenlist, topolist = ["EQUAL"]):
  1605. """ This function transfers boolean values from temporal expression
  1606. from one map list to another by their topology. These boolean
  1607. values are added to the maps as condition_value.
  1608. :param maplist: List of map objects containing boolean map values.
  1609. :param thenlist: List of map objects where the boolean values
  1610. should be added.
  1611. :return: List of maps from thenlist with added conditional boolean values.
  1612. """
  1613. # Get topology of then statement map list in relation to the other maplist
  1614. # and assign boolean values of the maplist to the thenlist.
  1615. containlist = self.perform_temporal_selection(thenlist, maplist,
  1616. assign_val = True,
  1617. topolist = topolist)
  1618. # Inverse selection of maps from thenlist and assigning False values.
  1619. #excludelist = self.perform_temporal_selection(thenlist, maplist,
  1620. # assign_val = True,
  1621. # inverse = True,
  1622. # topolist = topolist)
  1623. # Combining the selection and inverse selection list.
  1624. resultlist = containlist# + excludelist
  1625. return(resultlist)
  1626. def build_condition_list(self, tvarexpr, thenlist, topolist = ["EQUAL"]):
  1627. """ This function evaluates temporal variable expressions of a conditional
  1628. expression in two steps.
  1629. At first it combines stepwise the single conditions by their relations with LALR.
  1630. In this prossess sub condition map lists will be created which will include
  1631. information of the underlying single conditions. Important: The temporal
  1632. relations between conditions are evaluated by implicit aggregation.
  1633. In the second step the aggregated condition map list will be compared with the
  1634. map list of conclusion statements by the given temporal relation.
  1635. The result is writen as 'condition_value' attribute to the resulting map objects.
  1636. These attribute consists of boolean expressions and operators which can be
  1637. evaluated with the eval_condition_list function.
  1638. [True, '||', False, '&&', True]
  1639. For example: td(A) == 1 && start_day() > 5 --> [True || False]
  1640. (for one map.condition_value in a then map list)
  1641. :param tvarexpr: List of GlobalTemporalVar objects and map lists.
  1642. The list is constructed by the TemporalAlgebraParser
  1643. in order of expression evaluation in the parser.
  1644. :param thenlist: Map list object of the conclusion statement.
  1645. It will be compared and evaluated by the conditions.
  1646. :param topolist: List of temporal relations between the conditions and the
  1647. conclusions.
  1648. :return: Map list with conditional values for all temporal expressions.
  1649. """
  1650. # Evaluate the temporal variable expression and compute the temporal combination
  1651. # of conditions.
  1652. # Check if the input expression is a valid single global variable.
  1653. if isinstance(tvarexpr, GlobalTemporalVar) and tvarexpr.get_type() == "global" :
  1654. # Use method eval_global_var to evaluate expression.
  1655. resultlist = self.eval_global_var(tvarexpr, thenlist)
  1656. # Check if a given list is a list of maps.
  1657. elif all([issubclass(type(ele), AbstractMapDataset) for ele in tvarexpr]):
  1658. # Use method eval_map_list to evaluate map_list in comparison to thenlist.
  1659. resultlist = self.eval_map_list(tvarexpr, thenlist, topolist)
  1660. elif len(tvarexpr) % 2 != 0:
  1661. # Define variables for map list comparisons.
  1662. left_obj = []
  1663. operator = []
  1664. right_obj =[]
  1665. count = 0
  1666. #self.msgr.fatal("Condition list is not complete. Elements missing")
  1667. for iter in range(len(tvarexpr)):
  1668. expr = tvarexpr[iter]
  1669. operator = tvarexpr[iter +1]
  1670. relexpr = tvarexpr[iter +2]
  1671. if all([issubclass(type(ele), list) for ele in [expr, relexpr]]):
  1672. resultlist = self.get_temporal_topo_list(expr, relexpr)
  1673. # Loop through the list, search for map lists or global variables.
  1674. for expr in tvarexpr:
  1675. if isinstance(expr, list):
  1676. if all([issubclass(type(ele), AbstractMapDataset) for ele in expr]):
  1677. # Use method eval_map_list to evaluate map_list
  1678. resultlist = self.eval_map_list(expr, thenlist, topolist)
  1679. else:
  1680. # Recursive function call to look into nested list elements.
  1681. self.build_condition_list(expr, thenlist)
  1682. elif isinstance(expr, GlobalTemporalVar):
  1683. # Use according functions for different global variable types.
  1684. if expr.get_type() == "operator":
  1685. if all(["condition_value" in dir(map_i) for map_i in thenlist]):
  1686. # Add operator string to the condition list.
  1687. [map_i.condition_value.extend(expr.get_type_value()) for map_i in thenlist]
  1688. if expr.get_type() == "global":
  1689. # Use method eval_global_var to evaluate expression.
  1690. resultlist = self.eval_global_var(expr, thenlist)
  1691. # Sort resulting list of maps chronological.
  1692. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  1693. return(resultlist)
  1694. def eval_condition_list(self, maplist, inverse = False):
  1695. """ This function evaluates conditional values of a map list.
  1696. A recursive function is used to evaluate comparison statements
  1697. from left to right in the given conditional list.
  1698. For example:
  1699. - [True, '||', False, '&&', True] -> True
  1700. - [True, '||', False, '&&', False] -> False
  1701. - [True, '&&', False, '&&', True] -> False
  1702. - [False, '||', True, '||', False] -> True
  1703. - [False, '&&', True, '&&', True] -> False
  1704. - [True, '&&', True, '&&', True] -> True
  1705. - [True, '&&', True] -> True
  1706. - [True, '&&', False] -> False
  1707. - [False, '||', True] -> True
  1708. :param tvarexpr: List of GlobalTemporalVar objects and map lists.
  1709. The list is constructed by the TemporalAlgebraParser
  1710. in order of expression evaluation in the parser.
  1711. :return: Map list with conditional values for all temporal expressions.
  1712. """
  1713. def recurse_compare(conditionlist):
  1714. for ele in conditionlist:
  1715. if ele == '||':
  1716. ele_index = conditionlist.index(ele)
  1717. topolist = conditionlist.pop(ele_index -1)
  1718. right = conditionlist.pop(ele_index)
  1719. left = conditionlist.pop(ele_index - 2)
  1720. if any([left, right]):
  1721. result = True
  1722. else:
  1723. result = False
  1724. conditionlist[ele_index - 2] = result
  1725. recurse_compare(conditionlist)
  1726. if ele == '&&':
  1727. ele_index = conditionlist.index(ele)
  1728. topolist = conditionlist.pop(ele_index -1)
  1729. right = conditionlist.pop(ele_index)
  1730. left = conditionlist.pop(ele_index - 2)
  1731. if all([left, right]):
  1732. result = True
  1733. else:
  1734. result = False
  1735. conditionlist[ele_index - 2] = result
  1736. recurse_compare(conditionlist)
  1737. resultlist = conditionlist
  1738. return(resultlist)
  1739. resultlist = []
  1740. inverselist = []
  1741. # Loop through map list and evaluate conditional values.
  1742. for map_i in maplist:
  1743. if "condition_value" in dir(map_i):
  1744. # Get condition values from map object.
  1745. conditionlist = map_i.condition_value
  1746. # Evaluate conditions in list with recursive function.
  1747. resultbool = recurse_compare(conditionlist)
  1748. # Set conditional value of map to resulting boolean.
  1749. map_i.condition_value = resultbool
  1750. # Add all maps that fulfill the conditions to result list.
  1751. if resultbool[0]:
  1752. resultlist.append(map_i)
  1753. if self.debug:
  1754. print(map_i.get_map_id() + ' ' + str(map_i.condition_value))
  1755. else:
  1756. inverselist.append(map_i)
  1757. if inverse:
  1758. return(inverselist)
  1759. else:
  1760. return(resultlist)
  1761. ###########################################################################
  1762. def p_statement_assign(self, t):
  1763. # The expression should always return a list of maps.
  1764. """
  1765. statement : stds EQUALS expr
  1766. """
  1767. if self.run:
  1768. dbif, connected = init_dbif(self.dbif)
  1769. if isinstance(t[3], list):
  1770. num = len(t[3])
  1771. count = 0
  1772. register_list = []
  1773. if num > 0:
  1774. process_queue = pymod.ParallelModuleQueue(int(self.nprocs))
  1775. for map_i in t[3]:
  1776. # Test if temporal extents have been changed by temporal
  1777. # relation operators (i|r).
  1778. map_i_extent = map_i.get_temporal_extent_as_tuple()
  1779. map_test = map_i.get_new_instance(map_i.get_id())
  1780. map_test.select(dbif)
  1781. map_test_extent = map_test.get_temporal_extent_as_tuple()
  1782. if map_test_extent != map_i_extent:
  1783. # Create new map with basename
  1784. newident = self.basename + "_" + str(count)
  1785. map_result = map_i.get_new_instance(newident + "@" + self.mapset)
  1786. if map_test.map_exists() and self.overwrite == False:
  1787. self.msgr.fatal("Error raster maps with basename %s exist. Use --o flag to overwrite existing file" \
  1788. %(mapname))
  1789. map_result.set_temporal_extent(map_i.get_temporal_extent())
  1790. map_result.set_spatial_extent(map_i.get_spatial_extent())
  1791. # Attention we attach a new attribute
  1792. map_result.is_new = True
  1793. register_list.append(map_result)
  1794. # Copy the map
  1795. if map_i.get_type() == 'raster':
  1796. m = copy.deepcopy(self.m_copy)
  1797. m.inputs["rast"].value = map_i.get_id(), newident
  1798. m.flags["overwrite"].value = self.overwrite
  1799. process_queue.put(m)
  1800. elif map_i.get_type() == 'raster3d':
  1801. m = copy.deepcopy(self.m_copy)
  1802. m.inputs["rast3d"].value = map_i.get_id(), newident
  1803. m.flags["overwrite"].value = self.overwrite
  1804. process_queue.put(m)
  1805. elif map_i.get_type() == 'vector':
  1806. m = copy.deepcopy(self.m_copy)
  1807. m.inputs["vect"].value = map_i.get_id(), newident
  1808. m.flags["overwrite"].value = self.overwrite
  1809. process_queue.put(m)
  1810. else:
  1811. register_list.append(map_i)
  1812. count += 1
  1813. # Wait for running processes
  1814. process_queue.wait()
  1815. # Open connection to temporal database.
  1816. # Create result space time dataset.
  1817. resultstds = open_new_stds(t[1], self.stdstype, \
  1818. 'absolute', t[1], t[1], \
  1819. 'mean', self.dbif, \
  1820. overwrite = self.overwrite)
  1821. for map_i in register_list:
  1822. # Get meta data from grass database.
  1823. map_i.load()
  1824. # Check if temporal extents have changed and a new map was created
  1825. if hasattr(map_i, "is_new") is True:
  1826. # Do not register empty maps if not required
  1827. # In case of a null map continue, do not register null maps
  1828. if map_i.metadata.get_min() is None and \
  1829. map_i.metadata.get_max() is None:
  1830. if not self.register_null:
  1831. self.removable_maps[map_i.get_name()] = map_i
  1832. continue
  1833. if map_i.is_in_db(dbif) and self.overwrite:
  1834. # Update map in temporal database.
  1835. map_i.update_all(dbif)
  1836. elif map_i.is_in_db(dbif) and self.overwrite == False:
  1837. # Raise error if map exists and no overwrite flag is given.
  1838. self.msgr.fatal("Error map %s exist in temporal database. Use overwrite flag. : \n%s" \
  1839. %(map_i.get_map_id(), cmd.popen.stderr))
  1840. else:
  1841. # Insert map into temporal database.
  1842. map_i.insert(dbif)
  1843. # Register map in result space time dataset.
  1844. success = resultstds.register_map(map_i, dbif)
  1845. resultstds.update_from_registered_maps(dbif)
  1846. elif num == 0:
  1847. self.msgr.warning('Empty result space time dataset. No map has \
  1848. been registered in %s' %(t[1] ))
  1849. # Open connection to temporal database.
  1850. # Create result space time dataset.
  1851. resultstds = open_new_stds(t[1], self.stdstype, \
  1852. 'absolute', t[1], t[1], \
  1853. 'mean', dbif, \
  1854. overwrite = self.overwrite)
  1855. if connected:
  1856. dbif.close()
  1857. t[0] = t[3]
  1858. else:
  1859. t[0] = t[3]
  1860. if self.debug:
  1861. print t[1], "=", t[3]
  1862. def p_stds_1(self, t):
  1863. # Definition of a space time dataset
  1864. """
  1865. stds : NAME
  1866. """
  1867. t[0] = t[1]
  1868. def p_paren_expr(self, t):
  1869. """ expr : LPAREN expr RPAREN"""
  1870. t[0] = t[2]
  1871. def p_number(self,t):
  1872. """number : INT
  1873. | FLOAT
  1874. """
  1875. t[0] = t[1]
  1876. def p_expr_tmap_function(self, t):
  1877. # Add a single map.
  1878. # Only the spatial extent of the map is evaluated.
  1879. # Temporal extent is not existing.
  1880. # Examples:
  1881. # R = tmap(A)
  1882. """
  1883. expr : TMAP LPAREN stds RPAREN
  1884. """
  1885. if self.run:
  1886. # Check input map.
  1887. input = t[3]
  1888. if not isinstance(input, list):
  1889. # Check for mapset in given stds input.
  1890. if input.find("@") >= 0:
  1891. id_input = input
  1892. else:
  1893. id_input = input + "@" + self.mapset
  1894. # Create empty map dataset.
  1895. map_i = dataset_factory(self.maptype, id_input)
  1896. # Check for occurence of space time dataset.
  1897. if map_i.map_exists() == False:
  1898. raise FatalError(_("%s map <%s> not found in GRASS spatial database") %
  1899. (map_i.get_type(), id_input))
  1900. else:
  1901. # Select dataset entry from database.
  1902. map_i.select(dbif=self.dbif)
  1903. else:
  1904. raise FatalError(_("Wrong map type <%s> . TMAP only supports single maps that are registered in the temporal GRASS database") %
  1905. (map_i.get_type()))
  1906. # Return map object.
  1907. t[0] = [map_i]
  1908. else:
  1909. t[0] = "tmap(", t[3] , ")"
  1910. if self.debug:
  1911. print "tmap(", t[3] , ")"
  1912. def p_t_hash(self,t):
  1913. """
  1914. t_hash_var : stds HASH stds
  1915. """
  1916. if self.run:
  1917. maplistA = self.check_stds(t[1])
  1918. maplistB = self.check_stds(t[3])
  1919. resultlist = self.get_temporal_topo_list(maplistA, maplistB,
  1920. count_map = True)
  1921. t[0] = resultlist
  1922. def p_t_hash2(self,t):
  1923. """
  1924. t_hash_var : stds T_HASH_OPERATOR stds
  1925. """
  1926. if self.run:
  1927. maplistA = self.check_stds(t[1])
  1928. maplistB = self.check_stds(t[3])
  1929. topolist = self.eval_toperator(t[2], optype = 'hash')[0]
  1930. resultlist = self.get_temporal_topo_list(maplistA, maplistB, topolist,
  1931. count_map = True)
  1932. t[0] = resultlist
  1933. def p_t_td_var(self, t):
  1934. """
  1935. t_td_var : TD LPAREN stds RPAREN
  1936. | TD LPAREN expr RPAREN
  1937. """
  1938. if self.run:
  1939. maplist = self.check_stds(t[3])
  1940. for map_i in maplist:
  1941. if map_i.is_time_absolute:
  1942. start, end = map_i.get_absolute_time()
  1943. if end != None:
  1944. td = time_delta_to_relative_time(end - start)
  1945. else:
  1946. start, end, unit = current.get_relative_time()
  1947. if end != None:
  1948. td = end - start
  1949. if "map_value" in dir(map_i):
  1950. gvar = GlobalTemporalVar()
  1951. gvar.td = td
  1952. map_i.map_value.append(gvar)
  1953. else:
  1954. map_i.map_value = gvar
  1955. t[0] = maplist
  1956. else:
  1957. t[0] = "td(" + str(t[3]) + ")"
  1958. if self.debug:
  1959. print "td(" + str(t[3]) + ")"
  1960. def p_t_time_var(self, t):
  1961. # Temporal variables that return a double or integer value
  1962. """
  1963. t_var : START_DOY
  1964. | START_DOW
  1965. | START_YEAR
  1966. | START_MONTH
  1967. | START_WEEK
  1968. | START_DAY
  1969. | START_HOUR
  1970. | START_MINUTE
  1971. | START_SECOND
  1972. | END_DOY
  1973. | END_DOW
  1974. | END_YEAR
  1975. | END_MONTH
  1976. | END_WEEK
  1977. | END_DAY
  1978. | END_HOUR
  1979. | END_MINUTE
  1980. | END_SECOND
  1981. """
  1982. t[0] = t[1]
  1983. def p_compare_op(self, t):
  1984. # Compare operators that are supported for temporal expressions
  1985. """
  1986. comp_op : CEQUALS
  1987. | UNEQUALS
  1988. | LOWER
  1989. | LOWER_EQUALS
  1990. | GREATER
  1991. | GREATER_EQUALS
  1992. """
  1993. t[0] = t[1]
  1994. def p_t_var_expr_td_hash(self, t):
  1995. # Examples:
  1996. # A # B == 2
  1997. # td(A) < 31
  1998. """
  1999. t_var_expr : t_td_var comp_op number
  2000. | t_hash_var comp_op number
  2001. """
  2002. if self.run:
  2003. maplist = self.check_stds(t[1])
  2004. comp_op = t[2]
  2005. value = str(t[3])
  2006. for map_i in maplist:
  2007. # Evaluate time diferences and hash operator statements for each map.
  2008. try:
  2009. td = map_i.map_value[0].td
  2010. boolname = eval(str(td) + comp_op + value)
  2011. # Add conditional boolean value to the map.
  2012. if "condition_value" in dir(map_i):
  2013. map_i.condition_value.append(boolname)
  2014. else:
  2015. map_i.condition_value = boolname
  2016. except:
  2017. self.msgr.fatal("Error: the given expression does not contain a correct time difference object.")
  2018. t[0] = maplist
  2019. if self.debug:
  2020. print t[1], t[2], t[3]
  2021. def p_t_var_expr_number(self, t):
  2022. # Examples:
  2023. # start_month(A) > 2
  2024. # start_day(B) < 14
  2025. # start_day(B) < start_month(A)
  2026. """
  2027. t_var_expr : t_var LPAREN stds RPAREN comp_op number
  2028. | t_var LPAREN expr RPAREN comp_op number
  2029. """
  2030. # TODO: Implement comparison operator for map lists.
  2031. #| t_var LPAREN stds RPAREN comp_op t_var LPAREN stds RPAREN
  2032. #| t_var LPAREN stds RPAREN comp_op t_var LPAREN expr RPAREN
  2033. #| t_var LPAREN expr RPAREN comp_op t_var LPAREN expr RPAREN
  2034. #| t_var LPAREN expr RPAREN comp_op t_var LPAREN stds RPAREN
  2035. # TODO: Implement statement in backward direction:
  2036. # number comp_op t_var LPAREN stds RPAREN
  2037. if self.run:
  2038. maplist = self.check_stds(t[3])
  2039. gvar = GlobalTemporalVar()
  2040. gvar.tfunc = t[1]
  2041. gvar.compop = t[5]
  2042. gvar.value = t[6]
  2043. # Evaluate temporal variable for given maplist.
  2044. resultlist = self.eval_global_var(gvar, maplist)
  2045. t[0] = resultlist
  2046. if self.debug:
  2047. print t[1], t[3], t[5], t[6]
  2048. def p_t_var_expr_time(self, t):
  2049. # Examples:
  2050. # start_time(A) == "12:30:00"
  2051. # start_date(B) <= "2001-01-01"
  2052. # start_datetime(C) > "2001-01-01 12:30:00"
  2053. # TODO: Implement statement in backward direction:
  2054. # TIME comp_op START_TIME LPAREN stds RPAREN
  2055. """
  2056. t_var_expr : START_TIME LPAREN stds RPAREN comp_op TIME
  2057. | START_DATE LPAREN stds RPAREN comp_op DATE
  2058. | START_DATETIME LPAREN stds RPAREN comp_op DATETIME
  2059. | END_TIME LPAREN stds RPAREN comp_op TIME
  2060. | END_DATE LPAREN stds RPAREN comp_op DATE
  2061. | END_DATETIME LPAREN stds RPAREN comp_op DATETIME
  2062. | START_TIME LPAREN expr RPAREN comp_op TIME
  2063. | START_DATE LPAREN expr RPAREN comp_op DATE
  2064. | START_DATETIME LPAREN expr RPAREN comp_op DATETIME
  2065. | END_TIME LPAREN expr RPAREN comp_op TIME
  2066. | END_DATE LPAREN expr RPAREN comp_op DATE
  2067. | END_DATETIME LPAREN expr RPAREN comp_op DATETIME
  2068. """
  2069. if self.run:
  2070. # Check input maplist.
  2071. maplist = self.check_stds(t[3])
  2072. # Build global temporal variable.
  2073. gvar = GlobalTemporalVar()
  2074. gvar.tfunc = t[1]
  2075. gvar.compop = t[5]
  2076. gvar.value = t[6]
  2077. # Evaluate temporal variable for given maplist.
  2078. resultlist = self.eval_global_var(gvar, maplist)
  2079. t[0] = resultlist
  2080. if self.debug:
  2081. print t[1], t[3], t[5], t[6]
  2082. def p_t_var_expr_comp(self, t):
  2083. """
  2084. t_var_expr : t_var_expr AND AND t_var_expr
  2085. | t_var_expr OR OR t_var_expr
  2086. """
  2087. if self.run:
  2088. # Check input maplists and operators.
  2089. tvarexprA = t[1]
  2090. tvarexprB = t[4]
  2091. relations = ["EQUAL"]
  2092. temporal = "l"
  2093. function = t[2] + t[3]
  2094. aggregate = t[2]
  2095. # Build conditional values based on topological relationships.
  2096. complist = self.get_temporal_topo_list(tvarexprA, tvarexprB, topolist = relations,
  2097. compare_bool = True, compop = function[0], aggregate = aggregate)
  2098. # Set temporal extent based on topological relationships.
  2099. resultlist = self.set_temporal_extent_list(complist, topolist = relations,
  2100. temporal = temporal)
  2101. t[0] = resultlist
  2102. if self.debug:
  2103. print t[1], t[2] + t[3], t[4]
  2104. def p_t_var_expr_comp_op(self, t):
  2105. """
  2106. t_var_expr : t_var_expr T_COMP_OPERATOR t_var_expr
  2107. """
  2108. if self.run:
  2109. tvarexprA = t[1]
  2110. tvarexprB = t[3]
  2111. # Evaluate temporal comparison operator.
  2112. relations, temporal, function, aggregate = self.eval_toperator(t[2], optype = 'boolean')
  2113. # Build conditional values based on topological relationships.
  2114. complist = self.get_temporal_topo_list(tvarexprA, tvarexprB, topolist = relations,
  2115. compare_bool = True, compop = function[0], aggregate = aggregate)
  2116. # Set temporal extent based on topological relationships.
  2117. resultlist = self.set_temporal_extent_list(complist, topolist = relations,
  2118. temporal = temporal)
  2119. t[0] = resultlist
  2120. if self.debug:
  2121. print t[1], t[2], t[3]
  2122. def p_expr_t_select(self, t):
  2123. # Temporal equal selection
  2124. # The temporal topology relation equals is implicit
  2125. # Examples:
  2126. # A : B # Select the part of A that is temporally equal B
  2127. """
  2128. expr : stds T_SELECT stds
  2129. | expr T_SELECT stds
  2130. | stds T_SELECT expr
  2131. | expr T_SELECT expr
  2132. """
  2133. if self.run:
  2134. # Setup database connection.
  2135. # Check input stds.
  2136. maplistA = self.check_stds(t[1])
  2137. maplistB = self.check_stds(t[3])
  2138. # Perform selection.
  2139. selectlist = self.perform_temporal_selection(maplistA, maplistB)
  2140. # Return map list.
  2141. t[0] = selectlist
  2142. else:
  2143. t[0] = t[1] , "*"
  2144. if self.debug:
  2145. print str(t[1]), "* = ", t[1], t[2], t[3]
  2146. def p_expr_t_not_select(self, t):
  2147. # Temporal equal selection
  2148. # The temporal topology relation equals is implicit
  2149. # Examples:
  2150. # A !: B # Select the part of A that is temporally unequal to B
  2151. """
  2152. expr : stds T_NOT_SELECT stds
  2153. | expr T_NOT_SELECT stds
  2154. | stds T_NOT_SELECT expr
  2155. | expr T_NOT_SELECT expr
  2156. """
  2157. if self.run:
  2158. # Check input stds.
  2159. maplistA = self.check_stds(t[1])
  2160. maplistB = self.check_stds(t[3])
  2161. # Perform negative selection.
  2162. selectlist = self.perform_temporal_selection(maplistA, maplistB,
  2163. inverse = True)
  2164. # Return map list.
  2165. t[0] = selectlist
  2166. else:
  2167. t[0] = t[1] + "*"
  2168. if self.debug:
  2169. print t[1] + "* = ", t[1], t[2], t[3]
  2170. def p_expr_t_select_operator(self, t):
  2171. # Temporal equal selection
  2172. # The temporal topology relation equals is implicit
  2173. # Examples:
  2174. # A {!:} B # Select the part of A that is temporally unequal to B
  2175. # A {:} B # Select the part of A that is temporally equal B
  2176. # A {equals, !:} B # Select the part of A that is temporally unequal to B
  2177. # A {during, !:} B # Select the part of A that is temporally not during B
  2178. # A {overlaps, :} B # Select the part of A that temporally overlaps B
  2179. # A {overlaps|equals, :} B # Select the part of A that temporally overlaps or equals B
  2180. """
  2181. expr : stds T_SELECT_OPERATOR stds
  2182. | expr T_SELECT_OPERATOR stds
  2183. | stds T_SELECT_OPERATOR expr
  2184. | expr T_SELECT_OPERATOR expr
  2185. """
  2186. if self.run:
  2187. # Check input stds.
  2188. maplistA = self.check_stds(t[1])
  2189. maplistB = self.check_stds(t[3])
  2190. # Evaluate temporal operator.
  2191. operators = self.eval_toperator(t[2], optype = 'select')
  2192. # Check for negative selection.
  2193. if operators[2] == "!:":
  2194. negation = True
  2195. else:
  2196. negation = False
  2197. # Perform selection.
  2198. selectlist = self.perform_temporal_selection(maplistA, maplistB,
  2199. topolist = operators[0], inverse = negation)
  2200. selectlist = self.set_granularity(selectlist, maplistB, operators[1],
  2201. operators[0])
  2202. # Return map list.
  2203. t[0] = selectlist
  2204. else:
  2205. t[0] = t[1] + "*"
  2206. if self.debug:
  2207. print t[1] + "* = ", t[1], t[2], t[3]
  2208. def p_expr_condition_if(self, t):
  2209. # Examples
  2210. # if( start_date() < "2005-06-01", A:B)
  2211. """
  2212. expr : IF LPAREN t_var_expr COMMA stds RPAREN
  2213. | IF LPAREN t_var_expr COMMA expr RPAREN
  2214. """
  2215. if self.run:
  2216. # Get stds/map list of then statement.
  2217. thenlist = self.check_stds(t[5])
  2218. # Get temporal conditional statement.
  2219. tvarexpr = t[3]
  2220. thencond = self.build_condition_list(tvarexpr, thenlist)
  2221. thenresult = self.eval_condition_list(thencond)
  2222. # Clear the map and conditional values of the map list.
  2223. resultlist = self.check_stds(thenresult, clear = True)
  2224. # Return resulting map list.
  2225. t[0] = resultlist
  2226. else:
  2227. t[0] = t[5] + "*"
  2228. if self.debug:
  2229. print str(t[5]) + "* = ", "if condition", str(t[3]), ' then ', str(t[5])
  2230. def p_expr_condition_if_relation(self, t):
  2231. # Examples
  2232. # if({equal} start_date() < "2005-06-01", A:B)
  2233. """
  2234. expr : IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA stds RPAREN
  2235. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA expr RPAREN
  2236. """
  2237. if self.run:
  2238. # Get stds/map list of then statement.
  2239. thenlist = self.check_stds(t[7])
  2240. # Get temporal conditional statement.
  2241. tvarexpr = t[5]
  2242. topolist = self.eval_toperator(t[3], optype = 'relation')[0]
  2243. thencond = self.build_condition_list(tvarexpr, thenlist, topolist)
  2244. thenresult = self.eval_condition_list(thencond)
  2245. # Clear the map and conditional values of the map list.
  2246. resultlist = self.check_stds(thenresult, clear = True)
  2247. # Return resulting map list.
  2248. t[0] = resultlist
  2249. else:
  2250. t[0] = t[7] + "*"
  2251. if self.debug:
  2252. print "result* = ", "if ", str(t[3]), "condition", str(t[5]), " then ", str(t[7])
  2253. def p_expr_condition_elif(self, t):
  2254. # Examples
  2255. # if( start_date() < "2005-06-01", if(start_time() < "12:30:00", A:B), A!:B)
  2256. """
  2257. expr : IF LPAREN t_var_expr COMMA stds COMMA stds RPAREN
  2258. | IF LPAREN t_var_expr COMMA stds COMMA expr RPAREN
  2259. | IF LPAREN t_var_expr COMMA expr COMMA stds RPAREN
  2260. | IF LPAREN t_var_expr COMMA expr COMMA expr RPAREN
  2261. """
  2262. if self.run:
  2263. # Get stds/map list of then statement.
  2264. thenlist = self.check_stds(t[5])
  2265. elselist = self.check_stds(t[7])
  2266. # Get temporal conditional statement for then and else expressions.
  2267. tvarexpr = t[3]
  2268. thencond = self.build_condition_list(tvarexpr, thenlist)
  2269. thenresult = self.eval_condition_list(thencond)
  2270. elsecond = self.build_condition_list(tvarexpr, elselist)
  2271. elseresult = self.eval_condition_list(elsecond, inverse = True)
  2272. # Combine and sort else and then statement to result map list.
  2273. combilist = thenresult + elseresult
  2274. resultlist = sorted(combilist, key = AbstractDatasetComparisonKeyStartTime)
  2275. # Clear the map and conditional values of the map list.
  2276. resultlist = self.check_stds(resultlist, clear = True)
  2277. # Return resulting map list.
  2278. t[0] = resultlist
  2279. else:
  2280. t[0] = t[5] + "*"
  2281. if self.debug:
  2282. print str(t[5]) + "* = ", "if condition", str(t[3]), " then ", str(t[5]), ' else ', str(t[7])
  2283. def p_expr_condition_elif_relation(self, t):
  2284. # Examples
  2285. # if({equal}, start_date() < "2005-06-01", if(start_time() < "12:30:00", A:B), A!:B)
  2286. # The then and else statement using the same topological relationships.
  2287. # Feature request: Independent relationships for then and else to conditions.
  2288. """
  2289. expr : IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA stds COMMA stds RPAREN
  2290. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA stds COMMA expr RPAREN
  2291. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA expr COMMA stds RPAREN
  2292. | IF LPAREN T_REL_OPERATOR COMMA t_var_expr COMMA expr COMMA expr RPAREN
  2293. """
  2294. if self.run:
  2295. # Get stds/map list of then statement.
  2296. thenlist = self.check_stds(t[7])
  2297. elselist = self.check_stds(t[9])
  2298. # Get temporal conditional statement.
  2299. tvarexpr = t[5]
  2300. topolist = self.eval_toperator(t[3], optype = 'relation')[0]
  2301. thencond = self.build_condition_list(tvarexpr, thenlist, topolist)
  2302. thenresult = self.eval_condition_list(thencond)
  2303. elsecond = self.build_condition_list(tvarexpr, elselist, topolist)
  2304. elseresult = self.eval_condition_list(elsecond, inverse = True)
  2305. # Combine and sort else and then statement to result map list.
  2306. combilist = thenresult + elseresult
  2307. resultlist = sorted(combilist, key = AbstractDatasetComparisonKeyStartTime)
  2308. # Clear the map and conditional values of the map list.
  2309. resultlist = self.check_stds(resultlist, clear = True)
  2310. # Return resulting map list.
  2311. t[0] = resultlist
  2312. else:
  2313. if t[5]:
  2314. t[0] = str(t[7])
  2315. else:
  2316. t[0] = str(t[9])
  2317. if self.debug:
  2318. if t[5]:
  2319. print str(t[7]), "* = ", "if condition", str(t[5]), " then ", str(t[7]), ' else ', str(t[9])
  2320. else:
  2321. print str(t[9]), "* = ", "if condition", str(t[5]), " then ", str(t[7]), ' else ', str(t[9])
  2322. def p_expr_t_buff(self, t):
  2323. # Examples
  2324. # buff_t(A : B, "10 minutes") # Select the part of A that is temporally
  2325. # equal to B and create a buffer of 10 minutes around
  2326. """
  2327. expr : BUFF_T LPAREN stds COMMA QUOTE number NAME QUOTE RPAREN
  2328. | BUFF_T LPAREN expr COMMA QUOTE number NAME QUOTE RPAREN
  2329. | BUFF_T LPAREN stds COMMA number RPAREN
  2330. | BUFF_T LPAREN expr COMMA number RPAREN
  2331. """
  2332. if self.run:
  2333. # Check input stds.
  2334. bufflist = self.check_stds(t[3])
  2335. for map in bufflist:
  2336. # Get increment format.
  2337. if len(t) == 10:
  2338. increment = str(t[6]) + " " + t[7]
  2339. elif len(t) == 7:
  2340. increment = str(t[5])
  2341. # Perform buffering.
  2342. map.temporal_buffer(increment)
  2343. t[0] = bufflist
  2344. else:
  2345. t[0] = t[3] + "*"
  2346. if self.debug:
  2347. if len(t) == 10:
  2348. print str(t[3]) + "* = buff_t(", str(t[3]), "," , '"', str(t[6]), str(t[7]), '"', ")"
  2349. elif len(t) == 7:
  2350. print str(t[3]) + "* = buff_t(", str(t[3]), ",", str(t[5]), ")"
  2351. def p_expr_t_snap(self, t):
  2352. # Examples
  2353. # tsnap(A : B) # Snap the maps of A temporally.
  2354. """
  2355. expr : TSNAP LPAREN stds RPAREN
  2356. | TSNAP LPAREN expr RPAREN
  2357. """
  2358. if self.run:
  2359. # Check input stds.
  2360. maplist = self.check_stds(t[3])
  2361. # Perform snapping.
  2362. snaplist = AbstractSpaceTimeDataset.snap_map_list(maplist)
  2363. t[0] = snaplist
  2364. else:
  2365. t[0] = t[3] + "*"
  2366. if self.debug:
  2367. print str(t[3]) + "* = tsnap(", str(t[3]), ")"
  2368. def p_expr_t_shift(self, t):
  2369. # Examples
  2370. # tshift(A : B, "10 minutes") # Shift the selection from A temporally
  2371. # by 10 minutes.
  2372. """
  2373. expr : TSHIFT LPAREN stds COMMA QUOTE number NAME QUOTE RPAREN
  2374. | TSHIFT LPAREN expr COMMA QUOTE number NAME QUOTE RPAREN
  2375. | TSHIFT LPAREN stds COMMA number RPAREN
  2376. | TSHIFT LPAREN expr COMMA number RPAREN
  2377. """
  2378. if self.run:
  2379. # Check input stds.
  2380. maplist = self.check_stds(t[3])
  2381. # Get increment format.
  2382. if len(t) == 10:
  2383. increment = str(t[6]) + " " + t[7]
  2384. elif len(t) == 7:
  2385. increment = str(t[5])
  2386. # Perform shifting.
  2387. shiftlist = AbstractSpaceTimeDataset.shift_map_list(maplist, increment)
  2388. t[0] = shiftlist
  2389. else:
  2390. t[0] = t[3] + "*"
  2391. if self.debug:
  2392. if len(t) == 10:
  2393. print str(t[3]) + "* = tshift(", str(t[3]), "," , '"', str(t[6]), str(t[7]), '"', ")"
  2394. elif len(t) == 7:
  2395. print str(t[3]) + "* = tshift(", str(t[3]), ",", str(t[5]), ")"
  2396. # Handle errors.
  2397. def p_error(self, t):
  2398. if t:
  2399. raise SyntaxError("syntax error on line %d, token %s near '%s' expression '%s'" %
  2400. (t.lineno, t.type, t.value, self.expression))
  2401. else:
  2402. raise SyntaxError("Unexpected syntax error")
  2403. ###############################################################################
  2404. if __name__ == "__main__":
  2405. import doctest
  2406. doctest.testmod()