temporal_raster_base_algebra.py 126 KB

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