[GRASS-SVN] r68733 - grass-addons/grass7/raster/r.forestfrag

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jun 22 11:41:43 PDT 2016


Author: wenzeslaus
Date: 2016-06-22 11:41:43 -0700 (Wed, 22 Jun 2016)
New Revision: 68733

Modified:
   grass-addons/grass7/raster/r.forestfrag/r.forestfrag.py
Log:
r.forestfrag: use r.info wrapper to get raster metadata

The previous implementation did full export
and used NumPy but check only min and max.
The NumPy import also failed for non-CELL map.


Modified: grass-addons/grass7/raster/r.forestfrag/r.forestfrag.py
===================================================================
--- grass-addons/grass7/raster/r.forestfrag/r.forestfrag.py	2016-06-22 18:15:49 UTC (rev 68732)
+++ grass-addons/grass7/raster/r.forestfrag/r.forestfrag.py	2016-06-22 18:41:43 UTC (rev 68733)
@@ -74,7 +74,6 @@
 # import libraries
 import os
 import sys
-import numpy as np
 import uuid
 import atexit
 import tempfile
@@ -128,15 +127,18 @@
         grass.message("Setting region to input map...")
         grass.run_command('g.region', quiet=True, raster=ipl)
 
-    # Check if map values are limited to 1 and 0
-    tmpdir = tempfile.mkdtemp()
-    tmpfile = tmpdir + 'forestfrag1'
-    grass.run_command("r.stats", flags="cn", overwrite=True,
-                      quiet=True, input=ipl, output=tmpfile, separator="|")
-    tstf = np.loadtxt(tmpfile, delimiter="|", dtype="int")
-    os.remove(tmpfile)
-    if min(tstf[:,0]) != 0 or max(tstf[:,0]) != 1:
-        grass.fatal("Your input map must be binary, with values 0 and 1")
+    # check if map values are limited to 1 and 0
+    input_info = grass.raster_info(ipl)
+    # we know what we are doing only when input is integer
+    if input_info['datatype'] != 'CELL':
+        grass.fatal(_("The input raster map must have type CELL"
+                      " (integer)"))
+    # for integer, we just need to text min and max
+    if input_info['min'] != 0 or input_info['max'] != 1:
+        grass.fatal(_("The input raster map must be a binary raster,"
+                      " i.e. it should contain only values 0 and 1"
+                      " (now the minimum is %d and maximum is %d)")
+                    % (input_info['min'], input_info['max']))
 
     # computing pf values
     grass.info("Step 1: Computing Pf values...")
@@ -393,8 +395,8 @@
     # Clean up
     if flag_a:
         grass.run_command("g.region", region=regionoriginal, quiet=True, overwrite=True)
-    os.removedirs(tmpdir)
 
+
 if __name__ == "__main__":
     options, flags = grass.parser()
     atexit.register(cleanup)



More information about the grass-commit mailing list