Browse Source

r.compress: print flag added; examples added

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@65115 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Neteler 10 years ago
parent
commit
64e5944c2e
2 changed files with 128 additions and 15 deletions
  1. 79 10
      raster/r.compress/main.c
  2. 49 5
      raster/r.compress/r.compress.html

+ 79 - 10
raster/r.compress/main.c

@@ -8,7 +8,7 @@
  *
  *
  * PURPOSE:      Compress and decompress raster map files.
  * PURPOSE:      Compress and decompress raster map files.
  *
  *
- * COPYRIGHT:    (C) 2003 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2015 by the GRASS Development Team
  *
  *
  *               This program is free software under the GNU General Public
  *               This program is free software under the GNU General Public
  *               License (>=v2). Read the file COPYING that comes with GRASS
  *               License (>=v2). Read the file COPYING that comes with GRASS
@@ -20,6 +20,7 @@
  * compress_cell converts straight grid_cell files into compressed grid_cell
  * compress_cell converts straight grid_cell files into compressed grid_cell
  * files.  Compressed files have the following format:
  * files.  Compressed files have the following format:
  *
  *
+ * RLE:
  *  - Array of addresses pointing to the internal start of each row
  *  - Array of addresses pointing to the internal start of each row
  *    First byte of each row is the nuber of bytes per cell for that row
  *    First byte of each row is the nuber of bytes per cell for that row
  *    Remainder of the row is a series of byte groups that describe the data:
  *    Remainder of the row is a series of byte groups that describe the data:
@@ -45,6 +46,7 @@
 
 
 static off_t newsize, oldsize;
 static off_t newsize, oldsize;
 static int process(char *, int);
 static int process(char *, int);
+static int pprint(char *, int);
 static int doit(char *, int, RASTER_MAP_TYPE);
 static int doit(char *, int, RASTER_MAP_TYPE);
 
 
 int main(int argc, char *argv[])
 int main(int argc, char *argv[])
@@ -54,13 +56,14 @@ int main(int argc, char *argv[])
     char *name;
     char *name;
     struct GModule *module;
     struct GModule *module;
     struct Option *map;
     struct Option *map;
-    struct Flag *uncompress;
+    struct Flag *uncompress, *pflag;
 
 
     G_gisinit(argv[0]);
     G_gisinit(argv[0]);
 
 
     module = G_define_module();
     module = G_define_module();
     G_add_keyword(_("raster"));
     G_add_keyword(_("raster"));
     G_add_keyword(_("map management"));
     G_add_keyword(_("map management"));
+    G_add_keyword(_("compression"));
     module->description = _("Compresses and decompresses raster maps.");
     module->description = _("Compresses and decompresses raster maps.");
 
 
     map = G_define_option();
     map = G_define_option();
@@ -75,9 +78,19 @@ int main(int argc, char *argv[])
     uncompress->key = 'u';
     uncompress->key = 'u';
     uncompress->description = _("Uncompress the map");
     uncompress->description = _("Uncompress the map");
 
 
+    pflag = G_define_flag();
+    pflag->key = 'p';
+    pflag->description = _("Print compression information and data type of input map(s)");
+
     if (G_parser(argc, argv))
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 	exit(EXIT_FAILURE);
 
 
+    if(pflag->answer) {
+        for (n = 0; (name = map->answers[n]); n++)
+            pprint(name, pflag->answer);
+        exit(EXIT_SUCCESS);
+    }
+
     stat = 0;
     stat = 0;
     for (n = 0; (name = map->answers[n]); n++)
     for (n = 0; (name = map->answers[n]); n++)
 	if (process(name, uncompress->answer))
 	if (process(name, uncompress->answer))
@@ -95,26 +108,26 @@ static int process(char *name, int uncompress)
     int colr_ok;
     int colr_ok;
     int hist_ok;
     int hist_ok;
     int cats_ok;
     int cats_ok;
