temporal_raster_base_algebra.py 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. """!@package grass.temporal
  2. Temporal raster algebra
  3. (C) 2013 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. @author Thomas Leppelt and Soeren Gebbert
  8. @code
  9. >>> p = TemporalRasterAlgebraLexer()
  10. >>> p.build()
  11. >>> p.debug = True
  12. >>> expression = 'R = A / B * 20 + C - 2.45'
  13. >>> p.test(expression)
  14. R = A / B * 20 + C - 2.45
  15. LexToken(NAME,'R',1,0)
  16. LexToken(EQUALS,'=',1,2)
  17. LexToken(NAME,'A',1,4)
  18. LexToken(DIV,'/',1,6)
  19. LexToken(NAME,'B',1,8)
  20. LexToken(MULT,'*',1,10)
  21. LexToken(INT,20,1,12)
  22. LexToken(ADD,'+',1,15)
  23. LexToken(NAME,'C',1,17)
  24. LexToken(SUB,'-',1,19)
  25. LexToken(FLOAT,2.45,1,21)
  26. >>> expression = 'R = A {equal,|/} B'
  27. >>> p.test(expression)
  28. R = A {equal,|/} B
  29. LexToken(NAME,'R',1,0)
  30. LexToken(EQUALS,'=',1,2)
  31. LexToken(NAME,'A',1,4)
  32. LexToken(T_ARITH1_OPERATOR,'{equal,|/}',1,6)
  33. LexToken(NAME,'B',1,17)
  34. >>> expression = 'R = A {equal,||} B'
  35. >>> p.test(expression)
  36. R = A {equal,||} B
  37. LexToken(NAME,'R',1,0)
  38. LexToken(EQUALS,'=',1,2)
  39. LexToken(NAME,'A',1,4)
  40. LexToken(T_COMP_OPERATOR,'{equal,||}',1,6)
  41. LexToken(NAME,'B',1,17)
  42. >>> expression = 'R = A {equal,&&} B'
  43. >>> p.test(expression)
  44. R = A {equal,&&} B
  45. LexToken(NAME,'R',1,0)
  46. LexToken(EQUALS,'=',1,2)
  47. LexToken(NAME,'A',1,4)
  48. LexToken(T_COMP_OPERATOR,'{equal,&&}',1,6)
  49. LexToken(NAME,'B',1,17)
  50. >>> expression = 'R = A {equal | during,+*} B'
  51. >>> p.test(expression)
  52. R = A {equal | during,+*} B
  53. LexToken(NAME,'R',1,0)
  54. LexToken(EQUALS,'=',1,2)
  55. LexToken(NAME,'A',1,4)
  56. LexToken(T_ARITH1_OPERATOR,'{equal | during,+*}',1,6)
  57. LexToken(NAME,'B',1,26)
  58. >>> expression = 'R = A {equal | during,+:} B'
  59. >>> p.test(expression)
  60. R = A {equal | during,+:} B
  61. LexToken(NAME,'R',1,0)
  62. LexToken(EQUALS,'=',1,2)
  63. LexToken(NAME,'A',1,4)
  64. LexToken(T_SELECT_OPERATOR,'{equal | during,+:}',1,6)
  65. LexToken(NAME,'B',1,26)
  66. >>> expression = 'R = abs(A) {equal,+:} exp(B) / sqrt(C) - log(D)'
  67. >>> p.test(expression)
  68. R = abs(A) {equal,+:} exp(B) / sqrt(C) - log(D)
  69. LexToken(NAME,'R',1,0)
  70. LexToken(EQUALS,'=',1,2)
  71. LexToken(ABS,'abs',1,4)
  72. LexToken(LPAREN,'(',1,7)
  73. LexToken(NAME,'A',1,8)
  74. LexToken(RPAREN,')',1,9)
  75. LexToken(T_SELECT_OPERATOR,'{equal,+:}',1,11)
  76. LexToken(EXP,'exp',1,22)
  77. LexToken(LPAREN,'(',1,25)
  78. LexToken(NAME,'B',1,26)
  79. LexToken(RPAREN,')',1,27)
  80. LexToken(DIV,'/',1,29)
  81. LexToken(SQRT,'sqrt',1,31)
  82. LexToken(LPAREN,'(',1,35)
  83. LexToken(NAME,'C',1,36)
  84. LexToken(RPAREN,')',1,37)
  85. LexToken(SUB,'-',1,39)
  86. LexToken(LOG,'log',1,41)
  87. LexToken(LPAREN,'(',1,44)
  88. LexToken(NAME,'D',1,45)
  89. LexToken(RPAREN,')',1,46)
  90. @endcode
  91. """
  92. import grass.pygrass.modules as pymod
  93. from temporal_raster_operator import *
  94. from temporal_algebra import *
  95. ##############################################################################
  96. class TemporalRasterAlgebraLexer(TemporalAlgebraLexer):
  97. """!Lexical analyzer for the GRASS GIS temporal algebra"""
  98. def __init__(self):
  99. TemporalAlgebraLexer.__init__(self)
  100. # Supported r.mapcalc functions.
  101. mapcalc_functions = {
  102. 'exp' : 'EXP',
  103. 'log' : 'LOG',
  104. 'sqrt' : 'SQRT',
  105. 'abs' : 'ABS',
  106. 'cos' : 'COS',
  107. 'acos' : 'ACOS',
  108. 'sin' : 'SIN',
  109. 'asin' : 'ASIN',
  110. 'tan' : 'TAN',
  111. 'double' : 'DOUBLE',
  112. 'float' : 'FLOATEXP',
  113. 'int' : 'INTEXP',
  114. 'isnull' : 'ISNULL',
  115. 'null' : 'NULL',
  116. 'exist' : 'EXIST',
  117. }
  118. # This is the list of token names.
  119. raster_tokens = (
  120. 'MOD',
  121. 'DIV',
  122. 'MULT',
  123. 'ADD',
  124. 'SUB',
  125. 'T_ARITH1_OPERATOR',
  126. 'T_ARITH2_OPERATOR',
  127. 'L_SPAREN',
  128. 'R_SPAREN',
  129. )
  130. # Build the token list
  131. tokens = TemporalAlgebraLexer.tokens \
  132. + raster_tokens \
  133. + tuple(mapcalc_functions.values())
  134. # Regular expression rules for simple tokens
  135. t_MOD = r'[\%]'
  136. t_DIV = r'[\/]'
  137. t_MULT = r'[\*]'
  138. t_ADD = r'[\+]'
  139. t_SUB = r'[-]'
  140. t_T_ARITH1_OPERATOR = r'\{([a-zA-Z\| ]+[,])?([\|&+=]?[\%\*\/])\}'
  141. t_T_ARITH2_OPERATOR = r'\{([a-zA-Z\| ]+[,])?([\|&+=]?[+-])\}'
  142. t_L_SPAREN = r'\['
  143. t_R_SPAREN = r'\]'
  144. # Parse symbols
  145. def temporal_symbol(self, t):
  146. # Check for reserved words
  147. if t.value in TemporalRasterAlgebraLexer.time_functions.keys():
  148. t.type = TemporalRasterAlgebraLexer.time_functions.get(t.value)
  149. elif t.value in TemporalRasterAlgebraLexer.datetime_functions.keys():
  150. t.type = TemporalRasterAlgebraLexer.datetime_functions.get(t.value)
  151. elif t.value in TemporalRasterAlgebraLexer.conditional_functions.keys():
  152. t.type = TemporalRasterAlgebraLexer.conditional_functions.get(t.value)
  153. elif t.value in TemporalRasterAlgebraLexer.mapcalc_functions.keys():
  154. t.type = TemporalRasterAlgebraLexer.mapcalc_functions.get(t.value)
  155. else:
  156. t.type = 'NAME'
  157. return t
  158. ##############################################################################
  159. class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
  160. """The temporal algebra class"""
  161. # Get the tokens from the lexer class
  162. tokens = TemporalRasterAlgebraLexer.tokens
  163. # Setting equal precedence level for select and hash operations.
  164. precedence = (
  165. ('left', 'T_SELECT_OPERATOR', 'T_SELECT', 'T_NOT_SELECT'), # 1
  166. ('left', 'ADD', 'SUB', 'T_ARITH2_OPERATOR'), #2
  167. ('left', 'AND', 'OR', 'T_COMP_OPERATOR', 'MOD', 'DIV', 'MULT',
  168. 'T_ARITH1_OPERATOR'))
  169. def __init__(self, pid=None, run = True, debug = False, spatial = False, \
  170. nprocs = 1, null = False):
  171. TemporalAlgebraParser.__init__(self, pid, run, debug, spatial)
  172. self.nprocs = nprocs
  173. ######################### Temporal functions ##############################
  174. def eval_toperator(self, operator, comparison = False):
  175. """!This function evaluates a string containing temporal operations.
  176. @param operator String of temporal operations, e.g. {equal|during,=!:}.
  177. @return List of temporal relations (equal, during), the given function
  178. (!:) and the interval/instances (=).
  179. @code
  180. >>> init(True)
  181. >>> p = TemporalRasterBaseAlgebraParser()
  182. >>> operator = "{equal,:}"
  183. >>> p.eval_toperator(operator)
  184. (['EQUAL'], '=', ':')
  185. >>> operator = "{equal|during,:}"
  186. >>> p.eval_toperator(operator)
  187. (['EQUAL', 'DURING'], '=', ':')
  188. >>> operator = "{equal,!:}"
  189. >>> p.eval_toperator(operator)
  190. (['EQUAL'], '=', '!:')
  191. >>> operator = "{equal|during,!:}"
  192. >>> p.eval_toperator(operator)
  193. (['EQUAL', 'DURING'], '=', '!:')
  194. >>> operator = "{equal|during,=!:}"
  195. >>> p.eval_toperator(operator)
  196. (['EQUAL', 'DURING'], '=', '!:')
  197. >>> operator = "{equal|during|starts,#}"
  198. >>> p.eval_toperator(operator)
  199. (['EQUAL', 'DURING', 'STARTS'], '=', '#')
  200. >>> operator = "{!:}"
  201. >>> p.eval_toperator(operator)
  202. (['EQUAL'], '=', '!:')
  203. >>> operator = "{=:}"
  204. >>> p.eval_toperator(operator)
  205. (['EQUAL'], '=', ':')
  206. >>> operator = "{#}"
  207. >>> p.eval_toperator(operator)
  208. (['EQUAL'], '=', '#')
  209. >>> operator = "{equal|during}"
  210. >>> p.eval_toperator(operator)
  211. (['EQUAL', 'DURING'], None, None)
  212. >>> operator = "{equal}"
  213. >>> p.eval_toperator(operator)
  214. (['EQUAL'], None, None)
  215. >>> operator = "{equal,||}"
  216. >>> p.eval_toperator(operator, True)
  217. (['EQUAL'], '=', '||')
  218. >>> operator = "{equal|during,&&}"
  219. >>> p.eval_toperator(operator, True)
  220. (['EQUAL', 'DURING'], '=', '&&')
  221. @endcode
  222. """
  223. p = TemporalRasterOperatorParser()
  224. p.parse(operator, comparison)
  225. p.relations = [rel.upper() for rel in p.relations]
  226. return(p.relations, p.temporal, p.function)
  227. ###########################################################################
  228. def p_statement_assign(self, t):
  229. # The expression should always return a list of maps.
  230. """
  231. statement : stds EQUALS expr
  232. """
  233. if self.run:
  234. # Create the process queue for parallel mapcalc processing
  235. process_queue = pymod.ParallelModuleQueue(int(self.nprocs))
  236. if isinstance(t[3], list):
  237. num = len(t[3])
  238. count = 0
  239. returncode = 0
  240. register_list = []
  241. for i in range(num):
  242. # Check if resultmap names exist in GRASS database.
  243. rastername = self.basename + "_" + str(i) + "@" + self.mapset
  244. rastermap = RasterDataset(rastername)
  245. if rastermap.map_exists() and self.overwrite == False:
  246. self.msgr.fatal("Error raster maps with basename %s exist. Use --o flag to overwrite existing file" \
  247. %(rastername))
  248. map_test_list = []
  249. for map_i in t[3]:
  250. newident = self.basename + "_" + str(count)
  251. if "cmd_list" in dir(map_i):
  252. print(newident + ' = ' + map_i.cmd_list)
  253. # Build r.mapcalc module and execute expression.
  254. # Change map name to given basename.
  255. # Create deepcopy of r.mapcalc module.
  256. map_test = map_i.get_new_instance(newident + "@" + self.mapset)
  257. map_test.set_temporal_extent(map_i.get_temporal_extent())
  258. map_test.set_spatial_extent(map_i.get_spatial_extent())
  259. map_test_list.append(map_test)
  260. m = copy.deepcopy(self.m_mapcalc)
  261. m_expression = newident + "=" + map_i.cmd_list
  262. m.inputs["expression"].value = m_expression
  263. m.flags["overwrite"].value = self.overwrite
  264. process_queue.put(m)
  265. else:
  266. map_i.set_id(newident + "@" + self.mapset)
  267. map_test_list.append(map_i)
  268. count += 1
  269. process_queue.wait()
  270. for map_i in map_test_list:
  271. if not map_test.map_exists():
  272. self.msgr.error(_("Error computing map <%s>"%(map_i.get_id()) ))
  273. else:
  274. register_list.append(map_i)
  275. # Open connection to temporal database.
  276. dbif, connect = init_dbif(self.dbif)
  277. # Create result space time dataset.
  278. resultstds = open_new_space_time_dataset(t[1], self.stdstype, \
  279. 'absolute', t[1], t[1], \
  280. 'mean', self.dbif, \
  281. overwrite = self.overwrite)
  282. for map_i in register_list:
  283. # Get meta data from grass database.
  284. map_i.load()
  285. if map_i.is_in_db(dbif) and self.overwrite:
  286. # Update map in temporal database.
  287. map_i.update_all(dbif)
  288. elif map_i.is_in_db(dbif) and self.overwrite == False:
  289. # Raise error if map exists and no overwrite flag is given.
  290. self.msgr.fatal("Error vector map %s exist in temporal database. Use overwrite flag. : \n%s" \
  291. %(map_i.get_map_id(), cmd.popen.stderr))
  292. else:
  293. # Insert map into temporal database.
  294. map_i.insert(dbif)
  295. # Register map in result space time dataset.
  296. success = resultstds.register_map(map_i, dbif)
  297. resultstds.update_from_registered_maps(dbif)
  298. dbif.close()
  299. t[0] = register_list
  300. def p_arith1_operation(self, t):
  301. """
  302. expr : stds MOD stds
  303. | expr MOD stds
  304. | stds MOD expr
  305. | expr MOD expr
  306. | stds DIV stds
  307. | expr DIV stds
  308. | stds DIV expr
  309. | expr DIV expr
  310. | stds MULT stds
  311. | expr MULT stds
  312. | stds MULT expr
  313. | expr MULT expr
  314. """
  315. # Check input stds.
  316. maplistA = self.check_stds(t[1])
  317. maplistB = self.check_stds(t[3])
  318. topolist = self.get_temporal_topo_list(maplistA, maplistB)
  319. if self.run:
  320. resultlist = []
  321. for map_i in topolist:
  322. # Generate an intermediate map for the result map list.
  323. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  324. # Set first input for overlay module.
  325. mapainput = map_i.get_id()
  326. # Loop over temporal related maps and create overlay modules.
  327. tbrelations = map_i.get_temporal_relations()
  328. count = 0
  329. for map_j in (tbrelations['EQUAL']):
  330. # Create overlayed map extent.
  331. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  332. temp_op = '=')
  333. # Stop the loop if no temporal or spatial relationship exist.
  334. if returncode == 0:
  335. break
  336. if count == 0:
  337. # Set map name.
  338. name = map_new.get_id()
  339. else:
  340. # Generate an intermediate map
  341. name = self.generate_map_name()
  342. # Set first input for overlay module.
  343. mapbinput = map_j.get_id()
  344. # Create r.mapcalc expression string for the operation.
  345. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  346. cmdstring = "(%s %s %s)" %(map_new.cmd_list, t[2], mapbinput)
  347. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  348. cmdstring = "(%s %s %s)" %(mapainput, t[2], map_j.cmd_list)
  349. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  350. cmdstring = "(%s %s %s)" %(map_new.cmd_list, t[2], map_j.cmd_list)
  351. else:
  352. cmdstring = "(%s %s %s)" %(mapainput, t[2], mapbinput)
  353. # Conditional append of module command.
  354. map_new.cmd_list = cmdstring
  355. # Set new map name to temporary map name.
  356. mapainput = name
  357. count += 1
  358. # Append map to result map list.
  359. if returncode == 1:
  360. resultlist.append(map_new)
  361. t[0] = resultlist
  362. if self.debug:
  363. for map in resultlist:
  364. print map.cmd_list
  365. def p_arith1_operation_numeric1(self, t):
  366. """
  367. expr : stds MOD number
  368. | expr MOD number
  369. | stds DIV number
  370. | expr DIV number
  371. | stds MULT number
  372. | expr MULT number
  373. """
  374. # Check input stds.
  375. maplist = self.check_stds(t[1])
  376. if self.run:
  377. resultlist = []
  378. for map_i in maplist:
  379. mapinput = map_i.get_id()
  380. # Create r.mapcalc expression string for the operation.
  381. if "cmd_list" in dir(map_i):
  382. cmdstring = "(%s %s %s)" %(map_i.cmd_list, t[2], t[3])
  383. else:
  384. cmdstring = "(%s %s %s)" %(mapinput, t[2], t[3])
  385. # Conditional append of module command.
  386. map_i.cmd_list = cmdstring
  387. # Append map to result map list.
  388. resultlist.append(map_i)
  389. t[0] = resultlist
  390. if self.debug:
  391. for map in resultlist:
  392. print map.cmd_list
  393. def p_arith1_operation_numeric2(self, t):
  394. """
  395. expr : number MOD stds
  396. | number MOD expr
  397. | number DIV stds
  398. | number DIV expr
  399. | number MULT stds
  400. | number MULT expr
  401. """
  402. # Check input stds.
  403. maplist = self.check_stds(t[3])
  404. if self.run:
  405. resultlist = []
  406. for map_i in maplist:
  407. mapinput = map_i.get_id()
  408. # Create r.mapcalc expression string for the operation.
  409. if "cmd_list" in dir(map_i):
  410. cmdstring = "(%s %s %s)" %(t[1], t[2], map_i.cmd_list)
  411. else:
  412. cmdstring = "(%s %s %s)" %(t[1], t[2], mapinput)
  413. # Conditional append of module command.
  414. map_i.cmd_list = cmdstring
  415. # Append map to result map list.
  416. resultlist.append(map_i)
  417. t[0] = resultlist
  418. if self.debug:
  419. for map in resultlist:
  420. print map.cmd_list
  421. def p_arith2_operation(self, t):
  422. """
  423. expr : stds ADD stds
  424. | expr ADD stds
  425. | stds ADD expr
  426. | expr ADD expr
  427. | stds SUB stds
  428. | expr SUB stds
  429. | stds SUB expr
  430. | expr SUB expr
  431. """
  432. # Check input stds.
  433. maplistA = self.check_stds(t[1])
  434. maplistB = self.check_stds(t[3])
  435. topolist = self.get_temporal_topo_list(maplistA, maplistB)
  436. if self.run:
  437. resultlist = []
  438. for map_i in topolist:
  439. # Generate an intermediate map for the result map list.
  440. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  441. # Set first input for overlay module.
  442. mapainput = map_i.get_id()
  443. # Loop over temporal related maps and create overlay modules.
  444. tbrelations = map_i.get_temporal_relations()
  445. count = 0
  446. for map_j in (tbrelations['EQUAL']):
  447. # Create overlayed map extent.
  448. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  449. temp_op = '=')
  450. # Stop the loop if no temporal or spatial relationship exist.
  451. if returncode == 0:
  452. break
  453. if count == 0:
  454. # Set map name.
  455. name = map_new.get_id()
  456. else:
  457. # Generate an intermediate map
  458. name = self.generate_map_name()
  459. # Set first input for overlay module.
  460. mapbinput = map_j.get_id()
  461. # Create r.mapcalc expression string for the operation.
  462. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  463. cmdstring = "(%s %s %s)" %(map_new.cmd_list, t[2], mapbinput)
  464. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  465. cmdstring = "(%s %s %s)" %(mapainput, t[2], map_j.cmd_list)
  466. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  467. cmdstring = "(%s %s %s)" %(map_new.cmd_list, t[2], map_j.cmd_list)
  468. else:
  469. cmdstring = "(%s %s %s)" %(mapainput, t[2], mapbinput)
  470. # Conditional append of module command.
  471. map_new.cmd_list = cmdstring
  472. # Set new map name to temporary map name.
  473. mapainput = name
  474. count += 1
  475. # Append map to result map list.
  476. if returncode == 1:
  477. resultlist.append(map_new)
  478. t[0] = resultlist
  479. if self.debug:
  480. for map in resultlist:
  481. print map.cmd_list
  482. def p_arith2_operation_numeric1(self, t):
  483. """
  484. expr : stds ADD number
  485. | expr ADD number
  486. | stds SUB number
  487. | expr SUB number
  488. """
  489. # Check input stds.
  490. maplist = self.check_stds(t[1])
  491. if self.run:
  492. resultlist = []
  493. for map_i in maplist:
  494. mapinput = map_i.get_id()
  495. # Create r.mapcalc expression string for the operation.
  496. if "cmd_list" in dir(map_i):
  497. cmdstring = "(%s %s %s)" %(map_i.cmd_list, t[2], t[3])
  498. else:
  499. cmdstring = "(%s %s %s)" %(mapinput, t[2], t[3])
  500. # Conditional append of module command.
  501. map_i.cmd_list = cmdstring
  502. # Append map to result map list.
  503. resultlist.append(map_i)
  504. t[0] = resultlist
  505. if self.debug:
  506. for map in resultlist:
  507. print map.cmd_list
  508. def p_arith2_operation_numeric2(self, t):
  509. """
  510. expr : number ADD stds
  511. | number ADD expr
  512. | number SUB stds
  513. | number SUB expr
  514. """
  515. # Check input stds.
  516. maplist = self.check_stds(t[3])
  517. if self.run:
  518. resultlist = []
  519. for map_i in maplist:
  520. mapinput = map_i.get_id()
  521. # Create r.mapcalc expression string for the operation.
  522. if "cmd_list" in dir(map_i):
  523. cmdstring = "(%s %s %s)" %(t[1], t[2], map_i.cmd_list)
  524. else:
  525. cmdstring = "(%s %s %s)" %(t[1], t[2], mapinput)
  526. # Conditional append of module command.
  527. map_i.cmd_list = cmdstring
  528. # Append map to result map list.
  529. resultlist.append(map_i)
  530. t[0] = resultlist
  531. if self.debug:
  532. for map in resultlist:
  533. print map.cmd_list
  534. def p_arith1_operation_relation(self, t):
  535. """
  536. expr : stds T_ARITH1_OPERATOR stds
  537. | expr T_ARITH1_OPERATOR stds
  538. | stds T_ARITH1_OPERATOR expr
  539. | expr T_ARITH1_OPERATOR expr
  540. """
  541. # Check input stds.
  542. maplistA = self.check_stds(t[1])
  543. maplistB = self.check_stds(t[3])
  544. relations, temporal, function= self.eval_toperator(t[2])
  545. topolist = self.get_temporal_topo_list(maplistA, maplistB, topolist = relations)
  546. if self.run:
  547. resultlist = []
  548. for map_i in topolist:
  549. # Generate an intermediate map for the result map list.
  550. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  551. # Set first input for overlay module.
  552. mapainput = map_i.get_id()
  553. # Loop over temporal related maps and create overlay modules.
  554. tbrelations = map_i.get_temporal_relations()
  555. count = 0
  556. for topo in relations:
  557. if topo in tbrelations.keys():
  558. for map_j in (tbrelations[topo]):
  559. # Create overlayed map extent.
  560. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  561. temp_op = temporal)
  562. print(returncode)
  563. # Stop the loop if no temporal or spatial relationship exist.
  564. if returncode == 0:
  565. break
  566. if count == 0:
  567. # Set map name.
  568. name = map_new.get_id()
  569. else:
  570. # Generate an intermediate map
  571. name = self.generate_map_name()
  572. map_new.set_id(name + "@" + mapset)
  573. # Set second input for overlay module.
  574. mapbinput = map_j.get_id()
  575. # Create r.mapcalc expression string for the operation.
  576. if "cmd_list" in dir(map_new):
  577. cmdstring = "(%s %s %s)" %(map_new.cmd_list, function, mapbinput)
  578. print('with cmd in a: ' + map_j.get_id())
  579. elif "cmd_list" in dir(map_j):
  580. cmdstring = "(%s %s %s)" %(mapainput, function, map_j.cmd_list)
  581. print('with cmd in b')
  582. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  583. cmdstring = "(%s %s %s)" %(map_new.cmd_list, function, map_j.cmd_list)
  584. print('with cmd in b')
  585. else:
  586. cmdstring = "(%s %s %s)" %(mapainput, function, mapbinput)
  587. print(cmdstring)
  588. # Conditional append of module command.
  589. map_new.cmd_list = cmdstring
  590. # Set new map name to temporary map name.
  591. mapainput = name
  592. count += 1
  593. if returncode == 0:
  594. break
  595. # Append map to result map list.
  596. if returncode == 1:
  597. resultlist.append(map_new)
  598. t[0] = resultlist
  599. if self.debug:
  600. for map in resultlist:
  601. print map.cmd_list
  602. def p_arith2_operation_relation(self, t):
  603. """
  604. expr : stds T_ARITH2_OPERATOR stds
  605. | expr T_ARITH2_OPERATOR stds
  606. | stds T_ARITH2_OPERATOR expr
  607. | expr T_ARITH2_OPERATOR expr
  608. """
  609. # Check input stds.
  610. maplistA = self.check_stds(t[1])
  611. maplistB = self.check_stds(t[3])
  612. relations, temporal, function= self.eval_toperator(t[2])
  613. topolist = self.get_temporal_topo_list(maplistA, maplistB, topolist = relations)
  614. if self.run:
  615. resultlist = []
  616. for map_i in topolist:
  617. # Generate an intermediate map for the result map list.
  618. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  619. # Set first input for overlay module.
  620. mapainput = map_i.get_id()
  621. # Loop over temporal related maps and create overlay modules.
  622. tbrelations = map_i.get_temporal_relations()
  623. count = 0
  624. for topo in relations:
  625. if topo in tbrelations.keys():
  626. for map_j in (tbrelations[topo]):
  627. # Create overlayed map extent.
  628. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  629. temp_op = temporal)
  630. # Stop the loop if no temporal or spatial relationship exist.
  631. if returncode == 0:
  632. break
  633. if count == 0:
  634. # Set map name.
  635. name = map_new.get_id()
  636. else:
  637. # Generate an intermediate map
  638. name = self.generate_map_name()
  639. map_new.set_id(name + "@" + self.mapset)
  640. # Set second input for overlay module.
  641. mapbinput = map_j.get_id()
  642. # Create r.mapcalc expression string for the operation.
  643. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  644. cmdstring = "(%s %s %s)" %(map_new.cmd_list, function, mapbinput)
  645. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  646. cmdstring = "(%s %s %s)" %(mapainput, function, map_j.cmd_list)
  647. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  648. cmdstring = "(%s %s %s)" %(map_new.cmd_list, function, map_j.cmd_list)
  649. else:
  650. cmdstring = "(%s %s %s)" %(mapainput, function, mapbinput)
  651. # Conditional append of module command.
  652. map_new.cmd_list = cmdstring
  653. # Set new map name to temporary map name.
  654. mapainput = name
  655. count += 1
  656. if returncode == 0:
  657. break
  658. # Append map to result map list.
  659. if returncode == 1:
  660. resultlist.append(map_new)
  661. t[0] = resultlist
  662. if self.debug:
  663. for map in resultlist:
  664. print map.cmd_list
  665. def p_mapcalc_function(self, t):
  666. # Supported mapcalc functions.
  667. """
  668. mapcalc_arith : ABS
  669. | LOG
  670. | SQRT
  671. | EXP
  672. | COS
  673. | ACOS
  674. | SIN
  675. | ASIN
  676. | TAN
  677. | DOUBLE
  678. | FLOATEXP
  679. | INTEXP
  680. """
  681. t[0] = t[1]
  682. if self.debug:
  683. for map in resultlist:
  684. print map.cmd_list
  685. def p_mapcalc_operation(self, t):
  686. # Examples:
  687. # sin(A)
  688. # log(B)
  689. """
  690. expr : mapcalc_arith LPAREN stds RPAREN
  691. | mapcalc_arith LPAREN expr RPAREN
  692. """
  693. # Check input stds.
  694. maplist = self.check_stds(t[3])
  695. if self.run:
  696. resultlist = []
  697. for map_i in maplist:
  698. # Create r.mapcalc expression string for the operation.
  699. if "cmd_list" in dir(map_i):
  700. cmdstring = "%s(%s)" %(t[1].lower(), map_i.cmd_list)
  701. else:
  702. cmdstring = "%s(%s)" %(t[1].lower(), map_i.get_id())
  703. # Set new command list for map.
  704. map_i.cmd_list = cmdstring
  705. # Append map with updated command list to result list.
  706. resultlist.append(map_i)
  707. t[0] = resultlist
  708. if self.debug:
  709. for map in resultlist:
  710. print map.cmd_list
  711. def p_s_var_expr(self, t):
  712. # Examples:
  713. # isnull(A)
  714. """
  715. s_var_expr : ISNULL LPAREN stds RPAREN
  716. | ISNULL LPAREN expr RPAREN
  717. """
  718. # Check input stds.
  719. maplist = self.check_stds(t[3])
  720. if self.run:
  721. resultlist = []
  722. for map_i in maplist:
  723. # Create r.mapcalc expression string for the operation.
  724. if "cmd_list" in dir(map_i):
  725. cmdstring = "%s(%s)" %(t[1].lower(), map_i.cmd_list)
  726. else:
  727. cmdstring = "%s(%s)" %(t[1].lower(), map_i.get_id())
  728. # Set new command list for map.
  729. map_i.cmd_list = cmdstring
  730. # Append map with updated command list to result list.
  731. resultlist.append(map_i)
  732. t[0] = resultlist
  733. if self.debug:
  734. for map in resultlist:
  735. print map.cmd_list
  736. def p_s_var_expr2(self, t):
  737. # Examples:
  738. # A <= 2
  739. """
  740. s_var_expr : stds comp_op number
  741. | expr comp_op number
  742. """
  743. # Check input stds.
  744. maplist = self.check_stds(t[1])
  745. if self.run:
  746. resultlist = []
  747. for map_i in maplist:
  748. # Create r.mapcalc expression string for the operation.
  749. if "cmd_list" in dir(map_i):
  750. cmdstring = "%s %s %s" %(map_i.cmd_list, t[2], t[3])
  751. else:
  752. cmdstring = "%s %s %s" %(map_i.get_id(), t[2], t[3])
  753. # Set new command list for map.
  754. map_i.cmd_list = cmdstring
  755. # Append map with updated command list to result list.
  756. resultlist.append(map_i)
  757. t[0] = resultlist
  758. if self.debug:
  759. for map in resultlist:
  760. print map.cmd_list
  761. def p_s_var_expr3(self, t):
  762. # Examples:
  763. # A <= 2 || B == 10
  764. # A < 3 && A > 1
  765. """
  766. s_var_expr : s_var_expr AND AND s_var_expr
  767. | s_var_expr OR OR s_var_expr
  768. """
  769. # Check input stds.
  770. maplistA = self.check_stds(t[1])
  771. maplistB = self.check_stds(t[4])
  772. topolist = self.get_temporal_topo_list(maplistA, maplistB)
  773. if self.run:
  774. resultlist = []
  775. for map_i in topolist:
  776. # Loop over temporal related maps and create overlay modules.
  777. tbrelations = map_i.get_temporal_relations()
  778. count = 0
  779. for map_j in (tbrelations['EQUAL']):
  780. # Generate an intermediate map for the result map list.
  781. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  782. # Set first input for overlay module.
  783. mapainput = map_i.get_id()
  784. # Create overlayed map extent.
  785. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  786. temp_op = '=')
  787. # Stop the loop if no temporal or spatial relationship exist.
  788. if returncode == 0:
  789. break
  790. if count == 0:
  791. # Set map name.
  792. name = map_new.get_id()
  793. else:
  794. # Generate an intermediate map
  795. name = self.generate_map_name()
  796. # Set first input for overlay module.
  797. mapbinput = map_j.get_id()
  798. # Create r.mapcalc expression string for the operation.
  799. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  800. cmdstring = "%s %s %s" %(map_new.cmd_list, t[2] + t[3], mapbinput)
  801. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  802. cmdstring = "%s %s %s" %(mapainput, t[2] + t[3], map_j.cmd_list)
  803. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  804. cmdstring = "%s %s %s" %(map_new.cmd_list, t[2] + t[3], map_j.cmd_list)
  805. else:
  806. cmdstring = "%s %s %s" %(mapainput, t[2] + t[3], mapbinput)
  807. # Conditional append of module command.
  808. map_new.cmd_list = cmdstring
  809. # Set new map name to temporary map name.
  810. #mapainput = name
  811. count += 1
  812. # Append map to result map list.
  813. if returncode == 1:
  814. resultlist.append(map_new)
  815. t[0] = resultlist
  816. if self.debug:
  817. for map in resultlist:
  818. print map.cmd_list
  819. def p_s_var_expr4(self, t):
  820. # Examples:
  821. # exist(B)
  822. """
  823. s_var_expr : EXIST LPAREN stds RPAREN
  824. | EXIST LPAREN expr RPAREN
  825. """
  826. # Check input stds.
  827. maplist = self.check_stds(t[3])
  828. if self.run:
  829. resultlist = []
  830. for map_i in maplist:
  831. # Create r.mapcalc expression string for the operation.
  832. if "cmd_list" in dir(map_i):
  833. cmdstring = "%s" %(map_i.cmd_list)
  834. else:
  835. cmdstring = "%s" %(map_i.get_id())
  836. # Set new command list for map.
  837. map_i.cmd_list = cmdstring
  838. # Append map with updated command list to result list.
  839. resultlist.append(map_i)
  840. t[0] = resultlist
  841. if self.debug:
  842. for map in resultlist:
  843. print map.cmd_list
  844. def p_s_expr_condition_if(self, t):
  845. # Examples:
  846. # if(s_var_expr, B)
  847. # if(A == 1, B)
  848. """
  849. expr : IF LPAREN s_var_expr COMMA stds RPAREN
  850. | IF LPAREN s_var_expr COMMA expr RPAREN
  851. """
  852. ifmaplist = self.check_stds(t[3])
  853. thenmaplist = self.check_stds(t[5])
  854. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist)
  855. resultlist = []
  856. for map_i in topolist:
  857. #print(map_i.get_id())
  858. # Loop over temporal related maps and create overlay modules.
  859. tbrelations = map_i.get_temporal_relations()
  860. count = 0
  861. for map_j in (tbrelations['EQUAL']):
  862. # Generate an intermediate map for the result map list.
  863. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  864. # Set first input for overlay module.
  865. mapainput = map_i.get_id()
  866. # Create overlayed map extent.
  867. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  868. temp_op = '=')
  869. # Stop the loop if no temporal or spatial relationship exist.
  870. if returncode == 0:
  871. break
  872. if count == 0:
  873. # Set map name.
  874. name = map_new.get_id()
  875. else:
  876. # Generate an intermediate map
  877. name = self.generate_map_name()
  878. # Set first input for overlay module.
  879. mapbinput = map_j.get_id()
  880. # Create r.mapcalc expression string for the operation.
  881. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  882. cmdstring = "if(%s,%s)" %(map_new.cmd_list, mapbinput)
  883. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  884. cmdstring = "if(%s,%s)" %(mapainput, map_j.cmd_list)
  885. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  886. cmdstring = "if(%s,%s)" %(map_new.cmd_list, map_j.cmd_list)
  887. else:
  888. cmdstring = "if(%s,%s)" %(mapainput, mapbinput)
  889. # Conditional append of module command.
  890. map_new.cmd_list = cmdstring
  891. # Set new map name to temporary map name.
  892. #mapainput = name
  893. count += 1
  894. # Append map to result map list.
  895. if returncode == 1:
  896. resultlist.append(map_new)
  897. t[0] = resultlist
  898. if self.debug:
  899. for map in resultlist:
  900. print map.cmd_list
  901. def p_s_numeric_condition_if(self, t):
  902. # Examples:
  903. # if(s_var_expr, 1)
  904. # if(A == 5, 10)
  905. """
  906. expr : IF LPAREN s_var_expr COMMA number RPAREN
  907. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN RPAREN
  908. """
  909. ifmaplist = self.check_stds(t[3])
  910. resultlist = []
  911. # Select input for r.mapcalc expression based on length of PLY object.
  912. if len(t) == 7:
  913. numinput = t[5]
  914. elif len(t) == 9:
  915. numinput = t[5] + t[6] + t[7]
  916. # Iterate over condition map list.
  917. for map_i in ifmaplist:
  918. mapinput = map_i.get_id()
  919. # Create r.mapcalc expression string for the operation.
  920. if "cmd_list" in dir(map_i):
  921. cmdstring = "if(%s,%s)" %(map_i.cmd_list, numinput)
  922. else:
  923. cmdstring = "if(%s,%s)" %(mapinput, numinput)
  924. # Conditional append of module command.
  925. map_i.cmd_list = cmdstring
  926. # Append map to result map list.
  927. resultlist.append(map_i)
  928. t[0] = resultlist
  929. if self.debug:
  930. for map in resultlist:
  931. print map.cmd_list
  932. def p_s_expr_condition_if_relation(self, t):
  933. # Examples:
  934. # if({equal||during}, s_var_expr, A)
  935. """
  936. expr : IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds RPAREN
  937. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr RPAREN
  938. """
  939. relations, temporal, function= self.eval_toperator(t[3])
  940. ifmaplist = self.check_stds(t[5])
  941. thenmaplist = self.check_stds(t[7])
  942. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist,
  943. topolist = relations)
  944. resultlist = []
  945. for map_i in topolist:
  946. #print(map_i.get_id())
  947. # Loop over temporal related maps.
  948. tbrelations = map_i.get_temporal_relations()
  949. count = 0
  950. for topo in relations:
  951. if topo in tbrelations.keys():
  952. for map_j in (tbrelations[topo]):
  953. # Generate an intermediate map for the result map list.
  954. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  955. # Set first input for overlay module.
  956. mapainput = map_i.get_id()
  957. # Create overlayed map extent.
  958. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  959. temp_op = '=')
  960. # Stop the loop if no temporal or spatial relationship exist.
  961. if returncode == 0:
  962. break
  963. if count == 0:
  964. # Set map name.
  965. name = map_new.get_id()
  966. else:
  967. # Generate an intermediate map
  968. name = self.generate_map_name()
  969. # Set first input for overlay module.
  970. mapbinput = map_j.get_id()
  971. #print(mapbinput)
  972. #mapbinput = mapbinput.split('@')[0]
  973. # Create r.mapcalc expression string for the operation.
  974. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  975. cmdstring = "if(%s,%s)" %(map_new.cmd_list, mapbinput)
  976. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  977. cmdstring = "if(%s,%s)" %(mapainput, map_j.cmd_list)
  978. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  979. cmdstring = "if(%s,%s)" %(map_new.cmd_list, map_j.cmd_list)
  980. else:
  981. cmdstring = "if(%s,%s)" %(mapainput, mapbinput)
  982. # Conditional append of module command.
  983. map_new.cmd_list = cmdstring
  984. # Set new map name to temporary map name.
  985. #mapainput = name
  986. count += 1
  987. # Append map to result map list.
  988. if returncode == 1:
  989. resultlist.append(map_new)
  990. t[0] = resultlist
  991. if self.debug:
  992. for map in resultlist:
  993. print map.cmd_list
  994. def p_s_expr_condition_elif(self, t):
  995. # Examples:
  996. # if(s_var_expr, A, B)
  997. """
  998. expr : IF LPAREN s_var_expr COMMA stds COMMA stds RPAREN
  999. | IF LPAREN s_var_expr COMMA stds COMMA expr RPAREN
  1000. | IF LPAREN s_var_expr COMMA expr COMMA stds RPAREN
  1001. | IF LPAREN s_var_expr COMMA expr COMMA expr RPAREN
  1002. """
  1003. ifmaplist = self.check_stds(t[3])
  1004. thenmaplist = self.check_stds(t[5])
  1005. elsemaplist = self.check_stds(t[7])
  1006. resultlist = []
  1007. thendict = {}
  1008. elsedict = {}
  1009. # Get topologies for the appropriate conclusion term.
  1010. thentopolist = self.get_temporal_topo_list(ifmaplist, thenmaplist)
  1011. # Fill dictionaries with related maps for both conclusion terms.
  1012. for map_i in thentopolist:
  1013. thenrelations = map_i.get_temporal_relations()
  1014. relationmaps = thenrelations['EQUAL']
  1015. thendict[map_i.get_id()] = relationmaps
  1016. # Get topologies for the alternative conclusion term.
  1017. elsetopolist = self.get_temporal_topo_list(ifmaplist, elsemaplist)
  1018. for map_i in elsetopolist:
  1019. elserelations = map_i.get_temporal_relations()
  1020. relationmaps = elserelations['EQUAL']
  1021. elsedict[map_i.get_id()] = relationmaps
  1022. # Loop through conditional map list.
  1023. for map_i in ifmaplist:
  1024. if map_i.get_id() in thendict.keys():
  1025. thenlist = thendict[map_i.get_id()]
  1026. else:
  1027. thenlist = []
  1028. if map_i.get_id() in elsedict.keys():
  1029. elselist = elsedict[map_i.get_id()]
  1030. else:
  1031. elselist = []
  1032. # Set iteration amount to maximal or minimum number of related
  1033. # conclusion maps, depending on null map creation flag.
  1034. if self.null:
  1035. iternum = max(len(thenlist), len(elselist))
  1036. else:
  1037. iternum = min(len(thenlist), len(elselist))
  1038. # Calculate difference in conclusion lengths.
  1039. iterthen = iternum - len(thenlist)
  1040. iterelse = iternum - len(elselist)
  1041. # Extend null maps to the list to get conclusions with same length.
  1042. if iterthen != 0:
  1043. for i in range(iterthen):
  1044. thenlist.extend(['null()'])
  1045. if iterelse != 0:
  1046. for i in range(iterelse):
  1047. elselist.extend(['null()'])
  1048. # Combine the conclusions in a paired list.
  1049. conclusionlist = zip(thenlist, elselist)
  1050. for i in range(iternum):
  1051. conclusionmaps = conclusionlist[i]
  1052. # Generate an intermediate map for the result map list.
  1053. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1054. # Set first input for overlay module.
  1055. mapifinput = map_i.get_id()
  1056. # Get conclusion maps.
  1057. map_then = conclusionmaps[0]
  1058. map_else = conclusionmaps[1]
  1059. # Check if conclusions are map objects.
  1060. if map_then != 'null()':
  1061. # Create overlayed map extent.
  1062. returncode = self.overlay_map_extent(map_new, map_then, 'and', \
  1063. temp_op = '=')
  1064. maptheninput = map_then.get_id()
  1065. # Continue the loop if no temporal or spatial relationship exist.
  1066. if returncode == 0:
  1067. continue
  1068. else:
  1069. maptheninput = 'null()'
  1070. # Check if conclusions are map objects.
  1071. if map_else != 'null()':
  1072. # Create overlayed map extent.
  1073. returncode = self.overlay_map_extent(map_new, map_else, 'and', \
  1074. temp_op = '=')
  1075. mapelseinput = map_else.get_id()
  1076. # Continue the loop if no temporal or spatial relationship exist.
  1077. if returncode == 0:
  1078. continue
  1079. else:
  1080. mapelseinput = 'null()'
  1081. #if map_then != 'null()' and map_else != 'null()':
  1082. # Create r.mapcalc expression string for the operation.
  1083. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  1084. and "cmd_list" not in dir(map_else):
  1085. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  1086. mapelseinput)
  1087. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  1088. and "cmd_list" not in dir(map_else):
  1089. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  1090. mapelseinput)
  1091. elif "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  1092. and "cmd_list" in dir(map_else):
  1093. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  1094. map_else.cmd_list)
  1095. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  1096. and "cmd_list" in dir(map_else):
  1097. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  1098. map_else.cmd_list)
  1099. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  1100. and "cmd_list" not in dir(map_else):
  1101. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  1102. mapelseinput)
  1103. elif "cmd_list" not in dir(map_new) and "cmd_list" not in dir(map_then) \
  1104. and "cmd_list" in dir(map_else):
  1105. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  1106. map_else.cmd_list)
  1107. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  1108. and "cmd_list" in dir(map_else):
  1109. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  1110. map_else.cmd_list)
  1111. else:
  1112. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  1113. mapelseinput)
  1114. # Conditional append of module command.
  1115. map_new.cmd_list = cmdstring
  1116. # Append map to result map list.
  1117. if returncode == 1:
  1118. resultlist.append(map_new)
  1119. t[0] = resultlist
  1120. if self.debug:
  1121. for map in resultlist:
  1122. print map.cmd_list
  1123. def p_s_numeric_condition_elif(self, t):
  1124. # Examples:
  1125. # if(s_var_expr, 1, 2)
  1126. # if(A == 5, 10, 0)
  1127. """
  1128. expr : IF LPAREN s_var_expr COMMA number COMMA number RPAREN
  1129. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA number RPAREN
  1130. | IF LPAREN s_var_expr COMMA number COMMA NULL LPAREN RPAREN RPAREN
  1131. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA NULL LPAREN RPAREN RPAREN
  1132. """
  1133. ifmaplist = self.check_stds(t[3])
  1134. resultlist = []
  1135. # Select input for r.mapcalc expression based on length of PLY object.
  1136. if len(t) == 9:
  1137. numthen = t[5]
  1138. numelse = t[7]
  1139. elif len(t) == 11 and t[6] == '(':
  1140. numthen = t[5] + t[6] + t[7]
  1141. numelse = t[9]
  1142. elif len(t) == 11 and t[6] == ',':
  1143. numthen = t[5]
  1144. numelse = t[7] + t[8] + t[9]
  1145. elif len(t) == 13:
  1146. numthen = t[5] + t[6] + t[7]
  1147. numelse = t[9] + t[10] + t[11]
  1148. # Iterate over condition map list.
  1149. for map_i in ifmaplist:
  1150. mapinput = map_i.get_id()
  1151. # Create r.mapcalc expression string for the operation.
  1152. if "cmd_list" in dir(map_i):
  1153. cmdstring = "if(%s, %s, %s)" %(map_i.cmd_list, numthen, numelse)
  1154. else:
  1155. cmdstring = "if(%s, %s, %s)" %(mapinput, numthen, numelse)
  1156. # Conditional append of module command.
  1157. map_i.cmd_list = cmdstring
  1158. # Append map to result map list.
  1159. resultlist.append(map_i)
  1160. t[0] = resultlist
  1161. if self.debug:
  1162. for map in resultlist:
  1163. print map.cmd_list
  1164. def p_s_numeric_expr_condition_elif(self, t):
  1165. # Examples:
  1166. # if(s_var_expr, 1, A)
  1167. # if(A == 5 && C > 5, A, null())
  1168. """
  1169. expr : IF LPAREN s_var_expr COMMA number COMMA stds RPAREN
  1170. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  1171. | IF LPAREN s_var_expr COMMA number COMMA expr RPAREN
  1172. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  1173. | IF LPAREN s_var_expr COMMA stds COMMA number RPAREN
  1174. | IF LPAREN s_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  1175. | IF LPAREN s_var_expr COMMA expr COMMA number RPAREN
  1176. | IF LPAREN s_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  1177. """
  1178. ifmaplist = self.check_stds(t[3])
  1179. resultlist = []
  1180. thenmaplist = []
  1181. numthen = ''
  1182. elsemaplist = []
  1183. numelse = ''
  1184. # Select input for r.mapcalc expression based on length of PLY object.
  1185. if len(t) == 9:
  1186. try:
  1187. thenmaplist = self.check_stds(t[5])
  1188. except:
  1189. numthen = t[5]
  1190. try:
  1191. elsemaplist = self.check_stds(t[7])
  1192. except:
  1193. numelse = t[7]
  1194. elif len(t) == 11:
  1195. try:
  1196. thenmaplist = self.check_stds(t[5])
  1197. except:
  1198. numthen = t[5] + t[6] + t[7]
  1199. try:
  1200. elsemaplist = self.check_stds(t[9])
  1201. except:
  1202. numelse = t[7] + t[8] + t[9]
  1203. if thenmaplist != []:
  1204. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist)
  1205. elif elsemaplist != []:
  1206. topolist = self.get_temporal_topo_list(ifmaplist, elsemaplist)
  1207. if numthen != '':
  1208. numinput = numthen
  1209. elif numelse != '':
  1210. numinput = numelse
  1211. # Iterate over condition map lists with temporal relations.
  1212. for map_i in topolist:
  1213. # Loop over temporal related maps and create overlay modules.
  1214. tbrelations = map_i.get_temporal_relations()
  1215. count = 0
  1216. for map_j in (tbrelations['EQUAL']):
  1217. # Generate an intermediate map for the result map list.
  1218. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1219. # Set first input for overlay module.
  1220. mapainput = map_i.get_id()
  1221. # Create overlayed map extent.
  1222. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  1223. temp_op = '=')
  1224. # Stop the loop if no temporal or spatial relationship exist.
  1225. if returncode == 0:
  1226. break
  1227. if count == 0:
  1228. # Set map name.
  1229. name = map_new.get_id()
  1230. else:
  1231. # Generate an intermediate map
  1232. name = self.generate_map_name()
  1233. # Set first input for overlay module.
  1234. mapbinput = map_j.get_id()
  1235. # Create r.mapcalc expression string for the operation.
  1236. if thenmaplist != []:
  1237. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  1238. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, mapbinput, \
  1239. numinput)
  1240. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  1241. cmdstring = "if(%s,%s,%s)" %(mapainput, map_j.cmd_list, \
  1242. numinput)
  1243. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  1244. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, map_j.cmd_list, \
  1245. numinput)
  1246. else:
  1247. cmdstring = "if(%s,%s,%s)" %(mapainput, mapbinput, numinput)
  1248. if elsemaplist != []:
  1249. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  1250. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  1251. mapbinput)
  1252. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  1253. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, \
  1254. map_j.cmd_list)
  1255. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  1256. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  1257. map_j.cmd_list)
  1258. else:
  1259. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, mapbinput)
  1260. # Conditional append of module command.
  1261. map_new.cmd_list = cmdstring
  1262. # Set new map name to temporary map name.
  1263. #mapainput = name
  1264. count += 1
  1265. # Append map to result map list.
  1266. if returncode == 1:
  1267. resultlist.append(map_new)
  1268. t[0] = resultlist
  1269. if self.debug:
  1270. for map in resultlist:
  1271. print map.cmd_list
  1272. def p_s_numeric_expr_condition_elif_relation(self, t):
  1273. # Examples:
  1274. # if({during},s_var_expr, 1, A)
  1275. # if({during}, A == 5, A, null())
  1276. """
  1277. expr : IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA number COMMA stds RPAREN
  1278. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  1279. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA number COMMA expr RPAREN
  1280. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  1281. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA number RPAREN
  1282. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  1283. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA number RPAREN
  1284. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  1285. """
  1286. relations, temporal, function= self.eval_toperator(t[3])
  1287. ifmaplist = self.check_stds(t[5])
  1288. resultlist = []
  1289. thenmaplist = []
  1290. numthen = ''
  1291. elsemaplist = []
  1292. numelse = ''
  1293. # Select input for r.mapcalc expression based on length of PLY object.
  1294. if len(t) == 11:
  1295. try:
  1296. thenmaplist = self.check_stds(t[7])
  1297. except:
  1298. numthen = t[7]
  1299. try:
  1300. elsemaplist = self.check_stds(t[9])
  1301. except:
  1302. numelse = t[9]
  1303. elif len(t) == 13:
  1304. try:
  1305. thenmaplist = self.check_stds(t[7])
  1306. except:
  1307. numthen = t[9] + t[10] + t[11]
  1308. try:
  1309. elsemaplist = self.check_stds(t[11])
  1310. except:
  1311. numelse = t[9] + t[10] + t[11]
  1312. if thenmaplist != []:
  1313. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist, \
  1314. topolist = relations)
  1315. elif elsemaplist != []:
  1316. topolist = self.get_temporal_topo_list(ifmaplist, elsemaplist, \
  1317. topolist = relations)
  1318. if numthen != '':
  1319. numinput = numthen
  1320. elif numelse != '':
  1321. numinput = numelse
  1322. # Iterate over condition map lists with temporal relations.
  1323. for map_i in topolist:
  1324. # Loop over temporal related maps and create overlay modules.
  1325. tbrelations = map_i.get_temporal_relations()
  1326. count = 0
  1327. for topo in relations:
  1328. if topo in tbrelations.keys():
  1329. for map_j in (tbrelations[topo]):
  1330. # Generate an intermediate map for the result map list.
  1331. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1332. # Set first input for overlay module.
  1333. mapainput = map_i.get_id()
  1334. # Create overlayed map extent.
  1335. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  1336. temp_op = '=')
  1337. # Stop the loop if no temporal or spatial relationship exist.
  1338. if returncode == 0:
  1339. break
  1340. if count == 0:
  1341. # Set map name.
  1342. name = map_new.get_id()
  1343. else:
  1344. # Generate an intermediate map
  1345. name = self.generate_map_name()
  1346. # Set first input for overlay module.
  1347. mapbinput = map_j.get_id()
  1348. # Create r.mapcalc expression string for the operation.
  1349. if thenmaplist != []:
  1350. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  1351. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, mapbinput, \
  1352. numinput)
  1353. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  1354. cmdstring = "if(%s,%s,%s)" %(mapainput, map_j.cmd_list, \
  1355. numinput)
  1356. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  1357. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, map_j.cmd_list, \
  1358. numinput)
  1359. else:
  1360. cmdstring = "if(%s,%s,%s)" %(mapainput, mapbinput, numinput)
  1361. if elsemaplist != []:
  1362. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  1363. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  1364. mapbinput)
  1365. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  1366. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, \
  1367. map_j.cmd_list)
  1368. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  1369. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  1370. map_j.cmd_list)
  1371. else:
  1372. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, mapbinput)
  1373. # Conditional append of module command.
  1374. map_new.cmd_list = cmdstring
  1375. # Set new map name to temporary map name.
  1376. #mapainput = name
  1377. count += 1
  1378. # Append map to result map list.
  1379. if returncode == 1:
  1380. resultlist.append(map_new)
  1381. t[0] = resultlist
  1382. if self.debug:
  1383. for map in resultlist:
  1384. print map.cmd_list
  1385. def p_s_expr_condition_elif_relation(self, t):
  1386. # Examples:
  1387. # if({equal||during}, s_var_expr, A, B)
  1388. """
  1389. expr : IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA stds RPAREN
  1390. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA expr RPAREN
  1391. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA stds RPAREN
  1392. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA expr RPAREN
  1393. """
  1394. relations, temporal, function= self.eval_toperator(t[3])
  1395. ifmaplist = self.check_stds(t[5])
  1396. thenmaplist = self.check_stds(t[7])
  1397. elsemaplist = self.check_stds(t[9])
  1398. resultlist = []
  1399. thendict = {}
  1400. elsedict = {}
  1401. # Get topologies for the appropriate conclusion term.
  1402. thentopolist = self.get_temporal_topo_list(ifmaplist, thenmaplist, \
  1403. topolist = relations)
  1404. # Fill dictionaries with related maps for both conclusion terms.
  1405. for map_i in thentopolist:
  1406. thenrelations = map_i.get_temporal_relations()
  1407. relationmaps = []
  1408. for topo in relations:
  1409. if topo in thenrelations.keys():
  1410. relationmaps = relationmaps + thenrelations[topo]
  1411. thendict[map_i.get_id()] = relationmaps
  1412. # Get topologies for the alternative conclusion term.
  1413. elsetopolist = self.get_temporal_topo_list(ifmaplist, elsemaplist, \
  1414. topolist = relations)
  1415. for map_i in elsetopolist:
  1416. elserelations = map_i.get_temporal_relations()
  1417. relationmaps = []
  1418. for topo in relations:
  1419. if topo in elserelations.keys():
  1420. relationmaps = relationmaps + elserelations[topo]
  1421. elsedict[map_i.get_id()] = relationmaps
  1422. # Loop trough conditional map list.
  1423. for map_i in ifmaplist:
  1424. if map_i.get_id() in thendict.keys():
  1425. thenlist = thendict[map_i.get_id()]
  1426. else:
  1427. thenlist = []
  1428. if map_i.get_id() in elsedict.keys():
  1429. elselist = elsedict[map_i.get_id()]
  1430. else:
  1431. elselist = []
  1432. # Set iteration amount to maximal or minimum number of related
  1433. # conclusion maps, depending on null map creation flag.
  1434. if self.null:
  1435. iternum = max(len(thenlist), len(elselist))
  1436. else:
  1437. iternum = min(len(thenlist), len(elselist))
  1438. # Calculate difference in conclusion lengths.
  1439. iterthen = iternum - len(thenlist)
  1440. iterelse = iternum - len(elselist)
  1441. # Extend null maps to the list to get conclusions with same length.
  1442. if iterthen != 0:
  1443. for i in range(iterthen):
  1444. thenlist.extend(['null()'])
  1445. if iterelse != 0:
  1446. for i in range(iterelse):
  1447. elselist.extend(['null()'])
  1448. # Combine the conclusions in a paired list.
  1449. conclusionlist = zip(thenlist, elselist)
  1450. for i in range(iternum):
  1451. conclusionmaps = conclusionlist[i]
  1452. # Generate an intermediate map for the result map list.
  1453. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1454. # Set first input for overlay module.
  1455. mapifinput = map_i.get_id()
  1456. # Get conclusion maps.
  1457. map_then = conclusionmaps[0]
  1458. map_else = conclusionmaps[1]
  1459. # Check if conclusions are map objects.
  1460. if map_then != 'null()':
  1461. # Create overlayed map extent.
  1462. returncode = self.overlay_map_extent(map_new, map_then, 'and', \
  1463. temp_op = '=')
  1464. maptheninput = map_then.get_id()
  1465. # Continue the loop if no temporal or spatial relationship exist.
  1466. if returncode == 0:
  1467. continue
  1468. else:
  1469. maptheninput = 'null()'
  1470. # Check if conclusions are map objects.
  1471. if map_else != 'null()':
  1472. # Create overlayed map extent.
  1473. returncode = self.overlay_map_extent(map_new, map_else, 'and', \
  1474. temp_op = '=')
  1475. mapelseinput = map_else.get_id()
  1476. # Continue the loop if no temporal or spatial relationship exist.
  1477. if returncode == 0:
  1478. continue
  1479. else:
  1480. mapelseinput = 'null()'
  1481. #if map_then != 'null()' and map_else != 'null()':
  1482. # Create r.mapcalc expression string for the operation.
  1483. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  1484. and "cmd_list" not in dir(map_else):
  1485. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  1486. mapelseinput)
  1487. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  1488. and "cmd_list" not in dir(map_else):
  1489. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  1490. mapelseinput)
  1491. elif "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  1492. and "cmd_list" in dir(map_else):
  1493. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  1494. map_else.cmd_list)
  1495. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  1496. and "cmd_list" in dir(map_else):
  1497. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  1498. map_else.cmd_list)
  1499. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  1500. and "cmd_list" not in dir(map_else):
  1501. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  1502. mapelseinput)
  1503. elif "cmd_list" not in dir(map_new) and "cmd_list" not in dir(map_then) \
  1504. and "cmd_list" in dir(map_else):
  1505. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  1506. map_else.cmd_list)
  1507. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  1508. and "cmd_list" in dir(map_else):
  1509. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  1510. map_else.cmd_list)
  1511. else:
  1512. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  1513. mapelseinput)
  1514. # Conditional append of module command.
  1515. map_new.cmd_list = cmdstring
  1516. # Append map to result map list.
  1517. if returncode == 1:
  1518. resultlist.append(map_new)
  1519. t[0] = resultlist
  1520. if self.debug:
  1521. for map in resultlist:
  1522. print map.cmd_list
  1523. def p_ts_var_expr1(self, t):
  1524. # Combination of spatial and temporal conditional expressions.
  1525. # Examples:
  1526. # A <= 2 || start_date <= 2013-01-01
  1527. # end_date > 2013-01-15 && A > 10
  1528. # IMPORTANT: Only the intersection of map lists in conditionals are
  1529. # exported.
  1530. """
  1531. ts_var_expr : s_var_expr AND AND t_var_expr
  1532. | t_var_expr AND AND s_var_expr
  1533. | t_var_expr OR OR s_var_expr
  1534. | s_var_expr OR OR t_var_expr
  1535. | ts_var_expr AND AND s_var_expr
  1536. | ts_var_expr AND AND t_var_expr
  1537. | ts_var_expr OR OR s_var_expr
  1538. | ts_var_expr OR OR t_var_expr
  1539. | s_var_expr AND AND ts_var_expr
  1540. | t_var_expr AND AND ts_var_expr
  1541. | s_var_expr OR OR ts_var_expr
  1542. | t_var_expr OR OR ts_var_expr
  1543. """
  1544. # Check whether inputs are map lists or global temporal variables and
  1545. # store each in separate lists.
  1546. ts_var_dict = {"temporal" : [], "spatial" : []}
  1547. temporal_list = []
  1548. spatial_list = []
  1549. operator = t[2] + t[3]
  1550. temporalop = GlobalTemporalVar()
  1551. temporalop.relationop = operator
  1552. temporalop.topology.append("EQUAL")
  1553. if isinstance(t[1], dict) and "spatial" in t[1]:
  1554. temporal_list = temporal_list + t[1]["temporal"]
  1555. spatial_list.append(t[1]["spatial"])
  1556. elif isinstance(t[1], list):
  1557. if all([isinstance(i, ta.GlobalTemporalVar) for i in t[1]]):
  1558. temporal_list = temporal_list + t[1]
  1559. else:
  1560. tsexprA = self.check_stds(t[1])
  1561. spatial_list.append(tsexprA)
  1562. elif isinstance(t[1], ta.GlobalTemporalVar):
  1563. temporal_list.append(t[1])
  1564. if temporal_list != [] and \
  1565. isinstance(t[4], ta.GlobalTemporalVar):
  1566. temporal_list.append(temporalop)
  1567. if temporal_list != [] and \
  1568. isinstance(t[4], list) and \
  1569. all([isinstance(i, ta.GlobalTemporalVar) for i in t[4]]):
  1570. temporal_list.append(temporalop)
  1571. if isinstance(t[4], dict) and "spatial" in t[4]:
  1572. temporal_list = temporal_list + t[4]["temporal"]
  1573. spatial_list.append(t[4]["spatial"])
  1574. elif isinstance(t[4], list):
  1575. if all([isinstance(i, ta.GlobalTemporalVar) for i in t[4]]):
  1576. temporal_list = temporal_list + t[4]
  1577. else:
  1578. tsexprB = self.check_stds(t[4])
  1579. spatial_list.append(tsexprB)
  1580. elif isinstance(t[4], ta.GlobalTemporalVar):
  1581. temporal_list.append(t[4])
  1582. ts_var_dict["temporal"] = temporal_list
  1583. # Condition for two map lists in spatio temporal expression.
  1584. if len(spatial_list) == 2:
  1585. # Build topology for both map lists in spatio temporal expression.
  1586. topolist = self.get_temporal_topo_list(spatial_list[0], spatial_list[1])
  1587. resultlist = []
  1588. for map_i in topolist:
  1589. # Loop over temporal related maps and create overlay modules.
  1590. tbrelations = map_i.get_temporal_relations()
  1591. count = 0
  1592. for map_j in (tbrelations['EQUAL']):
  1593. # Generate an intermediate map for the result map list.
  1594. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1595. # Set first input for overlay module.
  1596. mapainput = map_i.get_id()
  1597. # Create overlayed map extent.
  1598. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  1599. temp_op = '=')
  1600. # Stop the loop if no temporal or spatial relationship exist.
  1601. if returncode == 0:
  1602. break
  1603. if count == 0:
  1604. # Set map name.
  1605. name = map_new.get_id()
  1606. else:
  1607. # Generate an intermediate map
  1608. name = self.generate_map_name()
  1609. # Set first input for overlay module.
  1610. mapbinput = map_j.get_id()
  1611. # Create r.mapcalc expression string for the operation.
  1612. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  1613. cmdstring = "%s %s %s" %(map_new.cmd_list, operator, mapbinput)
  1614. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  1615. cmdstring = "%s %s %s" %(mapainput, operator, map_j.cmd_list)
  1616. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  1617. cmdstring = "%s %s %s" %(map_new.cmd_list, operator, map_j.cmd_list)
  1618. else:
  1619. cmdstring = "%s %s %s" %(mapainput, operator, mapbinput)
  1620. # Conditional append of module command.
  1621. map_new.cmd_list = cmdstring
  1622. # Set new map name to temporary map name.
  1623. #mapainput = name
  1624. count += 1
  1625. # Append map to result map list.
  1626. if returncode == 1:
  1627. resultlist.append(map_new)
  1628. # Return dictionary with spatial map list of temporal
  1629. # intersected maps and temporal expression in list form.
  1630. ts_var_dict["spatial"] = resultlist
  1631. # Condition for only one map list in spatio temporal expression.
  1632. elif len(spatial_list) == 1:
  1633. ts_var_dict["spatial"] = spatial_list[0]
  1634. t[0] = ts_var_dict
  1635. def p_ts_expr_condition_if(self, t):
  1636. # Examples:
  1637. # if(ts_var_expr, A)
  1638. # if(start_year == 2013 || B != 5, A)
  1639. """
  1640. expr : IF LPAREN ts_var_expr COMMA stds RPAREN
  1641. | IF LPAREN ts_var_expr COMMA expr RPAREN
  1642. """
  1643. ts_var_dict = t[3]
  1644. spatialcond = ts_var_dict["spatial"]
  1645. # Extract spatial map list from condition.
  1646. ifmaplist = self.check_stds(spatialcond)
  1647. thenmaplist = self.check_stds(t[5])
  1648. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist)
  1649. resultlist = []
  1650. resultspatial = []
  1651. for map_i in topolist:
  1652. #print(map_i.get_id())
  1653. # Loop over temporal related maps and create overlay modules.
  1654. tbrelations = map_i.get_temporal_relations()
  1655. count = 0
  1656. for map_j in (tbrelations['EQUAL']):
  1657. # Generate an intermediate map for the result map list.
  1658. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1659. # Set first input for overlay module.
  1660. mapainput = map_i.get_id()
  1661. # Create overlayed map extent.
  1662. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  1663. temp_op = '=')
  1664. # Stop the loop if no temporal or spatial relationship exist.
  1665. if returncode == 0:
  1666. break
  1667. if count == 0:
  1668. # Set map name.
  1669. name = map_new.get_id()
  1670. else:
  1671. # Generate an intermediate map
  1672. name = self.generate_map_name()
  1673. # Set first input for overlay module.
  1674. mapbinput = map_j.get_id()
  1675. # Create r.mapcalc expression string for the operation.
  1676. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  1677. cmdstring = "if(%s,%s)" %(map_new.cmd_list, mapbinput)
  1678. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  1679. cmdstring = "if(%s,%s)" %(mapainput, map_j.cmd_list)
  1680. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  1681. cmdstring = "if(%s,%s)" %(map_new.cmd_list, map_j.cmd_list)
  1682. else:
  1683. cmdstring = "if(%s,%s)" %(mapainput, mapbinput)
  1684. # Conditional append of module command.
  1685. map_new.cmd_list = cmdstring
  1686. # Set new map name to temporary map name.
  1687. #mapainput = name
  1688. count += 1
  1689. # Append map to result map list.
  1690. if returncode == 1:
  1691. resultspatial.append(map_new)
  1692. # Evaluate temporal statements in spatio temporal condition.
  1693. #if len(ts_var_dict["temporal"]) == 1:
  1694. #temporalcond = ts_var_dict["temporal"][0]
  1695. #else:
  1696. temporalcond = ts_var_dict["temporal"]
  1697. resultspatial = self.check_stds(resultspatial)
  1698. thencond = self.build_condition_list(temporalcond, resultspatial)
  1699. thenresult = self.eval_condition_list(thencond)
  1700. # Clear the map list.
  1701. resultlist = self.check_stds(thenresult, clear = True)
  1702. t[0] = resultlist
  1703. if self.debug:
  1704. for map in resultlist:
  1705. print map.cmd_list
  1706. def p_ts_expr_condition_if_relation(self, t):
  1707. # Examples:
  1708. # if({equal||during}, ts_var_expr, A)
  1709. # if({starts||during}, B > 2 || end_month() == 4, A)
  1710. """
  1711. expr : IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds RPAREN
  1712. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr RPAREN
  1713. """
  1714. relations, temporal, function= self.eval_toperator(t[3])
  1715. ts_var_dict = t[5]
  1716. spatialcond = ts_var_dict["spatial"]
  1717. # Extract spatial map list from condition.
  1718. ifmaplist = self.check_stds(spatialcond)
  1719. thenmaplist = self.check_stds(t[7])
  1720. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist,
  1721. topolist = relations)
  1722. resultspatial = []
  1723. for map_i in topolist:
  1724. #print(map_i.get_id())
  1725. # Loop over temporal related maps and create overlay modules.
  1726. tbrelations = map_i.get_temporal_relations()
  1727. count = 0
  1728. for topo in relations:
  1729. if topo in tbrelations.keys():
  1730. for map_j in (tbrelations[topo]):
  1731. # Generate an intermediate map for the result map list.
  1732. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1733. # Set first input for overlay module.
  1734. mapainput = map_i.get_id()
  1735. # Create overlayed map extent.
  1736. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  1737. temp_op = '=')
  1738. # Stop the loop if no temporal or spatial relationship exist.
  1739. if returncode == 0:
  1740. break
  1741. if count == 0:
  1742. # Set map name.
  1743. name = map_new.get_id()
  1744. else:
  1745. # Generate an intermediate map
  1746. name = self.generate_map_name()
  1747. # Set first input for overlay module.
  1748. mapbinput = map_j.get_id()
  1749. # Create r.mapcalc expression string for the operation.
  1750. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  1751. cmdstring = "if(%s,%s)" %(map_new.cmd_list, mapbinput)
  1752. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  1753. cmdstring = "if(%s,%s)" %(mapainput, map_j.cmd_list)
  1754. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  1755. cmdstring = "if(%s,%s)" %(map_new.cmd_list, map_j.cmd_list)
  1756. else:
  1757. cmdstring = "if(%s,%s)" %(mapainput, mapbinput)
  1758. # Conditional append of module command.
  1759. map_new.cmd_list = cmdstring
  1760. # Set new map name to temporary map name.
  1761. #mapainput = name
  1762. count += 1
  1763. # Append map to result map list.
  1764. if returncode == 1:
  1765. resultspatial.append(map_new)
  1766. # Evaluate temporal statements in spatio temporal condition.
  1767. #if len(ts_var_dict["temporal"]) == 1:
  1768. #temporalcond = ts_var_dict["temporal"][0]
  1769. #else:
  1770. temporalcond = ts_var_dict["temporal"]
  1771. resultspatial = self.check_stds(resultspatial)
  1772. thencond = self.build_condition_list(temporalcond, resultspatial, relations)
  1773. thenresult = self.eval_condition_list(thencond)
  1774. # Clear the map list.
  1775. resultlist = self.check_stds(thenresult, clear = True)
  1776. t[0] = resultlist
  1777. if self.debug:
  1778. for map in resultlist:
  1779. print map.cmd_list
  1780. def p_ts_numeric_condition_if(self, t):
  1781. # Examples:
  1782. # if(ts_var_expr, 1)
  1783. # if(A == 5 && start_day() > 5, 10)
  1784. """
  1785. expr : IF LPAREN ts_var_expr COMMA number RPAREN
  1786. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN RPAREN
  1787. """
  1788. ts_var_dict = t[3]
  1789. spatialcond = ts_var_dict["spatial"]
  1790. ifmaplist = self.check_stds(spatialcond)
  1791. resultspatial = []
  1792. # Select input for r.mapcalc expression based on length of PLY object.
  1793. if len(t) == 7:
  1794. numinput = t[5]
  1795. elif len(t) == 9:
  1796. numinput = t[5] + t[6] + t[7]
  1797. # Iterate over condition map list.
  1798. for map_i in ifmaplist:
  1799. mapinput = map_i.get_id()
  1800. # Create r.mapcalc expression string for the operation.
  1801. if "cmd_list" in dir(map_i):
  1802. cmdstring = "if(%s,%s)" %(map_i.cmd_list, numinput)
  1803. else:
  1804. cmdstring = "if(%s,%s)" %(mapinput, numinput)
  1805. # Conditional append of module command.
  1806. map_i.cmd_list = cmdstring
  1807. # Append map to result map list.
  1808. resultspatial.append(map_i)
  1809. temporalcond = ts_var_dict["temporal"]
  1810. resultspatial = self.check_stds(resultspatial)
  1811. thencond = self.build_condition_list(temporalcond, resultspatial)
  1812. thenresult = self.eval_condition_list(thencond)
  1813. # Clear the map list.
  1814. resultlist = self.check_stds(thenresult, clear = True)
  1815. t[0] = resultlist
  1816. if self.debug:
  1817. for map in resultlist:
  1818. print map.cmd_list
  1819. def p_ts_expr_condition_elif(self, t):
  1820. # Examples:
  1821. # if(s_var_expr, A, B)
  1822. # if(start_day() < 20 && B > 2, A, B)
  1823. """
  1824. expr : IF LPAREN ts_var_expr COMMA stds COMMA stds RPAREN
  1825. | IF LPAREN ts_var_expr COMMA stds COMMA expr RPAREN
  1826. | IF LPAREN ts_var_expr COMMA expr COMMA stds RPAREN
  1827. | IF LPAREN ts_var_expr COMMA expr COMMA expr RPAREN
  1828. """
  1829. ts_var_dict = t[3]
  1830. spatialcond = ts_var_dict["spatial"]
  1831. ifmaplist = self.check_stds(spatialcond)
  1832. thenmaplist = self.check_stds(t[5])
  1833. elsemaplist = self.check_stds(t[7])
  1834. resultspatial = []
  1835. thendict = {}
  1836. elsedict = {}
  1837. # Get topologies for the appropriate conclusion term.
  1838. thentopolist = self.get_temporal_topo_list(ifmaplist, thenmaplist)
  1839. # Fill dictionaries with related maps for both conclusion terms.
  1840. for map_i in thentopolist:
  1841. thenrelations = map_i.get_temporal_relations()
  1842. relationmaps = thenrelations['EQUAL']
  1843. thendict[map_i.get_id()] = relationmaps
  1844. # Get topologies for the alternative conclusion term.
  1845. elsetopolist = self.get_temporal_topo_list(ifmaplist, elsemaplist)
  1846. for map_i in elsetopolist:
  1847. elserelations = map_i.get_temporal_relations()
  1848. relationmaps = elserelations['EQUAL']
  1849. elsedict[map_i.get_id()] = relationmaps
  1850. # Loop through conditional map list.
  1851. for map_i in ifmaplist:
  1852. if map_i.get_id() in thendict.keys():
  1853. thenlist = thendict[map_i.get_id()]
  1854. else:
  1855. thenlist = []
  1856. if map_i.get_id() in elsedict.keys():
  1857. elselist = elsedict[map_i.get_id()]
  1858. else:
  1859. elselist = []
  1860. # Set iteration amount to maximal or minimum number of related
  1861. # conclusion maps, depending on null map creation flag.
  1862. if self.null:
  1863. iternum = max(len(thenlist), len(elselist))
  1864. else:
  1865. iternum = min(len(thenlist), len(elselist))
  1866. # Calculate difference in conclusion lengths.
  1867. iterthen = iternum - len(thenlist)
  1868. iterelse = iternum - len(elselist)
  1869. # Extend null maps to the list to get conclusions with same length.
  1870. if iterthen != 0:
  1871. for i in range(iterthen):
  1872. thenlist.extend(['null()'])
  1873. if iterelse != 0:
  1874. for i in range(iterelse):
  1875. elselist.extend(['null()'])
  1876. # Combine the conclusions in a paired list.
  1877. conclusionlist = zip(thenlist, elselist)
  1878. for i in range(iternum):
  1879. conclusionmaps = conclusionlist[i]
  1880. # Generate an intermediate map for the result map list.
  1881. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  1882. # Set first input for overlay module.
  1883. mapifinput = map_i.get_id()
  1884. # Get conclusion maps.
  1885. map_then = conclusionmaps[0]
  1886. map_else = conclusionmaps[1]
  1887. # Check if conclusions are map objects.
  1888. if map_then != 'null()':
  1889. # Create overlayed map extent.
  1890. returncode = self.overlay_map_extent(map_new, map_then, 'and', \
  1891. temp_op = '=')
  1892. maptheninput = map_then.get_id()
  1893. # Continue the loop if no temporal or spatial relationship exist.
  1894. if returncode == 0:
  1895. continue
  1896. else:
  1897. maptheninput = 'null()'
  1898. # Check if conclusions are map objects.
  1899. if map_else != 'null()':
  1900. # Create overlayed map extent.
  1901. returncode = self.overlay_map_extent(map_new, map_else, 'and', \
  1902. temp_op = '=')
  1903. mapelseinput = map_else.get_id()
  1904. # Continue the loop if no temporal or spatial relationship exist.
  1905. if returncode == 0:
  1906. continue
  1907. else:
  1908. mapelseinput = 'null()'
  1909. #if map_then != 'null()' and map_else != 'null()':
  1910. # Create r.mapcalc expression string for the operation.
  1911. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  1912. and "cmd_list" not in dir(map_else):
  1913. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  1914. mapelseinput)
  1915. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  1916. and "cmd_list" not in dir(map_else):
  1917. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  1918. mapelseinput)
  1919. elif "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  1920. and "cmd_list" in dir(map_else):
  1921. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  1922. map_else.cmd_list)
  1923. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  1924. and "cmd_list" in dir(map_else):
  1925. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  1926. map_else.cmd_list)
  1927. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  1928. and "cmd_list" not in dir(map_else):
  1929. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  1930. mapelseinput)
  1931. elif "cmd_list" not in dir(map_new) and "cmd_list" not in dir(map_then) \
  1932. and "cmd_list" in dir(map_else):
  1933. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  1934. map_else.cmd_list)
  1935. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  1936. and "cmd_list" in dir(map_else):
  1937. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  1938. map_else.cmd_list)
  1939. else:
  1940. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  1941. mapelseinput)
  1942. # Conditional append of module command.
  1943. map_new.cmd_list = cmdstring
  1944. # Append map to result map list.
  1945. if returncode == 1:
  1946. resultspatial.append(map_new)
  1947. temporalcond = ts_var_dict["temporal"]
  1948. resultspatial = self.check_stds(resultspatial)
  1949. thencond = self.build_condition_list(temporalcond, resultspatial)
  1950. thenresult = self.eval_condition_list(thencond)
  1951. #elseresult = self.eval_condition_list(thencond, inverse = True)
  1952. # Combine and sort else and then statement to result map list.
  1953. #combilist = thenresult + elseresult
  1954. #resultlist = sorted(combilist, key = AbstractDatasetComparisonKeyStartTime)
  1955. # Clear the map list.
  1956. #resultlist = self.check_stds(resultlist, clear = True)
  1957. resultlist = self.check_stds(thenresult, clear = True)
  1958. t[0] = resultlist
  1959. if self.debug:
  1960. for map in resultlist:
  1961. print map.cmd_list
  1962. def p_ts_expr_condition_elif_relation(self, t):
  1963. # Examples:
  1964. # if({equal||during}, s_var_expr, A, B)
  1965. # if({contains}, start_day() == 3 || C != 2, A, B)
  1966. """
  1967. expr : IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA stds RPAREN
  1968. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA expr RPAREN
  1969. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA stds RPAREN
  1970. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA expr RPAREN
  1971. """
  1972. relations, temporal, function= self.eval_toperator(t[3])
  1973. ts_var_dict = t[5]
  1974. spatialcond = ts_var_dict["spatial"]
  1975. # Extract spatial map list from condition.
  1976. ifmaplist = self.check_stds(spatialcond)
  1977. thenmaplist = self.check_stds(t[7])
  1978. elsemaplist = self.check_stds(t[9])
  1979. resultspatial = []
  1980. thendict = {}
  1981. elsedict = {}
  1982. # Get topologies for the appropriate conclusion term.
  1983. thentopolist = self.get_temporal_topo_list(ifmaplist, thenmaplist, \
  1984. topolist = relations)
  1985. # Fill dictionaries with related maps for both conclusion terms.
  1986. for map_i in thentopolist:
  1987. thenrelations = map_i.get_temporal_relations()
  1988. relationmaps = []
  1989. for topo in relations:
  1990. if topo in thenrelations.keys():
  1991. relationmaps = relationmaps + thenrelations[topo]
  1992. thendict[map_i.get_id()] = relationmaps
  1993. # Get topologies for the alternative conclusion term.
  1994. elsetopolist = self.get_temporal_topo_list(ifmaplist, elsemaplist, \
  1995. topolist = relations)
  1996. for map_i in elsetopolist:
  1997. elserelations = map_i.get_temporal_relations()
  1998. relationmaps = []
  1999. for topo in relations:
  2000. if topo in elserelations.keys():
  2001. relationmaps = relationmaps + elserelations[topo]
  2002. elsedict[map_i.get_id()] = relationmaps
  2003. # Loop trough conditional map list.
  2004. for map_i in ifmaplist:
  2005. if map_i.get_id() in thendict.keys():
  2006. thenlist = thendict[map_i.get_id()]
  2007. else:
  2008. thenlist = []
  2009. if map_i.get_id() in elsedict.keys():
  2010. elselist = elsedict[map_i.get_id()]
  2011. else:
  2012. elselist = []
  2013. # Set iteration amount to maximal or minimum number of related
  2014. # conclusion maps, depending on null map creation flag.
  2015. if self.null:
  2016. iternum = max(len(thenlist), len(elselist))
  2017. else:
  2018. iternum = min(len(thenlist), len(elselist))
  2019. # Calculate difference in conclusion lengths.
  2020. iterthen = iternum - len(thenlist)
  2021. iterelse = iternum - len(elselist)
  2022. # Extend null maps to the list to get conclusions with same length.
  2023. if iterthen != 0:
  2024. for i in range(iterthen):
  2025. thenlist.extend(['null()'])
  2026. if iterelse != 0:
  2027. for i in range(iterelse):
  2028. elselist.extend(['null()'])
  2029. # Combine the conclusions in a paired list.
  2030. conclusionlist = zip(thenlist, elselist)
  2031. for i in range(iternum):
  2032. conclusionmaps = conclusionlist[i]
  2033. # Generate an intermediate map for the result map list.
  2034. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  2035. # Set first input for overlay module.
  2036. mapifinput = map_i.get_id()
  2037. # Get conclusion maps.
  2038. map_then = conclusionmaps[0]
  2039. map_else = conclusionmaps[1]
  2040. # Check if conclusions are map objects.
  2041. if map_then != 'null()':
  2042. # Create overlayed map extent.
  2043. returncode = self.overlay_map_extent(map_new, map_then, 'and', \
  2044. temp_op = '=')
  2045. maptheninput = map_then.get_id()
  2046. # Continue the loop if no temporal or spatial relationship exist.
  2047. if returncode == 0:
  2048. continue
  2049. else:
  2050. maptheninput = 'null()'
  2051. # Check if conclusions are map objects.
  2052. if map_else != 'null()':
  2053. # Create overlayed map extent.
  2054. returncode = self.overlay_map_extent(map_new, map_else, 'and', \
  2055. temp_op = '=')
  2056. mapelseinput = map_else.get_id()
  2057. # Continue the loop if no temporal or spatial relationship exist.
  2058. if returncode == 0:
  2059. continue
  2060. else:
  2061. mapelseinput = 'null()'
  2062. #if map_then != 'null()' and map_else != 'null()':
  2063. # Create r.mapcalc expression string for the operation.
  2064. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  2065. and "cmd_list" not in dir(map_else):
  2066. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  2067. mapelseinput)
  2068. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  2069. and "cmd_list" not in dir(map_else):
  2070. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  2071. mapelseinput)
  2072. elif "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_then) \
  2073. and "cmd_list" in dir(map_else):
  2074. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, maptheninput,
  2075. map_else.cmd_list)
  2076. elif "cmd_list" in dir(map_new) and "cmd_list" in dir(map_then) \
  2077. and "cmd_list" in dir(map_else):
  2078. cmdstring = "if(%s, %s, %s)" %(map_new.cmd_list, map_then.cmd_list,
  2079. map_else.cmd_list)
  2080. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  2081. and "cmd_list" not in dir(map_else):
  2082. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  2083. mapelseinput)
  2084. elif "cmd_list" not in dir(map_new) and "cmd_list" not in dir(map_then) \
  2085. and "cmd_list" in dir(map_else):
  2086. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  2087. map_else.cmd_list)
  2088. elif "cmd_list" not in dir(map_new) and "cmd_list" in dir(map_then) \
  2089. and "cmd_list" in dir(map_else):
  2090. cmdstring = "if(%s, %s, %s)" %(mapifinput, map_then.cmd_list,
  2091. map_else.cmd_list)
  2092. else:
  2093. cmdstring = "if(%s, %s, %s)" %(mapifinput, maptheninput,
  2094. mapelseinput)
  2095. # Conditional append of module command.
  2096. map_new.cmd_list = cmdstring
  2097. # Append map to result map list.
  2098. if returncode == 1:
  2099. resultspatial.append(map_new)
  2100. temporalcond = ts_var_dict["temporal"]
  2101. resultspatial = self.check_stds(resultspatial)
  2102. thencond = self.build_condition_list(temporalcond, resultspatial, relations)
  2103. thenresult = self.eval_condition_list(thencond)
  2104. #elseresult = self.eval_condition_list(thencond, inverse = True)
  2105. # Combine and sort else and then statement to result map list.
  2106. #combilist = thenresult + elseresult
  2107. #resultlist = sorted(combilist, key = AbstractDatasetComparisonKeyStartTime)
  2108. # Clear the map list.
  2109. resultlist = self.check_stds(thenresult, clear = True)
  2110. t[0] = resultlist
  2111. if self.debug:
  2112. for map in resultlist:
  2113. print map.cmd_list
  2114. def p_ts_numeric_condition_elif(self, t):
  2115. # Examples:
  2116. # if(ts_var_expr, 1, 2)
  2117. # if(A == 1 || end_year == 2013, 10, null())
  2118. """
  2119. expr : IF LPAREN ts_var_expr COMMA number COMMA number RPAREN
  2120. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA number RPAREN
  2121. | IF LPAREN ts_var_expr COMMA number COMMA NULL LPAREN RPAREN RPAREN
  2122. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA NULL LPAREN RPAREN RPAREN
  2123. """
  2124. ts_var_dict = t[3]
  2125. spatialcond = ts_var_dict["spatial"]
  2126. # Extract spatial map list from condition.
  2127. ifmaplist = self.check_stds(spatialcond)
  2128. resultspatial = []
  2129. # Select input for r.mapcalc expression based on length of PLY object.
  2130. if len(t) == 9:
  2131. numthen = t[5]
  2132. numelse = t[7]
  2133. elif len(t) == 11 and t[6] == '(':
  2134. numthen = t[5] + t[6] + t[7]
  2135. numelse = t[9]
  2136. elif len(t) == 11 and t[6] == ',':
  2137. numthen = t[5]
  2138. numelse = t[7] + t[8] + t[9]
  2139. elif len(t) == 13:
  2140. numthen = t[5] + t[6] + t[7]
  2141. numelse = t[9] + t[10] + t[11]
  2142. # Iterate over condition map list.
  2143. for map_i in ifmaplist:
  2144. mapinput = map_i.get_id()
  2145. # Create r.mapcalc expression string for the operation.
  2146. if "cmd_list" in dir(map_i):
  2147. cmdstring = "if(%s, %s, %s)" %(map_i.cmd_list, numthen, numelse)
  2148. else:
  2149. cmdstring = "if(%s, %s, %s)" %(mapinput, numthen, numelse)
  2150. # Conditional append of module command.
  2151. map_i.cmd_list = cmdstring
  2152. # Append map to result map list.
  2153. resultspatial.append(map_i)
  2154. temporalcond = ts_var_dict["temporal"]
  2155. resultspatial = self.check_stds(resultspatial)
  2156. thencond = self.build_condition_list(temporalcond, resultspatial)
  2157. thenresult = self.eval_condition_list(thencond)
  2158. #elseresult = self.eval_condition_list(thencond, inverse = True)
  2159. # Combine and sort else and then statement to result map list.
  2160. #combilist = thenresult + elseresult
  2161. #resultlist = sorted(combilist, key = AbstractDatasetComparisonKeyStartTime)
  2162. # Clear the map list.
  2163. #resultlist = self.check_stds(resultlist, clear = True)
  2164. resultlist = self.check_stds(thenresult, clear = True)
  2165. t[0] = resultlist
  2166. def p_ts_numeric_expr_condition_elif(self, t):
  2167. # Examples:
  2168. # if(ts_var_expr, 1, A)
  2169. # if(A == 5 && start_day() > 5, A, null())
  2170. """
  2171. expr : IF LPAREN ts_var_expr COMMA number COMMA stds RPAREN
  2172. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  2173. | IF LPAREN ts_var_expr COMMA number COMMA expr RPAREN
  2174. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  2175. | IF LPAREN ts_var_expr COMMA stds COMMA number RPAREN
  2176. | IF LPAREN ts_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  2177. | IF LPAREN ts_var_expr COMMA expr COMMA number RPAREN
  2178. | IF LPAREN ts_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  2179. """
  2180. ts_var_dict = t[3]
  2181. spatialcond = ts_var_dict["spatial"]
  2182. ifmaplist = self.check_stds(spatialcond)
  2183. resultspatial = []
  2184. thenmaplist = []
  2185. numthen = ''
  2186. elsemaplist = []
  2187. numelse = ''
  2188. # Select input for r.mapcalc expression based on length of PLY object.
  2189. if len(t) == 9:
  2190. try:
  2191. thenmaplist = self.check_stds(t[5])
  2192. except:
  2193. numthen = t[5]
  2194. try:
  2195. elsemaplist = self.check_stds(t[7])
  2196. except:
  2197. numelse = t[7]
  2198. elif len(t) == 11:
  2199. try:
  2200. thenmaplist = self.check_stds(t[5])
  2201. except:
  2202. numthen = t[5] + t[6] + t[7]
  2203. try:
  2204. elsemaplist = self.check_stds(t[9])
  2205. except:
  2206. numelse = t[7] + t[8] + t[9]
  2207. if thenmaplist != []:
  2208. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist)
  2209. elif elsemaplist != []:
  2210. topolist = self.get_temporal_topo_list(ifmaplist, elsemaplist)
  2211. if numthen != '':
  2212. numinput = numthen
  2213. elif numelse != '':
  2214. numinput = numelse
  2215. # Iterate over condition map lists with temporal relations.
  2216. for map_i in topolist:
  2217. # Loop over temporal related maps and create overlay modules.
  2218. tbrelations = map_i.get_temporal_relations()
  2219. count = 0
  2220. for map_j in (tbrelations['EQUAL']):
  2221. # Generate an intermediate map for the result map list.
  2222. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  2223. # Set first input for overlay module.
  2224. mapainput = map_i.get_id()
  2225. # Create overlayed map extent.
  2226. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  2227. temp_op = '=')
  2228. # Stop the loop if no temporal or spatial relationship exist.
  2229. if returncode == 0:
  2230. break
  2231. if count == 0:
  2232. # Set map name.
  2233. name = map_new.get_id()
  2234. else:
  2235. # Generate an intermediate map
  2236. name = self.generate_map_name()
  2237. # Set first input for overlay module.
  2238. mapbinput = map_j.get_id()
  2239. # Create r.mapcalc expression string for the operation.
  2240. if thenmaplist != []:
  2241. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  2242. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, mapbinput, \
  2243. numinput)
  2244. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  2245. cmdstring = "if(%s,%s,%s)" %(mapainput, map_j.cmd_list, \
  2246. numinput)
  2247. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  2248. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, map_j.cmd_list, \
  2249. numinput)
  2250. else:
  2251. cmdstring = "if(%s,%s,%s)" %(mapainput, mapbinput, numinput)
  2252. if elsemaplist != []:
  2253. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  2254. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  2255. mapbinput)
  2256. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  2257. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, \
  2258. map_j.cmd_list)
  2259. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  2260. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  2261. map_j.cmd_list)
  2262. else:
  2263. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, mapbinput)
  2264. # Conditional append of module command.
  2265. map_new.cmd_list = cmdstring
  2266. # Set new map name to temporary map name.
  2267. #mapainput = name
  2268. count += 1
  2269. # Append map to result map list.
  2270. if returncode == 1:
  2271. resultspatial.append(map_new)
  2272. temporalcond = ts_var_dict["temporal"]
  2273. resultspatial = self.check_stds(resultspatial)
  2274. thencond = self.build_condition_list(temporalcond, resultspatial)
  2275. thenresult = self.eval_condition_list(thencond)
  2276. # Clear the map list.
  2277. resultlist = self.check_stds(thenresult, clear = True)
  2278. t[0] = resultlist
  2279. if self.debug:
  2280. for map in resultlist:
  2281. print map.cmd_list
  2282. def p_ts_numeric_expr_condition_elif_relation(self, t):
  2283. # Examples:
  2284. # if({during},ts_var_expr, 1, A)
  2285. # if({during}, A == 5 && start_day() > 5, A, null())
  2286. """
  2287. expr : IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA number COMMA stds RPAREN
  2288. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  2289. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA number COMMA expr RPAREN
  2290. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  2291. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA number RPAREN
  2292. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  2293. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA number RPAREN
  2294. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  2295. """
  2296. relations, temporal, function= self.eval_toperator(t[3])
  2297. ts_var_dict = t[5]
  2298. spatialcond = ts_var_dict["spatial"]
  2299. ifmaplist = self.check_stds(spatialcond)
  2300. resultspatial = []
  2301. thenmaplist = []
  2302. numthen = ''
  2303. elsemaplist = []
  2304. numelse = ''
  2305. # Select input for r.mapcalc expression based on length of PLY object.
  2306. if len(t) == 11:
  2307. try:
  2308. thenmaplist = self.check_stds(t[7])
  2309. except:
  2310. numthen = t[7]
  2311. try:
  2312. elsemaplist = self.check_stds(t[9])
  2313. except:
  2314. numelse = t[9]
  2315. elif len(t) == 13:
  2316. try:
  2317. thenmaplist = self.check_stds(t[7])
  2318. except:
  2319. numthen = t[9] + t[10] + t[11]
  2320. try:
  2321. elsemaplist = self.check_stds(t[11])
  2322. except:
  2323. numelse = t[9] + t[10] + t[11]
  2324. if thenmaplist != []:
  2325. topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist, \
  2326. topolist = relations)
  2327. elif elsemaplist != []:
  2328. topolist = self.get_temporal_topo_list(ifmaplist, elsemaplist, \
  2329. topolist = relations)
  2330. if numthen != '':
  2331. numinput = numthen
  2332. elif numelse != '':
  2333. numinput = numelse
  2334. # Iterate over condition map lists with temporal relations.
  2335. for map_i in topolist:
  2336. # Loop over temporal related maps and create overlay modules.
  2337. tbrelations = map_i.get_temporal_relations()
  2338. count = 0
  2339. for topo in relations:
  2340. if topo in tbrelations.keys():
  2341. for map_j in (tbrelations[topo]):
  2342. # Generate an intermediate map for the result map list.
  2343. map_new = self.generate_new_map(base_map=map_i, bool_op = 'and', copy = True)
  2344. # Set first input for overlay module.
  2345. mapainput = map_i.get_id()
  2346. # Create overlayed map extent.
  2347. returncode = self.overlay_map_extent(map_new, map_j, 'and', \
  2348. temp_op = '=')
  2349. # Stop the loop if no temporal or spatial relationship exist.
  2350. if returncode == 0:
  2351. break
  2352. if count == 0:
  2353. # Set map name.
  2354. name = map_new.get_id()
  2355. else:
  2356. # Generate an intermediate map
  2357. name = self.generate_map_name()
  2358. # Set first input for overlay module.
  2359. mapbinput = map_j.get_id()
  2360. # Create r.mapcalc expression string for the operation.
  2361. if thenmaplist != []:
  2362. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  2363. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, mapbinput, \
  2364. numinput)
  2365. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  2366. cmdstring = "if(%s,%s,%s)" %(mapainput, map_j.cmd_list, \
  2367. numinput)
  2368. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  2369. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, map_j.cmd_list, \
  2370. numinput)
  2371. else:
  2372. cmdstring = "if(%s,%s,%s)" %(mapainput, mapbinput, numinput)
  2373. if elsemaplist != []:
  2374. if "cmd_list" in dir(map_new) and "cmd_list" not in dir(map_j):
  2375. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  2376. mapbinput)
  2377. elif "cmd_list" in dir(map_j) and "cmd_list" not in dir(map_new):
  2378. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, \
  2379. map_j.cmd_list)
  2380. elif "cmd_list" in dir(map_j) and "cmd_list" in dir(map_new):
  2381. cmdstring = "if(%s,%s,%s)" %(map_new.cmd_list, numinput, \
  2382. map_j.cmd_list)
  2383. else:
  2384. cmdstring = "if(%s,%s,%s)" %(mapainput, numinput, mapbinput)
  2385. # Conditional append of module command.
  2386. map_new.cmd_list = cmdstring
  2387. # Set new map name to temporary map name.
  2388. #mapainput = name
  2389. count += 1
  2390. # Append map to result map list.
  2391. if returncode == 1:
  2392. resultspatial.append(map_new)
  2393. temporalcond = ts_var_dict["temporal"]
  2394. resultspatial = self.check_stds(resultspatial)
  2395. thencond = self.build_condition_list(temporalcond, resultspatial)
  2396. thenresult = self.eval_condition_list(thencond)
  2397. # Clear the map list.
  2398. resultlist = self.check_stds(thenresult, clear = True)
  2399. t[0] = resultlist
  2400. if self.debug:
  2401. for map in resultlist:
  2402. print map.cmd_list
  2403. ###############################################################################
  2404. if __name__ == "__main__":
  2405. import doctest
  2406. doctest.testmod()