temporal_raster_base_algebra.py 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. """@package grass.temporal
  2. Temporal raster algebra
  3. (C) 2013 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. :authors: Thomas Leppelt and Soeren Gebbert
  8. .. code-block:: python
  9. >>> p = TemporalRasterAlgebraLexer()
  10. >>> p.build()
  11. >>> p.debug = True
  12. >>> expression = 'R = A {+,equal,l} B'
  13. >>> p.test(expression)
  14. R = A {+,equal,l} B
  15. LexToken(NAME,'R',1,0)
  16. LexToken(EQUALS,'=',1,2)
  17. LexToken(NAME,'A',1,4)
  18. LexToken(T_ARITH2_OPERATOR,'{+,equal,l}',1,6)
  19. LexToken(NAME,'B',1,18)
  20. >>> expression = 'R = A {*,equal|during,r} B'
  21. >>> p.test(expression)
  22. R = A {*,equal|during,r} B
  23. LexToken(NAME,'R',1,0)
  24. LexToken(EQUALS,'=',1,2)
  25. LexToken(NAME,'A',1,4)
  26. LexToken(T_ARITH1_OPERATOR,'{*,equal|during,r}',1,6)
  27. LexToken(NAME,'B',1,25)
  28. >>> expression = 'R = A {+,equal|during} B'
  29. >>> p.test(expression)
  30. R = A {+,equal|during} B
  31. LexToken(NAME,'R',1,0)
  32. LexToken(EQUALS,'=',1,2)
  33. LexToken(NAME,'A',1,4)
  34. LexToken(T_ARITH2_OPERATOR,'{+,equal|during}',1,6)
  35. LexToken(NAME,'B',1,23)
  36. """
  37. from __future__ import print_function
  38. import grass.pygrass.modules as pymod
  39. from .temporal_operator import *
  40. from .temporal_algebra import *
  41. ##############################################################################
  42. class TemporalRasterAlgebraLexer(TemporalAlgebraLexer):
  43. """Lexical analyzer for the GRASS GIS temporal algebra"""
  44. def __init__(self):
  45. TemporalAlgebraLexer.__init__(self)
  46. # Supported r.mapcalc functions.
  47. mapcalc_functions = {
  48. 'exp' : 'EXP',
  49. 'log' : 'LOG',
  50. 'sqrt' : 'SQRT',
  51. 'abs' : 'ABS',
  52. 'cos' : 'COS',
  53. 'acos' : 'ACOS',
  54. 'sin' : 'SIN',
  55. 'asin' : 'ASIN',
  56. 'tan' : 'TAN',
  57. 'double' : 'DOUBLE',
  58. 'float' : 'FLOATEXP',
  59. 'int' : 'INTEXP',
  60. 'isnull' : 'ISNULL',
  61. 'isntnull': 'ISNTNULL',
  62. 'null' : 'NULL',
  63. 'exist' : 'EXIST',
  64. }
  65. # Functions that defines single maps with time stamp and without temporal extent.
  66. map_functions = {'map' : 'MAP'}
  67. # This is the list of token names.
  68. raster_tokens = (
  69. 'MOD',
  70. 'DIV',
  71. 'MULT',
  72. 'ADD',
  73. 'SUB',
  74. 'T_ARITH1_OPERATOR',
  75. 'T_ARITH2_OPERATOR',
  76. 'L_SPAREN',
  77. 'R_SPAREN',
  78. )
  79. # Build the token list
  80. tokens = TemporalAlgebraLexer.tokens \
  81. + raster_tokens \
  82. + tuple(mapcalc_functions.values()) \
  83. + tuple(map_functions.values())
  84. # Regular expression rules for simple tokens
  85. t_MOD = r'[\%]'
  86. t_DIV = r'[\/]'
  87. t_MULT = r'[\*]'
  88. t_ADD = r'[\+]'
  89. t_SUB = r'[-]'
  90. t_T_ARITH1_OPERATOR = r'\{[\%\*\/][,]?[a-zA-Z\| ]*([,])?([lrudi]|left|right|union|disjoint|intersect)?\}'
  91. t_T_ARITH2_OPERATOR = r'\{[+-][,]?[a-zA-Z\| ]*([,])?([lrudi]|left|right|union|disjoint|intersect)?\}'
  92. t_L_SPAREN = r'\['
  93. t_R_SPAREN = r'\]'
  94. # Parse symbols
  95. def temporal_symbol(self, t):
  96. # Check for reserved words
  97. if t.value in TemporalRasterAlgebraLexer.time_functions.keys():
  98. t.type = TemporalRasterAlgebraLexer.time_functions.get(t.value)
  99. elif t.value in TemporalRasterAlgebraLexer.datetime_functions.keys():
  100. t.type = TemporalRasterAlgebraLexer.datetime_functions.get(t.value)
  101. elif t.value in TemporalRasterAlgebraLexer.conditional_functions.keys():
  102. t.type = TemporalRasterAlgebraLexer.conditional_functions.get(t.value)
  103. elif t.value in TemporalRasterAlgebraLexer.mapcalc_functions.keys():
  104. t.type = TemporalRasterAlgebraLexer.mapcalc_functions.get(t.value)
  105. elif t.value in TemporalRasterAlgebraLexer.map_functions.keys():
  106. t.type = TemporalRasterAlgebraLexer.map_functions.get(t.value)
  107. else:
  108. t.type = 'NAME'
  109. return t
  110. ##############################################################################
  111. class TemporalRasterBaseAlgebraParser(TemporalAlgebraParser):
  112. """The temporal algebra class"""
  113. # Get the tokens from the lexer class
  114. tokens = TemporalRasterAlgebraLexer.tokens
  115. # Setting equal precedence level for select and hash operations.
  116. precedence = (
  117. ('left', 'T_SELECT_OPERATOR', 'T_SELECT', 'T_NOT_SELECT'), # 1
  118. ('left', 'ADD', 'SUB', 'T_ARITH2_OPERATOR', 'T_HASH_OPERATOR', 'HASH'), #2
  119. ('left', 'AND', 'OR', 'T_COMP_OPERATOR', 'MOD', 'DIV', 'MULT',
  120. 'T_ARITH1_OPERATOR'))
  121. def __init__(self, pid=None, run=True,
  122. debug=False, spatial=False,
  123. register_null=False,
  124. dry_run=False, nprocs=1):
  125. TemporalAlgebraParser.__init__(self,
  126. pid=pid,
  127. run=run,
  128. debug=debug,
  129. spatial=spatial,
  130. register_null=register_null,
  131. dry_run=dry_run,
  132. nprocs=nprocs)
  133. def check_null(self, t):
  134. try:
  135. int(t)
  136. return t
  137. except ValueError:
  138. return "null()"
  139. ######################### Temporal functions ##############################
  140. def get_temporal_topo_list(self, maplistA, maplistB=None, topolist=["EQUAL"],
  141. assign_val=False, count_map=False, compare_bool=False,
  142. compare_cmd=False, compop=None, aggregate=None,
  143. new=False, convert=False, operator_cmd=False):
  144. """Build temporal topology for two space time data sets, copy map objects
  145. for given relation into map list.
  146. :param maplistA: List of maps.
  147. :param maplistB: List of maps.
  148. :param topolist: List of strings of temporal relations.
  149. :param assign_val: Boolean for assigning a boolean map value based on
  150. the map_values from the compared map list by
  151. topological relationships.
  152. :param count_map: Boolean if the number of topological related maps
  153. should be returned.
  154. :param compare_bool: Boolean for comparing boolean map values based on
  155. related map list and compariosn operator.
  156. :param compare_cmd: Boolean for comparing command list values based on
  157. related map list and compariosn operator.
  158. :param compop: Comparison operator, && or ||.
  159. :param aggregate: Aggregation operator for relation map list, & or |.
  160. :param new: Boolean if new temporary maps should be created.
  161. :param convert: Boolean if conditional values should be converted to
  162. r.mapcalc command strings.
  163. :param operator_cmd: Boolean for aggregate arithmetic operators implicitly
  164. in command list values based on related map lists.
  165. :return: List of maps from maplistA that fulfil the topological relationships
  166. to maplistB specified in topolist.
  167. """
  168. topologylist = ["EQUAL", "FOLLOWS", "PRECEDES", "OVERLAPS", "OVERLAPPED",
  169. "DURING", "STARTS", "FINISHES", "CONTAINS", "STARTED",
  170. "FINISHED"]
  171. complementdict = {"EQUAL": "EQUAL", "FOLLOWS" : "PRECEDES",
  172. "PRECEDES" : "FOLLOWS", "OVERLAPS" : "OVERLAPPED",
  173. "OVERLAPPED" : "OVERLAPS", "DURING" : "CONTAINS",
  174. "CONTAINS" : "DURING", "STARTS" : "STARTED",
  175. "STARTED" : "STARTS", "FINISHES" : "FINISHED",
  176. "FINISHED" : "FINISHES"}
  177. resultdict = {}
  178. # Check if given temporal relation are valid.
  179. for topo in topolist:
  180. if topo.upper() not in topologylist:
  181. raise SyntaxError("Unpermitted temporal relation name '" + topo + "'")
  182. # Create temporal topology for maplistA to maplistB.
  183. tb = SpatioTemporalTopologyBuilder()
  184. # Dictionary with different spatial variables used for topology builder.
  185. spatialdict = {'strds' : '2D', 'stvds' : '2D', 'str3ds' : '3D'}
  186. # Build spatial temporal topology
  187. if self.spatial:
  188. tb.build(maplistA, maplistB, spatial=spatialdict[self.stdstype])
  189. else:
  190. tb.build(maplistA, maplistB)
  191. # Iterate through maps in maplistA and search for relationships given
  192. # in topolist.
  193. for map_i in maplistA:
  194. tbrelations = map_i.get_temporal_relations()
  195. # Check for boolean parameters for further calculations.
  196. if assign_val:
  197. self.assign_bool_value(map_i, tbrelations, topolist)
  198. elif compare_bool:
  199. self.compare_bool_value(map_i, tbrelations, compop, aggregate, topolist)
  200. elif compare_cmd:
  201. self.compare_cmd_value(map_i, tbrelations, compop, aggregate, topolist, convert)
  202. elif operator_cmd:
  203. self.operator_cmd_value(map_i, tbrelations, compop, topolist)
  204. for topo in topolist:
  205. if topo.upper() in tbrelations.keys():
  206. if count_map:
  207. relationmaplist = tbrelations[topo.upper()]
  208. gvar = GlobalTemporalVar()
  209. gvar.td = len(relationmaplist)
  210. if "map_value" in dir(map_i):
  211. map_i.map_value.append(gvar)
  212. else:
  213. map_i.map_value = gvar
  214. # Use unique identifier, since map names may be equal
  215. resultdict[map_i.uid] = map_i
  216. resultlist = resultdict.values()
  217. # Sort list of maps chronological.
  218. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  219. return(resultlist)
  220. def build_command_string(self, map_i, relmap, operator = None, cmd_type = None):
  221. """This function build the r.mapcalc command string for conditionals,
  222. spatial variable combinations and boolean comparisons.
  223. For Example: 'if(a1 == 1, b1, c2)' or 'exist(a1) && sin(b1)'
  224. :param map_i: map object with temporal extent and built relations.
  225. :param relmap: map object with defined temporal relation to map_i.
  226. :param operator: String representing operator between two spatial variables
  227. (&&,||,+,-,*,/).
  228. :param cmd_type: map object with defined temporal relation to map_i:
  229. condition, conclusion or operator.
  230. :return: the resulting command string for conditionals or spatial variable
  231. combinations
  232. """
  233. def sub_cmdstring(map_i):
  234. """This function search for command string in a map object and
  235. return substitute string (contained commandstring or map name)"""
  236. if "cmd_list" in dir(map_i):
  237. map_sub = map_i.cmd_list
  238. elif "map_value" in dir(map_i) and len(map_i.map_value) > 0 and map_i.map_value[0].get_type() == "timediff":
  239. map_sub = map_i.map_value[0].get_type_value()[0]
  240. else:
  241. try:
  242. map_sub = map_i.get_id()
  243. except:
  244. map_sub = map_i
  245. return(map_sub)
  246. # Check for type of operation, conditional or spatial variable combination
  247. # and Create r.mapcalc expression string for the operation.
  248. cmdstring = ""
  249. if cmd_type == 'condition':
  250. conditionsub = sub_cmdstring(map_i)
  251. conclusionsub = sub_cmdstring(relmap)
  252. cmdstring = "if(%s, %s)" %(conditionsub, conclusionsub)
  253. elif cmd_type == 'conclusion':
  254. thensub = sub_cmdstring(map_i)
  255. elsesub = sub_cmdstring(relmap)
  256. cmdstring = "%s, %s" %(thensub, elsesub)
  257. elif cmd_type == 'operator':
  258. leftsub = sub_cmdstring(map_i)
  259. rightsub = sub_cmdstring(relmap)
  260. if operator == None:
  261. self.msgr.fatal("Error: Can't build command string for map %s, operator is missing"
  262. %(map_i.get_map_id()))
  263. cmdstring = "(%s %s %s)" %(leftsub, operator, rightsub)
  264. return(cmdstring)
  265. def compare_cmd_value(self, map_i, tbrelations, compop, aggregate,
  266. topolist = ["EQUAL"], convert = False):
  267. """ Function to evaluate two map lists with boolean values by boolean
  268. comparison operator.
  269. Extended temporal algebra version with command
  270. list builder for temporal raster algebra.
  271. :param map_i: Map object with temporal extent.
  272. :param tbrelations: List of temporal relation to map_i.
  273. :param topolist: List of strings for given temporal relations.
  274. :param compop: Comparison operator, && or ||.
  275. :param aggregate: Aggregation operator for relation map list, & or |.
  276. :param convert: Boolean if conditional values should be converted to
  277. r.mapcalc command strings.
  278. :return: Map object with conditional value that has been evaluated by
  279. comparison operators.
  280. """
  281. # Build comandlist list with elements from related maps and given relation operator.
  282. if convert and "condition_value" in dir(map_i):
  283. if map_i.condition_value != []:
  284. cmdstring = str(int(map_i.condition_value[0]))
  285. map_i.cmd_list = cmdstring
  286. if "cmd_list" in dir(map_i):
  287. leftcmd = map_i.cmd_list
  288. cmd_value_list = [leftcmd]
  289. count = 0
  290. for topo in topolist:
  291. if topo.upper() in tbrelations.keys():
  292. relationmaplist = tbrelations[topo.upper()]
  293. if count == 0 and "cmd_list" in dir(map_i):
  294. cmd_value_list.append(compop)
  295. cmd_value_list.append('(')
  296. for relationmap in relationmaplist:
  297. if convert and "condition_value" in dir(relationmap):
  298. if relationmap.condition_value != []:
  299. cmdstring = str(int(relationmap.condition_value[0]))
  300. relationmap.cmd_list = cmdstring
  301. if "cmd_list" in dir(relationmap):
  302. if count > 0:
  303. cmd_value_list.append(aggregate + aggregate)
  304. cmd_value_list.append(relationmap.cmd_list)
  305. count = count + 1
  306. if count > 0:
  307. cmd_value_list.append(')')
  308. cmd_value_str = ''.join(map(str, cmd_value_list))
  309. # Add command list to result map.
  310. map_i.cmd_list = cmd_value_str
  311. return(cmd_value_str)
  312. def operator_cmd_value(self, map_i, tbrelations, operator, topolist = ["EQUAL"]):
  313. """ Function to evaluate two map lists by given arithmetic operator.
  314. :param map_i: Map object with temporal extent.
  315. :param tbrelations: List of temporal relation to map_i.
  316. :param topolist: List of strings for given temporal relations.
  317. :param operator: Arithmetic operator, +-*/%.
  318. :return: Map object with command list with operators that has been
  319. evaluated by implicit aggregration.
  320. """
  321. # Build comandlist list with elements from related maps and given relation operator.
  322. leftcmd = map_i
  323. cmdstring = ""
  324. for topo in topolist:
  325. if topo.upper() in tbrelations.keys():
  326. relationmaplist = tbrelations[topo.upper()]
  327. for relationmap in relationmaplist:
  328. # Create r.mapcalc expression string for the operation.
  329. cmdstring = self.build_command_string(leftcmd,
  330. relationmap,
  331. operator=operator,
  332. cmd_type="operator")
  333. leftcmd = cmdstring
  334. # Add command list to result map.
  335. map_i.cmd_list = cmdstring
  336. return(cmdstring)
  337. def set_temporal_extent_list(self, maplist, topolist=["EQUAL"], temporal='l' ,
  338. cmd_bool=False, cmd_type=None, operator=None):
  339. """ Change temporal extent of map list based on temporal relations to
  340. other map list and given temporal operator.
  341. :param maplist: List of map objects for which relations has been build
  342. correctely.
  343. :param topolist: List of strings of temporal relations.
  344. :param temporal: The temporal operator specifying the temporal
  345. extent operation (intersection, union, disjoint
  346. union, right reference, left reference).
  347. :param cmd_bool: Boolean if command string should be merged for related maps.
  348. :param cmd_type: map object with defined temporal relation to map_i:
  349. condition, conclusion or operator.
  350. :param operator: String defining the type of operator.
  351. :return: Map list with specified temporal extent and optional command string.
  352. """
  353. resultdict = {}
  354. for map_i in maplist:
  355. # Loop over temporal related maps and create overlay modules.
  356. tbrelations = map_i.get_temporal_relations()
  357. # Generate an intermediate map for the result map list.
  358. map_new = self.generate_new_map(base_map=map_i,
  359. bool_op='and',
  360. copy=True,
  361. rename=True)
  362. # Combine temporal and spatial extents of intermediate map with related maps.
  363. for topo in topolist:
  364. if topo in tbrelations.keys():
  365. for map_j in (tbrelations[topo]):
  366. if temporal == 'r':
  367. # Generate an intermediate map for the result map list.
  368. map_new = self.generate_new_map(base_map=map_i,
  369. bool_op='and',
  370. copy=True,
  371. rename=True)
  372. # Create overlayed map extent.
  373. returncode = self.overlay_map_extent(map_new, map_j,
  374. 'and',
  375. temp_op = temporal)
  376. # Stop the loop if no temporal or spatial relationship exist.
  377. if returncode == 0:
  378. break
  379. # Append map to result map list.
  380. elif returncode == 1:
  381. # print(map_new.cmd_list)
  382. # resultlist.append(map_new)
  383. if cmd_bool:
  384. # Create r.mapcalc expression string for the operation.
  385. cmdstring = self.build_command_string(map_i,
  386. map_j,
  387. operator=operator,
  388. cmd_type=cmd_type)
  389. # Conditional append of module command.
  390. map_new.cmd_list = cmdstring
  391. # Write map object to result dictionary.
  392. resultdict[map_new.uid] = map_new
  393. if returncode == 0:
  394. break
  395. # Append map to result map list.
  396. #if returncode == 1:
  397. # resultlist.append(map_new)
  398. # Get sorted map objects as values from result dictionoary.
  399. resultlist = resultdict.values()
  400. resultlist = sorted(resultlist, key = AbstractDatasetComparisonKeyStartTime)
  401. return(resultlist)
  402. def build_condition_cmd_list(self, iflist, thenlist, elselist=None,
  403. condition_topolist=["EQUAL"],
  404. conclusion_topolist=["EQUAL"],
  405. temporal='l', null=False):
  406. """This function build the r.mapcalc command strings for spatial conditionals.
  407. For Example: 'if(a1 == 1, b1, c2)'
  408. :param iflist: Map list with temporal extents and command list.
  409. :param thenlist: Map list with temporal extents and command list or numeric string.
  410. :param elselist: Map list with temporal extents and command list or numeric string.
  411. :param condition_topolist: List of strings for given temporal relations between
  412. conditions and conclusions.
  413. :param conclusion_topolist: List of strings for given temporal relations between
  414. conditions (then and else).
  415. :param temporal: The temporal operator specifying the temporal
  416. extent operation (intersection, union, disjoint
  417. union, right reference, left reference).
  418. :param null: Boolean if null map support should be activated.
  419. :return: map list with resulting command string for given condition type.
  420. """
  421. resultlist = []
  422. # First merge conclusion command maplists or strings.
  423. # Check if alternative conclusion map list is given.
  424. if all([isinstance(thenlist, list), isinstance(elselist, list)]):
  425. # Build conclusion command map list.
  426. conclusiontopolist = self.get_temporal_topo_list(thenlist, elselist,
  427. conclusion_topolist)
  428. conclusionlist = self.set_temporal_extent_list(conclusiontopolist,
  429. topolist=conclusion_topolist,
  430. temporal=temporal ,
  431. cmd_bool=True,
  432. cmd_type="conclusion")
  433. # Check if any conclusion is a numeric statements.
  434. elif any([isinstance(thenlist, str), isinstance(elselist, str)]):
  435. conclusionlist = []
  436. # Check if only alternative conclusion is a numeric statements.
  437. if all([isinstance(thenlist, list), isinstance(elselist, str)]):
  438. listinput = thenlist
  439. numinput = elselist
  440. for map_i in listinput:
  441. # Create r.mapcalc expression string for the operation.
  442. cmdstring = self.build_command_string(map_i,
  443. numinput,
  444. cmd_type='conclusion')
  445. # Conditional append of module command.
  446. map_i.cmd_list = cmdstring
  447. # Append map to result map list.
  448. conclusionlist.append(map_i)
  449. # Check if only direct conclusion is a numeric statements.
  450. elif all([isinstance(thenlist, str), isinstance(elselist, list)]):
  451. listinput = elselist
  452. numinput = thenlist
  453. for map_i in listinput:
  454. # Create r.mapcalc expression string for the operation.
  455. cmdstring = self.build_command_string(numinput,
  456. map_i,
  457. cmd_type='conclusion')
  458. # Conditional append of module command.
  459. map_i.cmd_list = cmdstring
  460. # Append map to result map list.
  461. conclusionlist.append(map_i)
  462. elif all([isinstance(thenlist, str), isinstance(elselist, str)]):
  463. conclusionlist = thenlist + ',' + elselist
  464. else:
  465. # The direct conclusion is used.
  466. conclusionlist = thenlist
  467. # Use the conclusion map or string to merge it with the condition and
  468. # return maplist.
  469. if isinstance(conclusionlist, str):
  470. resultlist = []
  471. for map_i in iflist:
  472. # Create r.mapcalc expression string for the operation.
  473. cmdstring = self.build_command_string(map_i,
  474. conclusionlist,
  475. cmd_type='condition')
  476. # Conditional append of module command.
  477. map_i.cmd_list = cmdstring
  478. # Append map to result map list.
  479. resultlist.append(map_i)
  480. return(resultlist)
  481. elif isinstance(conclusionlist, list):
  482. # Build result command map list between conditions and conclusions.
  483. conditiontopolist = self.get_temporal_topo_list(iflist,
  484. conclusionlist,
  485. topolist=condition_topolist)
  486. resultlist = self.set_temporal_extent_list(conditiontopolist,
  487. topolist=condition_topolist,
  488. temporal='r',
  489. cmd_bool=True,
  490. cmd_type="condition")
  491. return(resultlist)
  492. ###########################################################################
  493. def p_statement_assign(self, t):
  494. # This function executes the processing of raster/raster3d algebra
  495. # that was build based on the expression
  496. """
  497. statement : stds EQUALS expr
  498. """
  499. if self.run:
  500. # Create the process queue for parallel mapcalc processing
  501. if self.dry_run is False:
  502. process_queue = pymod.ParallelModuleQueue(int(self.nprocs))
  503. if isinstance(t[3], list):
  504. num = len(t[3])
  505. count = 0
  506. register_list = []
  507. for i in range(num):
  508. # Check if resultmap names exist in GRASS database.
  509. map_name = self.basename + "_" + str(i) + "@" + self.mapset
  510. if self.stdstype == "strds":
  511. new_map = RasterDataset(map_name)
  512. else:
  513. new_map = Raster3DDataset(map_name)
  514. if new_map.map_exists() and self.overwrite is False:
  515. self.msgr.fatal("Error maps with basename %s exist. "
  516. "Use --o flag to overwrite existing file"%map_name)
  517. map_test_list = []
  518. for map_i in t[3]:
  519. newident = self.basename + "_" + str(count)
  520. if "cmd_list" in dir(map_i):
  521. # Build r.mapcalc module and execute expression.
  522. # Change map name to given basename.
  523. # Create deepcopy of r.mapcalc module.
  524. new_map = map_i.get_new_instance(newident + "@" + self.mapset)
  525. new_map.set_temporal_extent(map_i.get_temporal_extent())
  526. new_map.set_spatial_extent(map_i.get_spatial_extent())
  527. map_test_list.append(new_map)
  528. m = copy.deepcopy(self.m_mapcalc)
  529. m_expression = newident + "=" + map_i.cmd_list
  530. m.inputs["expression"].value = str(m_expression)
  531. m.flags["overwrite"].value = self.overwrite
  532. #print(m.get_bash())
  533. self.process_chain_dict["processes"].append(m.get_dict())
  534. if self.dry_run is False:
  535. process_queue.put(m)
  536. elif map_i.map_exists():
  537. # Copy map if it exists b = a
  538. new_map = map_i.get_new_instance(newident + "@" + self.mapset)
  539. new_map.set_temporal_extent(map_i.get_temporal_extent())
  540. new_map.set_spatial_extent(map_i.get_spatial_extent())
  541. map_test_list.append(new_map)
  542. m = copy.deepcopy(self.m_mapcalc)
  543. m_expression = newident + "=" + map_i.get_map_id()
  544. m.inputs["expression"].value = str(m_expression)
  545. m.flags["overwrite"].value = self.overwrite
  546. #print(m.get_bash())
  547. self.process_chain_dict["processes"].append(m.get_dict())
  548. if self.dry_run is False:
  549. process_queue.put(m)
  550. else:
  551. self.msgr.error(_("Error computing map <%s>"%map_i.get_id()))
  552. count += 1
  553. if self.dry_run is False:
  554. process_queue.wait()
  555. for map_i in map_test_list:
  556. register_list.append(map_i)
  557. # Open connection to temporal database.
  558. dbif, connect = init_dbif(self.dbif)
  559. # Create result space time dataset.
  560. if self.dry_run is False:
  561. resultstds = open_new_stds(t[1], self.stdstype,
  562. 'absolute', t[1], t[1],
  563. 'mean', self.dbif,
  564. overwrite = self.overwrite)
  565. for map_i in register_list:
  566. # Put the map into the process dictionary
  567. start, end = map_i.get_temporal_extent_as_tuple()
  568. self.process_chain_dict["register"].append((map_i.get_name(),
  569. str(start),
  570. str(end)))
  571. if self.dry_run is False:
  572. # Get meta data from grass database.
  573. map_i.load()
  574. # Do not register empty maps if not required
  575. # In case of a null map continue, do not register null maps
  576. if map_i.metadata.get_min() is None and \
  577. map_i.metadata.get_max() is None:
  578. if not self.register_null:
  579. self.removable_maps[map_i.get_name()] = map_i
  580. continue
  581. if map_i.is_in_db(dbif) and self.overwrite:
  582. # Update map in temporal database.
  583. if self.dry_run is False:
  584. map_i.update_all(dbif)
  585. elif map_i.is_in_db(dbif) and self.overwrite is False:
  586. # Raise error if map exists and no overwrite flag is given.
  587. self.msgr.fatal("Error raster map %s exist in temporal database. "
  588. "Use overwrite flag."%map_i.get_map_id())
  589. else:
  590. # Insert map into temporal database.
  591. if self.dry_run is False:
  592. map_i.insert(dbif)
  593. # Register map in result space time dataset.
  594. if self.dry_run is False:
  595. success = resultstds.register_map(map_i, dbif)
  596. if self.dry_run is False:
  597. resultstds.update_from_registered_maps(dbif)
  598. self.process_chain_dict["STDS"]["name"] = t[1]
  599. self.process_chain_dict["STDS"]["stdstype"] = self.stdstype
  600. self.process_chain_dict["STDS"]["temporal_type"] = 'absolute'
  601. dbif.close()
  602. t[0] = register_list
  603. # Remove intermediate maps
  604. self.remove_maps()
  605. def p_expr_spmap_function(self, t):
  606. # Add a single map.
  607. # Only the spatial extent of the map is evaluated.
  608. # Temporal extent is not existing.
  609. # Examples:
  610. # R = map(A)
  611. """
  612. mapexpr : MAP LPAREN stds RPAREN
  613. """
  614. if self.run:
  615. # Check input map.
  616. input = t[3]
  617. if not isinstance(input, list):
  618. # Check for mapset in given stds input.
  619. if input.find("@") >= 0:
  620. id_input = input
  621. else:
  622. id_input = input + "@" + self.mapset
  623. # Create empty map dataset.
  624. map_i = dataset_factory(self.maptype, id_input)
  625. # Check for occurrence of space time dataset.
  626. if map_i.map_exists() == False:
  627. raise FatalError(_("%s map <%s> not found in GRASS spatial database") %
  628. (map_i.get_type(), id_input))
  629. else:
  630. # Select dataset entry from database.
  631. map_i.select(dbif=self.dbif)
  632. # Create command list for map object.
  633. cmdstring = "(%s)" %(map_i.get_map_id())
  634. map_i.cmd_list = cmdstring
  635. # Return map object.
  636. t[0] = cmdstring
  637. else:
  638. t[0] = "map(" + t[3] + ")"
  639. if self.debug:
  640. print("map(" + t[3] + ")")
  641. def p_arith1_operation(self, t):
  642. # A % B
  643. # A / B
  644. # A * B
  645. # A % td(B)
  646. # A * td(B)
  647. # A / td(B)
  648. """
  649. expr : stds MOD stds
  650. | expr MOD stds
  651. | stds MOD expr
  652. | expr MOD expr
  653. | stds DIV stds
  654. | expr DIV stds
  655. | stds DIV expr
  656. | expr DIV expr
  657. | stds MULT stds
  658. | expr MULT stds
  659. | stds MULT expr
  660. | expr MULT expr
  661. | stds MOD t_td_var
  662. | expr MOD t_td_var
  663. | stds DIV t_td_var
  664. | expr DIV t_td_var
  665. | stds MULT t_td_var
  666. | expr MULT t_td_var
  667. """
  668. # Check input stds.
  669. maplistA = self.check_stds(t[1])
  670. maplistB = self.check_stds(t[3])
  671. topolist = self.get_temporal_topo_list(maplistA, maplistB)
  672. if self.run:
  673. resultlist = []
  674. for map_i in topolist:
  675. # Generate an intermediate map for the result map list.
  676. map_new = self.generate_new_map(base_map=map_i,
  677. bool_op='and',
  678. copy=True)
  679. # Loop over temporal related maps and create overlay modules.
  680. tbrelations = map_i.get_temporal_relations()
  681. count = 0
  682. for map_j in (tbrelations['EQUAL']):
  683. # Create overlayed map extent.
  684. returncode = self.overlay_map_extent(map_new, map_j,
  685. 'and',
  686. temp_op='l')
  687. # Stop the loop if no temporal or spatial relationship exist.
  688. if returncode == 0:
  689. break
  690. if count == 0:
  691. # Set map name.
  692. name = map_new.get_id()
  693. else:
  694. # Generate an intermediate map
  695. name = self.generate_map_name()
  696. # Create r.mapcalc expression string for the operation.
  697. cmdstring = self.build_command_string(map_i, map_j,
  698. operator=t[2],
  699. cmd_type="operator")
  700. # Conditional append of module command.
  701. map_new.cmd_list = cmdstring
  702. count += 1
  703. # Append map to result map list.
  704. if returncode == 1:
  705. resultlist.append(map_new)
  706. t[0] = resultlist
  707. if self.debug:
  708. for map in resultlist:
  709. print(map.cmd_list)
  710. def p_arith1_operation_numeric1(self, t):
  711. # A % 1
  712. # A / 4
  713. # A * 5
  714. # A % map(b1)
  715. # A * map(b2)
  716. # A / map(b3)
  717. """
  718. expr : stds MOD number
  719. | expr MOD number
  720. | stds DIV number
  721. | expr DIV number
  722. | stds MULT number
  723. | expr MULT number
  724. | stds MOD numberstr
  725. | expr MOD numberstr
  726. | stds DIV numberstr
  727. | expr DIV numberstr
  728. | stds MULT numberstr
  729. | expr MULT numberstr
  730. | stds MOD mapexpr
  731. | expr MOD mapexpr
  732. | stds DIV mapexpr
  733. | expr DIV mapexpr
  734. | stds MULT mapexpr
  735. | expr MULT mapexpr
  736. """
  737. # Check input stds.
  738. maplist = self.check_stds(t[1])
  739. if self.run:
  740. resultlist = []
  741. for map_i in maplist:
  742. mapinput = map_i.get_id()
  743. # Create r.mapcalc expression string for the operation.
  744. if "cmd_list" in dir(map_i):
  745. cmdstring = "(%s %s %s)" %(map_i.cmd_list, t[2], t[3])
  746. else:
  747. cmdstring = "(%s %s %s)" %(mapinput, t[2], t[3])
  748. # Conditional append of module command.
  749. map_i.cmd_list = cmdstring
  750. # Append map to result map list.
  751. resultlist.append(map_i)
  752. t[0] = resultlist
  753. if self.debug:
  754. for map in resultlist:
  755. print(map.cmd_list)
  756. def p_arith1_operation_numeric2(self, t):
  757. # 1 % A
  758. # 4 / A
  759. # 5 * A
  760. # map(b1) % A
  761. # map(b4) / A
  762. # map(b5) * A
  763. """
  764. expr : number MOD stds
  765. | number MOD expr
  766. | number DIV stds
  767. | number DIV expr
  768. | number MULT stds
  769. | number MULT expr
  770. | numberstr MOD stds
  771. | numberstr MOD expr
  772. | numberstr DIV stds
  773. | numberstr DIV expr
  774. | numberstr MULT stds
  775. | numberstr MULT expr
  776. | mapexpr MOD stds
  777. | mapexpr MOD expr
  778. | mapexpr DIV stds
  779. | mapexpr DIV expr
  780. | mapexpr MULT stds
  781. | mapexpr MULT expr
  782. """
  783. # Check input stds.
  784. maplist = self.check_stds(t[3])
  785. if self.run:
  786. resultlist = []
  787. for map_i in maplist:
  788. mapinput = map_i.get_id()
  789. # Create r.mapcalc expression string for the operation.
  790. if "cmd_list" in dir(map_i):
  791. cmdstring = "(%s %s %s)" %(t[1], t[2], map_i.cmd_list)
  792. else:
  793. cmdstring = "(%s %s %s)" %(t[1], t[2], mapinput)
  794. # Conditional append of module command.
  795. map_i.cmd_list = cmdstring
  796. # Append map to result map list.
  797. resultlist.append(map_i)
  798. t[0] = resultlist
  799. if self.debug:
  800. for map in resultlist:
  801. print(map.cmd_list)
  802. def p_arith2_operation(self, t):
  803. # A + B
  804. # A - B
  805. # A + td(B)
  806. # A - td(B)
  807. """
  808. expr : stds ADD stds
  809. | expr ADD stds
  810. | stds ADD expr
  811. | expr ADD expr
  812. | stds SUB stds
  813. | expr SUB stds
  814. | stds SUB expr
  815. | expr SUB expr
  816. | stds ADD t_td_var
  817. | expr ADD t_td_var
  818. | expr SUB t_td_var
  819. | stds SUB t_td_var
  820. """
  821. # Check input stds.
  822. maplistA = self.check_stds(t[1])
  823. maplistB = self.check_stds(t[3])
  824. topolist = self.get_temporal_topo_list(maplistA, maplistB)
  825. if self.run:
  826. resultlist = []
  827. for map_i in topolist:
  828. # Generate an intermediate map for the result map list.
  829. map_new = self.generate_new_map(base_map=map_i,
  830. bool_op='and',
  831. copy=True)
  832. # Loop over temporal related maps and create overlay modules.
  833. tbrelations = map_i.get_temporal_relations()
  834. count = 0
  835. for map_j in (tbrelations['EQUAL']):
  836. # Create overlayed map extent.
  837. returncode = self.overlay_map_extent(map_new,
  838. map_j,
  839. 'and',
  840. temp_op='l')
  841. # Stop the loop if no temporal or spatial relationship exist.
  842. if returncode == 0:
  843. break
  844. if count == 0:
  845. # Set map name.
  846. name = map_new.get_id()
  847. else:
  848. # Generate an intermediate map
  849. name = self.generate_map_name()
  850. # Create r.mapcalc expression string for the operation.
  851. cmdstring = self.build_command_string(map_i,
  852. map_j,
  853. operator=t[2],
  854. cmd_type="operator")
  855. # Conditional append of module command.
  856. map_new.cmd_list = cmdstring
  857. count += 1
  858. # Append map to result map list.
  859. if returncode == 1:
  860. resultlist.append(map_new)
  861. t[0] = resultlist
  862. if self.debug:
  863. for map in resultlist:
  864. print(map.cmd_list)
  865. def p_arith2_operation_numeric1(self, t):
  866. # A + 2
  867. # A - 3
  868. # A + map(b4)
  869. # A - map(b5)
  870. """
  871. expr : stds ADD number
  872. | expr ADD number
  873. | stds SUB number
  874. | expr SUB number
  875. | stds ADD numberstr
  876. | expr ADD numberstr
  877. | stds SUB numberstr
  878. | expr SUB numberstr
  879. | stds ADD mapexpr
  880. | expr ADD mapexpr
  881. | stds SUB mapexpr
  882. | expr SUB mapexpr
  883. """
  884. # Check input stds.
  885. maplist = self.check_stds(t[1])
  886. if self.run:
  887. resultlist = []
  888. for map_i in maplist:
  889. mapinput = map_i.get_id()
  890. # Create r.mapcalc expression string for the operation.
  891. if "cmd_list" in dir(map_i):
  892. cmdstring = "(%s %s %s)" %(map_i.cmd_list, t[2], t[3])
  893. else:
  894. cmdstring = "(%s %s %s)" %(mapinput, t[2], t[3])
  895. # Conditional append of module command.
  896. map_i.cmd_list = cmdstring
  897. # Append map to result map list.
  898. resultlist.append(map_i)
  899. t[0] = resultlist
  900. if self.debug:
  901. for map in resultlist:
  902. print(map.cmd_list)
  903. def p_arith2_operation_numeric2(self, t):
  904. # 2 + A
  905. # 3 - A
  906. # map(b2) + A
  907. # map(b3) - A
  908. """
  909. expr : number ADD stds
  910. | number ADD expr
  911. | number SUB stds
  912. | number SUB expr
  913. | numberstr ADD stds
  914. | numberstr ADD expr
  915. | numberstr SUB stds
  916. | numberstr SUB expr
  917. | mapexpr ADD stds
  918. | mapexpr ADD expr
  919. | mapexpr SUB stds
  920. | mapexpr SUB expr
  921. """
  922. # Check input stds.
  923. maplist = self.check_stds(t[3])
  924. if self.run:
  925. resultlist = []
  926. for map_i in maplist:
  927. mapinput = map_i.get_id()
  928. # Create r.mapcalc expression string for the operation.
  929. if "cmd_list" in dir(map_i):
  930. cmdstring = "(%s %s %s)" %(t[1], t[2], map_i.cmd_list)
  931. else:
  932. cmdstring = "(%s %s %s)" %(t[1], t[2], mapinput)
  933. # Conditional append of module command.
  934. map_i.cmd_list = cmdstring
  935. # Append map to result map list.
  936. resultlist.append(map_i)
  937. t[0] = resultlist
  938. if self.debug:
  939. for map in resultlist:
  940. print(map.cmd_list)
  941. def p_arith1_operation_relation(self, t):
  942. # A {*, equal, l} B
  943. # A {*, equal, l} td(B)
  944. # A {*, equal, l} B {/, during, r} C
  945. # A {*, equal, l} B {/, equal, l} C {/, during, r} D
  946. """
  947. expr : stds T_ARITH1_OPERATOR stds
  948. | expr T_ARITH1_OPERATOR stds
  949. | stds T_ARITH1_OPERATOR expr
  950. | expr T_ARITH1_OPERATOR expr
  951. | stds T_ARITH1_OPERATOR t_td_var
  952. | expr T_ARITH1_OPERATOR t_td_var
  953. """
  954. if self.run:
  955. # Check input stds.
  956. maplistA = self.check_stds(t[1])
  957. maplistB = self.check_stds(t[3])
  958. relations, temporal, function, aggregate = self.eval_toperator(t[2], optype='raster')
  959. # Build conditional values based on topological relationships.
  960. complist = self.get_temporal_topo_list(maplistA,
  961. maplistB,
  962. topolist=relations,
  963. operator_cmd=True,
  964. compop=function)
  965. # Set temporal extent based on topological relationships.
  966. resultlist = self.set_temporal_extent_list(complist,
  967. topolist=relations,
  968. temporal=temporal)
  969. t[0] = resultlist
  970. if self.debug:
  971. for map in resultlist:
  972. print(map.cmd_list)
  973. def p_arith2_operation_relation(self, t):
  974. # A {+, equal, l} B
  975. # A {+, equal, l} td(b)
  976. # A {+, equal, l} B {-, during, r} C
  977. # A {+, equal, l} B {+, equal, l} C {-, during, r} D
  978. """
  979. expr : stds T_ARITH2_OPERATOR stds
  980. | expr T_ARITH2_OPERATOR stds
  981. | stds T_ARITH2_OPERATOR expr
  982. | expr T_ARITH2_OPERATOR expr
  983. | stds T_ARITH2_OPERATOR t_td_var
  984. | expr T_ARITH2_OPERATOR t_td_var
  985. """
  986. if self.run:
  987. # Check input stds.
  988. maplistA = self.check_stds(t[1])
  989. maplistB = self.check_stds(t[3])
  990. relations, temporal, function, aggregate = self.eval_toperator(t[2], optype='raster')
  991. # Build conditional values based on topological relationships.
  992. complist = self.get_temporal_topo_list(maplistA,
  993. maplistB,
  994. topolist=relations,
  995. operator_cmd=True,
  996. compop=function)
  997. # Set temporal extent based on topological relationships.
  998. resultlist = self.set_temporal_extent_list(complist,
  999. topolist=relations,
  1000. temporal=temporal)
  1001. t[0] = resultlist
  1002. if self.debug:
  1003. for map in resultlist:
  1004. print(map.cmd_list)
  1005. def p_arith_operation_numeric_string(self, t):
  1006. # 1 + 1
  1007. # 1 - 1
  1008. # 1 * 1
  1009. # 1 / 1
  1010. # 1 % 1
  1011. """
  1012. numberstr : number ADD number
  1013. | number SUB number
  1014. | number DIV number
  1015. | number MULT number
  1016. | number MOD number
  1017. """
  1018. numstring = "(%s %s %s)" %(t[1], t[2], t[3])
  1019. t[0] = numstring
  1020. if self.debug:
  1021. print(numstring)
  1022. def p_mapcalc_function(self, t):
  1023. # Supported mapcalc functions.
  1024. """
  1025. mapcalc_arith : ABS
  1026. | LOG
  1027. | SQRT
  1028. | EXP
  1029. | COS
  1030. | ACOS
  1031. | SIN
  1032. | ASIN
  1033. | TAN
  1034. | DOUBLE
  1035. | FLOATEXP
  1036. | INTEXP
  1037. """
  1038. t[0] = t[1]
  1039. if self.debug:
  1040. print(t[1])
  1041. def p_mapcalc_operation1(self, t):
  1042. # sin(A)
  1043. # log(B)
  1044. """
  1045. expr : mapcalc_arith LPAREN stds RPAREN
  1046. | mapcalc_arith LPAREN expr RPAREN
  1047. """
  1048. # Check input stds.
  1049. maplist = self.check_stds(t[3])
  1050. if self.run:
  1051. resultlist = []
  1052. for map_i in maplist:
  1053. # Create r.mapcalc expression string for the operation.
  1054. if "cmd_list" in dir(map_i):
  1055. cmdstring = "%s(%s)" %(t[1].lower(), map_i.cmd_list)
  1056. else:
  1057. cmdstring = "%s(%s)" %(t[1].lower(), map_i.get_id())
  1058. # Set new command list for map.
  1059. map_i.cmd_list = cmdstring
  1060. # Append map with updated command list to result list.
  1061. resultlist.append(map_i)
  1062. t[0] = resultlist
  1063. if self.debug:
  1064. for map in resultlist:
  1065. print(map.cmd_list)
  1066. def p_mapexpr_operation(self, t):
  1067. # sin(map(a))
  1068. """
  1069. mapexpr : mapcalc_arith LPAREN mapexpr RPAREN
  1070. """
  1071. # Check input stds.
  1072. mapstring = t[3]
  1073. if self.run:
  1074. cmdstring = "%s(%s)" %(t[1].lower(), mapstring)
  1075. t[0] = cmdstring
  1076. if self.debug:
  1077. print(mapstring)
  1078. def p_s_var_expr_1(self, t):
  1079. # isnull(A)
  1080. """
  1081. s_var_expr : ISNULL LPAREN stds RPAREN
  1082. | ISNULL LPAREN expr RPAREN
  1083. """
  1084. # Check input stds.
  1085. maplist = self.check_stds(t[3])
  1086. if self.run:
  1087. resultlist = []
  1088. for map_i in maplist:
  1089. # Create r.mapcalc expression string for the operation.
  1090. if "cmd_list" in dir(map_i):
  1091. cmdstring = "%s(%s)" %(t[1].lower(), map_i.cmd_list)
  1092. else:
  1093. cmdstring = "%s(%s)" %(t[1].lower(), map_i.get_id())
  1094. # Set new command list for map.
  1095. map_i.cmd_list = cmdstring
  1096. # Append map with updated command list to result list.
  1097. resultlist.append(map_i)
  1098. t[0] = resultlist
  1099. if self.debug:
  1100. for map in resultlist:
  1101. print(map.cmd_list)
  1102. def p_s_var_expr_2(self, t):
  1103. # isntnull(A)
  1104. """
  1105. s_var_expr : ISNTNULL LPAREN stds RPAREN
  1106. | ISNTNULL LPAREN expr RPAREN
  1107. """
  1108. # Check input stds.
  1109. maplist = self.check_stds(t[3])
  1110. if self.run:
  1111. resultlist = []
  1112. for map_i in maplist:
  1113. # Create r.mapcalc expression string for the operation.
  1114. if "cmd_list" in dir(map_i):
  1115. cmdstring = "!isnull(%s)" %(map_i.cmd_list)
  1116. else:
  1117. cmdstring = "!isnull(%s)" %(map_i.get_id())
  1118. # Set new command list for map.
  1119. map_i.cmd_list = cmdstring
  1120. # Append map with updated command list to result list.
  1121. resultlist.append(map_i)
  1122. t[0] = resultlist
  1123. if self.debug:
  1124. for map in resultlist:
  1125. print(map.cmd_list)
  1126. def p_s_var_expr_3(self, t):
  1127. # A <= 2
  1128. """
  1129. s_var_expr : stds comp_op number
  1130. | expr comp_op number
  1131. """
  1132. # Check input stds.
  1133. maplist = self.check_stds(t[1])
  1134. if self.run:
  1135. resultlist = []
  1136. for map_i in maplist:
  1137. # Create r.mapcalc expression string for the operation.
  1138. if "cmd_list" in dir(map_i):
  1139. cmdstring = "%s %s %s" %(map_i.cmd_list, t[2], t[3])
  1140. else:
  1141. cmdstring = "%s %s %s" %(map_i.get_id(), t[2], t[3])
  1142. # Set new command list for map.
  1143. map_i.cmd_list = cmdstring
  1144. # Append map with updated command list to result list.
  1145. resultlist.append(map_i)
  1146. t[0] = resultlist
  1147. if self.debug:
  1148. for map in resultlist:
  1149. print(map.cmd_list)
  1150. def p_s_var_expr_4(self, t):
  1151. # exist(B)
  1152. """
  1153. s_var_expr : EXIST LPAREN stds RPAREN
  1154. | EXIST LPAREN expr RPAREN
  1155. """
  1156. # Check input stds.
  1157. maplist = self.check_stds(t[3])
  1158. if self.run:
  1159. resultlist = []
  1160. for map_i in maplist:
  1161. # Create r.mapcalc expression string for the operation.
  1162. if "cmd_list" in dir(map_i):
  1163. cmdstring = "%s" %(map_i.cmd_list)
  1164. else:
  1165. cmdstring = "%s" %(map_i.get_id())
  1166. # Set new command list for map.
  1167. map_i.cmd_list = cmdstring
  1168. # Append map with updated command list to result list.
  1169. resultlist.append(map_i)
  1170. t[0] = resultlist
  1171. if self.debug:
  1172. for map in resultlist:
  1173. print(map.cmd_list)
  1174. def p_s_var_expr_comp(self, t):
  1175. # A <= 2 || B == 10
  1176. # A < 3 && A > 1
  1177. """
  1178. s_var_expr : s_var_expr AND AND s_var_expr
  1179. | s_var_expr OR OR s_var_expr
  1180. """
  1181. if self.run:
  1182. # Check input stds.
  1183. s_var_exprA = self.check_stds(t[1])
  1184. s_var_exprB = self.check_stds(t[4])
  1185. relations = ["EQUAL"]
  1186. temporal = "l"
  1187. function = t[2] + t[3]
  1188. aggregate = t[2]
  1189. # Build conditional values based on topological relationships.
  1190. complist = self.get_temporal_topo_list(s_var_exprA,
  1191. s_var_exprB,
  1192. topolist=relations,
  1193. compare_cmd=True,
  1194. compop=function,
  1195. aggregate=aggregate)
  1196. # Set temporal extent based on topological relationships.
  1197. resultlist = self.set_temporal_extent_list(complist,
  1198. topolist=relations,
  1199. temporal=temporal)
  1200. t[0] = resultlist
  1201. if self.debug:
  1202. for map in resultlist:
  1203. print(map.cmd_list)
  1204. def p_s_var_expr_comp_op(self, t):
  1205. # A <= 2 {||} B == 10
  1206. # A < 3 {&&, equal} A > 1
  1207. """
  1208. s_var_expr : s_var_expr T_COMP_OPERATOR s_var_expr
  1209. """
  1210. if self.run:
  1211. # Check input stds.
  1212. s_var_exprA = self.check_stds(t[1])
  1213. s_var_exprB = self.check_stds(t[3])
  1214. # Evaluate temporal comparison operator.
  1215. relations, temporal, function, aggregate = self.eval_toperator(t[2], optype='boolean')
  1216. # Build conditional values based on topological relationships.
  1217. complist = self.get_temporal_topo_list(s_var_exprA,
  1218. s_var_exprB,
  1219. topolist=relations,
  1220. compare_cmd=True,
  1221. compop=function,
  1222. aggregate=aggregate)
  1223. # Set temporal extent based on topological relationships.
  1224. resultlist = self.set_temporal_extent_list(complist,
  1225. topolist=relations,
  1226. temporal=temporal)
  1227. t[0] = resultlist
  1228. if self.debug:
  1229. for map in resultlist:
  1230. print(map.cmd_list)
  1231. def p_s_expr_condition_if(self, t):
  1232. # if(s_var_expr, B)
  1233. # if(A == 1, B)
  1234. """
  1235. expr : IF LPAREN s_var_expr COMMA stds RPAREN
  1236. | IF LPAREN s_var_expr COMMA expr RPAREN
  1237. | IF LPAREN ts_var_expr COMMA stds RPAREN
  1238. | IF LPAREN ts_var_expr COMMA expr RPAREN
  1239. """
  1240. ifmaplist = self.check_stds(t[3])
  1241. thenmaplist = self.check_stds(t[5])
  1242. resultlist = self.build_condition_cmd_list(ifmaplist,
  1243. thenmaplist,
  1244. elselist=None,
  1245. condition_topolist=["EQUAL"],
  1246. conclusion_topolist=["EQUAL"],
  1247. temporal='r',
  1248. null=False)
  1249. t[0] = resultlist
  1250. if self.debug:
  1251. for map in resultlist:
  1252. print(map.cmd_list)
  1253. def p_s_numeric_condition_if(self, t):
  1254. # if(s_var_expr, 1)
  1255. # if(A == 5, 10)
  1256. """
  1257. expr : IF LPAREN s_var_expr COMMA number RPAREN
  1258. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN RPAREN
  1259. | IF LPAREN ts_var_expr COMMA number RPAREN
  1260. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN RPAREN
  1261. """
  1262. ifmaplist = self.check_stds(t[3])
  1263. resultlist = []
  1264. # Select input for r.mapcalc expression based on length of PLY object.
  1265. if len(t) == 7:
  1266. numinput = str(t[5])
  1267. elif len(t) == 9:
  1268. numinput = str(t[5] + t[6] + t[7])
  1269. # Iterate over condition map list.
  1270. for map_i in ifmaplist:
  1271. # Create r.mapcalc expression string for the operation.
  1272. cmdstring = self.build_command_string(map_i, numinput,
  1273. cmd_type='condition')
  1274. # Conditional append of module command.
  1275. map_i.cmd_list = cmdstring
  1276. # Append map to result map list.
  1277. resultlist.append(map_i)
  1278. t[0] = resultlist
  1279. if self.debug:
  1280. for map in resultlist:
  1281. print(map.cmd_list)
  1282. def p_s_expr_condition_if_relation(self, t):
  1283. # if({equal||during}, s_var_expr, A)
  1284. """
  1285. expr : IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds RPAREN
  1286. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr RPAREN
  1287. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds RPAREN
  1288. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr RPAREN
  1289. """
  1290. relations, temporal, function, aggregation = self.eval_toperator(t[3],
  1291. optype='relation')
  1292. ifmaplist = self.check_stds(t[5])
  1293. thenmaplist = self.check_stds(t[7])
  1294. resultlist = self.build_condition_cmd_list(ifmaplist,
  1295. thenmaplist,
  1296. elselist=None,
  1297. condition_topolist=relations,
  1298. conclusion_topolist=["EQUAL"],
  1299. temporal='r',
  1300. null=False)
  1301. t[0] = resultlist
  1302. if self.debug:
  1303. for map in resultlist:
  1304. print(map.cmd_list)
  1305. def p_s_expr_condition_elif(self, t):
  1306. # if(s_var_expr, A, B)
  1307. """
  1308. expr : IF LPAREN s_var_expr COMMA stds COMMA stds RPAREN
  1309. | IF LPAREN s_var_expr COMMA stds COMMA expr RPAREN
  1310. | IF LPAREN s_var_expr COMMA expr COMMA stds RPAREN
  1311. | IF LPAREN s_var_expr COMMA expr COMMA expr RPAREN
  1312. | IF LPAREN ts_var_expr COMMA stds COMMA stds RPAREN
  1313. | IF LPAREN ts_var_expr COMMA stds COMMA expr RPAREN
  1314. | IF LPAREN ts_var_expr COMMA expr COMMA stds RPAREN
  1315. | IF LPAREN ts_var_expr COMMA expr COMMA expr RPAREN
  1316. """
  1317. # Check map list inputs.
  1318. ifmaplist = self.check_stds(t[3])
  1319. thenmaplist = self.check_stds(t[5])
  1320. elsemaplist = self.check_stds(t[7])
  1321. # Create conditional command map list.
  1322. resultlist = self.build_condition_cmd_list(ifmaplist,
  1323. thenmaplist,
  1324. elselist=elsemaplist,
  1325. condition_topolist=["EQUAL"],
  1326. conclusion_topolist=["EQUAL"],
  1327. temporal='r',
  1328. null=False)
  1329. t[0] = resultlist
  1330. if self.debug:
  1331. for map in resultlist:
  1332. print(map.cmd_list)
  1333. def p_s_numeric_condition_elif(self, t):
  1334. # if(s_var_expr, 1, 2)
  1335. # if(A == 5, 10, 0)
  1336. """
  1337. expr : IF LPAREN s_var_expr COMMA number COMMA number RPAREN
  1338. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA number RPAREN
  1339. | IF LPAREN s_var_expr COMMA number COMMA NULL LPAREN RPAREN RPAREN
  1340. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA NULL LPAREN RPAREN RPAREN
  1341. | IF LPAREN ts_var_expr COMMA number COMMA number RPAREN
  1342. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA number RPAREN
  1343. | IF LPAREN ts_var_expr COMMA number COMMA NULL LPAREN RPAREN RPAREN
  1344. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA NULL LPAREN RPAREN RPAREN
  1345. """
  1346. ifmaplist = self.check_stds(t[3])
  1347. # Select input for r.mapcalc expression based on length of PLY object.
  1348. if len(t) == 9:
  1349. numthen = t[5]
  1350. numelse = t[7]
  1351. elif len(t) == 11 and t[6] == '(':
  1352. numthen = t[5] + t[6] + t[7]
  1353. numelse = t[9]
  1354. elif len(t) == 11 and t[6] == ',':
  1355. numthen = t[5]
  1356. numelse = t[7] + t[8] + t[9]
  1357. elif len(t) == 13:
  1358. numthen = t[5] + t[6] + t[7]
  1359. numelse = t[9] + t[10] + t[11]
  1360. numthen = str(numthen)
  1361. numelse = str(numelse)
  1362. print(numthen + " " +numelse )
  1363. # Create conditional command map list.
  1364. resultlist = self.build_condition_cmd_list(ifmaplist,
  1365. numthen,
  1366. numelse,
  1367. condition_topolist=["EQUAL"],
  1368. conclusion_topolist=["EQUAL"],
  1369. temporal='r',
  1370. null=False)
  1371. t[0] = resultlist
  1372. if self.debug:
  1373. for map in resultlist:
  1374. print(map.cmd_list)
  1375. def p_s_numeric_expr_condition_elif(self, t):
  1376. # if(s_var_expr, 1, A)
  1377. # if(A == 5 && C > 5, A, null())
  1378. """
  1379. expr : IF LPAREN s_var_expr COMMA number COMMA stds RPAREN
  1380. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  1381. | IF LPAREN s_var_expr COMMA number COMMA expr RPAREN
  1382. | IF LPAREN s_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  1383. | IF LPAREN s_var_expr COMMA stds COMMA number RPAREN
  1384. | IF LPAREN s_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  1385. | IF LPAREN s_var_expr COMMA expr COMMA number RPAREN
  1386. | IF LPAREN s_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  1387. | IF LPAREN ts_var_expr COMMA number COMMA stds RPAREN
  1388. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  1389. | IF LPAREN ts_var_expr COMMA number COMMA expr RPAREN
  1390. | IF LPAREN ts_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  1391. | IF LPAREN ts_var_expr COMMA stds COMMA number RPAREN
  1392. | IF LPAREN ts_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  1393. | IF LPAREN ts_var_expr COMMA expr COMMA number RPAREN
  1394. | IF LPAREN ts_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  1395. """
  1396. ifmaplist = self.check_stds(t[3])
  1397. # Select input for r.mapcalc expression based on length of PLY object.
  1398. if len(t) == 9:
  1399. if isinstance(t[5], int):
  1400. theninput = str(t[5])
  1401. elseinput = self.check_stds(t[7])
  1402. elif isinstance(t[7], int):
  1403. theninput = self.check_stds(t[5])
  1404. elseinput = str(t[7])
  1405. elif len(t) == 11:
  1406. if t[5] == 'null':
  1407. theninput = str(t[5] + t[6] + t[7])
  1408. elseinput = self.check_stds(t[9])
  1409. elif t[7] == 'null':
  1410. theninput = self.check_stds(t[5])
  1411. elseinput = str(t[7] + t[8] + t[9])
  1412. # Create conditional command map list.
  1413. resultlist = self.build_condition_cmd_list(ifmaplist,
  1414. theninput,
  1415. elseinput,
  1416. condition_topolist=["EQUAL"],
  1417. conclusion_topolist=["EQUAL"],
  1418. temporal='r',
  1419. null=False)
  1420. t[0] = resultlist
  1421. if self.debug:
  1422. for map in resultlist:
  1423. print(map.cmd_list)
  1424. def p_s_numeric_expr_condition_elif_relation(self, t):
  1425. # if({during},s_var_expr, 1, A)
  1426. # if({during}, A == 5, A, null())
  1427. """
  1428. expr : IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA number COMMA stds RPAREN
  1429. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  1430. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA number COMMA expr RPAREN
  1431. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  1432. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA number RPAREN
  1433. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  1434. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA number RPAREN
  1435. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  1436. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA number COMMA stds RPAREN
  1437. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA NULL LPAREN RPAREN COMMA stds RPAREN
  1438. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA number COMMA expr RPAREN
  1439. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA NULL LPAREN RPAREN COMMA expr RPAREN
  1440. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA number RPAREN
  1441. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA NULL LPAREN RPAREN RPAREN
  1442. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA number RPAREN
  1443. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA NULL LPAREN RPAREN RPAREN
  1444. """
  1445. relations, temporal, function, aggregation = self.eval_toperator(t[3], optype='relation')
  1446. ifmaplist = self.check_stds(t[5])
  1447. # Select input for r.mapcalc expression based on length of PLY object.
  1448. if len(t) == 11:
  1449. if isinstance(t[7], int):
  1450. theninput = str(t[7])
  1451. elseinput = self.check_stds(t[9])
  1452. elif isinstance(t[9], int):
  1453. theninput = self.check_stds(t[7])
  1454. elseinput = str(t[9])
  1455. elif len(t) == 13:
  1456. if t[7] == 'null':
  1457. theninput = str(t[7] + t[8] + t[9])
  1458. elseinput = self.check_stds(t[11])
  1459. elif t[9] == 'null':
  1460. theninput = self.check_stds(t[7])
  1461. elseinput = str(t[9] + t[10] + t[11])
  1462. # Create conditional command map list.
  1463. resultlist = self.build_condition_cmd_list(ifmaplist,
  1464. theninput,
  1465. elseinput,
  1466. condition_topolist=relations,
  1467. conclusion_topolist=["EQUAL"],
  1468. temporal='r',
  1469. null=False)
  1470. t[0] = resultlist
  1471. if self.debug:
  1472. for map in resultlist:
  1473. print(map.cmd_list)
  1474. def p_s_expr_condition_elif_relation(self, t):
  1475. # if({equal||during}, s_var_expr, A, B)
  1476. """
  1477. expr : IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA stds RPAREN
  1478. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA stds COMMA expr RPAREN
  1479. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA stds RPAREN
  1480. | IF LPAREN T_REL_OPERATOR COMMA s_var_expr COMMA expr COMMA expr RPAREN
  1481. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA stds RPAREN
  1482. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA stds COMMA expr RPAREN
  1483. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA stds RPAREN
  1484. | IF LPAREN T_REL_OPERATOR COMMA ts_var_expr COMMA expr COMMA expr RPAREN
  1485. """
  1486. relations, temporal, function, aggregation = self.eval_toperator(t[3], optype='relation')
  1487. ifmaplist = self.check_stds(t[5])
  1488. thenmaplist = self.check_stds(t[7])
  1489. elsemaplist = self.check_stds(t[9])
  1490. # Create conditional command map list.
  1491. resultlist = self.build_condition_cmd_list(ifmaplist,
  1492. thenmaplist,
  1493. elsemaplist,
  1494. condition_topolist=relations,
  1495. conclusion_topolist=["EQUAL"],
  1496. temporal='r',
  1497. null=False)
  1498. t[0] = resultlist
  1499. if self.debug:
  1500. for map in resultlist:
  1501. print(map.cmd_list)
  1502. def p_ts_var_expr1(self, t):
  1503. # Combination of spatial and temporal conditional expressions.
  1504. # Examples:
  1505. # A <= 2 || start_date <= 2013-01-01
  1506. # end_date > 2013-01-15 && A > 10
  1507. # IMPORTANT: Only the intersection of map lists in conditionals are
  1508. # exported.
  1509. """
  1510. ts_var_expr : s_var_expr AND AND t_var_expr
  1511. | t_var_expr AND AND s_var_expr
  1512. | t_var_expr OR OR s_var_expr
  1513. | s_var_expr OR OR t_var_expr
  1514. | ts_var_expr AND AND s_var_expr
  1515. | ts_var_expr AND AND t_var_expr
  1516. | ts_var_expr OR OR s_var_expr
  1517. | ts_var_expr OR OR t_var_expr
  1518. | s_var_expr AND AND ts_var_expr
  1519. | t_var_expr AND AND ts_var_expr
  1520. | s_var_expr OR OR ts_var_expr
  1521. | t_var_expr OR OR ts_var_expr
  1522. """
  1523. if self.run:
  1524. # Check input stds.
  1525. s_var_exprA = self.check_stds(t[1])
  1526. s_var_exprB = self.check_stds(t[4])
  1527. relations = ["EQUAL"]
  1528. temporal = "l"
  1529. function = t[2] + t[3]
  1530. aggregate = t[2]
  1531. # Build conditional values based on topological relationships.
  1532. complist = self.get_temporal_topo_list(s_var_exprA,
  1533. s_var_exprB,
  1534. topolist=relations,
  1535. compare_cmd=True,
  1536. compop=function,
  1537. aggregate=aggregate,
  1538. convert=True)
  1539. # Set temporal extent based on topological relationships.
  1540. resultlist = self.set_temporal_extent_list(complist,
  1541. topolist=relations,
  1542. temporal=temporal)
  1543. t[0] = resultlist
  1544. def p_hash_operation(self, t):
  1545. # Calculate the number of maps within an interval of another map from a
  1546. # second space time dataset.
  1547. # A # B
  1548. # A {equal,r#} B
  1549. """
  1550. expr : t_hash_var
  1551. """
  1552. # Check input stds.
  1553. maplist = self.check_stds(t[1])
  1554. if self.run:
  1555. resultlist = []
  1556. for map_i in maplist:
  1557. for obj in map_i.map_value:
  1558. if isinstance(obj, GlobalTemporalVar):
  1559. n_maps = obj.td
  1560. mapinput = map_i.get_id()
  1561. # Create r.mapcalc expression string for the operation.
  1562. cmdstring = "(%s)" %(n_maps)
  1563. # Append module command.
  1564. map_i.cmd_list = cmdstring
  1565. # Append map to result map list.
  1566. resultlist.append(map_i)
  1567. t[0] = resultlist
  1568. if self.debug:
  1569. for map in resultlist:
  1570. print(map.cmd_list)
  1571. ###############################################################################
  1572. if __name__ == "__main__":
  1573. import doctest
  1574. doctest.testmod()