#!/usr/bin/python # -*- coding:utf-8 -*- ############################################################################ # # MODULE: v.sample.class.py # # AUTHOR(S): M. Cannata, M. Molinari (IST-SUPSI www.istgis.ist.supsi.ch:8001/GEOMATICA) # # PURPOSE: Field values calculator # # COPYRIGHT: (c) 2007 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. # ############################################################################ #%Module #% description: Field values calculator #% keywords: vector, field, calculator #%end #%option #% key: input #% type: string #% gisprompt: old,vector,vector #% key_desc : name #% description: input #% required : yes #%end #%option #% key: field #% type: string #% key_desc : name #% description: field to store results #% required : yes #%end #%option #% key: exp #% type: string #% key_desc : operation #% description: expression to execute #% required : yes #%end #%option #% key: where #% type: string #% key_desc : expression #% description: where condition to execute #% required : no #%end import os,sys,subprocess,string,random,math,tempfile,time,re def main(): table = os.getenv('GIS_OPT_input').split('@')[0] field = os.getenv('GIS_OPT_field') exp = os.getenv('GIS_OPT_exp') wherest = os.getenv('GIS_OPT_where') #Get vector fields name cmdargs0=["db.columns","table=%s" %table, "--q"] proc0 = subprocess.Popen(cmdargs0, stdout=subprocess.PIPE,stderr=subprocess.PIPE) colnames = proc0.communicate()[0].split("\n") #print "cols:\n" #for item in colnames: # print each item # print item + "\n" #Parse operation expression spe = re.split("([\[\]])", exp) #Check exp field names validity and get required field list expfields = [] for i in range(len(spe)): if(spe[i]=="["): if spe[i+1] not in colnames: print "FATAL ERROR: field " + spe[i+1] + " not found in table " + table + "\n" print "tried SQL: " + spe[i+1] + " not found in table " + table + "\n" return -1 else: expfields.append(spe[i+1]) #Get unique list of required fields uf = list(set(expfields)) #Prepare query statement lf = ", ".join(uf) sel = "SELECT cat" if len(uf)>0: sel = sel + "," + lf sel = sel + " from " + table if wherest: sel = sel + " WHERE " + wherest #Execute query cmdargs1=["db.select","sql=%s" %sel, "--q", "-c"] proc1 = subprocess.Popen(cmdargs1, stdout=subprocess.PIPE,stderr=subprocess.PIPE) lines = proc1.communicate()[0].split("\n") #print "query: " + sel #print "unique list:\n" #for item in uf: # print each item # print item + "\n" #print "selection:\n" #for item in lines: # print each item # print item + "\n" #Check query results if len(lines) == 1: print "WARNING: no record selected\n" print "executed query: " + sel + "\n" return -1 #Process and UPDATE else: print "please wait while updating values...." #process lines for l in range(0,len(lines)-1): #if l%10==0: # sys.stdout.write(".") cols = lines[l].split("|") evalexp = exp for i in range(len(uf)): evalexp = evalexp.replace("["+uf[i]+"]",cols[i+1]) try: #print evalexp + "\n" value = eval(str(evalexp)) #except ArithmeticError, e: except StandardError, e: print "\n%s\nIn command: %s" % (repr(e), str(evalexp)) return -1 #print value updst = "UPDATE " + table + " SET " + field + "=" + str(value) + " WHERE cat=" + cols[0] #print updst cmdargs2=["map=%s"%table,"col=%s"%field,"val=%s"%str(value),"where=cat=%s"%cols[0],"--q"] os.spawnvp(os.P_WAIT,"v.db.update", ["v.db.update"] + cmdargs2) print "....DONE" return if __name__ == "__main__": if ( len(sys.argv) <= 1 or sys.argv[1] != "@ARGS_PARSED@" ): os.execvp("g.parser", [sys.argv[0]] + sys.argv) else: main();