[GRASS-SVN] r64996 - in grass-addons/grass7/imagery: . i.segment.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Apr 6 10:16:46 PDT 2015


Author: mlennert
Date: 2015-04-06 10:16:46 -0700 (Mon, 06 Apr 2015)
New Revision: 64996

Added:
   grass-addons/grass7/imagery/i.segment.stats/
   grass-addons/grass7/imagery/i.segment.stats/Makefile
   grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html
   grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py
Log:
New module for calculating statistics for raster areas (mostly meant as complement to i.segment in an object-based image classification toolchain)


Added: grass-addons/grass7/imagery/i.segment.stats/Makefile
===================================================================
--- grass-addons/grass7/imagery/i.segment.stats/Makefile	                        (rev 0)
+++ grass-addons/grass7/imagery/i.segment.stats/Makefile	2015-04-06 17:16:46 UTC (rev 64996)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = i.segment.stats
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html
===================================================================
--- grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html	                        (rev 0)
+++ grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.html	2015-04-06 17:16:46 UTC (rev 64996)
@@ -0,0 +1,34 @@
+<h2>DESCRIPTION</h2>
+
+<em>i.segment.stats</em> calculates statistics for areas in a raster map. Areas are defined by adjacent pixels with the same value. Such areas can be the output of <a href="i.segment">i.segment</a> or <a href="r.clump">r.clump</a>.
+
+Available statistics are those related to the shape and size of the areas (see the <a href="v.to.db">v.to.db</a> man page for more information on the statistics) and aggregated statistics of pixel values of other raster maps (see <a href="v.univar">v.univar</a> for details).
+
+The user can chose between output in the form of a vector map of the areas with the statistics in the attribute table and/or in the form of a csv text file.
+
+<h2>NOTES</h2>
+
+This module is a simple front-end to <a href="v.univar">v.univar</a> and <a href="v.to.db">v.to.db</a>. If other statistics are desired, these should probably be implemented in those (or other) modules which can then be called from this module.
+
+<h2>EXAMPLE</h2>
+
+<div class="code"><pre>
+i.group group=landsat_pan input=lsat7_2002_80
+g.region rast=lsat7_2002_80 -p
+i.segment group=landsat_pan output=ls_pan_seg01 threshold=0.1 memory=4000 minsize=50
+i.segment.stats map=ls_pan_seg01 csvfile=segstats.csv vectormap=ls_pan_seg01 rasters=lsat7_2002_10,lsat7_2002_20,lsat7_2002_30,lsat7_2002_40,lsat7_2002_50,lsat7_2002_70
+</pre></div>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="i.segment">i.segment</a>,
+<a href="v.to.db">v.to.db</a>,
+<a href="v.univar">v.univar</a>,
+<a href="v.rast.stats">v.rast.stats</a> 
+</em>
+
+<h2>AUTHOR</h2>
+Moritz Lennert
+
+<p><i>Last changed: $Date: 2015-04-06 01:19:56 +0200 (lun 06 avr 2015) $</i>

