[GRASS-SVN] r43910 - grass-addons/vector/v.rast.stats2
svn_grass at osgeo.org
svn_grass at osgeo.org
Thu Oct 14 11:24:20 EDT 2010
Author: mmetz
Date: 2010-10-14 08:24:19 -0700 (Thu, 14 Oct 2010)
New Revision: 43910
Modified:
grass-addons/vector/v.rast.stats2/v.rast.stats2
Log:
do not ignore layer option, use vector in current mapset if vector with same name exists in different, accessible, mapset, use table name not vector name to update table (vector name and layer changes should be done for v.rast.stats too)
Modified: grass-addons/vector/v.rast.stats2/v.rast.stats2
===================================================================
--- grass-addons/vector/v.rast.stats2/v.rast.stats2 2010-10-14 11:05:58 UTC (rev 43909)
+++ grass-addons/vector/v.rast.stats2/v.rast.stats2 2010-10-14 15:24:19 UTC (rev 43910)
@@ -16,7 +16,7 @@
#############################################################################
#%Module
-#% description: Calculates univariate statistics from a GRASS raster map based on vector polygons and uploads statistics to new attribute columns. Needs r.univar2.zonal from GRASS addons repository.
+#% description: Calculates univariate statistics from a GRASS raster map based on vector polygons and uploads statistics to new attribute columns. Needs r.univar.zonal from GRASS addons repository.
#% keywords: vector, raster, statistics
#%End
#%flag
@@ -38,6 +38,7 @@
#%option
#% key: layer
#% type: integer
+#% gisprompt: old_layer,layer,layer
#% description: Layer to which the table to be changed is connected
#% answer: 1
#% required : no
@@ -111,7 +112,7 @@
cleanup()
{
- #restore settings:
+ # restore settings:
g.region region="$TMPNAME"
g.remove region="$TMPNAME" --quiet
g.remove rast="${VECTOR}_${TMPNAME}" --quiet
@@ -152,16 +153,17 @@
exit 1
else
VECTOR=`echo "$GIS_OPT_VECTOR" | cut -f1 -d'@'`
+ VECTORFULL="${VECTOR}@${VECT_MAPSET}"
fi
-#check the input raster map
+# check the input raster map
eval `g.findfile element=cell file="$RASTER"`
if [ ! "$file" ] ; then
g.message -e "Raster map <$RASTER> not found"
exit 1
fi
-#check presence of raster MASK, put it aside
+# check presence of raster MASK, put it aside
MASKFOUND=0
eval `g.findfile element=cell file=MASK`
if [ "$file" ] ; then
@@ -170,8 +172,8 @@
MASKFOUND=1
fi
-#get RASTER resolution of map which we want to query:
-#fetch separated to permit for non-square cells (latlong etc)
+# get RASTER resolution of map which we want to query:
+# fetch separated to permit for non-square cells (latlong etc)
NSRES=`r.info -s "$RASTER" | grep nsres | cut -d'=' -f2`
if [ $? -ne 0 ] ; then
g.message -e "An error occurred reading the input raster map resolution."
@@ -180,15 +182,15 @@
fi
EWRES=`r.info -s "$RASTER" | grep ewres | cut -d'=' -f2`
-#save current settings:
+# save current settings:
g.region save="$TMPNAME" --quiet
-#Temporarily setting raster resolution to $RASTER resolution
-#keep boundary settings
+# temporarily setting raster resolution to $RASTER resolution
+# keep boundary settings
g.region nsres=$NSRES ewres=$EWRES -a
# create raster from vector map for r.univar.zonal
-v.to.rast in="$VECTOR" out="${VECTOR}_${TMPNAME}" use=attr \
+v.to.rast in="$VECTORFULL" out="${VECTOR}_${TMPNAME}" use=attr \
col="$KEYCOL" type=area --quiet
if [ $? -ne 0 ] ; then
g.message -e "An error occurred while converting vector to raster"
@@ -196,9 +198,9 @@
exit 1
fi
-#dump cats to file to avoid "too many argument" problem:
+# dump cats to file to avoid "too many argument" problem:
r.category "${VECTOR}_${TMPNAME}" fs=';' --quiet | cut -d';' -f1 > "$TMP.cats"
-#echo "List of categories found: $CATSLIST"
+# echo "List of categories found: $CATSLIST"
NUMBER=`cat "$TMP.cats" | wc -l | awk '{print $1}'`
if [ $NUMBER -lt 1 ] ; then
g.message -e "No categories found in raster map"
@@ -206,9 +208,9 @@
exit 1
fi
-#check if DBF driver used, in this case cut to 10 chars col names:
+# check if DBF driver used, in this case cut to 10 chars col names:
DBFDRIVER=0
-v.db.connect -g "$VECTOR" fs=";" | grep -w "$GIS_OPT_LAYER" | cut -d';' -f5 | grep -i dbf --quiet
+v.db.connect -g "$VECTORFULL" fs=";" | grep -w "$GIS_OPT_LAYER" | cut -d';' -f5 | grep -i dbf --quiet
if [ $? -eq 0 ] ; then
DBFDRIVER=1
else
@@ -216,11 +218,11 @@
DBFDRIVER=0
fi
# we need this for non-DBF driver:
-DB_SQLDRIVER=`v.db.connect -g "$VECTOR" fs=";" | cut -d';' -f5`
-DB_DATABASE="`v.db.connect -g "$VECTOR" fs=";" | cut -d';' -f4`"
+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`"
-#Find out which table is linked to the vector map on the given layer
-TABLE=`v.db.connect map="$VECTOR" -g fs=";" | grep -w "$GIS_OPT_LAYER" | awk -F ";" '{print $2}'`
+# 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}'`
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
@@ -243,13 +245,13 @@
unset ADDCOLS
for i in $BASECOLS $EXTRACOLS ; do
- #check if column already present
+ # check if column already present
if [ $DBFDRIVER -eq 1 ] ; then
CURRCOLUMN="`echo "${COLPREFIX}_${i}" | cut -b1-10`"
else
CURRCOLUMN="${COLPREFIX}_${i}"
fi
- v.info -c $VECTOR --quiet | sed 's+^+|+g' | sed 's+$+|+g' | \
+ v.info -c $VECTORFULL layer="$GIS_OPT_LAYER" --quiet | sed 's+^+|+g' | sed 's+$+|+g' | \
grep "|$CURRCOLUMN|" --quiet
if [ $? -eq 0 ] ; then
if [ $GIS_FLAG_C -ne 1 ] ; then
@@ -267,16 +269,18 @@
else
COLTYPE="DOUBLE PRECISION"
fi
- ADDCOLS="$ADDCOLS $CURRCOLUMN $COLTYPE"
+ ADDCOLS="${ADDCOLS}$CURRCOLUMN $COLTYPE"
fi
done
-g.message -v "Adding columns <$ADDCOLS>"
-v.db.addcol map="$VECTOR" columns="$ADDCOLS"
-if [ $? -ne 0 ] ; then
- g.message -e "Cannot continue (problem adding columns)."
- cleanup
- exit 1
+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
+ exit 1
+ fi
fi
# calculate statistics for zones:
@@ -317,9 +321,9 @@
# create SQL file for extended and normal statistics
g.message -v "Creating SQL file ..."
if [ $GIS_FLAG_E -eq 1 ] ; then
- sed -e '1d' "$STATSTMP" | awk -F "|" '{printf "\nUPDATE '${VECTOR}' SET '${col[0]}' = %i , '${col[1]}' = %.2f , '${col[2]}' = %.2f , '${col[3]}' = %.2f , '${col[4]}' = %.2f , '${col[5]}' = %.2f , '${col[6]}' = %.2f , '${col[7]}' = %.2f , '${col[8]}' = %.2f , '${col[9]}' = %.2f , '${col[10]}' = %.2f , '${col[11]}' = %2f , '${col[12]}' = %.2f WHERE '${KEYCOL}' = %i;", $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$1}' > "$SQLTMP"
+ sed -e '1d' "$STATSTMP" | awk -F "|" '{printf "\nUPDATE '${TABLE}' SET '${col[0]}' = %i , '${col[1]}' = %.2f , '${col[2]}' = %.2f , '${col[3]}' = %.2f , '${col[4]}' = %.2f , '${col[5]}' = %.2f , '${col[6]}' = %.2f , '${col[7]}' = %.2f , '${col[8]}' = %.2f , '${col[9]}' = %.2f , '${col[10]}' = %.2f , '${col[11]}' = %2f , '${col[12]}' = %.2f WHERE '${KEYCOL}' = %i;", $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$1}' > "$SQLTMP"
else
- sed -e '1d' "$STATSTMP" | awk -F "|" '{printf "\nUPDATE '${VECTOR}' SET '${col[0]}' = %i , '${col[1]}' = %.2f , '${col[2]}' = %.2f , '${col[3]}' = %.2f , '${col[4]}' = %.2f , '${col[5]}' = %.2f , '${col[6]}' = %.2f , '${col[7]}' = %.2f , '${col[8]}' = %.2f WHERE '${KEYCOL}' = %i;", $2,$3,$4,$5,$6,$7,$8,$9,$10,$1}' > "$SQLTMP"
+ sed -e '1d' "$STATSTMP" | awk -F "|" '{printf "\nUPDATE '${TABLE}' SET '${col[0]}' = %i , '${col[1]}' = %.2f , '${col[2]}' = %.2f , '${col[3]}' = %.2f , '${col[4]}' = %.2f , '${col[5]}' = %.2f , '${col[6]}' = %.2f , '${col[7]}' = %.2f , '${col[8]}' = %.2f WHERE '${KEYCOL}' = %i;", $2,$3,$4,$5,$6,$7,$8,$9,$10,$1}' > "$SQLTMP"
fi
g.message -v "Updating the database ..."
More information about the grass-commit
mailing list