[GRASS-SVN] r67940 - in grass/trunk: lib/python/temporal temporal/t.rast.extract temporal/t.rast.extract/testsuite temporal/t.rast3d.extract temporal/t.rast3d.extract/testsuite temporal/t.vect.extract temporal/t.vect.extract/testsuite
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Feb 24 07:26:55 PST 2016
Author: lucadelu
Date: 2016-02-24 07:26:55 -0800 (Wed, 24 Feb 2016)
New Revision: 67940
Added:
grass/trunk/temporal/t.vect.extract/testsuite/
grass/trunk/temporal/t.vect.extract/testsuite/test_vect_extract.py
Modified:
grass/trunk/lib/python/temporal/extract.py
grass/trunk/temporal/t.rast.extract/t.rast.extract.py
grass/trunk/temporal/t.rast.extract/testsuite/test_extract.py
grass/trunk/temporal/t.rast3d.extract/t.rast3d.extract.py
grass/trunk/temporal/t.rast3d.extract/testsuite/test_extract.py
grass/trunk/temporal/t.vect.extract/t.vect.extract.py
Log:
temporal extract: added suffix option
Modified: grass/trunk/lib/python/temporal/extract.py
===================================================================
--- grass/trunk/lib/python/temporal/extract.py 2016-02-24 15:09:50 UTC (rev 67939)
+++ grass/trunk/lib/python/temporal/extract.py 2016-02-24 15:26:55 UTC (rev 67940)
@@ -12,6 +12,9 @@
from grass.script.utils import get_num_suffix
from space_time_datasets import *
from open_stds import *
+from datetime_math import create_suffix_from_datetime
+from datetime_math import create_time_suffix
+from datetime_math import create_numeric_suffic
from multiprocessing import Process
import grass.script as gscript
from grass.exceptions import CalledModuleError
@@ -19,9 +22,9 @@
############################################################################
-def extract_dataset(input, output, type, where, expression, base, nprocs=1,
- register_null=False, layer=1,
- vtype="point,line,boundary,centroid,area,face"):
+def extract_dataset(input, output, type, where, expression, base, time_suffix,
+ nprocs=1, register_null=False, layer=1,
+ vtype="point,line,boundary,centroid,area,face", ):
"""Extract a subset of a space time raster, raster3d or vector dataset
A mapcalc expression can be provided to process the temporal extracted
@@ -37,6 +40,8 @@
statement
:param base: The base name of the new created maps in case a mapclac
expression is provided
+ :param time_suffix: string to choose which suffix to use: gran, time, num%*
+ (where * are digits)
:param nprocs: The number of parallel processes to be used for mapcalc
processing
:param register_null: Set this number True to register empty maps
@@ -85,9 +90,19 @@
if count % 10 == 0:
msgr.percent(count, num_rows, 1)
- map_name = "{base}_{suffix}".format(base=base,
- suffix=get_num_suffix(count,
- num_rows))
+ if sp.get_temporal_type() == 'absolute' and time_suffix == 'gran':
+ old_map = sp.get_new_map_instance(row["id"])
+ old_map.select(dbif)
+ suffix = create_suffix_from_datetime(old_map.temporal_extent.get_start_time(),
+ sp.get_granularity())
+ map_name = "{ba}_{su}".format(ba=base, su=suffix)
+ elif sp.get_temporal_type() == 'absolute' and time_suffix == 'time':
+ old_map = sp.get_new_map_instance(row["id"])
+ old_map.select(dbif)
+ suffix = create_time_suffix(old_map)
+ map_name = "{ba}_{su}".format(ba=base, su=suffix)
+ else:
+ map_name = create_numeric_suffic(base, count, time_suffix)
# We need to modify the r(3).mapcalc expression
if type != "vector":
Modified: grass/trunk/temporal/t.rast.extract/t.rast.extract.py
===================================================================
--- grass/trunk/temporal/t.rast.extract/t.rast.extract.py 2016-02-24 15:09:50 UTC (rev 67939)
+++ grass/trunk/temporal/t.rast.extract/t.rast.extract.py 2016-02-24 15:26:55 UTC (rev 67940)
@@ -50,6 +50,15 @@
#%end
#%option
+#% key: suffix
+#% type: string
+#% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %05)
+#% answer: gran
+#% required: no
+#% multiple: no
+#%end
+
+#%option
#% key: nprocs
#% type: integer
#% description: Number of r.mapcalc processes to run in parallel
@@ -63,9 +72,9 @@
#% description: Register Null maps
#%end
+
import grass.script as grass
import grass.temporal as tgis
-from multiprocessing import Process
############################################################################
@@ -80,12 +89,13 @@
base = options["basename"]
nprocs = int(options["nprocs"])
register_null = flags["n"]
+ time_suffix = options["suffix"]
# Make sure the temporal database exists
tgis.init()
tgis.extract_dataset(input, output, "raster", where, expression,
- base, nprocs, register_null)
+ base, time_suffix, nprocs, register_null)
###############################################################################
Modified: grass/trunk/temporal/t.rast.extract/testsuite/test_extract.py
===================================================================
--- grass/trunk/temporal/t.rast.extract/testsuite/test_extract.py 2016-02-24 15:09:50 UTC (rev 67939)
+++ grass/trunk/temporal/t.rast.extract/testsuite/test_extract.py 2016-02-24 15:26:55 UTC (rev 67940)
@@ -22,7 +22,20 @@
cls.use_temp_region()
cls.runModule("g.gisenv", set="TGIS_USE_CURRENT_MAPSET=1")
cls.runModule("g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10)
+ cls.runModule("r.mapcalc", expression="prec_1 = 100", overwrite=True)
+ cls.runModule("r.mapcalc", expression="prec_2 = 200", overwrite=True)
+ cls.runModule("r.mapcalc", expression="prec_3 = 300", overwrite=True)
+ cls.runModule("r.mapcalc", expression="prec_4 = 400", overwrite=True)
+ cls.runModule("r.mapcalc", expression="prec_5 = 500", overwrite=True)
+ cls.runModule("r.mapcalc", expression="prec_6 = 600", overwrite=True)
+ cls.runModule("t.create", type="strds", temporaltype="absolute",
+ output="precip_abs1", title="A test",
+ description="A test", overwrite=True)
+ cls.runModule("t.register", flags="i", type="raster", input="precip_abs1",
+ maps="prec_1,prec_2,prec_3,prec_4,prec_5,prec_6",
+ start="2001-01-01", increment="3 months", overwrite=True)
+
@classmethod
def tearDownClass(cls):
"""Remove the temporary region
@@ -49,7 +62,7 @@
def tearDown(self):
"""Remove generated data"""
self.runModule("t.remove", flags="rf", type="strds",
- inputs="precip_abs1,precip_abs2")
+ inputs="precip_abs2")
def test_selection(self):
"""Perform a simple selection by datetime"""
@@ -147,6 +160,27 @@
info = SimpleModule("t.info", flags="g", input="precip_abs2")
self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
+
+ def test_time_suffix_with_expression(self):
+ """Perform extract with time suffix support and test if maps exists"""
+ self.assertModule("t.rast.extract", flags="n", input="precip_abs1",
+ output="precip_abs2", basename="new_prec",
+ nprocs=2, overwrite=True, suffix="time",
+ expression="if(precip_abs1 > 400, precip_abs1, null())")
+ self.assertRasterExists('new_prec_2001_01_01T00_00_00')
+ self.assertRasterDoesNotExist('new_prec_2001_01')
+
+ def test_num_suffix_with_expression(self):
+ """Perform extract with time suffix support and test if maps exists"""
+ self.assertModule("t.rast.extract", flags="n", input="precip_abs1",
+ output="precip_abs2", basename="new_prec",
+ nprocs=2, overwrite=True, suffix='num%03',
+ expression="if(precip_abs1 > 400, precip_abs1, null())")
+ self.assertRasterExists('new_prec_001')
+ self.assertRasterDoesNotExist('new_prec_00001')
+
+
+
class TestRasterExtractionFails(TestCase):
@classmethod
Modified: grass/trunk/temporal/t.rast3d.extract/t.rast3d.extract.py
===================================================================
--- grass/trunk/temporal/t.rast3d.extract/t.rast3d.extract.py 2016-02-24 15:09:50 UTC (rev 67939)
+++ grass/trunk/temporal/t.rast3d.extract/t.rast3d.extract.py 2016-02-24 15:26:55 UTC (rev 67940)
@@ -49,6 +49,15 @@
#%end
#%option
+#% key: suffix
+#% type: string
+#% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %05)
+#% answer: gran
+#% required: no
+#% multiple: no
+#%end
+
+#%option
#% key: nprocs
#% type: integer
#% description: Number of r3.mapcalc processes to run in parallel
@@ -78,12 +87,13 @@
base = options["basename"]
nprocs = int(options["nprocs"])
register_null = flags["n"]
+ time_suffix = options["suffix"]
# Make sure the temporal database exists
tgis.init()
tgis.extract_dataset(input, output, "raster3d", where, expression,
- base, nprocs, register_null)
+ base, time_suffix, nprocs, register_null)
if __name__ == "__main__":
options, flags = grass.parser()
Modified: grass/trunk/temporal/t.rast3d.extract/testsuite/test_extract.py
===================================================================
--- grass/trunk/temporal/t.rast3d.extract/testsuite/test_extract.py 2016-02-24 15:09:50 UTC (rev 67939)
+++ grass/trunk/temporal/t.rast3d.extract/testsuite/test_extract.py 2016-02-24 15:26:55 UTC (rev 67940)
@@ -116,6 +116,23 @@
info = SimpleModule("t.info", flags="g", type="str3ds", input="B")
self.assertModuleKeyValue(module=info, reference=tinfo_string, precision=2, sep="=")
+ def test_time_suffix_with_expression(self):
+ """Perform extract with time suffix support and test if maps exists"""
+ self.assertModule("t.rast3d.extract", flags="n", input="A", nprocs=2,
+ output="B", basename="b", overwrite=True,
+ suffix="time", expression="if(A > 400, A, null())")
+ self.assertRaster3dExists('b_2001_01_01T00_00_00')
+ self.assertRaster3dDoesNotExist('b_2001_01')
+
+ def test_num_suffix_with_expression(self):
+ """Perform extract with time suffix support and test if maps exists"""
+ self.assertModule("t.rast3d.extract", flags="n", input="A", nprocs=2,
+ output="B", basename="b", overwrite=True,
+ suffix='num%03', expression="if(A > 400, A, null())")
+ self.assertRaster3dExists('b_001')
+ self.assertRaster3dDoesNotExist('b_00001')
+
+
class TestRaster3dExtractionFails(TestCase):
@classmethod
Modified: grass/trunk/temporal/t.vect.extract/t.vect.extract.py
===================================================================
--- grass/trunk/temporal/t.vect.extract/t.vect.extract.py 2016-02-24 15:09:50 UTC (rev 67939)
+++ grass/trunk/temporal/t.vect.extract/t.vect.extract.py 2016-02-24 15:26:55 UTC (rev 67940)
@@ -52,6 +52,15 @@
#%end
#%option
+#% key: suffix
+#% type: string
+#% description: Suffix to add at basename: set 'gran' for granularity, 'time' for the full time format, 'num' for numerical suffix with a specific number of digits (default %05)
+#% answer: gran
+#% required: no
+#% multiple: no
+#%end
+
+#%option
#% key: nprocs
#% type: integer
#% description: The number of v.extract processes to run in parallel. Use only if database backend is used which supports concurrent writing
@@ -67,7 +76,6 @@
import grass.script as grass
import grass.temporal as tgis
-from multiprocessing import Process
############################################################################
@@ -84,12 +92,13 @@
base = options["basename"]
nprocs = int(options["nprocs"])
register_null = flags["n"]
+ time_suffix = options["suffix"]
# Make sure the temporal database exists
tgis.init()
tgis.extract_dataset(input, output, "vector", where, expression,
- base, nprocs, register_null, layer, type)
+ base, time_suffix, nprocs, register_null, layer, type)
###############################################################################
Added: grass/trunk/temporal/t.vect.extract/testsuite/test_vect_extract.py
===================================================================
--- grass/trunk/temporal/t.vect.extract/testsuite/test_vect_extract.py (rev 0)
+++ grass/trunk/temporal/t.vect.extract/testsuite/test_vect_extract.py 2016-02-24 15:26:55 UTC (rev 67940)
@@ -0,0 +1,107 @@
+"""Test t.vector.extract
+
+(C) 2014 by the GRASS Development Team
+This program is free software under the GNU General Public
+License (>=v2). Read the file COPYING that comes with GRASS
+for details.
+
+:authors: Luca Delucchi
+"""
+
+from grass.gunittest.case import TestCase
+from grass.gunittest.gmodules import SimpleModule
+
+class TestVectorExtraction(TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ """Initiate the temporal GIS and set the region
+ """
+ cls.use_temp_region()
+
+ cls.runModule("g.region", s=0, n=80, w=0, e=120, res=10)
+ # Use always the current mapset as temporal database
+ for i in range(1, 11):
+ cls.runModule("v.random", output="a{c}".format(c=i), npoints=20,
+ overwrite=True)
+ cls.runModule("v.db.addtable", map="a{c}".format(c=i),
+ columns="value integer")
+ cls.runModule("v.db.update", map="a{c}".format(c=i),
+ column="value", value="'random()'")
+ # Create the temporal database
+ cls.runModule("t.connect", flags="d")
+ cls.runModule("t.info", flags="s")
+ cls.runModule("t.create", type="stvds", temporaltype="absolute",
+ output="A", title="A testvect", description="A testvect",
+ overwrite=True)
+ cls.runModule("t.register", flags="i", type="vector", input="A",
+ maps="a1,a2,a3,a4,a5,a6,a7,a8,a9,a10", start="2001-01-01",
+ increment="3 months", overwrite=True)
+
+ @classmethod
+ def tearDownClass(cls):
+ """Remove the temporary region
+ """
+ cls.del_temp_region()
+ cls.runModule("t.remove", flags="rf", type="stvds", inputs="A")
+
+ def test_selection(self):
+ """Perform a simple selection by datetime"""
+ self.assertModule("t.vect.extract", input="A", output="B",
+ where="start_time > '2001-06-01'", overwrite=True)
+
+ tinfo_string="""start_time=2001-07-01 00:00:00
+ end_time=2003-07-01 00:00:00
+ granularity=3 months
+ map_time=interval
+ number_of_maps=8
+ primitives=160
+ points=160"""
+
+ info = SimpleModule("t.info", flags="g", type="stvds", input="B")
+ self.assertModuleKeyValue(module=info, reference=tinfo_string,
+ precision=2, sep="=")
+
+ def test_selection_no_suffix(self):
+ """Perform a simple selection by datetime"""
+ self.assertModule("t.vect.extract", input="A", output="B",
+ where="start_time > '2001-06-01'", basename="b",
+ overwrite=True)
+ self.assertVectorDoesNotExist('b_2001_07')
+ self.runModule("t.remove", flags="rf", type="stvds", inputs="B")
+
+ def test_selection_suffix(self):
+ """Perform a simple selection by datetime"""
+ self.assertModule("t.vect.extract", input="A", output="B",
+ expression="value > 0", basename="b",
+ overwrite=True)
+ self.assertVectorDoesNotExist('b_2001_07')
+ self.runModule("t.remove", flags="rf", type="stvds", inputs="B")
+
+ def test_selection_time_suffix(self):
+ """Perform a simple selection by datetime"""
+ self.assertModule("t.vect.extract", input="A", output="B",
+ expression="value > 0", basename="b", suffix="time",
+ overwrite=True)
+ self.assertVectorExists('b_2001_01_01T00_00_00')
+ self.runModule("t.remove", flags="rf", type="stvds", inputs="B")
+
+ def test_selection_num_suffix(self):
+ """Perform a simple selection by datetime"""
+ self.assertModule("t.vect.extract", input="A", output="B",
+ expression="value > 0", basename="b", suffix="num",
+ overwrite=True)
+ self.assertVectorExists('b_00001')
+ self.runModule("t.remove", flags="rf", type="stvds", inputs="B")
+
+ def test_selection_num3_suffix(self):
+ """Perform a simple selection by datetime"""
+ self.assertModule("t.vect.extract", input="A", output="B",
+ expression="value > 0", basename="b",
+ suffix="num%03", overwrite=True)
+ self.assertVectorExists('b_001')
+ self.runModule("t.remove", flags="rf", type="stvds", inputs="B")
+
+if __name__ == '__main__':
+ from grass.gunittest.main import test
+ test()
More information about the grass-commit
mailing list