[GRASS-SVN] r58909 - grass/branches/develbranch_6/raster/r.li/r.li.setup

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Feb 6 14:09:37 PST 2014


Author: hamish
Date: 2014-02-06 14:09:37 -0800 (Thu, 06 Feb 2014)
New Revision: 58909

Modified:
   grass/branches/develbranch_6/raster/r.li/r.li.setup/sample_area_vector.sh
Log:
more updates, this script now ready for testing

Modified: grass/branches/develbranch_6/raster/r.li/r.li.setup/sample_area_vector.sh
===================================================================
--- grass/branches/develbranch_6/raster/r.li/r.li.setup/sample_area_vector.sh	2014-02-06 22:06:42 UTC (rev 58908)
+++ grass/branches/develbranch_6/raster/r.li/r.li.setup/sample_area_vector.sh	2014-02-06 22:09:37 UTC (rev 58909)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # This program is free software under the GPL (>=v2)
 # Read the COPYING file that comes with GRASS for details.
@@ -43,12 +43,6 @@
    exec g.parser "$0" "$@"
 fi
 
-#### set temporary files
-TMP="`g.tempfile pid=$$`"
-if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
-   echo "ERROR: unable to create temporary files" 1>&2
-   exit 1
-fi
 
 #### environment variables
 GISDBASE=`g.gisenv get=GISDBASE`
@@ -56,29 +50,34 @@
 MAPSET=`g.gisenv get=MAPSET`
 : ${GISDBASE?} ${LOCATION?} ${MAPSET?}
 
-f_path="$GISBASE/etc/r.li.setup"
 
-##############################################################
-# read categories from input vector, extract,
-# convert to raster and save the bounds to configuration file
-##############################################################
+#### set temporary files
+TMP="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
+   echo "ERROR: unable to create temporary files" 1>&2
+   exit 1
+fi
 
-# using v.category instead of v.build with cdump
-v.category input=$GIS_OPT_vector option=print | sort | uniq > "$TMP.cat"
 
-# get input vector name
-GIS_OPT_input_vector=`echo $GIS_OPT_vector| cut -d'@' -f 1`
+cleanup()
+{
+   # remove temporary region
+   eval `g.findfile elem=windows file="tmp_rli_sampvect.$$" | grep '^name='`
+   if [ -n "$name" ] ; then
+      g.region region="tmp_rli_sampvect.$$"
+      g.remove region="tmp_rli_sampvect.$$" --quiet
+   fi
 
-# get input vector mapset
-GIS_OPT_input_mapset=`echo $GIS_OPT_vector| cut -d'@' -f 2`
+   rm -f "$TMP"*
+}
+trap "cleanup" 2 3 15
 
-# read input vector categories into CAT_LIST array
-IFS=$'\r\n' CAT_LIST=($(cat $TMP.cat))
 
-# save the current region settings temporarily to avoid surpirses later.
-TMP_REGION="tmp_rlisetup.sampvect.$$"
-g.region save="$TMP_REGION"
+# setup internal region
+#  (WIND_OVERRIDE is already present by the main r.li.setup script)
+g.region save="tmp_rli_sampvect.$$"
 
+
 # find a free Xmonitor
 XMON=x1
 for i in 1 2 3 4 5 6 7 ; do
@@ -89,48 +88,66 @@
    fi
 done
 
+# open Xmonitor
+d.mon stop="$XMON"
+d.mon start="$XMON"
 
-# process each feature in the vector having category values in the CAT_LIST array
-for CAT in "${CAT_LIST[@]}"
-do
-    # vector to store a feature fro $GIS_OPT_vector with category value $CAT.
-    # This temporary vector will be removed at the end.
-    EXTRACT=$GIS_OPT_input_vector"_"$CAT"_part@"$GIS_OPT_input_mapset
 
-    # extract only a part of $GIS_OPT_vector where category = $CAT and store in $EXTRACT
-    v.extract input=$GIS_OPT_vector output=$EXTRACT \
-       type=point,line,boundary,centroid,area,face \
-       new=-1 -d where='CAT='$CAT
+f_path="$GISBASE/etc/r.li.setup"
 
-    # open Xmonitor
-    d.mon stop="$XMON"
-    d.mon start="$XMON"
+##############################################################
+# read categories from input vector, extract,
+# convert to raster and save the bounds to configuration file
+##############################################################
 
+# using v.category instead of v.build with cdump
+v.category input="$GIS_OPT_vector" option=print | \
+  sort -n | uniq > "$TMP.cat"
+
+# crop away @mapset part, if present
+input_vector=`echo "$GIS_OPT_vector" | cut -d'@' -f 1`
+
+
+# process each feature in the vector that has a cat
+while read CAT ; do
+   # skip blank lines..
+    if [ -z "$CAT" ] ; then
+       continue
+    fi
+
+    # Temporary vector map to store an individual feature from the input vector
+    #  It will be removed at the end of the iteration.
+    EXTRACT="tmp_$$_${input_vector}_${CAT}"
+
+    v.extract input="$GIS_OPT_vector" output="$EXTRACT" \
+       type=point,line,centroid,area \
+       new=-1 -d list="$CAT"  # where="CAT = $CAT"
+
     # set region with raster resolution
     g.region vect="$EXTRACT" align="$GIS_OPT_raster"
+
+    d.erase
     d.rast -o "$GIS_OPT_raster"
 
-    # render extracted vector map
+    # render extracted vector map  (prehaps fcolor=none for areas?)
     d.vect "$EXTRACT"
 
     # ask the user to analyse this vector and a name for raster in a Tcl GUI
     name="$TMP.val" # where find the answer
     export name
 
+    # ask if it's ok, save 0,1 to "$name" temp file
     "$GRASS_WISH" "$f_path/area_query"
 
     ok=`cat "$name" | cut -f1 -d ' '`
-    #cat "$name" | cut -f1 -d ' ' > "$name.var"
-    #read ok < "$name.var"
     r_name=`cat "$name" | cut -f2 -d' '`
-    #cat "$name" | cut -f2 -d' ' > "$name.var"
-    #r_name=""
-    #read r_name < "$name.var"
+
+    # debug or needed?
     echo "$r_name"
 
     if [ "$ok" -eq 1 ] ; then
 	#area selected, create mask
-	v.to.rast input="$EXTRACT" output="$r_name" use=cat value=1 rows=4096
+	v.to.rast input="$EXTRACT" output="$r_name" use=cat
 
 	# save the region settings into the configuration file
 	eval `g.region -g`
@@ -138,17 +155,13 @@
     fi
 
     #remove temporary vector map created from v.extract
-    g.remove vect="$EXTRACT"
-    #rm -fR "$GISDBASE"/"$LOCATION"/"$MAPSET"/vector/$GIS_OPT_vector"part"$I
-    #echo DROP TABLE $GIS_OPT_vector"part"$I | db.execute
-done
+    g.remove vect="$EXTRACT" --quiet
 
+done < "$TMP.cat"
+
+
 d.mon stop="$XMON"
 
-# restore the region (which itself is a WIND_OVERRIDE temporary region by
-#  the r.li.setup main script)
-g.region region="$TMP_REGION"
-g.remove region="$TMP_REGION"
 
-# clean tmp files
-rm -f "$TMP"*
+# clean tmp files and restore region
+cleanup



More information about the grass-commit mailing list