[GRASS-SVN] r31279 - grass-addons/vector/v.random.cover

svn_grass at osgeo.org svn_grass at osgeo.org
Wed May 7 04:28:26 EDT 2008


Author: hamish
Date: 2008-05-07 04:28:26 -0400 (Wed, 07 May 2008)
New Revision: 31279

Added:
   grass-addons/vector/v.random.cover/Makefile
   grass-addons/vector/v.random.cover/description.html
Modified:
   grass-addons/vector/v.random.cover/v.random.cover
Log:
bugfix: area column from v.report is not hardcoded
- add support for querying raster values
- add support files
- set exe bit


Added: grass-addons/vector/v.random.cover/Makefile
===================================================================
--- grass-addons/vector/v.random.cover/Makefile	                        (rev 0)
+++ grass-addons/vector/v.random.cover/Makefile	2008-05-07 08:28:26 UTC (rev 31279)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.random.cover
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script

Added: grass-addons/vector/v.random.cover/description.html
===================================================================
--- grass-addons/vector/v.random.cover/description.html	                        (rev 0)
+++ grass-addons/vector/v.random.cover/description.html	2008-05-07 08:28:26 UTC (rev 31279)
@@ -0,0 +1,50 @@
+<H2>DESCRIPTION</H2>
+
+<em>v.random.cover</em> is a shell script for creating random points
+constrained within an irregularly shaped vector area. (<em>v.random</em>
+places points only in the current region rectangle)
+<P>
+Optionally the values of a raster map at the new sites can be uploaded.
+
+<H2>EXAMPLE</H2>
+
+(Spearfish dataset)<BR>
+Query vegitation cover at 500 random points in the Black Hills Natl. Forest.
+
+<div class="code"><pre>
+  # identify category number of the forest in the fields map
+  v.db.select fields where="label = 'Black Hills Natl. Forest'"
+  # shows that the forest is category 63
+
+  g.region rast=landcover.30m
+  v.random.cover cover=fields cat=63 out=random_NP_cover n=500 \
+     raster=landcover.30m
+
+  # upload category legend from the raster file
+  v.db.addcol random_NP_cover column='landcover varchar(50)'
+  r.category landcover.30m | \
+    ( while read LINE ; do
+        CAT=`echo "$LINE" | cut -f1`
+        LABEL=`echo "$LINE" | cut -f2`
+        v.db.update map=random_NP_cover column=landcover \
+           value="$LABEL" where="sampled = $CAT"
+      done )
+</pre></div>
+
+
+<H2>SEE ALSO</H2>
+
+<em>
+<a HREF="r.random.html">r.random</a> </em>(<tt>cover= vector_output=</tt>)<BR>
+<em>
+<a HREF="v.db.addtable.html">v.db.addtable</a><BR>
+<a HREF="v.db.select.html">v.db.select</a><BR>
+<a HREF="v.random.html">v.random</a><BR>
+<a HREF="v.rast.stats.html">v.rast.stats</a><BR>
+<a HREF="v.what.rast.html">v.what.rast</a>
+</em>
+
+
+<H2>AUTHOR</H2>
+Hamish Bowman<BR>
+<i>Dunedin, New Zealand</i>

Modified: grass-addons/vector/v.random.cover/v.random.cover
===================================================================
--- grass-addons/vector/v.random.cover/v.random.cover	2008-05-07 08:08:25 UTC (rev 31278)
+++ grass-addons/vector/v.random.cover/v.random.cover	2008-05-07 08:28:26 UTC (rev 31279)
@@ -55,6 +55,21 @@
 #% description: Number of points to be created
 #% required: yes
 #%end
+#%Option
+#% key: raster
+#% type: string
+#% required: no
+#% key_desc: name
+#% description: Name of raster map to be sampled
+#% gisprompt: old,cell,raster
+#%End
+#%Option
+#% key: column
+#% type: string
+#% required: no
+#% description: Column name to contain raster samples
+#% answer: sampled
+#%End
 
 
 if  [ -z "$GISBASE" ] ; then
@@ -82,11 +97,18 @@
 LC_NUMERIC=C
 export LC_NUMERIC
 
+#### setup temporary file
+TMP="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
+    g.message -e "Unable to create temporary files"
+    exit 1
+fi
 
 cleanup()
 {
    g.mremove -f vect="vranda_*_$$"
    g.remove region="vrandarea.$$"
+   \rm "$TMP"
 }
 
 # what to do in case of user break:
@@ -132,8 +154,9 @@
 NS_EXT=`g.region -em | grep north | cut -f2 -d':' | awk '{print $1}'`
 EW_EXT=`g.region -em | grep east | cut -f2 -d':' | awk '{print $1}'`
 REGION_AREA=`echo $NS_EXT $EW_EXT | awk '{print $1 * $2}'`
-BOUND_AREA=`v.report map="vranda_cover_$$" option=area units=meters 2>/dev/null \
-   | cut -f3 -d'|' | grep -v '^area$' | tr '\n' ' '`
+v.report map="vranda_cover_$$" option=area units=meters > "$TMP"
+AREA_COLUMN=`head -n 1 "$TMP" | tr '|' '\n' | grep -wn area | cut -f1 -d':'`
+BOUND_AREA=`cut -f${AREA_COLUMN} -d'|' "$TMP" | grep -v '^area$' | tr '\n' ' '`
 
 # for use with multiple cats
 BOUND_AREA_TOT=0
@@ -191,7 +214,46 @@
 fi
 
 cleanup
-
 eval `v.info -t "$GIS_OPT_OUTPUT" | grep points`
+echo "Created <$GIS_OPT_OUTPUT> with $points random points" 1>&2
 
-echo "Created <$GIS_OPT_OUTPUT> with $points random points" 1>&2
+
+# optionally upload values at those sites sampled from a raster map
+if [ -n "$GIS_OPT_RASTER" ] ; then
+   # restore starting region
+   unset WIND_OVERRIDE
+
+   RAST_TYPE=`r.info -t "$GIS_OPT_RASTER"`
+   if [ $? -ne 0 ] ; then
+      # map not found
+      exit
+   fi
+
+   eval `echo $RAST_TYPE`
+   case "$datatype" in
+      CELL)
+        COL_TYPE="integer"
+	;;
+      FCELL | DCELL)
+        COL_TYPE="double precision"
+	;;
+   esac
+
+   # check if column already exists, if not, make it.
+   v.db.addtable map="$GIS_OPT_OUTPUT" \
+      columns="cat integer, $GIS_OPT_COLUMN $COL_TYPE"
+   if [ $? -ne 0 ] ; then
+      # problem creating table
+      exit
+   fi
+
+   v.what.rast vector="$GIS_OPT_OUTPUT" \
+      raster="$GIS_OPT_RASTER" column="$GIS_OPT_COLUMN"
+   if [ $? -ne 0 ] ; then
+      # problem populating table
+      exit
+   fi
+
+   echo "Raster values from <$GIS_OPT_RASTER> uploaded to column <$GIS_OPT_COLUMN>" 1>&2
+fi
+


Property changes on: grass-addons/vector/v.random.cover/v.random.cover
___________________________________________________________________
Name: svn:executable
   + *



More information about the grass-commit mailing list