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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Nov 15 05:55:17 EST 2010


Author: mmetz
Date: 2010-11-15 02:55:17 -0800 (Mon, 15 Nov 2010)
New Revision: 44318

Modified:
   grass-addons/vector/v.rast.stats2/v.rast.stats2
Log:
use r.univar if possible, avoid v.db.addcol

Modified: grass-addons/vector/v.rast.stats2/v.rast.stats2
===================================================================
--- grass-addons/vector/v.rast.stats2/v.rast.stats2	2010-11-15 10:16:37 UTC (rev 44317)
+++ grass-addons/vector/v.rast.stats2/v.rast.stats2	2010-11-15 10:55:17 UTC (rev 44318)
@@ -95,6 +95,21 @@
 LC_NUMERIC=C
 export LC_NUMERIC
 
+# this can surely be done in a  more elegant way...
+univar_test=` r.univar --script | grep "key: zones"`
+if [ -n "$univar_test" ] ; then
+    g.message -v "using r.univar"
+    univar_cmd="r.univar"
+else
+    # check if we have r.univar.zonal
+    if [ ! -f "$GISBASE"/bin/r.univar.zonal ] ; then
+      g.message -w "r.univar.zonal is required, please install r.univar.zonal from grass-addons" 
+      exit 1
+    fi
+    g.message -v "using r.univar.zonal"
+    univar_cmd="r.univar.zonal"
+fi
+
 #### setup temporary file
 TMP="`g.tempfile pid=$$`"
 if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
@@ -255,8 +270,8 @@
     grep "|$CURRCOLUMN|" --quiet
   if [ $? -eq 0 ] ; then
     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."
+       g.message -w "Cannot create column <$CURRCOLUMN> (already present)."
+       g.message -e "Use -c flag to update values in this column."
        cleanup
        exit 1
     fi
@@ -275,12 +290,28 @@
 
 if [ -n "$ADDCOLS" ] ; then
    g.message -v "Adding columns <$ADDCOLS>"
-   v.db.addcol map="$VECTORFULL" columns="$ADDCOLS" layer="$GIS_OPT_LAYER"
-   if [ $? -ne 0 ] ; then
-      g.message -e "Cannot continue (problem adding columns)."
-      cleanup
+#   v.db.addcol map="$VECTORFULL" columns="$ADDCOLS" layer="$GIS_OPT_LAYER"
+#   if [ $? -ne 0 ] ; then
+#      g.message -e "Cannot continue (problem adding columns)."
+#      cleanup
+#      exit 1
+#   fi
+
+  # code borrowed from v.db.addcol
+  colnum=`echo "$ADDCOLS" | awk -F, '{print NF}'`
+
+  n=1
+  while [ "$n" -le "$colnum" ]
+  do
+    col=`echo "$ADDCOLS" | cut -d',' -f$n`
+    
+    echo "ALTER TABLE $TABLE ADD COLUMN $col" | db.execute database="${DB_DATABASE}" driver="${DB_SQLDRIVER}"
+    if [ $? -eq 1 ] ; then
+      g.message -e "Cannot continue (problem adding column)."
       exit 1
-   fi
+    fi
+    n=`expr $n + 1`
+  done
 fi
 
 # calculate statistics for zones:
@@ -292,11 +323,11 @@
 # Processing statistics 
 unset $BASECOLS $EXTRACOLS
 if [ $GIS_FLAG_E -eq 1 ] ; then 
-    r.univar.zonal -t -e map="$RASTER" \
+    $univar_cmd -t -e map="$RASTER" \
     zones="${VECTOR}_${TMPNAME}" percentile="$GIS_OPT_PERCENTILE" | \
     cut -f1,3,5-8,10-13,15-18 -d'|' | sed 's+nan+NULL+g' > "$STATSTMP"
 else
-    r.univar.zonal -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
 



More information about the grass-commit mailing list