-    int quant_ok;
+    int quant_ok=0;
     off_t diff;
     off_t diff;
     RASTER_MAP_TYPE map_type;
     RASTER_MAP_TYPE map_type;
     char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
     char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
 
 
     if (G_find_raster(name, G_mapset()) == NULL) {
     if (G_find_raster(name, G_mapset()) == NULL) {
-	G_warning(_("[%s] not found"), name);
+	G_warning(_("Raster map <%s> not found"), name);
 	return 1;
 	return 1;
     }
     }
     if (Rast_is_reclass(name, G_mapset(), rname, rmapset) > 0) {
     if (Rast_is_reclass(name, G_mapset(), rname, rmapset) > 0) {
 	G_warning(uncompress
 	G_warning(uncompress
 		  ?
 		  ?
-		  _("[%s] is a reclass file of map <%s> in mapset <%s> - can't uncompress")
+		  _("<%s> is a reclass file of map <%s> in mapset <%s> - can't uncompress")
 		  :
 		  :
-		  _("[%s] is a reclass file of map <%s> in mapset <%s> - can't compress"),
+		  _("<%s> is a reclass file of map <%s> in mapset <%s> - can't compress"),
 		  name, rname, rmapset);
 		  name, rname, rmapset);
 	return 1;
 	return 1;
     }
     }
     if (G_find_file2_misc("cell_misc", "gdal", name, G_mapset())) {
     if (G_find_file2_misc("cell_misc", "gdal", name, G_mapset())) {
-	G_warning(_("[%s] is a GDAL-linked map - can't (un)compress"), name);
+	G_warning(_("<%s> is a GDAL-linked map - can't (un)compress"), name);
 	return 1;
 	return 1;
     }
     }
 
 
@@ -180,15 +193,15 @@ static int doit(char *name, int uncompress, RASTER_MAP_TYPE map_type)
 
 
     /* check if already compressed/decompressed */
     /* check if already compressed/decompressed */
     if (uncompress && cellhd.compressed == 0) {
     if (uncompress && cellhd.compressed == 0) {
-	G_warning(_("[%s] already uncompressed"), name);
+	G_warning(_("<%s> already uncompressed"), name);
 	return 1;
 	return 1;
     }
     }
     else if (!uncompress && cellhd.compressed > 0) {
     else if (!uncompress && cellhd.compressed > 0) {
-	G_warning(_("[%s] already compressed"), name);
+	G_warning(_("<%s> already compressed"), name);
 	return 1;
 	return 1;
     }
     }
 
 
-    G_message(_("\n%sCOMPRESS [%s]"), uncompress ? "UN" : "", name);
+    G_message(_("\n%sCOMPRESS <%s>"), uncompress ? "UN" : "", name);
 
 
     Rast_set_window(&cellhd);
     Rast_set_window(&cellhd);
 
 
@@ -231,3 +244,59 @@ static int doit(char *name, int uncompress, RASTER_MAP_TYPE map_type)
     Rast_close(old);
     Rast_close(old);
     return 0;
     return 0;
 }
 }
+
+
+static int pprint(char *name, int printstyle)
+{
+    struct Cell_head cellhd;
+    char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
+    int done;
+    RASTER_MAP_TYPE map_type;
+
+    if (G_find_raster(name, G_mapset()) == NULL) {
+        G_warning(_("Raster map <%s> not found"), name);
+        return 1;
+    }
+    if (G_find_file2_misc("cell_misc", "gdal", name, G_mapset())) {
+        G_message(_("<%s> is a GDAL-linked map"), name);
+        return 1;
+    }
+
+    Rast_get_cellhd(name, G_mapset(), &cellhd);
+    map_type = Rast_map_type(name, G_mapset());
+
+    done = 0;
+    if (Rast_is_reclass(name, G_mapset(), rname, rmapset) > 0) {
+        G_message(_("<%s> is a reclass file of map <%s> in mapset <%s>"),
+                  name, rname, rmapset);
+        done = 1;
+    }
+
+    if (G_find_file2_misc("cell_misc", "gdal", name, G_mapset())) {
+        G_message(_("<%s> is a GDAL-linked map"), name);
+        done = 1;
+    }
+
+    /* Integer (CELL) compression:
+     *    cellhd.compressed == 0: uncompressed
+     *    cellhd.compressed == 1: RLE compressed
+     *    cellhd.compressed == 2: DEFLATE compressed
+     */
+    if (!done && cellhd.compressed == 0) {
+        G_message(_("<%s> is uncompressed (level %i: %s). Data type: <%s>"), name, cellhd.compressed,
+                    "NONE",
+                     (map_type == CELL_TYPE ? "CELL" :
+                       (map_type == DCELL_TYPE ? "DCELL" :
+                         (map_type == FCELL_TYPE ? "FCELL" : "??"))));
+    }
+    else if (!done && cellhd.compressed > 0) {
+        G_message(_("<%s> is compressed (level %i: %s). Data type: <%s>"), name, cellhd.compressed,
+                    cellhd.compressed == 1 ? "RLE" : "DEFLATE",
+                     (map_type == CELL_TYPE ? "CELL" :
+                       (map_type == DCELL_TYPE ? "DCELL" :
+                         (map_type == FCELL_TYPE ? "FCELL" : "??"))));
+    }
+
+    return 0;
+}
+

