temporal_raster_base_algebra.py 118 KB

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