[GRASS-SVN] r59029 - grass/trunk/lib/python/temporal
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Feb 13 16:04:59 PST 2014
Author: huhabla
Date: 2014-02-13 16:04:59 -0800 (Thu, 13 Feb 2014)
New Revision: 59029
Modified:
grass/trunk/lib/python/temporal/temporal_algebra.py
grass/trunk/lib/python/temporal/temporal_raster3d_algebra.py
grass/trunk/lib/python/temporal/temporal_raster_algebra.py
grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py
Log:
Fixed several bugs in raster algebra. Added removing of empty maps.
Modified: grass/trunk/lib/python/temporal/temporal_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_algebra.py 2014-02-13 23:35:01 UTC (rev 59028)
+++ grass/trunk/lib/python/temporal/temporal_algebra.py 2014-02-14 00:04:59 UTC (rev 59029)
@@ -391,7 +391,6 @@
pass
import os
-import grass.script as grass
from space_time_datasets import *
from factory import *
from open_stds import *
@@ -653,6 +652,15 @@
###############################################################################
+class FatalError(Exception):
+ def __init__(self, msg):
+ self.value = msg
+
+ def __str__(self):
+ return self.value
+
+###############################################################################
+
class TemporalAlgebraParser(object):
"""The temporal algebra class"""
@@ -806,18 +814,16 @@
"""
if not isinstance(input, list):
- # Get mapset input.
- mapset = get_current_mapset()
# Check for mapset in given stds input.
if input.find("@") >= 0:
id_input = input
else:
- id_input = input + "@" + mapset
+ id_input = input + "@" + self.mapset
# Create empty spacetime dataset.
stds = dataset_factory(self.stdstype, id_input)
# Check for occurence of space time dataset.
if stds.is_in_db(dbif=self.dbif) == False:
- self.msgr.fatal(_("Space time %s dataset <%s> not found") %
+ raise FatalError(_("Space time %s dataset <%s> not found") %
(stds.get_new_map_instance(None).get_type(), id_input))
else:
# Select temporal dataset entry from database.
Modified: grass/trunk/lib/python/temporal/temporal_raster3d_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_raster3d_algebra.py 2014-02-13 23:35:01 UTC (rev 59028)
+++ grass/trunk/lib/python/temporal/temporal_raster3d_algebra.py 2014-02-14 00:04:59 UTC (rev 59029)
@@ -19,10 +19,11 @@
class TemporalRaster3DAlgebraParser(TemporalRasterBaseAlgebraParser):
"""The temporal raster algebra class"""
- def __init__(self, pid=None, run=False, debug=True, spatial = False, nprocs = 1):
- TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial, nprocs)
+ def __init__(self, pid=None, run=False, debug=True, spatial = False, nprocs = 1, register_null = False):
+ TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial, nprocs, register_null)
self.m_mapcalc = pymod.Module('r3.mapcalc')
+ self.m_mremove = pymod.Module('g.mremove')
def parse(self, expression, basename = None, overwrite=False):
self.lexer = TemporalRasterAlgebraLexer()
@@ -36,6 +37,23 @@
self.expression = expression
self.parser.parse(expression)
+ def remove_empty_maps(self):
+ """! Removes the intermediate vector maps.
+ """
+ if self.empty_maps:
+ self.msgr.message(_("Removing empty 3D raster maps"))
+ namelist = self.empty_maps.values()
+ max = 100
+ chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
+ for chunk in chunklist:
+ stringlist = ",".join(chunk)
+
+ if self.run:
+ m = copy.deepcopy(self.m_mremove)
+ m.inputs["rast3d"].value = stringlist
+ m.flags["f"].value = True
+ m.run()
+
######################### Temporal functions ##############################
def p_statement_assign(self, t):
Modified: grass/trunk/lib/python/temporal/temporal_raster_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_raster_algebra.py 2014-02-13 23:35:01 UTC (rev 59028)
+++ grass/trunk/lib/python/temporal/temporal_raster_algebra.py 2014-02-14 00:04:59 UTC (rev 59029)
@@ -60,10 +60,11 @@
class TemporalRasterAlgebraParser(TemporalRasterBaseAlgebraParser):
"""The temporal raster algebra class"""
- def __init__(self, pid=None, run=False, debug=True, spatial = False, nprocs = 1):
- TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial, nprocs)
+ def __init__(self, pid=None, run=False, debug=True, spatial = False, nprocs = 1, register_null = False):
+ TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial, nprocs, register_null)
self.m_mapcalc = pymod.Module('r.mapcalc')
+ self.m_mremove = pymod.Module('g.mremove')
def parse(self, expression, basename = None, overwrite=False):
self.lexer = TemporalRasterAlgebraLexer()
@@ -77,6 +78,23 @@
self.expression = expression
self.parser.parse(expression)
+ def remove_empty_maps(self):
+ """! Removes the intermediate vector maps.
+ """
+ if self.empty_maps:
+ self.msgr.message(_("Removing empty raster maps"))
+ namelist = self.empty_maps.values()
+ max = 100
+ chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
+ for chunk in chunklist:
+ stringlist = ",".join(chunk)
+
+ if self.run:
+ m = copy.deepcopy(self.m_mremove)
+ m.inputs["rast"].value = stringlist
+ m.flags["f"].value = True
+ m.run()
+
######################### Temporal functions ##############################
def p_statement_assign(self, t):
Modified: grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py
===================================================================
--- grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py 2014-02-13 23:35:01 UTC (rev 59028)
+++ grass/trunk/lib/python/temporal/temporal_raster_base_algebra.py 2014-02-14 00:04:59 UTC (rev 59029)
@@ -123,6 +123,7 @@
'float' : 'FLOATEXP',
'int' : 'INTEXP',
'isnull' : 'ISNULL',
+ 'isntnull': 'ISNTNULL',
'null' : 'NULL',
'exist' : 'EXIST',
}
@@ -187,10 +188,19 @@
'T_ARITH1_OPERATOR'))
def __init__(self, pid=None, run = True, debug = False, spatial = False, \
- nprocs = 1, null = False):
+ nprocs = 1, register_null = False):
TemporalAlgebraParser.__init__(self, pid, run, debug, spatial)
self.nprocs = nprocs
+ self.empty_maps = {}
+ self.register_null = register_null
+ def check_null(self, t):
+ try:
+ int(t)
+ return t
+ except ValueError:
+ return "null()"
+
######################### Temporal functions ##############################
def eval_toperator(self, operator, comparison = False):
@@ -317,6 +327,15 @@
for map_i in register_list:
# Get meta data from grass database.
map_i.load()
+ # Do not register empty maps if not required
+
+ # In case of a null map continue, do not register null maps
+ if map_i.metadata.get_min() is None and \
+ map_i.metadata.get_max() is None:
+ if not self.register_null:
+ self.empty_maps[map_i.get_name()] = map_i.get_name()
+ continue
+
if map_i.is_in_db(dbif) and self.overwrite:
# Update map in temporal database.
map_i.update_all(dbif)
@@ -333,6 +352,8 @@
dbif.close()
t[0] = register_list
+ self.remove_empty_maps()
+
def p_arith1_operation(self, t):
"""
expr : stds MOD stds
@@ -790,6 +811,7 @@
for map in resultlist:
print map.cmd_list
+
def p_s_var_expr(self, t):
# Examples:
# isnull(A)
@@ -819,6 +841,35 @@
for map in resultlist:
print map.cmd_list
+ def p_s_var_expr_1(self, t):
+ # Examples:
+ # isntnull(A)
+ """
+ s_var_expr : ISNTNULL LPAREN stds RPAREN
+ | ISNTNULL LPAREN expr RPAREN
+ """
+ # Check input stds.
+ maplist = self.check_stds(t[3])
+
+ if self.run:
+ resultlist = []
+ for map_i in maplist:
+ # Create r.mapcalc expression string for the operation.
+ if "cmd_list" in dir(map_i):
+ cmdstring = "!isnull(%s)" %(map_i.cmd_list)
+ else:
+ cmdstring = "!isnull(%s)" %(map_i.get_id())
+ # Set new command list for map.
+ map_i.cmd_list = cmdstring
+ # Append map with updated command list to result list.
+ resultlist.append(map_i)
+
+ t[0] = resultlist
+
+ if self.debug:
+ for map in resultlist:
+ print map.cmd_list
+
def p_s_var_expr2(self, t):
# Examples:
# A <= 2
@@ -1015,7 +1066,8 @@
if len(t) == 7:
numinput = t[5]
elif len(t) == 9:
- numinput = t[5] + t[6] + t[7]
+ numinput = self.check_null(t[5])
+
# Iterate over condition map list.
for map_i in ifmaplist:
mapinput = map_i.get_id()
@@ -1304,20 +1356,21 @@
try:
thenmaplist = self.check_stds(t[5])
except:
- numthen = t[5]
+ numthen = self.check_null(t[5])
try:
elsemaplist = self.check_stds(t[7])
except:
- numelse = t[7]
+ numelse = self.check_null(t[7])
elif len(t) == 11:
try:
thenmaplist = self.check_stds(t[5])
except:
- numthen = t[5] + t[6] + t[7]
+ numthen = self.check_null(t[5])
try:
elsemaplist = self.check_stds(t[9])
except:
- numelse = t[7] + t[8] + t[9]
+ numelse = self.check_null(t[7])
+
if thenmaplist != []:
topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist)
elif elsemaplist != []:
@@ -1418,20 +1471,21 @@
try:
thenmaplist = self.check_stds(t[7])
except:
- numthen = t[7]
+ numthen = self.check_null(t[7])
try:
elsemaplist = self.check_stds(t[9])
except:
- numelse = t[9]
+ numelse = self.check_null(t[9])
elif len(t) == 13:
try:
thenmaplist = self.check_stds(t[7])
except:
- numthen = t[9] + t[10] + t[11]
+ numthen = self.check_null(t[7])
try:
elsemaplist = self.check_stds(t[11])
except:
- numelse = t[9] + t[10] + t[11]
+ numelse = self.check_null(t[9])
+
if thenmaplist != []:
topolist = self.get_temporal_topo_list(ifmaplist, thenmaplist, \
topolist = relations)
@@ -2302,15 +2356,13 @@
if len(t) == 9:
numthen = t[5]
numelse = t[7]
- elif len(t) == 11 and t[6] == '(':
- numthen = t[5] + t[6] + t[7]
- numelse = t[9]
- elif len(t) == 11 and t[6] == ',':
- numthen = t[5]
- numelse = t[7] + t[8] + t[9]
+ elif len(t) == 11:
+ numthen = self.check_null(t[5])
+ numelse = self.check_null(t[7])
elif len(t) == 13:
- numthen = t[5] + t[6] + t[7]
- numelse = t[9] + t[10] + t[11]
+ numthen = self.check_null(t[5])
+ numelse = self.check_null(t[9])
+
# Iterate over condition map list.
for map_i in ifmaplist:
mapinput = map_i.get_id()
More information about the grass-commit
mailing list