[GRASS-SVN] r33686 - in grass-addons/vector: . v.what.rast.buffer
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Oct 6 04:52:12 EDT 2008
Author: hamish
Date: 2008-10-06 04:52:12 -0400 (Mon, 06 Oct 2008)
New Revision: 33686
Added:
grass-addons/vector/v.what.rast.buffer/
grass-addons/vector/v.what.rast.buffer/v.what.rast.buffer
Log:
script to extract stats in a buffer around vector points
Added: grass-addons/vector/v.what.rast.buffer/v.what.rast.buffer
===================================================================
--- grass-addons/vector/v.what.rast.buffer/v.what.rast.buffer (rev 0)
+++ grass-addons/vector/v.what.rast.buffer/v.what.rast.buffer 2008-10-06 08:52:12 UTC (rev 33686)
@@ -0,0 +1,212 @@
+#!/bin/bash
+############################################################################
+#
+# MODULE: v.what.rast.buffer
+# AUTHOR(S): Hamish Bowman, Dunedin, New Zealand
+# PURPOSE: Calculates univariate statistics from GRASS raster map(s)
+# from buffers around vector points and writes to .csv.
+# COPYRIGHT: (C) 2008 Hamish Bowman, 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.
+#
+#############################################################################
+#
+# based on DiveSite_extract.sh for GRASS 5
+# script to do site extractions for Fiordland Dive Sites
+# Hamish Bowman Jan-Feb 2004
+#
+# extract value at each dive site and do stats for cells within a 100m radius
+
+#%Module
+#% label: Calculates univariate statistics of raster map(s) from buffers around vector points.
+#% description: Results are written to a file. Resultion is taken from each input map.
+#% keywords: vector, raster, statistics
+#%End
+#%option
+#% key: input
+#% type: string
+#% key_desc: name
+#% gisprompt: old,vector,vector
+#% description: Points vector map containing query positions
+#% required : yes
+#%end
+#%option
+#% key: raster
+#% type: string
+#% key_desc: name
+#% gisprompt: old,cell,raster
+#% description: Name of raster map(s) to calculate statistics from
+#% multiple: yes
+#% required : yes
+#%end
+#%option
+#% key: buffer
+#% type: integer
+#% description: Buffer distance in map units
+#% answer: 100
+#% required : yes
+#%end
+#%option
+#% key: output
+#% type: string
+#% gisprompt: new_file,file,output
+#% key_desc: name
+#% description: Name for output file (if omitted or "-" output to stdout)
+#% required: no
+#%end
+#%option
+#% key: fs
+#% type: string
+#% key_desc: character
+#% description: Field separator in output file
+#% answer: |
+#% required: no
+#%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
+
+
+
+SITES_FILE="$GIS_OPT_INPUT" # sites file to use at extraction points
+QUERY_MAPS="$GIS_OPT_RASTER" # raster file(s) to extract from
+OUTPUT_FILE="$GIS_OPT_OUTPUT"
+BUFFER="$GIS_OPT_BUFFER"
+FS="$GIS_OPT_FS"
+
+eval `g.findfile elem=cell file=MASK`
+if [ "$file" ] ; then
+ g.message -e "Please remove MASK first"
+ exit 1
+fi
+eval `g.findfile elem=vector file="$SITES_FILE"`
+if [ ! "$file" ] ; then
+ g.message -e "Vector file [$SITES_FILE] does not exist"
+ exit 1
+fi
+if [ -e "$OUTPUT_FILE" ] ; then
+ g.message -e "Output File [$OUTPUT_FILE] already exists"
+ exit 1
+fi
+if [ "$BUFFER" -le 0 ] ; then
+ g.message -e "Bad buffer distance"
+ exit 1
+fi
+
+
+#### setup temporary file
+TMP="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
+ g.message -e "Unable to create temporary files"
+ exit 1
+fi
+
+# clone current region
+g.region save="v.what.rast.buffer.$$"
+
+cleanup()
+{
+ #remove temporary region file
+ unset WIND_OVERRIDE
+ g.remove region="v.what.rast.buffer.$$" --quiet
+ g.remove MASK --quiet 2> /dev/null
+ \rm -f "$TMP"
+}
+
+#### trap ctrl-c so that we can clean up tmp
+trap 'cleanup; exit 1' 2 3 15
+
+#echo "Extractions of [$MAP] at [$SITES_FILE] `date`" > "$TMP"
+echo "map_name${FS}cat_id${FS}easting${FS}northing${FS}value${FS}mean_${BUFFER}m_radius${FS}" \
+ "stdev_${BUFFER}m_radius${FS}max_${BUFFER}m_radius${FS}min_${BUFFER}m_radius${FS}" \
+ "number of "$RES"m^2 cells within ${BUFFER}m_radius" > "$TMP"
+
+
+#### per raster map
+IFS=,
+for MAP in $QUERY_MAPS ; do
+
+ g.message "[$MAP]"
+ g.message -v "`basename $0` -- $SITES_FILE -> $MAP @ $GIS_OPT_BUFFER m"
+
+ unset WIND_OVERRIDE
+ g.message -v "Using resolution from: $MAP"
+ WIND_OVERRIDE="v.what.rast.buffer.$$"
+ export WIND_OVERRIDE
+ g.region rast="$MAP"
+
+ eval `r.info -s "$MAP"`
+ RES="$nsres" # FIXME
+ g.region rast="$MAP"
+
+ g.message -v "Extracting [$MAP] at [$SITES_FILE]"
+
+ unset IFS
+ for POS in `v.out.ascii "$SITES_FILE"` ; do
+ # clear old values
+ unset EASTING
+ unset NORTHING
+ unset ID
+ unset val
+ unset max
+ unset min
+ unset n
+ unset mean
+ unset stddev
+
+ EASTING=`echo "$POS" | cut -f1 -d"|"`
+ NORTHING=`echo "$POS" | cut -f2 -d"|"`
+ ID=`echo "$POS" | cut -f3 -d"|"`
+ g.message -v message="site e=$EASTING n=$NORTHING ID=$ID"
+
+ int_EASTING=`echo $EASTING | cut -f1 -d.`
+ int_NORTHING=`echo $NORTHING | cut -f1 -d.`
+
+ # create region bounds so we only process local area
+ R_NORTH=`echo $(((($int_NORTHING / $RES) * $RES) + ($RES * 10) ))`
+ R_SOUTH=`echo $(((($int_NORTHING / $RES) * $RES) - ($RES * 10) ))`
+ R_EAST=`echo $(((($int_EASTING / $RES) * $RES) + ($RES * 10) ))`
+ R_WEST=`echo $(((($int_EASTING / $RES) * $RES) - ($RES * 10) ))`
+
+ # find site value
+ g.region rast="$MAP"
+ val=`r.what in="$MAP" null=nan east_north="$EASTING,$NORTHING" | cut -f4 -d"|"`
+
+ # zoom in on local region (for speed)
+ g.region n=$R_NORTH s=$R_SOUTH e=$R_EAST w=$R_WEST res=$RES
+
+ # get stats about surrounding area, 100m radius
+ g.message -v " Building Buffer ..."
+ r.circle -b output=MASK coord="$EASTING,$NORTHING" min=0 max="$RES" --quiet
+
+ g.message " Calculating stats for cat $ID ..."
+ eval `r.univar -g map="$MAP"`
+
+ g.message -v " Removing Spent Buffer ..."
+ g.remove rast=MASK --quiet
+
+ echo "$MAP${FS}$ID${FS}$EASTING${FS}$NORTHING${FS}$val${FS}$mean${FS}$stddev${FS}$max${FS}$min${FS}$n" \
+ >> "$TMP"
+ done
+done
+
+# write results
+if [ "$OUTPUT_FILE" ] && [ "$OUTPUT_FILE" != "-" ] ; then
+ mv "$TMP" "$OUTPUT_FILE"
+else
+ cat "$TMP"
+fi
+
+
+cleanup
+
+g.message -v " "
+g.message -v 'Done!'
Property changes on: grass-addons/vector/v.what.rast.buffer/v.what.rast.buffer
___________________________________________________________________
Name: svn:executable
+ *
More information about the grass-commit
mailing list