[GRASS-SVN] r33558 - in grass/trunk: lib/python
scripts/db.droptable scripts/db.out.ogr scripts/r.in.aster
scripts/r.mask scripts/v.db.reconnect.all scripts/v.dissolve
scripts/v.in.e00 scripts/v.in.geonames scripts/v.in.gns
scripts/v.in.wfs scripts/v.rast.stats
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Sep 26 10:54:34 EDT 2008
Author: glynn
Date: 2008-09-26 10:54:34 -0400 (Fri, 26 Sep 2008)
New Revision: 33558
Added:
grass/trunk/scripts/db.droptable/db.droptable.py
grass/trunk/scripts/db.out.ogr/db.out.ogr.py
grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py
Modified:
grass/trunk/lib/python/grass.py
grass/trunk/scripts/r.in.aster/r.in.aster.py
grass/trunk/scripts/r.mask/r.mask.py
grass/trunk/scripts/v.dissolve/v.dissolve.py
grass/trunk/scripts/v.in.e00/v.in.e00.py
grass/trunk/scripts/v.in.geonames/v.in.geonames.py
grass/trunk/scripts/v.in.gns/v.in.gns.py
grass/trunk/scripts/v.in.wfs/v.in.wfs.py
grass/trunk/scripts/v.rast.stats/v.rast.stats.py
Log:
Convert db.droptable, db.out.ogr, v.db.reconnect.all to Python
Add, use several utility functions:
overwrite, basename, find_program, try_remove, try_rmdir
Modified: grass/trunk/lib/python/grass.py
===================================================================
--- grass/trunk/lib/python/grass.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/lib/python/grass.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -1,5 +1,4 @@
import os
-import os.path
import sys
import types
import subprocess
@@ -249,3 +248,51 @@
return tuple(float(v) / 255 for v in vals)
return dflt
+
+# check GRASS_OVERWRITE
+
+def overwrite():
+ owstr = 'GRASS_OVERWRITE'
+ return owstr in os.environ and os.environ[owstr] != '0'
+
+## various utilities, not specific to GRASS
+
+# basename inc. extension stripping
+
+def basename(path, ext = None):
+ name = os.path.basename(path)
+ if not ext:
+ return name
+ fs = name.rsplit('.', 1)
+ if len(fs) > 1 and fs[1].lower() == ext:
+ name = fs[0]
+ return name
+
+# find a program (replacement for "which")
+
+def find_program(pgm):
+ nuldev = file(os.devnull, 'w+')
+ try:
+ subprocess.call([pgm], stdin = nuldev, stdout = nuldev, stderr = nuldev)
+ found = True
+ except:
+ found = False
+ nuldev.close()
+ return found
+
+# try to remove a file, without complaints
+
+def try_remove(path):
+ try:
+ os.remove(path)
+ except:
+ pass
+
+# try to remove a directory, without complaints
+
+def try_rmdir(path):
+ try:
+ os.rmdir(path)
+ except:
+ pass
+
Added: grass/trunk/scripts/db.droptable/db.droptable.py
===================================================================
--- grass/trunk/scripts/db.droptable/db.droptable.py (rev 0)
+++ grass/trunk/scripts/db.droptable/db.droptable.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE: db.droptable
+# AUTHOR(S): Markus Neteler
+# Converted to Python by Glynn Clements
+# PURPOSE: interface to db.execute to drop an attribute table
+# COPYRIGHT: (C) 2007 by Markus Neteler and 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: Drops an attribute table.
+#% keywords: database, attribute table
+#%End
+
+#%flag
+#% key: f
+#% description: Force removal (required for actual deletion of files)
+#%end
+
+#%option
+#% key: table
+#% type: string
+#% key_desc : name
+#% description: Table to drop
+#% required : yes
+#% gisprompt: old,dbtable,dbtable
+#%end
+
+import sys
+import os
+import grass
+import subprocess
+
+def main():
+ table = options['table']
+ force = flags['f']
+
+ # check if DB parameters are set, and if not set them.
+ grass.run_command('db.connect', flags = 'c')
+
+ s = grass.read_command('db.connect', flags = 'p')
+ kv = grass.parse_key_val(s, sep = ':')
+ database = kv['database']
+ driver = kv['driver']
+ # schema needed for PG?
+
+ if force:
+ grass.message("Forcing ...")
+
+ # check if table exists
+ nuldev = file(os.devnull, 'w')
+ if grass.run_command('db.describe', flags = 'c', table = table,
+ stdout = nuldev, stderr = nuldev):
+ grass.fatal("Table <%s> not found in current mapset" % table)
+
+ # check if table is used somewhere (connected to vector map)
+ used = []
+ vects = grass.list_strings('vect')
+ for vect in vects:
+ s = grass.read_command('v.db.connect', flags = 'g', map = vect, stderr = nuldev)
+ if not s:
+ continue
+ for l in s.splitlines():
+ f = l.split()
+ if f[1] == table:
+ used.append(vect)
+ break
+ if used:
+ grass.warning("Deleting table <%s> which is attached to following map(s):" % table)
+ for vect in used:
+ grass.message(vect)
+
+ if not force:
+ grass.message("The table <%s> would be deleted." % table)
+ grass.message("")
+ grass.message("You must use the force flag to actually remove it. Exiting.")
+ sys.exit(0)
+
+ p = grass.start_command('db.execute', database = database, driver = driver,
+ stdin = subprocess.PIPE)
+ p.stdin.write("DROP TABLE " + table)
+ p.stdin.close()
+ p.wait()
+ if p.returncode != 0:
+ grass.fatal("Cannot continue (problem deleting table).")
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ main()
+
Property changes on: grass/trunk/scripts/db.droptable/db.droptable.py
___________________________________________________________________
Name: svn:executable
+ *
Added: grass/trunk/scripts/db.out.ogr/db.out.ogr.py
===================================================================
--- grass/trunk/scripts/db.out.ogr/db.out.ogr.py (rev 0)
+++ grass/trunk/scripts/db.out.ogr/db.out.ogr.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -0,0 +1,103 @@
+#!/usr/bin/env python
+
+############################################################################
+#
+# MODULE: db.out.ogr
+# AUTHOR(S): Markus Neteler
+# Converted to Python by Glynn Clements
+# PURPOSE: exports attribute tables into various formats
+# COPYRIGHT: (C) 2007 by Markus Neteler and 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: Exports attribute tables into various formats.
+#% keywords: database, attribute table
+#%End
+
+#%option
+#% key: input
+#% type: string
+#% key_desc : name
+#% description: GRASS table name
+#% required : yes
+#%end
+
+#%option
+#% key: dsn
+#% type: string
+#% key_desc : name
+#% gisprompt: new_file,file,input
+#% description: Table file to be exported or DB connection string
+#% required : yes
+#%end
+
+#%option
+#% key: format
+#% type: string
+#% description: Table format
+#% required : yes
+#% options: CSV,DBF,GML,MySQL,PostgreSQL,SQLite
+#% answer: DBF
+#%end
+
+#%option
+#% key: db_table
+#% type: string
+#% key_desc : name
+#% description: Name for output table
+#% required : no
+#%end
+
+import sys
+import os
+import grass
+
+def main():
+ input = options['input']
+ format = options['format']
+ dsn = options['dsn']
+ table = options['db_table']
+
+ if format.lower() == 'dbf':
+ format = "ESRI_Shapefile"
+
+ olayer = {}
+ if format.lower() == 'csv':
+ olayer['olayer'] = grass.basename(dsn, 'csv')
+
+ #is there a simpler way of testing for --overwrite?
+ dbffile = input + '.dbf'
+ if os.path.exists(dbffile) and not grass.overwrite():
+ grass.fatal("File <%s> already exists" % dbffile)
+
+ if grass.run_command('v.out.ogr', quiet = True, input = input, dsn = dsn,
+ format = format, type = 'point', **olayer) != 0:
+ sys.exit(1)
+
+ if format == "ESRI_Shapefile":
+ exts = ['shp', 'shx', 'prj']
+ if dsn.endswith('.dbf'):
+ outname = grass.basename(dsn, 'dbf')
+ for ext in exts:
+ try_remove("%s.%s" % (outname, ext))
+ outname += '.dbf'
+ else:
+ for ext in exts:
+ try_remove(os.path.join(dsn, "%s.%s" % (input, ext)))
+ outname = os.path.join(dsn, input + ".dbf")
+ elif format.lower() == 'csv':
+ outname = dsn + '.csv'
+ else:
+ outname = input
+
+ grass.message("Exported table <%s>" % outname)
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ main()
+
Property changes on: grass/trunk/scripts/db.out.ogr/db.out.ogr.py
___________________________________________________________________
Name: svn:executable
+ *
Modified: grass/trunk/scripts/r.in.aster/r.in.aster.py
===================================================================
--- grass/trunk/scripts/r.in.aster/r.in.aster.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/r.in.aster/r.in.aster.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -169,7 +169,7 @@
#cleanup
message("Cleaning up ...")
- os.remove(tempfile)
+ grass.try_remove(tempfile)
message("Done.")
return
Modified: grass/trunk/scripts/r.mask/r.mask.py
===================================================================
--- grass/trunk/scripts/r.mask/r.mask.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/r.mask/r.mask.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -70,9 +70,7 @@
else:
grass.warning("No existing MASK to remove")
else:
- owstr = 'GRASS_OVERWRITE'
- overwrite = owstr in os.environ and os.environ[owstr] != '0'
- if exists and not overwrite:
+ if exists and not grass.overwrite():
grass.fatal("MASK already found in current mapset. Delete first or overwrite")
p = grass.start_command('r.reclass', input = input, output = 'MASK', overwrite = True,
Added: grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py
===================================================================
--- grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py (rev 0)
+++ grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+############################################################################
+#
+# MODULE: v.db.reconnect.all
+# AUTHOR(S): Radim Blazek
+# Converted to Python by Glynn Clements
+# PURPOSE: Reconnect vectors
+# COPYRIGHT: (C) 2004 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.
+#
+#############################################################################
+
+#%Module
+#% description: Reconnects vectors to a new database.
+#% keywords: vector, database, attribute table
+#%End
+#%option
+#% key: old_database
+#% type: string
+#% description: Name of old database. The database must be in form printed by v.db.connect -g, i.e. with substituted variables
+#% required : yes
+#%END
+#%option
+#% key: new_database
+#% type: string
+#% description: Name of new database
+#% required : yes
+#%END
+#%option
+#% key: old_schema
+#% type: string
+#% description: Old schema
+#% required : no
+#%END
+#%option
+#% key: new_schema
+#% type: string
+#% description: New schema
+#% required : no
+#%END
+
+import sys
+import os
+import grass
+import subprocess
+
+def main():
+ old_database = options['old_database']
+ new_database = options['new_database']
+ old_schema = options['old_schema']
+ new_schema = options['new_schema']
+
+ mapset = grass.gisenv()['MAPSET']
+
+ nuldev = file(os.devnull, 'w')
+
+ for vect in grass.list_grouped('vect')[mapset]:
+ vect = "%s@%s" % (vect, mapset)
+ grass.message("Reconnecting vector <%s>" % vect)
+ s = grass.read_command('v.db.connect', flags = 'g', map = vect, stderr = nuldev)
+ for link in s.splitlines():
+ f = link.split()
+ layer = f[0]
+ schema_table = f[1]
+ key = f[2]
+ database = f[3]
+ driver = f[4]
+ if '.' in schema_table:
+ st = schema_table.split('.', 1)
+ schema = st[0]
+ table = st[1]
+ else:
+ schema = ''
+ table = schema_table
+
+ if new_schema:
+ new_schema_table = "%s.%s" % (new_schema, table)
+ else:
+ new_schema_table = table
+
+ grass.message("SCHEMA = %s TABLE = %s NEW_SCHEMA_TABLE = %s" % (schema, table, new_schema_table))
+ if database == old_database and schema == old_schema:
+ grass.message("Reconnecting layer " + layer)
+ grass.message("v.db.connect -o map=%s layer=%s driver=%s database=%s table=%s key=%s" %
+ (vect, layer, driver, new_database, new_schema_table, key))
+ grass.run_command('v.db.connect', flags = 'o', map = vect,
+ layer = layer, driver = driver, database = new_database,
+ table = new_schema_table, key = key)
+ else:
+ grass.message("Layer <%s> will not be reconnected, database or schema do not match." % layer)
+
+if __name__ == "__main__":
+ options, flags = grass.parser()
+ main()
+
Property changes on: grass/trunk/scripts/v.db.reconnect.all/v.db.reconnect.all.py
___________________________________________________________________
Name: svn:executable
+ *
Modified: grass/trunk/scripts/v.dissolve/v.dissolve.py
===================================================================
--- grass/trunk/scripts/v.dissolve/v.dissolve.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/v.dissolve/v.dissolve.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -67,7 +67,7 @@
column = options['column']
# save command line
- cmdline = os.path.basename(sys.argv[0])
+ cmdline = grass.basename(sys.argv[0])
cmdline += ' input=' + input
cmdline += ' output=' + output
if layer:
Modified: grass/trunk/scripts/v.in.e00/v.in.e00.py
===================================================================
--- grass/trunk/scripts/v.in.e00/v.in.e00.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/v.in.e00/v.in.e00.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -58,24 +58,13 @@
import glob
import grass
-def find_program(pgm):
- nuldev = file(os.devnull, 'w+')
- try:
- subprocess.call([pgm], stdin = nuldev, stdout = nuldev, stderr = nuldev)
- found = True
- except:
- found = False
- nuldev.close()
- return found
-
def main():
-
filename = options['file']
type = options['type']
vect = options['vect']
# save command line
- cmdline = os.path.basename(sys.argv[0])
+ cmdline = grass.basename(sys.argv[0])
cmdline += ' file=' + filename
cmdline += ' type=' + type
if vect:
@@ -84,13 +73,13 @@
e00tmp = str(os.getpid())
#### check for avcimport
- if not find_program('avcimport'):
+ if not grass.find_program('avcimport'):
grass.fatal("'avcimport' program not found, install it first" +
"\n" +
"http://avce00.maptools.org")
#### check for e00conv
- if not find_program('e00conv'):
+ if not grass.find_program('e00conv'):
grass.fatal("'e00conv' program not found, install it first" +
"\n" +
"http://avce00.maptools.org")
@@ -99,10 +88,7 @@
if type not in ['point','line','area']:
grass.fatal('Must specify one of "point", "line", or "area".')
- e00name = os.path.basename(filename)
- fs = os.path.splitext(e00name)
- if fs[1].lower() == '.e00':
- e00name = fs[0]
+ e00name = grass.basename(filename, 'e00')
# avcimport only accepts 13 chars:
e00shortname = e00name[:13]
@@ -121,10 +107,7 @@
#make a temporary directory
tmpdir = grass.tempfile()
- try:
- os.remove(tmpdir)
- except:
- pass
+ grass.try_remove(tmpdir)
os.mkdir(tmpdir)
files = glob.glob(e00name + '.e[0-9][0-9]') + glob.glob(e00name + '.E[0-9][0-9]')
@@ -154,11 +137,8 @@
grass.message("E00 ASCII found and converted to Arc Coverage in current directory")
else:
grass.message("E00 Compressed ASCII found. Will uncompress first...")
- try:
- os.remove(e00shortname)
- os.remove(info)
- except:
- pass
+ grass.try_remove(e00shortname)
+ grass.try_remove(info)
subprocess.call(['e00conv', filename, e00tmp + '.e00'])
grass.message("...converted to Arc Coverage in current directory")
subprocess.call(['avcimport', e00tmp + '.e00', e00shortname], stderr = nuldev)
@@ -183,16 +163,10 @@
for root, dirs, files in os.walk('.', False):
for f in files:
path = os.path.join(root, f)
- try:
- os.remove(path)
- except:
- pass
+ grass.try_remove(path)
for d in dirs:
path = os.path.join(root, d)
- try:
- os.rmdir(path)
- except:
- pass
+ grass.try_rmdir(path)
os.chdir('..')
os.rmdir(tmpdir)
Modified: grass/trunk/scripts/v.in.geonames/v.in.geonames.py
===================================================================
--- grass/trunk/scripts/v.in.geonames/v.in.geonames.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/v.in.geonames/v.in.geonames.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -52,7 +52,7 @@
outfile = options['output']
# save command line
- cmdline = os.path.basename(sys.argv[0])
+ cmdline = grass.basename(sys.argv[0])
if infile:
cmdline += ' input=' + infile
if outfile:
@@ -148,7 +148,7 @@
input = tmpfile, output = outfile,
columns = columns)
- os.remove(tmpfile)
+ grass.try_remove(tmpfile)
# write cmd history:
grass.run_command('v.support', map = outfile, cmdhist = cmdline)
Modified: grass/trunk/scripts/v.in.gns/v.in.gns.py
===================================================================
--- grass/trunk/scripts/v.in.gns/v.in.gns.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/v.in.gns/v.in.gns.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -52,17 +52,14 @@
filevect = options['vect']
# save command line
- cmdline = os.path.basename(sys.argv[0])
+ cmdline = grass.basename(sys.argv[0])
if fileorig:
cmdline += ' file=' + fileorig
if filevect:
cmdline += ' vect=' + filevect
if not filevect:
- filevect = os.path.basename(fileorig)
- fs = os.path.splitext(filevect)
- if fs[1].lower() == '.txt':
- filevect = fs[0]
+ filevect = grass.basename(fileorig, 'txt')
#are we in LatLong location?
s = grass.read_command("g.proj", flags='j')
@@ -154,7 +151,7 @@
input = tmpfile, output = filevect,
columns = columns)
- os.remove(tmpfile)
+ grass.try_remove(tmpfile)
# write cmd history:
grass.run_command('v.support', map = filevect, cmdhist = cmdline)
Modified: grass/trunk/scripts/v.in.wfs/v.in.wfs.py
===================================================================
--- grass/trunk/scripts/v.in.wfs/v.in.wfs.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/v.in.wfs/v.in.wfs.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -56,7 +56,7 @@
outf.close()
grass.run_command('v.in.ogr', flags = 'o', dsn = tmpxml, out = out)
- os.remove(tmpxml)
+ grass.try_remove(tmpxml)
grass.message("Vector points map <%s> imported from WFS." % out)
Modified: grass/trunk/scripts/v.rast.stats/v.rast.stats.py
===================================================================
--- grass/trunk/scripts/v.rast.stats/v.rast.stats.py 2008-09-26 06:49:40 UTC (rev 33557)
+++ grass/trunk/scripts/v.rast.stats/v.rast.stats.py 2008-09-26 14:54:34 UTC (rev 33558)
@@ -87,10 +87,7 @@
grass.message("Restoring previous MASK...")
grass.run_command('g.rename', rast = (tmpname + "_origmask", 'MASK'), quiet = True)
for f in [tmp, tmpname, sqltmp]:
- try:
- os.remove(f)
- except:
- pass
+ grass.try_remove(f)
def main():
global tmp, sqltmp, tmpname, nuldev, vector, mask_found
@@ -98,7 +95,7 @@
tmp = grass.tempfile()
sqltmp = tmp + ".sql"
# we need a random name
- tmpname = os.path.basename(tmp)
+ tmpname = grass.basename(tmp)
nuldev = file(os.devnull, 'w')
@@ -222,10 +219,7 @@
grass.verbose("Processing data ...")
# get rid of any earlier attempts
- try:
- os.remove(sqltmp)
- except:
- pass
+ grass.try_remove(sqltmp)
# do extended stats?
if flags['e']:
More information about the grass-commit
mailing list