[GRASS-SVN] r38191 - grass-addons/raster/r.denoise

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jul 3 12:26:57 EDT 2009


Author: stevensj
Date: 2009-07-03 12:26:57 -0400 (Fri, 03 Jul 2009)
New Revision: 38191

Added:
   grass-addons/raster/r.denoise/r.denoise
Log:
r.denoise script for denoising raster data

Added: grass-addons/raster/r.denoise/r.denoise
===================================================================
--- grass-addons/raster/r.denoise/r.denoise	                        (rev 0)
+++ grass-addons/raster/r.denoise/r.denoise	2009-07-03 16:26:57 UTC (rev 38191)
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+############################################################################
+#
+# MODULE:       r.denoise
+# AUTHOR(S):    John A Stevenson
+# PURPOSE:      Run Sun's denoising algorithm from within GRASS
+# COPYRIGHT:    (C) 2009 GRASS Development Team/John A Stevenson
+#
+# NOTES:	Requires Sun's denoising algorithm executable (mdenoise).
+#		Instructions for installation of mdenoise are given on the
+#		html manual page (g.manual r.denoise).
+#
+#  This program is free software; you can redistribute it and/or modify
+#  it under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#############################################################################/
+
+#%Module
+#%  description: r.denoise - denoise topographic data
+#%End
+
+#%option
+#% key: input
+#% type: string
+#% gisprompt: old,cell,raster
+#% description: Raster input map
+#% required : yes
+#%end
+
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new,cell,raster
+#% description: Denoised raster output map
+#% required : yes
+#%end
+
+#%option
+#% key: iterations
+#% type: integer
+#% description: Number of normal-updating iterations
+#% answer: 5
+#% options: 1-50
+#% required : no
+#%end
+
+#%option
+#% key: threshold
+#% type: double
+#% description: Edge-sharpness threshold
+#% answer: 0.93
+#% options: 0-1
+#% required : no
+#%end
+
+#%option
+#% key: epsg
+#% type: integer
+#% description: EPSG projection code (required if current location is not projected)
+#%end
+
+if [ -z "$GISBASE" ] ; then
+    echo "You must be in GRASS GIS to run this program." 1>&2
+    exit 1
+fi
+
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+    exec g.parser "$0" "$@"
+fi
+
+
+
+###### set up variables ######
+
+# Test that mdenoise is present
+if [ ! -x "`which mdenoise`" ] ; then
+	echo
+	g.message -e "ERROR: mdenoise required.  Follow instructions in html manual page to install it." 
+	exit 1
+fi
+
+# Assign variables from user input
+INPUT="$GIS_OPT_INPUT"
+OUTPUT="$GIS_OPT_OUTPUT"
+ITER="$GIS_OPT_ITERATIONS"
+THRES="$GIS_OPT_THRESHOLD"
+EPSG="$GIS_OPT_EPSG"
+
+# Test for projected location
+if [ "`g.proj -w | grep PROJCS | wc -l`" -eq "0" ] ; then
+	# If not projected, check if EPSG code was supplied.
+	if [ "$EPSG" = "" ] ; then
+		# Without EPSG code, warn and exit
+		g.message -e "ERROR: during processing r.denoise needs to convert the input map to a projected coordinate system; please specify a suitable EPSG code."
+		exit 1
+	else	# With EPSG code, check the code
+		if [ "`echo 0 0 | cs2cs -v +init=epsg:$EPSG | grep "+units=" | wc -l`" -eq "0" ] ; then
+			g.message -e "ERROR: EPSG code is not suitable.  A projected coordinate system is required"
+			exit 1
+		else
+			REPROJECT=1	
+		fi
+	fi
+else
+	REPROJECT=0
+fi
+
+# prepare temporary files to store points
+temp=`g.tempfile pid=$$`
+tempDN=`g.tempfile pid=$$`
+
+
+
+###### carry out the denoising ######
+
+# Export the map to xyz points.
+g.message "Exporting points..."
+r.stats -1g $INPUT > $temp.xyz
+
+# Reproject if necessary
+if [ "$REPROJECT" -eq "1" ] ; then
+	templl=`g.tempfile pid=$$`
+	mv $temp.xyz $templl.xyz
+	g.message ""
+	g.message "Projecting to EPSG:$EPSG..."
+	cat $templl.xyz | cs2cs `g.proj -jf` +to +init=epsg:$EPSG > $temp.xyz
+fi
+
+# Denoise.  The -z flag preserves the xy positions of the points.
+g.message "Denoising..."
+mdenoise -i $temp.xyz -t $THRES -n $ITER -z -o $tempDN.xyz
+
+if [ "$REPROJECT" -eq "1" ] ; then
+	# If reprojected, it is necessary to return to original coordinate system.
+	g.message "Returning to original coordinate system..."
+	cat $tempDN.xyz | cs2cs -f %.8f +init=epsg:$EPSG +to `g.proj -jf` > $templl\DN.xyz
+	mv $templl.xyz $temp.xyz
+	mv $templl\DN.xyz $tempDN.xyz
+	rm $templl*
+fi
+
+# As only the z coordinates have changed in denoising, to prevent rounding
+# errors, the new z coordinates are combined with the original xy coordinates.
+cat $temp.xyz | awk {'print $1" "$2}' > $temp.xy
+cat $tempDN.xyz | awk {'print $3}' > $tempDN.z
+paste -d " " $temp.xy $tempDN.z > $tempDN.xyz
+
+# Reload data
+g.message "Reloading data..."
+r.in.xyz $tempDN.xyz output=$OUTPUT fs=" " x=1 y=2 z=3 --q
+
+# Clean up
+rm $temp.* $tempDN.* 


Property changes on: grass-addons/raster/r.denoise/r.denoise
___________________________________________________________________
Added: svn:executable
   + *



More information about the grass-commit mailing list