[GRASS-SVN] r35698 - in grass-addons/vector: . v.selmany
svn_grass at osgeo.org
svn_grass at osgeo.org
Sun Feb 1 09:49:15 EST 2009
Author: vincent
Date: 2009-02-01 09:49:15 -0500 (Sun, 01 Feb 2009)
New Revision: 35698
Added:
grass-addons/vector/v.selmany/
grass-addons/vector/v.selmany/Makefile
grass-addons/vector/v.selmany/v.selmany
Log:
New entry for vector add-ons, v.selmany interactively selects a set of vector objects, then assigns associated attribute values. This module is based on v.digatt, originally written by Andreas Philipp
Added: grass-addons/vector/v.selmany/Makefile
===================================================================
--- grass-addons/vector/v.selmany/Makefile (rev 0)
+++ grass-addons/vector/v.selmany/Makefile 2009-02-01 14:49:15 UTC (rev 35698)
@@ -0,0 +1,7 @@
+MODULE_TOPDIR = ../..
+
+PGM = v.selmany
+
+include $(MODULE_TOPDIR)/include/Make/Script.make
+
+default: script
Property changes on: grass-addons/vector/v.selmany/Makefile
___________________________________________________________________
Name: svn:executable
+
Added: grass-addons/vector/v.selmany/v.selmany
===================================================================
--- grass-addons/vector/v.selmany/v.selmany (rev 0)
+++ grass-addons/vector/v.selmany/v.selmany 2009-02-01 14:49:15 UTC (rev 35698)
@@ -0,0 +1,289 @@
+#!/bin/sh
+#
+############################################################################
+#
+# MODULE: v.selmany
+# AUTHOR(S): Vincent Bain, based on v.digatt, originally written by Andreas Philipp.
+# PURPOSE: Interactively select a set of vector objects, then assign attribute values.
+# COPYRIGHT: (C) 2009 by the GRASS Development Team
+#
+# This program is free software under the GNU General Public
+# License (>=v2). Read the file COPYING that comes with GRASS
+# for details.
+#
+# VERSION: 0.1 First draft release. Only tested with PostgreSQL driver, fails with DBF default driver, should work with MySQL.
+# This module may soon become obsolete, depending on enhancements foreseen in future versions of GRASS.
+#
+#
+#
+#############################################################################
+
+
+#%Module
+#% description: Interactively selects a set of vector objects, then assigns associated attribute values.
+#%End
+
+#%option
+#% key: vector
+#% type: string
+#% gisprompt: old,vector,vector
+#% description: vector map we need to edit
+#% required : yes
+#%end
+
+#%option
+#% key: type
+#% type: string
+#% description: feature type to edit
+#%options:point,line,boundary,centroid
+#% required : yes
+#%end
+
+#%option
+#% key: layer
+#% type: integer
+#% description: layer number. Default is 1
+#% answer: 1
+#% required : yes
+#%end
+
+#%option
+#% key: bgrast
+#% type: string
+#% gisprompt: old,raster,raster
+#% description: raster background map
+#% required : no
+#%end
+
+#%option
+#% key: bgvect
+#% type: string
+#% gisprompt: old,vector,vector
+#% description: vector background map
+#% required : no
+#%end
+
+#---------------------------------------------
+# GET ENVIRONMENT
+if [ -z "$GISBASE" ] ; then
+ echo "You must be in GRASS GIS to run this program." 1>&2
+ exit 1
+fi
+
+GIS_DBASE=`g.gisenv get=GISDBASE`
+GIS_LOCATION_NAME=`g.gisenv get=LOCATION_NAME`
+GIS_MAPSET=`g.gisenv get=MAPSET`
+GIS_MAP_PATH="${GIS_DBASE}/${GIS_LOCATION_NAME}/${GIS_MAPSET}"
+
+
+#---------------------------------------------
+# CHECK FOR ARGS
+if [ $# = 0 ] ; then
+g.message -w "This is a simple command line script, no GUI.available...\
+Try \"`basename $0` help\", exiting!"
+exit 1
+fi
+
+
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+exec g.parser "$0" "$@"
+fi
+
+
+#---------------------------------------------
+# CHECK FOR AWK
+if ! echo test | awk '{print $0}' > /dev/null 2>&1; then
+ g.message -w "`basename $0`: awk required, please install awk/gawk first" 1>&2
+ exit 1
+fi
+
+
+#---------------------------------------------
+# CHECK FOR MONITOR
+
+if [ `d.mon -p | cut -f1 -d' '` != "Currently" ] ; then
+ g.message -e "Sorry, no monitor currently selected for output"
+ exit 1
+fi
+
+
+#---------------------------------------------
+#CASE OF USER BREAK
+cleanup()
+{
+g.message "User break!"
+if [ $TMP ]; then g.remove vect=$TMP 2>/dev/null; fi
+exit 1
+}
+trap "cleanup" 2 3 15
+
+
+#---------------------------------------------
+#TEST FOR INPUT MAPS
+eval `g.findfile element=vector file="$GIS_OPT_VECTOR"`
+if [ ! "$file" ] ; then
+ g.message -e "Vector map '$GIS_OPT_VECTOR' not found"
+ exit 1
+fi
+
+if [ ${GIS_OPT_BGVECT} ] ; then
+ eval `g.findfile element=vector file="$GIS_OPT_BGVECT"`
+ if [ ! "$file" ] ; then
+ g.message -e "Background vector map '$GIS_OPT_BGVECT' not found"
+ exit 1
+ fi
+fi
+
+if [ ${GIS_OPT_BGRAST} ] ; then
+ eval `g.findfile element=raster file="$GIS_OPT_BGRAST"`
+ if [ ! "$file" ] ; then
+ g.message -e "Background raster map '$GIS_OPT_BGRAST' not found"
+ exit 1
+ fi
+fi
+
+
+#---------------------------------------------
+# CHECK FOR CATEGORIES
+# if v.category reports nothing, there are no categories in the geometry file
+echo
+echo checking for object register \(categories\) in geometry file ...
+catreport=`v.category input=$GIS_OPT_VECTOR option=report`
+if [ ! "${catreport}" ] ; then
+ g.message -e message="Input map $GIS_OPT_VECTOR seems to have no categories.
+ Check and try to run v.category :
+ v.category --o input=$GIS_OPT_VECTOR output=${GIS_OPT_VECTOR}c option=add step=1 type=${GIS_OPT_TYPE}
+ for registering objects and restart v.selmany with vector=${GIS_OPT_VECTOR}c"
+ exit 1
+fi
+
+#---------------------------------------------
+# CHECK FOR TABLE
+# if v.db.connect -c says nothing we have no connected table
+g.message ""
+g.message "Checking database table..."
+tablecheck=`v.db.connect -c map=${GIS_OPT_VECTOR} layer=${GIS_OPT_LAYER}`
+if [ ! "${tablecheck}" ] ; then
+ echo "Layer ${GIS_OPT_LAYER} of input map is not connected to any database table!"
+ echo "Should it be connected to a new table? [y]es or [n]o ? (default [n])"
+ read ANS1
+ case $ANS1 in
+ y* | Y*)
+ g.message "Running: v.db.addtable map=${GIS_OPT_VECTOR} layer=${GIS_OPT_LAYER}."
+ v.db.addtable map=${GIS_OPT_VECTOR} layer=${GIS_OPT_LAYER}
+ ;;
+ n* | N*|'')
+ g.message -w "Cannot proceed without connected table, exiting!"
+ exit 1
+ ;;
+ esac
+
+fi
+
+
+#---------------------------------------------
+# GET ARGUMENTS FOR SQL STATEMENTS
+DBINTABLE=`v.db.connect $GIS_OPT_VECTOR -g | grep -w $GIS_OPT_LAYER | awk '{print $2}'`
+DBDATABASE=`v.db.connect $GIS_OPT_VECTOR -g | grep -w $GIS_OPT_LAYER | awk '{print $4}'`
+DBDRIVER=`v.db.connect $GIS_OPT_VECTOR -g | grep -w $GIS_OPT_LAYER | awk '{print $5}'`
+
+g.region vect=${GIS_OPT_VECTOR}
+
+d.erase
+
+# SHOW BACKGROUND MAPS
+if [ ${GIS_OPT_BGRAST} ] ; then d.rast ${GIS_OPT_BGRAST} ; fi
+if [ ${GIS_OPT_BGVECT} ] ; then d.vect ${GIS_OPT_BGVECT} color=gray; fi
+
+# SHOW VECTOR MAP
+d.vect map=${GIS_OPT_VECTOR} color=gray
+d.vect map=${GIS_OPT_VECTOR} type=${GIS_OPT_TYPE} layer=${GIS_OPT_LAYER} size=2 color=black
+
+
+
+###########################################################################################
+# THE MAIN PROCESS LOOP
+
+cycle=1
+while [ $cycle -eq 1 ] ; do
+
+ echo '>>' \[q\]: quit
+ echo '>>' \[z\]: zoom
+ echo '>>' \[s\]: select objects
+ echo '>>' \[v\]: assign values
+
+ read ARG
+ case "$ARG" in
+
+ q) if [ $TMP ]; then g.remove vect=$TMP 2>/dev/null; fi
+ exit 0
+ ;;
+
+ z) echo '>>' use mouse to zoom:
+ d.erase
+ d.vect map=${GIS_OPT_VECTOR} color=gray
+ d.vect map=${GIS_OPT_VECTOR} type=${GIS_OPT_TYPE} layer=${GIS_OPT_LAYER} size=2
+ if [ ${GIS_OPT_BGRAST} ] ; then d.rast -o ${GIS_OPT_BGRAST} ; fi
+ if [ ${GIS_OPT_BGVECT} ] ; then d.vect ${GIS_OPT_BGVECT} color=gray; fi
+ d.zoom -h 2>/dev/null
+ ;;
+
+ s) echo '>>' draw box with left mouse button to add to select
+ echo '>>' draw box with middle mouse button to remove from select
+ echo '>>' apply multiple selection
+ catlist=""
+ g.remove vect=$TMP 2>/dev/null
+ TMP="selmany_$$"
+
+ d.extract input=$GIS_OPT_VECTOR output=$TMP type=$GIS_OPT_TYPE hcolor=blue 2>/dev/null
+ catlist=`v.category -g $TMP type=$GIS_OPT_TYPE layer=$GIS_OPT_LAYER option=print | tr '\n' ',' | sed "s/,$//g"`
+
+ if [ -z "$catlist" ] ; then
+ g.message -w "Selection set is empty! Try again (\"s\" key)."
+ else
+ d.erase
+ d.vect map=${GIS_OPT_VECTOR} color=gray
+ d.vect map=${GIS_OPT_VECTOR} type=${GIS_OPT_TYPE} layer=${GIS_OPT_LAYER} size=1
+ if [ ${GIS_OPT_BGRAST} ] ; then d.rast -o ${GIS_OPT_BGRAST} ; fi
+ if [ ${GIS_OPT_BGVECT} ] ; then d.vect ${GIS_OPT_BGVECT} color=gray; fi
+ d.vect $TMP type=$GIS_OPT_TYPE layer=$GIS_OPT_LAYER color=blue size=4
+ g.message "Cats $catlist are now selected."
+ fi
+ ;;
+
+ v) if [ -z "$catlist" ] ; then
+ g.message -w "Selection set is empty! You should first select a set of objects (\"s\" key)."
+ else
+ echo '>>' Assign values in the form '<'column_name'>' '<'value'>'
+ read INSTR
+ col=`echo $INSTR | awk '{print $1}'`
+ val=`echo $INSTR | awk '{print $2}'`
+ g.message "Checking table column \"$col\" in layer ${GIS_OPT_LAYER} ..."
+ if test "`v.db.connect -c map=${GIS_OPT_VECTOR} layer=${GIS_OPT_LAYER} | grep $col`" = "" ; then
+ v.db.connect -c map=${GIS_OPT_VECTOR} layer=${GIS_OPT_LAYER}
+ g.message -e "Vector attribute column \"$col\" not found in the table for layer ${GIS_OPT_LAYER}!"
+ else
+ wherestring="cat in ($catlist)"
+ echo "Running db.execute with: \"UPDATE $DBINTABLE SET $col=$val where $wherestring\" : [y]es or [n]o ? (default [y])"
+ read ANS2
+ case $ANS2 in
+ y* | Y*|'')
+ echo "UPDATE $DBINTABLE SET $col=$val where $wherestring" | db.execute database=$DBDATABASE driver=$DBDRIVER
+ catlist=""
+ ;;
+ n* | N*)
+ ;;
+ esac
+ g.remove vect=$TMP 2>/dev/null
+ fi
+ fi
+ ;;
+ esac
+done
+g.remove vect=$TMP 2>/dev/null
+
+# END OF MAIN PROCESS LOOP
+###########################################################################################
+
+
+exit 0
Property changes on: grass-addons/vector/v.selmany/v.selmany
___________________________________________________________________
Name: svn:executable
+
More information about the grass-commit
mailing list