[GRASS-SVN] r48295 - grass-addons/vector/v.mkhexgrid
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Sep 14 13:10:43 EDT 2011
Author: tsw
Date: 2011-09-14 10:10:43 -0700 (Wed, 14 Sep 2011)
New Revision: 48295
Modified:
grass-addons/vector/v.mkhexgrid/v.mkhexgrid
Log:
modified to bypass db.in.ogr bug
Modified: grass-addons/vector/v.mkhexgrid/v.mkhexgrid
===================================================================
--- grass-addons/vector/v.mkhexgrid/v.mkhexgrid 2011-09-14 11:43:20 UTC (rev 48294)
+++ grass-addons/vector/v.mkhexgrid/v.mkhexgrid 2011-09-14 17:10:43 UTC (rev 48295)
@@ -1,8 +1,12 @@
-#!/usr/bin/env python
+#!/usr/bin/python
############################################################################
#
# MODULE: v.mkhexgrid for GRASS 6.4 (2010-11-23)
+# modified (2011-09-14)
+# - use of sql update over db.in.ogr because of
+# difficulty diagnosing error in db.in.ogr with
+# fields causing erroneous error messages
#
# AUTHOR(S): Trevor Wiens
#
@@ -10,7 +14,7 @@
#
# REQUIREMENTS: GRASS 6.4 and above
#
-# COPYRIGHT: (C) Trevor Wiens, 2010 GRASS Development Team
+# COPYRIGHT: (C) 2010 GRASS Development Team
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
@@ -45,12 +49,30 @@
#% required: yes
#%end
#%option
+#% key: size_method
+#% type: string
+#% options: side_length,area
+#% answer: area
+#% description: Method to calculate size
+#% multiple: no
+#% required: yes
+#%end
+#%option
#% key: sidelength
#% type: double
#% label: Side length
+#% answer: 100.0
#% description: Size of side length in map units
#% required: yes
#%end
+#%option
+#% key: hexarea
+#% type: double
+#% label: Hexagon Area
+#% answer: 200.0
+#% description: Area of hexagon in map units
+#% required: yes
+#%end
import sys
import os
@@ -68,7 +90,7 @@
#
#
-def WriteHexGridVectors(vf, af, side_len, xstart, ystart, xlimit, ylimit):
+def WriteHexGridVectors(vf, sf, table_name, schema, side_len, xstart, ystart, xlimit, ylimit):
# basic trig
angle_a = math.radians(30)
@@ -87,8 +109,13 @@
name = 'x%f_y%f' % (x,y)
outstring = CreateHexagon((x, y), side_len, cat)
vf.write(outstring)
- outstring = '%d,%d,%d,"%d_%d"\n' % (cat, row, col, row, col)
- af.write(outstring)
+ if schema == '':
+ outstring = "UPDATE %s SET row_num = %d, col_num = %d, row_col = '%d_%d' WHERE cat = %d;\n" % \
+ (table_name,row,col,row,col,cat)
+ else:
+ outstring = "UPDATE %s.%s SET row_num = %d, col_num = %d, row_col = '%d_%d' WHERE cat = %d;\n" % \
+ (schema,table_name,row,col,row,col,cat)
+ sf.write(outstring)
cat = cat + 1
x = x + (2*side_len) + (2*side_a)
col = col + 1
@@ -180,15 +207,39 @@
f.write(outtext)
return()
+#
+#
+# calc_side_length()
+#
+#
+def calc_side_length(hexarea):
+
+ tarea = hexarea/6.0
+ #
+ # area of an equilateral triangle = length^2 * sqrt(3)/4
+ # sqrt(3)/4 * area = length^2
+ # sqrt( sqrt(3)/4 * area) = length
+ #
+ retval = math.sqrt( tarea / (math.sqrt(3.0)/4.0) )
+ return(retval)
+
def main():
+ if options['size_method'] == 'area':
+ try:
+ side_len = calc_side_length(float(options['hexarea']))
+ except:
+ msgtext="An error occured in calculating side length for specified area. \n"
+ grass.message(msgtext, flag='e')
+ return(-1)
+ else:
+ side_len = options['sidelength']
errpipe = subprocess.PIPE
outpipe = subprocess.PIPE
grass.message('Creating GRASS ascii file with hexagons')
# assign name to temporary text file
tempname= 'tempgrid%d' % os.getpid()
vectorfile = tempname + '.asc'
- attrfile = tempname + '.csv'
- attrdesc = tempname + '.csvt'
+ sqlfile = tempname + '.sql'
# set region
try:
r = grass.run_command('g.region', region='%s' % options['regionname'])
@@ -203,18 +254,15 @@
ystart = current_region['s']
xmax = current_region['e']
ymax = current_region['n']
- side_len = options['sidelength']
#print "filename: %s\nx: %s, y: %s\nxmax: %s, ymax: %s\nside_length: %s" % (outfile, xstart, ystart, xmax, ymax, side_len)
vf = open(vectorfile, "w")
- af = open(attrfile, "w")
- af.write('"cat","row_num","col_num","row_col"\n')
- adf = open(attrdesc, "w")
- adf.write('"Integer","Integer","Integer","String"')
- adf.close()
+ sf = open(sqlfile, "w")
WriteGrassHeader(vf,vectorfile,xstart,ystart,xmax,ymax)
- WriteHexGridVectors(vf, af, float(side_len), float(xstart), float(ystart), float(xmax), float(ymax))
+ table_name = options['output'].split('@')[0]
+ schema = gdb.db_connection()['schema']
+ WriteHexGridVectors(vf, sf, table_name, schema, float(side_len), float(xstart), float(ystart), float(xmax), float(ymax))
vf.close()
- af.close()
+ sf.close()
# now import and clean up the vector
grass.message('Importing GRASS ascii file')
try:
@@ -228,7 +276,7 @@
grass.message('Cleaning and building topology')
try:
r = grass.run_command('v.clean', flags='-q', input=tempname, \
- output=options['output'], type='point,line,boundary,centroid,area', tool='rmdupl')
+ output=table_name, type='point,line,boundary,centroid,area', tool='rmdupl')
if r <> 0:
raise
except:
@@ -236,36 +284,22 @@
grass.message(msgtext, flag='e')
return(-1)
grass.message('Importing and linking attributes')
+ grass.message('Creating table')
# import attributes
- table_name = options['output'].split('@')[0]
- schema = gdb.db_connection()['schema']
+ grass.message(vectorfile)
try:
- # import under temporary name
- r = grass.run_command('db.in.ogr', dsn=attrfile, output=tempname)
- except:
- pass
- try:
- # drop connection
- r = grass.run_command('v.db.connect', flags='d', map=tempname)
+ # add attribute table
+ r = grass.run_command('v.db.addtable', map=table_name, table=schema+'.'+table_name, layer=1, columns='cat integer, row_num integer, col_num integer, row_col varchar(255)' )
if r <> 0:
raise
except:
- msgtext="An error occured separating attribute table from empty vector. \n"
+ msgtext="An error occured when importing creating attribute table. \n"
grass.message(msgtext, flag='e')
return(-1)
+ grass.message('Updating attributes')
try:
- # rename table
- sqltext = "ALTER TABLE %s.%s RENAME TO %s;" % (schema, tempname, table_name)
- r = grass.write_command('db.execute', stdin = sqltext, stdout = outpipe, stderr = errpipe)
- if r <> 0:
- raise
- except:
- msgtext="An error occured renaming attribute table. \n"
- grass.message(msgtext + '\n' + sqltext, flag='e')
- return(-1)
- try:
# link to proper table
- r = grass.run_command('v.db.connect', flags='o', map=options['output'], table=schema+'.'+table_name)
+ r = grass.run_command('db.execute', input=sqlfile)
# drop temp vector
if r <> 0:
raise
@@ -283,8 +317,7 @@
grass.message(msgtext, flag='e')
return(-1)
os.remove(vectorfile)
- os.remove(attrfile)
- os.remove(attrdesc)
+ os.remove(sqlfile)
if __name__ == "__main__":
options, flags = grass.parser()
More information about the grass-commit
mailing list