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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jul 8 06:25:50 EDT 2009


Author: stevensj
Date: 2009-07-08 06:25:49 -0400 (Wed, 08 Jul 2009)
New Revision: 38307

Modified:
   grass-addons/raster/r.denoise/r.denoise
Log:
fixes to variable and tempfile handling; output file metadata now gets updated

Modified: grass-addons/raster/r.denoise/r.denoise
===================================================================
--- grass-addons/raster/r.denoise/r.denoise	2009-07-08 06:42:11 UTC (rev 38306)
+++ grass-addons/raster/r.denoise/r.denoise	2009-07-08 10:25:49 UTC (rev 38307)
@@ -83,7 +83,7 @@
 # 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." 
+	g.message -e "ERROR: mdenoise required.  Follow instructions in html manual page to install it (g.manual r.denoise)." 
 	exit 1
 fi
 
@@ -95,14 +95,15 @@
 EPSG="$GIS_OPT_EPSG"
 
 # Test for projected location
-if [ "`g.proj -w | grep PROJCS | wc -l`" -eq "0" ] ; then
+if [ `g.proj -j | grep -c '+proj=longlat'` -eq 1  ] ; then
 	# If not projected, check if EPSG code was supplied.
-	if [ "$EPSG" = "" ] ; then
+	if [ -z "$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."
+		g.message -e "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
+	else	# With EPSG code, check that it corresponds to a projected
+		# locality by converting null data in verbose mode.
+		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
@@ -113,49 +114,57 @@
 	REPROJECT=0
 fi
 
-# prepare temporary files to store points
-temp=`g.tempfile pid=$$`
-tempDN=`g.tempfile pid=$$`
+# prepare temporary files to store points and check they were created
+temp="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "${temp}" ] ; then
+    g.message -e "unable to create temporary files"
+    exit 1
+fi
 
-
-
 ###### carry out the denoising ######
 
 # Export the map to xyz points.
 g.message "Exporting points..."
-r.stats -1g $INPUT > $temp.xyz
+r.stats -1g "$INPUT" > "$temp".xyz
 
 # Reproject if necessary
 if [ "$REPROJECT" -eq "1" ] ; then
+	# Create tempfile and check it worked
 	templl=`g.tempfile pid=$$`
-	mv $temp.xyz $templl.xyz
+	if [ $? -ne 0 ] || [ -z "${templl}" ] ; then
+    		g.message -e "unable to create temporary files"
+		exit 1
+	fi
+	\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
+	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
+mdenoise -i "$temp".xyz -t "$THRES" -n "$ITER" -z -o "$temp".DN.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*
+	cat "$temp".DN.xyz | cs2cs -f %.8f +init=epsg:$EPSG +to `g.proj -jf` > "$templl".DN.xyz
+	\mv "$templl".xyz "$temp".xyz
+	\mv "$templl".DN.xyz "$temp".DN.xyz
 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
+cat "$temp".xyz | awk '{print $1" "$2}' > "$temp".xy
+cat "$temp".DN.xyz | awk '{print $3}' > "$temp".DN.z
+paste -d " " "$temp".xy "$temp".DN.z > "$temp".DN.xyz
 
 # Reload data
 g.message "Reloading data..."
-r.in.xyz $tempDN.xyz output=$OUTPUT fs=" " x=1 y=2 z=3 --q
+r.in.xyz "$temp".DN.xyz output="$OUTPUT" fs=space x=1 y=2 z=3 --q
 
+# Edit metadata to record denoising parameters
+r.support "$OUTPUT" title="A denoised version of <$INPUT>" source1="Generated by: r.denoise $INPUT iterations=$ITER threshold=$THRES"
+
 # Clean up
-rm $temp.* $tempDN.* 
+\rm "$temp".*



More information about the grass-commit mailing list