[GRASS-SVN] r48059 - grass-addons/vector/v.rast.stats2

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 2 05:11:23 EDT 2011


Author: mmetz
Date: 2011-09-02 02:11:23 -0700 (Fri, 02 Sep 2011)
New Revision: 48059

Modified:
   grass-addons/vector/v.rast.stats2/v.rast.stats2
Log:
update grass-addon v.rast.stats2

Modified: grass-addons/vector/v.rast.stats2/v.rast.stats2
===================================================================
--- grass-addons/vector/v.rast.stats2/v.rast.stats2	2011-09-02 09:02:58 UTC (rev 48058)
+++ grass-addons/vector/v.rast.stats2/v.rast.stats2	2011-09-02 09:11:23 UTC (rev 48059)
@@ -12,7 +12,6 @@
 #		License (>=v2). Read the file COPYING that comes with GRASS
 #		for details.
 #
-# TODO: do we need layer= management?
 #############################################################################
 
 #%Module
@@ -47,7 +46,6 @@
 #% key: keycol
 #% type: string
 #% description: Key column of attribute table used to join statistics 
-#% answer: cat
 #% required : no
 #%end
 #%option
@@ -111,32 +109,32 @@
 fi
 
 #### setup temporary file
-TMP="`g.tempfile pid=$$`"
-if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
+TEMPFILE="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "$TEMPFILE" ] ; then
     g.message -e "Unable to create temporary files" 
     exit 1
 fi
 
 ### variables for temp files
-SQLTMP="$TMP.sql"
-STATSTMP="$TMP.csv"
-COLNAMETMP="$TMP.col"
+SQLTMP="$TEMPFILE.sql"
+STATSTMP="$TEMPFILE.csv"
+COLNAMETMP="$TEMPFILE.col"
 
-### we need a random name
-TMPNAME=`basename "$TMP"`
+# we need a random name
+TMPNAME=`basename "$TEMPFILE"`
 
 cleanup()
 {
    # restore settings:
    g.region region="$TMPNAME"
    g.remove region="$TMPNAME" --quiet
-   g.remove rast="${VECTOR}_${TMPNAME}" --quiet
+   g.remove rast="${VECTOR}_$TMPNAME" --quiet
    g.remove MASK --quiet 2>/dev/null
    if [ $MASKFOUND -eq 1 ] ; then
       g.message "Restoring previous MASK..."
       g.rename "${TMPNAME}_origmask",MASK --quiet
    fi
-   rm -f "$TMP" "$TMPNAME" "$TMP.cats" "$SQLTMP"
+   rm -f "$TEMPFILE" "$TMPNAME" "$TEMPFILE.cats" "$SQLTMP"
 }
 
 # what to do in case of user break:
@@ -151,7 +149,6 @@
 
 RASTER="$GIS_OPT_RASTER"
 COLPREFIX="$GIS_OPT_COLPREFIX"
-KEYCOL="$GIS_OPT_KEYCOL"
 
 ### setup enviro vars ###
 eval `g.gisenv`
@@ -200,13 +197,11 @@
 # save current settings:
 g.region save="$TMPNAME" --quiet
 
-# temporarily setting raster resolution to $RASTER resolution
-# keep boundary settings
-g.region nsres=$NSRES ewres=$EWRES -a
+# temporarily aligning current region to raster <$RASTER>
+g.region align="$RASTER"
 
-# create raster from vector map for r.univar.zonal 
-v.to.rast in="$VECTORFULL" out="${VECTOR}_${TMPNAME}" use=attr \
-col="$KEYCOL" type=area --quiet
+# create raster from vector map for r.univar 
+v.to.rast in="$VECTORFULL" out="${VECTOR}_$TMPNAME" layer="$GIS_OPT_LAYER" use=cat type=area --quiet
 if [ $? -ne 0 ] ; then
    g.message -e "An error occurred while converting vector to raster"
    cleanup
@@ -214,9 +209,9 @@
 fi
 
 # dump cats to file to avoid "too many argument" problem:
-r.category "${VECTOR}_${TMPNAME}" fs=';' --quiet | cut -d';' -f1 > "$TMP.cats"
+r.category "${VECTOR}_$TMPNAME" fs=';' --quiet | cut -d';' -f1 > "$TEMPFILE.cats"
 # echo "List of categories found: $CATSLIST"
-NUMBER=`cat "$TMP.cats" | wc -l | awk '{print $1}'`
+NUMBER=`cat "$TEMPFILE.cats" | wc -l | awk '{print $1}'`
 if [ $NUMBER -lt 1 ] ; then
    g.message -e "No categories found in raster map"
    cleanup
@@ -225,30 +220,52 @@
 
 # check if DBF driver used, in this case cut to 10 chars col names:
 DBFDRIVER=0
-v.db.connect -g "$VECTORFULL" fs=";" | grep -w "$GIS_OPT_LAYER" | cut -d';' -f5 | grep -i dbf --quiet
+v.db.connect -gl map="$VECTOR" fs="|" layer="$GIS_OPT_LAYER" | \
+   cut -d'|' -f5 | grep -i 'dbf' --quiet
 if [ $? -eq 0 ] ; then
    DBFDRIVER=1
 else
    # in case a table is missing, we'll trap a crash later...
    DBFDRIVER=0
 fi
+
+if [ $DBFDRIVER -eq 1 ] ; then
+   # `wc -c` reports number of chars + 1 for the newline
+   if [ `echo "$COLPREFIX" | wc -c` -gt 10 ] ; then
+       g.message -e "Cannot create unique names for columns. \
+         Either use a shorter column prefix or switch to another DB \
+	 backend such as SQLite. DBF limits the length to 10 characters"
+       cleanup
+       exit 1
+   fi
+fi
+
 # we need this for non-DBF driver:
