浏览代码

t.rast.export: added type option to propagate in r.out.gdal

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@67961 15284696-431f-4ddb-bdfa-cd5b030d7da7
Luca Delucchi 9 年之前
父节点
当前提交
fe23c6e20e

+ 10 - 4
lib/python/temporal/stds_export.py

@@ -52,7 +52,8 @@ exported_maps = {}
 ############################################################################
 ############################################################################
 
 
 
 
-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:
     for row in rows:
         name = row["name"]
         name = row["name"]
         start = row["start_time"]
         start = row["start_time"]
@@ -70,7 +71,7 @@ def _export_raster_maps_as_gdal(rows, tar, list_file, new_cwd, fs, format_):
             if format_ == "GTiff":
             if format_ == "GTiff":
                 # Export the raster map with r.out.gdal as tif
                 # Export the raster map with r.out.gdal as tif
                 out_name = name + ".tif"
                 out_name = name + ".tif"
-                if datatype == "CELL":
+                if datatype == "CELL" and not type_:
                     nodata = max_val + 1
                     nodata = max_val + 1
                     if nodata < 256 and min_val >= 0:
                     if nodata < 256 and min_val >= 0:
                         gdal_type = "Byte"
                         gdal_type = "Byte"
@@ -83,6 +84,10 @@ def _export_raster_maps_as_gdal(rows, tar, list_file, new_cwd, fs, format_):
                     gscript.run_command("r.out.gdal", flags="c", input=name,
                     gscript.run_command("r.out.gdal", flags="c", input=name,
                                         output=out_name, nodata=nodata,
                                         output=out_name, nodata=nodata,
                                         type=gdal_type, format="GTiff")
                                         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:
                 else:
                     gscript.run_command("r.out.gdal", flags="c",
                     gscript.run_command("r.out.gdal", flags="c",
                                         input=name, output=out_name,
                                         input=name, output=out_name,
@@ -227,7 +232,7 @@ def _export_raster3d_maps(rows, tar, list_file, new_cwd, fs):
 
 
 
 
 def export_stds(input, output, compression, directory, where, format_="pack",
 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
     """Export space time datasets as tar archive with optional compression
 
 
         This method should be used to export space time datasets
         This method should be used to export space time datasets
@@ -259,6 +264,7 @@ def export_stds(input, output, compression, directory, where, format_="pack",
               - "strds" Space time raster dataset
               - "strds" Space time raster dataset
               - "str3ds" Space time 3D raster dataset
               - "str3ds" Space time 3D raster dataset
               - "stvds" Space time vector dataset
               - "stvds" Space time vector dataset
+        :param datatype: Force the output datatype for r.out.gdal
     """
     """
 
 
     # Save current working directory path
     # Save current working directory path
@@ -295,7 +301,7 @@ def export_stds(input, output, compression, directory, where, format_="pack",
         if type_ == "strds":
         if type_ == "strds":
             if format_ == "GTiff" or format_ == "AAIGrid":
             if format_ == "GTiff" or format_ == "AAIGrid":
                 _export_raster_maps_as_gdal(
                 _export_raster_maps_as_gdal(
-                    rows, tar, list_file, new_cwd, fs, format_)
+                    rows, tar, list_file, new_cwd, fs, format_, datatype)
             else:
             else:
                 _export_raster_maps(rows, tar, list_file, new_cwd, fs)
                 _export_raster_maps(rows, tar, list_file, new_cwd, fs)
         elif type_ == "stvds":
         elif type_ == "stvds":

+ 15 - 2
temporal/t.rast.export/t.rast.export.py

@@ -57,6 +57,16 @@
 #% answer: GTiff
 #% answer: GTiff
 #%end
 #%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
 #%option G_OPT_T_WHERE
 #%end
 #%end
 
 
@@ -74,12 +84,15 @@ def main():
     directory = options["directory"]
     directory = options["directory"]
     where = options["where"]
     where = options["where"]
     _format = options["format"]
     _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
     # Make sure the temporal database exists
     tgis.init()
     tgis.init()
     # Export the space time raster dataset
     # 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__":
 if __name__ == "__main__":

+ 74 - 0
temporal/t.rast.export/testsuite/test_rast_export.py

@@ -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.
+
+@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()