temporal_raster_base_algebra.py 114 KB

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