[GRASS-SVN] r73630 - grass-addons/grass7/imagery/i.segment.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Oct 31 03:53:58 PDT 2018


Author: mlennert
Date: 2018-10-31 03:53:58 -0700 (Wed, 31 Oct 2018)
New Revision: 73630

Modified:
   grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html
   grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py
Log:
i.segment.stats: add a check for null files in the raster map to avoid errors

Modified: grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html
===================================================================
--- grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html	2018-10-31 10:19:51 UTC (rev 73629)
+++ grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html	2018-10-31 10:53:58 UTC (rev 73630)
@@ -27,6 +27,16 @@
 areas with the statistics in the attribute table (<b>vectormap</b>)
 and/or in the form of a CSV text file (<b>csvfile</b>).
 
+<p>
+Because of the way <em><a href="r.univar.html">r.univar</a></em> functions, it
+is difficult to handle cases where in some raster maps values are all null 
+in some of the areas. Because of this, <em>i.segment.stats</em> checks the 
+raster maps for existing null values and excludes them if it find any, emitting 
+a warning to inform the user. The user can decide to ignore this check using the
+<b>c</b> flag, for example when there are only a few null cells and no complete
+areas with only null cells (i.e. the module can calculate statistics for areas 
+with some null cells in them).
+
 <h2>NOTES</h2>
 
 <p>
@@ -35,7 +45,7 @@
 calculating the statistics.
 
 <p>
-This module is a simple front-end to <em><a href="v.univar.html">v.univar</a></em>
+This module is a simple front-end to <em><a href="r.univar.html">r.univar</a></em>
 and the 
 <em><a href="r.object.geometry.html">r.object.geometry</a></em>
 add-on. It is the user's responsibility to install the
@@ -76,7 +86,7 @@
 
 <em>
 <a href="i.segment.html">i.segment</a>,
-<a href="v.univar.html">v.univar</a>,
+<a href="r.univar.html">r.univar</a>,
 <a href="v.rast.stats.html">v.rast.stats</a> 
 </em>
 <p>

Modified: grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py
===================================================================
--- grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py	2018-10-31 10:19:51 UTC (rev 73629)
+++ grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py	2018-10-31 10:53:58 UTC (rev 73630)
@@ -91,6 +91,10 @@
 #% key: s
 #% description: Do not calculate any shape statistics
 #% guisection: Shape statistics
+#%end
+#%flag
+#% key: c
+#% description: Do not check rasters for null cells
 #%END
 
 
@@ -229,37 +233,54 @@
 		output_dict[values[0]] = [values[x] for x in stat_indices]
 
     if rasters:
-        gscript.message(_("Calculating statistics for raster maps..."))
-        for raster in rasters:
-            if not gscript.find_file(raster, element='cell')['name']:
-                gscript.message(_("Cannot find raster '%s'" % raster))
-                gscript.message(_("Removing this raster from list."))
-                rasters.remove(raster)
+        if not flags['c']:
+            gscript.message(_("Checking usability of raster maps..."))
+            for raster in rasters:
+                if not gscript.find_file(raster, element='cell')['name']:
+                    gscript.message(_("Cannot find raster '%s'" % raster))
+                    gscript.message(_("Removing this raster from list."))
+                    rasters.remove(raster)
+                raster_info = gscript.parse_command('r.univar',
+                                                    flags='g',
+                                                    map_=raster,
+                                                    quiet=True)
+                if len(raster_info) == 0 or int(raster_info['null_cells']) > 0: 
+                    message = 'Raster %s contains null values.\n' % raster
+                    message += 'This can lead to errors in the calculations.\n'
+                    message += 'Check region settings and raster extent.\n'
+                    message += 'Possibly fill null values of raster.\n'
+                    message += 'Removing this raster from list.'
+                    gscript.warning(message)
+                    while raster in rasters: 
+                        rasters.remove(raster)
+                    continue
 
-        if len(rasters) < processes:
-            processes = len(rasters)
-            gscript.message(_("Only one process per raster. Reduced number of processes to %i." % processes))
+        if len(rasters) > 0:
+            gscript.message(_("Calculating statistics for raster maps..."))
+            if len(rasters) < processes:
+                processes = len(rasters)
+                gscript.message(_("Only one process per raster. Reduced number of processes to %i." % processes))
 
-        stat_indices = [raster_stat_dict[x] for x in raster_statistics]
-        pool = Pool(processes)
-        func = partial(worker, segment_map, stats_temp_file)
-        pool.map(func, rasters)
-        pool.close()
-        pool.join()
+            stat_indices = [raster_stat_dict[x] for x in raster_statistics]
+            pool = Pool(processes)
+            func = partial(worker, segment_map, stats_temp_file)
+            pool.map(func, rasters)
+            pool.close()
+            pool.join()
 
-        for raster in rasters:
-            rastername = raster.split('@')[0]
-            rastername = rastername.replace('.', '_')
-            temp_file = stats_temp_file + '.' + rastername
-            output_header += [rastername + "_" + x for x in raster_statistics]
-            firstline = True
-            with open(temp_file, 'r') as fin:
-                for line in fin:
-                    if firstline:
-                        firstline = False
-                        continue
-                    values = line.rstrip().split('|')
-                    output_dict[values[0]] = output_dict[values[0]] + [values[x] for x in stat_indices]
+            for raster in rasters:
+                rastername = raster.split('@')[0]
+                rastername = rastername.replace('.', '_')
+                temp_file = stats_temp_file + '.' + rastername
+                output_header += [rastername + "_" + x for x in raster_statistics]
+                firstline = True
+                with open(temp_file, 'r') as fin:
+                    for line in fin:
+                        if firstline:
+                            firstline = False
+                            continue
+                        values = line.rstrip().split('|')
+                        output_dict[values[0]] = output_dict[values[0]] + [values[x] for x in stat_indices]
 
 
     # Calculating neighborhood statistics if requested



More information about the grass-commit mailing list