temporal_raster_base_algebra.py 118 KB

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