-DB_SQLDRIVER=`v.db.connect -g "$VECTORFULL" fs=";" | grep -w "$GIS_OPT_LAYER" | cut -d';' -f5`
-DB_DATABASE="`v.db.connect -g "$VECTORFULL" fs=";" | grep -w "$GIS_OPT_LAYER" | cut -d';' -f4`"
+DB_SQLDRIVER=`v.db.connect -gl map="$VECTOR" fs="|" layer="$GIS_OPT_LAYER" | cut -d'|' -f5`
+g.message -d message="sql driver: [$DB_SQLDRIVER]"
 
+DB_DATABASE="`v.db.connect -gl map=$VECTOR fs='|' layer=$GIS_OPT_LAYER | cut -d'|' -f4`"
+g.message -d message="database: [$DB_DATABASE]"
+
 # find out which table is linked to the vector map on the given layer
-TABLE=`v.db.connect map="$VECTORFULL" -g fs=";" | grep -w "$GIS_OPT_LAYER" | awk -F ";" '{print $2}'`
+TABLE=`v.db.connect map="$VECTOR" -gl fs='|' layer="$GIS_OPT_LAYER" | cut -d'|' -f2`
 if [ -z "$TABLE" ] ; then
    g.message -e 'There is no table connected to this map! Run v.db.connect or v.db.addtable first.'
    exit 1
 fi
+g.message -d message="table: [$TABLE]"
 
+# get key column
+if [ -z "$GIS_OPT_KEYCOL" ] ; then
+  KEYCOL=`v.db.connect map="$VECTOR" -gl fs='|' layer="$GIS_OPT_LAYER" | cut -d'|' -f3`
+fi
+
 BASECOLS="n min max range mean stddev variance cf_var sum"
 
 # do extended stats?
 if [ $GIS_FLAG_E -eq 1 ] ; then
    # namespace is limited in DBF but the % value is important
-   if [ $DBFDRIVER -eq 1 ] ; then
+   if [ "$DBFDRIVER" -eq 1 ] ; then
       PERCCOL="per$GIS_OPT_PERCENTILE"
    else
       PERCCOL="percentile_$GIS_OPT_PERCENTILE"
@@ -263,21 +280,22 @@
 for i in $BASECOLS $EXTRACOLS ; do
   # check if column already present
   if [ $DBFDRIVER -eq 1 ] ; then
-     CURRCOLUMN="`echo "${COLPREFIX}_${i}" | cut -b1-10`"
+     CURRCOLUMN="`echo "${COLPREFIX}_$i" | cut -b1-10`"
   else
-     CURRCOLUMN="${COLPREFIX}_${i}"
+     CURRCOLUMN="${COLPREFIX}_$i"
   fi
   if [ -n "$COLNAMES" ] ; then
      COLNAMES="${COLNAMES},$CURRCOLUMN"
   else
      COLNAMES="$CURRCOLUMN"
   fi
-  v.info -c $VECTORFULL layer="$GIS_OPT_LAYER" --quiet | sed 's+^+|+g' | sed 's+$+|+g' | \
+  g.message -d message="Looking for <$CURRCOLUMN> in <$VECTOR> ..."
+  v.info -c "$VECTORFULL" layer="$GIS_OPT_LAYER" --quiet | sed -e 's+^+|+' -e 's+$+|+' | \
     grep "|$CURRCOLUMN|" --quiet
   if [ $? -eq 0 ] ; then
-    if [ $GIS_FLAG_C -ne 1 ] ; then
-       g.message -w "Cannot create column <$CURRCOLUMN> (already present)."
-       g.message -e "Use -c flag to update values in this column."
+    if [ "$GIS_FLAG_C" -ne 1 ] ; then
+       g.message -e "Cannot create column <$CURRCOLUMN> (already present). \
+         Use -c flag to update values in this column."
        cleanup
        exit 1
     fi
@@ -330,10 +348,10 @@
 unset $BASECOLS $EXTRACOLS
 if [ $GIS_FLAG_E -eq 1 ] ; then 
     $univar_cmd -t -e map="$RASTER" \
-    zones="${VECTOR}_${TMPNAME}" percentile="$GIS_OPT_PERCENTILE" | \
+    zones="${VECTOR}_$TMPNAME" percentile="$GIS_OPT_PERCENTILE" | \
     cut -f1,3,5-8,10-13,15-18 -d'|' | sed 's+nan+NULL+g' > "$STATSTMP"
 else
-    $univar_cmd -t map="$RASTER" zones="${VECTOR}_${TMPNAME}" | \
+    $univar_cmd -t map="$RASTER" zones="${VECTOR}_$TMPNAME" | \
     cut -f1,3,5-8,10-13 -d'|' | sed 's+nan+NULL+g' > "$STATSTMP"
 fi
 
@@ -366,7 +384,7 @@
     sed -e '1d' "$STATSTMP" | awk -F "|" '{printf "\nUPDATE '${TABLE}' SET '${col1}' = %i , '${col2}' = %.2f , '${col3}' = %.2f , '${col4}' = %.2f , '${col5}' = %.2f , '${col6}' = %.2f , '${col7}' = %.2f , '${col8}' = %.2f , '${col9}' = %.2f WHERE '${KEYCOL}' = %i;", $2,$3,$4,$5,$6,$7,$8,$9,$10,$1}' > "$SQLTMP"   
 fi
 
-g.message -v "Updating the database ..."
+g.message message="Updating the database ..."
 db.execute input="$SQLTMP" database="${DB_DATABASE}" driver="$DB_SQLDRIVER"
 EXITCODE=$?
 



More information about the grass-commit mailing list