Added: grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py
===================================================================
--- grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py	                        (rev 0)
+++ grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py	2015-04-06 17:16:46 UTC (rev 64996)
@@ -0,0 +1,174 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE:       i.segment.stats
+# AUTHOR:       Moritz Lennert
+# PURPOSE:      Calculates statistics describing raster areas
+#               (notably for segments resulting from i.segment)
+#
+# COPYRIGHT:    (c) 2015 Moritz Lennert, and 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.
+#
+#############################################################################
+
+#%module
+#% description: Calculates statistics describing raster areas
+#% keyword: imagery
+#% keyword: segmentation
+#% keyword: statistics
+#%end
+#%option G_OPT_R_MAP
+#% label: Raster map with areas (all pixels of an area have same id)
+#% description: Raster map with areas, such as the output of i.segment
+#% required: yes
+#%end
+#%option G_OPT_R_INPUTS
+#% key: rasters
+#% description: Name of input raster maps for statistics
+#% multiple: yes
+#% required: no
+#% guisection: raster_statistics
+#%end
+#%option
+#% key: raster_statistics
+#% type: string
+#% label: Statistics to calulate for each input raster map
+#% required: no
+#% multiple: yes
+#% options: min,max,range,mean,mean_of_abs,stddev,variance,coeff_var,sum,sum_abs,first_quart,median,third_quart,perc_90
+#% answer: mean,stddev,sum
+#% guisection: raster_statistics
+#%end
+#%rules
+#% requires: raster_statistics,rasters
+#%end
+#%option
+#% key: area_measures
+#% type: string
+#% label: Area measurements to include in the output
+#% required: no
+#% multiple: yes
+#% options: area,perimeter,compact,fd
+#% answer: area,perimeter,compact,fd
+#% guisection: shape_statistics
+#%end
+#%option G_OPT_F_OUTPUT
+#% key: csvfile
+#% label: CSV output file containing statistics
+#% required: no
+#% guisection: output
+#%end
+#% option G_OPT_V_OUTPUT
+#% key: vectormap
+#% label: Optional vector output map with statistics as attributes
+#% required: no
+#% guisection: output
+#%end
+#%rules
+#% required: csvfile,vectormap
+#%end
+
+import os
+import atexit
+import collections
+import math
+import grass.script as grass
+    
+
+def cleanup():
+
+    if grass.find_file(temporary_vect, element='vector')['name']:
+            grass.run_command('g.remove', flags='f', type_='vector',
+                    name=temporary_vect)
+    if insert_sql:
+        os.remove(insert_sql)
+
+
+def main():
+
+    grass.use_temp_region()
+
+    segment_map = options['map']
+    csvfile = options['csvfile'] if options['csvfile'] else []
+    vectormap = options['vectormap'] if options['vectormap'] else []
+    rasters = options['rasters'].split(',') if options['rasters'] else []
+    area_measures = options['area_measures'].split(',') if options['area_measures'] else []
+    raster_statistics = options['raster_statistics'].split(',') if options['raster_statistics'] else []
+
+    output_header = ['cat']
+    output_dict = collections.defaultdict(list)
+
+    global insert_sql
+    insert_sql = None
+
+    global temporary_vect
+    temporary_vect = 'segmstat_tmp_vect_%d' % os.getpid()
+
+    raster_stat_dict = {'zone': 0, 'min': 4, 'third_quart': 16, 'max': 5, 'sum':
+            12, 'null_cells': 3, 'median': 15, 'label': 1, 'first_quart': 14,
+            'range': 6, 'mean_of_abs': 8, 'stddev': 9, 'non_null_cells': 2,
+            'coeff_var': 11, 'variance': 10, 'sum_abs': 13, 'perc_90': 17,
+            'mean': 7}
+    
+    grass.run_command('g.region', raster=segment_map)
+    grass.run_command('r.to.vect', input_=segment_map, output=temporary_vect,
+            type_='area' , flags='vt')
+
+    for area_measure in area_measures:
+        output_header.append(area_measure)
+        res=grass.read_command('v.to.db', map_=temporary_vect,
+                option=area_measure, column=area_measure,
+                flags='p').splitlines()[1:]
+        for element in res:
+            values = element.split('|')
+            output_dict[values[0]].append(values[1])
+
+    for raster in rasters:
+        if not grass.find_file(raster, element='raster')['name']:
+            grass.message(_("Cannot find raster %s" % raster))
+            continue
+        grass.run_command('g.region', raster=raster)
+        rastername=raster.split('@')[0]
+        output_header = output_header + [rastername + "_" + x 
+                for x in raster_statistics]
+        stat_indices = [raster_stat_dict[x] for x in raster_statistics]
+        res=grass.read_command('r.univar', map_=raster, zones=segment_map,
+                flags='et').splitlines()[1:]
+        print res
+        for element in res:
+            values = element.split('|')
+            output_dict[values[0]] = output_dict[values[0]]+ [values[x] for x in
+                    stat_indices]
+
+    if vectormap:
+        insert_sql=grass.tempfile()
+        fsql = open(insert_sql, 'w')
+        fsql.write('BEGIN TRANSACTION;\n')
+        create_statement = 'CREATE TABLE ' + vectormap + ' (cat int, '
+        for header in output_header[1:-1]:
+            create_statement += header +  ' double precision, '
+        create_statement += output_header[-1] + ' double precision);\n'
+        fsql.write(create_statement)
+        for key in sorted(output_dict):
+                fsql.write("INSERT INTO " + vectormap + " VALUES (" +
+                        key+","+",".join(output_dict[key])+");\n")
+        fsql.write('END TRANSACTION;')
+        fsql.close()
+        grass.run_command('g.copy', vector=temporary_vect+','+vectormap)
+        grass.run_command('db.execute', input=insert_sql)
+        grass.run_command('v.db.connect', map_=vectormap, table=vectormap)
+
+
+    if csvfile:
+        with open(csvfile, 'wb') as f:
+            f.write(",".join(output_header)+"\n")
+            for key in sorted(output_dict):
+                f.write(key+","+",".join(output_dict[key])+"\n")
+        f.close()
+
+if __name__ == "__main__":
+    options, flags = grass.parser()
+    atexit.register(cleanup)
+    main()


Property changes on: grass-addons/grass7/imagery/i.segment.stats/i.segment.stats.py
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list