+ 49 - 5
raster/r.compress/r.compress.html

@@ -1,7 +1,8 @@
 <h2>DESCRIPTION</h2>
 <h2>DESCRIPTION</h2>
 
 
 <em>r.compress</em> can be used to compress and decompress
 <em>r.compress</em> can be used to compress and decompress
-raster map layers.
+raster map layers. Additionally, it prints information about the
+map compression and data type of the input raster map(s).
 
 
 <p>
 <p>
 During compression, this program reformats raster maps using a run-length-encoding
 During compression, this program reformats raster maps using a run-length-encoding
@@ -51,9 +52,10 @@ they are either compressed with ZLIB or uncompressed.
 <p>
 <p>
 Integer (CELL) raster maps are by default ZLIB compressed or may remain
 Integer (CELL) raster maps are by default ZLIB compressed or may remain
 uncompressed. If the environment variable <tt>GRASS_INT_ZLIB</tt>
 uncompressed. If the environment variable <tt>GRASS_INT_ZLIB</tt>
-exists and has the value 0, newly generated compressed integer (CELL type) raster maps will
-be compressed using RLE compression instead of ZLIB. In the internal
-cellhd file, the value for "compressed" is 1 for RLE and 2 for ZLIB.
+exists and has the value 0, newly generated compressed integer (CELL type)
+raster maps will be compressed using RLE compression instead of ZLIB.
+<p>
+In the internal cellhd file, the value for "compressed" is 1 for RLE and 2 for ZLIB.
 <p>
 <p>
 Obviously, decompression is controlled by the raster map's compression,
 Obviously, decompression is controlled by the raster map's compression,
 not the environment variable.
 not the environment variable.
@@ -150,9 +152,51 @@ files).  But even in this case the raster data layer would
 only be larger by the size of the address array and the
 only be larger by the size of the address array and the
 single byte preceding each row.
 single byte preceding each row.
 
 
+<p>
+Since GRASS GIS 7.0.0, the default compression method for
+ Integer (CELL) maps is deflate and not any more the RLE compression.
+
+<h2>EXAMPLES</h2>
+
+Printing of current compression state:
+<div class="code"><pre>
+r.compress compressed_no -p
+  &lt;compressed_no&gt; (level 0: NONE). Data type: &lt;CELL&gt;
+</pre></div>
+
+<p>
+Applying RLE compression to a copy of the uncompressed map:
+<div class="code"><pre>
+# compression of map using RLE compression
+g.copy raster=compressed_no,compressed_RLE
+
+export GRASS_INT_ZLIB=0 # RLE
+r.compress compressed_RLE 
+r.compress compressed_RLE -p 
+  &lt;compressed_RLE&gt; is compressed (level 1: RLE). Data type: &lt;CELL&gt;
+unset GRASS_INT_ZLIB
+</pre></div>
+
+<p>
+Applying DEFLATE (ZLIB) compression to a copy of the uncompressed map:
+<div class="code"><pre>
+# compression of map using DEFLATE compression
+g.copy raster=compressed_no,compressed_DEFLATE
+
+export GRASS_INT_ZLIB=1 # deflate
+r.compress compressed_DEFLATE
+r.compress compressed_DEFLATE -p
+  &lt;compressed_DEFLATE&gt; is compressed (level 2: DEFLATE). Data type: &lt;CELL&gt;
+unset GRASS_INT_ZLIB
+</pre></div>
+
+
 <h2>SEE ALSO</h2>
 <h2>SEE ALSO</h2>
 
 
-<em><a href="r.support.html">r.support</a></em>
+<em>
+<a href="r.info.html">r.info</a>,
+<a href="r.support.html">r.support</a>
+</em>
 
 
 <h2>AUTHORS</h2>
 <h2>AUTHORS</h2>