[GRASS-SVN] r67961 - in grass/trunk: lib/python/temporal temporal/t.rast.export temporal/t.rast.export/testsuite

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Feb 26 05:45:17 PST 2016


Author: lucadelu
Date: 2016-02-26 05:45:16 -0800 (Fri, 26 Feb 2016)
New Revision: 67961

Added:
   grass/trunk/temporal/t.rast.export/testsuite/
   grass/trunk/temporal/t.rast.export/testsuite/test_rast_export.py
Modified:
   grass/trunk/lib/python/temporal/stds_export.py
   grass/trunk/temporal/t.rast.export/t.rast.export.py
Log:
t.rast.export: added type option to propagate in r.out.gdal

Modified: grass/trunk/lib/python/temporal/stds_export.py
===================================================================
--- grass/trunk/lib/python/temporal/stds_export.py	2016-02-26 12:24:50 UTC (rev 67960)
+++ grass/trunk/lib/python/temporal/stds_export.py	2016-02-26 13:45:16 UTC (rev 67961)
@@ -52,7 +52,8 @@
 ############################################################################
 
 
-def _export_raster_maps_as_gdal(rows, tar, list_file, new_cwd, fs, format_):
+def _export_raster_maps_as_gdal(rows, tar, list_file, new_cwd, fs, format_, 
+                                type_):
     for row in rows:
         name = row["name"]
         start = row["start_time"]
@@ -70,7 +71,7 @@
             if format_ == "GTiff":
                 # Export the raster map with r.out.gdal as tif
                 out_name = name + ".tif"
-                if datatype == "CELL":
+                if datatype == "CELL" and not type_:
                     nodata = max_val + 1
                     if nodata < 256 and min_val >= 0:
                         gdal_type = "Byte"
@@ -83,6 +84,10 @@
                     gscript.run_command("r.out.gdal", flags="c", input=name,
                                         output=out_name, nodata=nodata,
                                         type=gdal_type, format="GTiff")
+                elif type_:
+                    gscript.run_command("r.out.gdal", flags="cf", input=name,
+                                        output=out_name,
+                                        type=type_, format="GTiff")
                 else:
                     gscript.run_command("r.out.gdal", flags="c",
                                         input=name, output=out_name,
@@ -227,7 +232,7 @@
 
 
 def export_stds(input, output, compression, directory, where, format_="pack",
-                type_="strds"):
+                type_="strds", datatype=None):
     """Export space time datasets as tar archive with optional compression
 
         This method should be used to export space time datasets
@@ -259,6 +264,7 @@
               - "strds" Space time raster dataset
               - "str3ds" Space time 3D raster dataset
               - "stvds" Space time vector dataset
+        :param datatype: Force the output datatype for r.out.gdal
     """
 
     # Save current working directory path
@@ -295,7 +301,7 @@
         if type_ == "strds":
             if format_ == "GTiff" or format_ == "AAIGrid":
                 _export_raster_maps_as_gdal(
-                    rows, tar, list_file, new_cwd, fs, format_)
+                    rows, tar, list_file, new_cwd, fs, format_, datatype)
             else:
                 _export_raster_maps(rows, tar, list_file, new_cwd, fs)
         elif type_ == "stvds":

Modified: grass/trunk/temporal/t.rast.export/t.rast.export.py
===================================================================
--- grass/trunk/temporal/t.rast.export/t.rast.export.py	2016-02-26 12:24:50 UTC (rev 67960)
+++ grass/trunk/temporal/t.rast.export/t.rast.export.py	2016-02-26 13:45:16 UTC (rev 67961)
@@ -57,6 +57,16 @@
 #% answer: GTiff
 #%end
 
+#%option
+#% key: type
+#% type: string
+#% label: Data type
+#% description: Supported only for GTiff
+#% required: no
+#% multiple: no
+#% options: Byte,Int16,UInt16,Int32,UInt32,Float32,Float64,CInt16,CInt32,CFloat32,CFloat64
+#%end
+
 #%option G_OPT_T_WHERE
 #%end
 
@@ -74,12 +84,15 @@
     directory = options["directory"]
     where = options["where"]
     _format = options["format"]
+    _type = options["type"]
 
+    if _type and _format in ["pack", "AAIGrid"]:
+        grass.warning(_("Type options is not working with pack format, it will be skipped"))
     # Make sure the temporal database exists
     tgis.init()
     # Export the space time raster dataset
-    tgis.export_stds(
-        _input, output, compression, directory, where, _format, "strds")
+    tgis.export_stds(_input, output, compression, directory, where, _format,
+                     "strds", _type)
 
 ############################################################################
 if __name__ == "__main__":

Added: grass/trunk/temporal/t.rast.export/testsuite/test_rast_export.py
===================================================================
--- grass/trunk/temporal/t.rast.export/testsuite/test_rast_export.py	                        (rev 0)
+++ grass/trunk/temporal/t.rast.export/testsuite/test_rast_export.py	2016-02-26 13:45:16 UTC (rev 67961)
@@ -0,0 +1,74 @@
+"""
+Test t.rast.export
+
+(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.
+
+ at author: lucadelu
+"""
+
+from grass.gunittest.case import TestCase
+import grass.script as gscript
+import os
+
+class TestRasterExport(TestCase):
+
+    tmp = gscript.tempdir()
+    float_ = os.path.join(tmp, "geotiffloat")
+    int_ = os.path.join(tmp, "geotifint")
+    grid = os.path.join(tmp, "grid")
+    pack = os.path.join(tmp, "pack")
+
+    @classmethod
+    def setUpClass(cls):
+        """Initiate the temporal GIS and set the region
+        """
+        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)
+        maps = []
+        for i in range(10):
+            cls.runModule("r.mapcalc", expression="a_{id_} = rand(0.1,1.0)".format(id_=i),
+                          flags="s", overwrite=True)
+            maps.append("a_{id_}".format(id_=i))
+
+        cls.runModule("t.create", type="strds", temporaltype="absolute",  
+                      output="A", title="A test", description="A test",
+                      overwrite=True)
+        cls.runModule("t.register", flags="i", type="raster", input="A",  
+                      maps=maps, start="2001-01-01",
+                      increment="4 months", overwrite=True)
+
+    @classmethod
+    def tearDownClass(cls):
+        """Remove the temporary region
+        """
+        cls.del_temp_region()
+        cls.runModule("t.remove", flags="rf", inputs="A")
+
+    def test_simple_geotif(self):
+        self.assertModule("t.rast.export", input="A", output=self.float_, 
+                          overwrite=True)
+        self.assertFileExists(self.float_)
+                          
+    def test_simple_geotif_int(self):
+        self.assertModule("t.rast.export", input="A", output=self.int_, 
+                          overwrite=True, type="Int16")
+        self.assertFileExists(self.int_)
+                          
+    def test_simple_grid(self):
+        self.assertModule("t.rast.export", input="A", output=self.grid, 
+                          overwrite=True, format="AAIGrid")
+        self.assertFileExists(self.grid)
+                          
+    def test_simple_pack(self):
+        self.assertModule("t.rast.export", input="A", output=self.pack, 
+                          overwrite=True, format="pack")
+        self.assertFileExists(self.pack)
+
+if __name__ == '__main__':
+    from grass.gunittest.main import test
+    test()



More information about the grass-commit mailing list