[GRASS-SVN] r37053 - grass/trunk/scripts/r.in.wms
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri May 8 12:44:24 EDT 2009
Author: martinl
Date: 2009-05-08 12:44:24 -0400 (Fri, 08 May 2009)
New Revision: 37053
Added:
grass/trunk/scripts/r.in.wms/gdalwarp.py
grass/trunk/scripts/r.in.wms/wms_parse.py
Modified:
grass/trunk/scripts/r.in.wms/Makefile
grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py
grass/trunk/scripts/r.in.wms/r.in.wms.html
grass/trunk/scripts/r.in.wms/r.in.wms.py
grass/trunk/scripts/r.in.wms/wms_download.py
grass/trunk/scripts/r.in.wms/wms_request.py
Log:
r.in.wms: various fixes, new modules (parse, gdalwarp)
Modified: grass/trunk/scripts/r.in.wms/Makefile
===================================================================
--- grass/trunk/scripts/r.in.wms/Makefile 2009-05-08 14:33:58 UTC (rev 37052)
+++ grass/trunk/scripts/r.in.wms/Makefile 2009-05-08 16:44:24 UTC (rev 37053)
@@ -4,7 +4,7 @@
include $(MODULE_TOPDIR)/include/Make/Script.make
-SRCFILES = r.in.gdalwarp.py wms_request.py wms_download.py
+SRCFILES = wms_parse.py wms_request.py wms_download.py gdalwarp.py r.in.gdalwarp.py
DSTFILES := $(patsubst %,$(ETC)/r.in.wms/%,$(SRCFILES))
default: script
Added: grass/trunk/scripts/r.in.wms/gdalwarp.py
===================================================================
--- grass/trunk/scripts/r.in.wms/gdalwarp.py (rev 0)
+++ grass/trunk/scripts/r.in.wms/gdalwarp.py 2009-05-08 16:44:24 UTC (rev 37053)
@@ -0,0 +1,349 @@
+############################################################################
+#
+# MODULE: r.in.wms / wms_gdalwarp.py
+#
+# AUTHOR(S): Cedric Shock, 2006
+# Update for GRASS 7 by Martin Landa <landa.martin gmail.com>, 2009
+#
+# PURPOSE: To import data from web mapping servers
+# (based on Bash script by Cedric Shock)
+#
+# COPYRIGHT: (C) 2009 Martin Landa, and 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.
+#
+#############################################################################
+
+import os
+import subprocess
+
+import grass
+
+class GDALWarp():
+ def __init__(self, flags, options):
+ self.flags = flags
+ self.options = options
+
+ self.tmp = grass.tempfile()
+
+ self.suffixes = []
+ self.patches = []
+ self.maplist = []
+ self.tiler = 0
+
+ if not flags['p']:
+ # todo: check if gdalwarp is available
+ pass
+
+ # flags for r.in.gdal
+ self.gdal_flags = ''
+ if flags['e']:
+ self.gdal_flags += 'e'
+ if flags['k']:
+ self.gdal_flags += 'k'
+
+ # options for gdalwarp
+ method_opt = options['method']
+ if method_opt == 'nearest':
+ self.gdal_method = '-rn'
+ elif method_opt == 'bilinear':
+ self.gdal_method = '-rb'
+ elif method_opt == 'cubic':
+ self.gdal_method = '-rc'
+ elif method_opt == 'cubicspline':
+ self.gdal_method = '-rcs'
+
+ def run(self):
+ # import tiles
+ tiler = 0
+ for input in self.options['input'].split(','):
+ tmptilename = self.options['output'] + '_tile_' + str(tiler)
+ if not os.path.exists(input):
+ grass.warning("Missing input '%s'" % input)
+ continue
+ grass.info('Importing tile <%s>...' % os.path.basename(input))
+ if self.flags['p']:
+ self.nowarp_import(input, tmptilename, self.gdal_flags)
+ else:
+ self.warp_import(input, tmptilename, self.gdal_method)
+
+ maplist.append(tmptilename)
+ tiler += 1
+
+ if tiler < 1:
+ grass.message("Nothing imported")
+ return 0
+
+ # if there's more than one tile patch them together, otherwise
+ # just rename that tile.
+ if tiler == 1:
+ if len(channel_suffixes) > 0:
+ # multi-band
+ for suffix in channel_suffixes:
+ # rename tile 0 to be the output
+ ffile = self.options['output'] + '_tile_0' + suffix
+ tfile = self.options['output'] + suffix
+ if grass.run_command('g.rename',
+ quiet = True,
+ rast = ffile + ',' + tfile) != 0:
+ grass.fatal('g.rename failed')
+ else: # single-band, single-tile
+ ffile = self.options['output'] + '_tile_0' # + sfx ?
+ tfile = self.options['output'] # + sfx ?
+ if grass.run_command('g.rename',
+ quiet = True,
+ rast = ffile + ',' + tfile) != 0:
+ grass.fatal('g.rename failed')
+ else:
+ # patch together each channel
+ grass.debug('suffixes: %s' % ','.join(suffixes))
+ if len(suffixes) > 0:
+ # multi-band data
+ for suffix in suffixes:
+ suffix = suffix.replace('.', '_')
+ # patch these together (using nulls only)
+ grass.message("Patching '%s' channel..." % suffix)
+ if grass.run_command('r.patch',
+ quiet = True,
+ input = patches, # ???
+ output = self.options['output'] + suffix) != 0:
+ grass.fatal('r.patch failed')
+
+ # remove the temporary patches we made
+ if grass.run_command('g.remove',
+ quiet = True,
+ rast = patches) != 0:
+ grass.fatal('g.remove failed')
+ else:
+ # single-band data
+ grass.info("Patching tiles (this may take some time)...")
+ grass.debug("patch list = %s" % ','.join(maplist))
+
+ # HACK: for 8bit PNG, GIF all the different tiles can have
+ # different color tables so patches output end up all freaky.
+ # r.mapcalc r#,g#,b# manual patching + r.composite?
+ # or d.out.file + r.in.png + r.region?
+ # *** there is a GDAL utility to do this: gdal_merge.py
+ # http://www.gdal.org/gdal_merge.html
+ for color in ('r', 'g', 'b'):
+ maplist_color = []
+ for map in maplist:
+ outmap = map + '_' + color
+ maplist_color.append(outmap)
+ if grass.run_command('r.mapcalc',
+ quiet = True,
+ expression = '%s = %s#%s' % (outmap, color, map)) != 0:
+ grass.fatal('r.mapcalc failed')
+ if grass.run_command('r.colors',
+ quiet = True,
+ map = outmap,
+ color = 'grey255') != 0:
+ grass.fatal('r.colors failed')
+ if grass.run_command('r.null',
+ quiet = True,
+ map = outmap,
+ setnull = 255) != 0:
+ grass.fatal('r.null failed')
+
+ if grass.run_command('r.patch',
+ input = ','.join(maplist_color),
+ output = outmap + '_all') != 0:
+ grass.fatal('r.patch failed')
+
+ if grass.run_command('r.composite',
+ quiet = True,
+ red = map + '_r_all',
+ green = map + '_g_all',
+ blue = map + '_b_all',
+ output = self.options['output']) != 0:
+ grass.fatal('r.composite failed')
+
+ if grass.run_command('g.mremove',
+ quiet = True,
+ flags = 'f',
+ rast = map + '*') != 0:
+ grass.fatal('g.remove failed')
+
+ # there's already a no suffix, can't make colors
+ # can only go up from here ;)
+ colors = 4 # ???
+ for suffix in suffixes:
+ if suffix in ('.red', '.green', '.blue'):
+ colors += 1
+
+ # make a composite image if asked for and colors exist
+ if colors == 3 or self.flags['c']:
+ grass.message("Building color image <%s>..." % self.options['output'])
+ if grass.run_command('r.composite',
+ quiet = True,
+ red = self.options['output'] + '.red',
+ green = self.options['output'] + '.green',
+ blue = self.options['output'] + '.blue',
+ output = option['output']) != 0:
+ grass.fatal('r.composite failed')
+
+ return 0
+
+ def warp_import(self, file, map, method):
+ """Wrap raster file using gdalwarp and import wrapped file
+ into GRASS"""
+ warpfile = self.tmp + 'warped.geotiff'
+ tmpmapname = map + '_tmp'
+
+ t_srs = grass.read_command('g.proj',
+ quiet = True,
+ flags = 'wf')
+ if not t_srs:
+ grass.fatal('g.proj failed')
+
+ grass.debug('gdalwarp -s_srs "%s" -t_srs "%s" "%s" "%s" %s %s' % \
+ (self.options['srs'], t_srs, file,
+ warpfile, self.options['warpoptions'], method))
+ grass.verbose("Warping input file '%s'..." % file)
+ ps = subprocess.Popen(['gdalwarp',
+ '-s_srs', self.options['srs'],
+ '-t_srs', t_srs,
+ file, warpfile,
+ self.options['warpoptions'],
+ method])
+ if ps.wait() != 0:
+ grass.fatal('gdalwarp failed')
+
+ # import it into a temporary map
+ grass.info('Importing temporary raster map...')
+ if grass.run_command('r.in.gdal',
+ quiet = True,
+ flags = gdal_flags,
+ input = warpfile,
+ output = tmpmapname) != 0:
+ grass.fatal('r.in.gdal failed')
+
+ os.remove(warpfile)
+
+ # get list of channels
+ pattern = tmpmapfile + '*'
+ grass.debug('Pattern: %s' % pattern)
+ mapset = grass.gisenv()['MAPSET']
+ channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)
+ grass.debug('Channel list: %s' % ','.join(channel_list))
+
+ if len(channel_list) < 2: # test for single band data
+ channel_suffixes = []
+ else:
+ channel_suffixes = channel_list # ???
+
+ grass.debug('Channel suffixes: %s', ','.join(channel_suffixes))
+
+ # add to the list of all suffixes
+ self.suffixes = self.suffixes + channel_suffixes
+ self.suffixes.sort()
+
+ # get last suffix
+ if len(channel_suffixes) > 0:
+ last_suffix = channel_suffixes[-1]
+ else:
+ last_suffix = ''
+
+ # find the alpha layer
+ if self.flags['k']:
+ alphalayer = tmpmapname + last_suffix
+ else:
+ alphalayer = tmpmapname + '.alpha'
+
+ # test to see if the alpha map exists
+ if not grass.find_file(element = 'cell', file = alphalayer)['name']:
+ alphalayer = ''
+
+ # calculate the new maps:
+ for suffix in channel_suffixes:
+ grass.debug("alpha=%s MAPsfx=%s% tmpname=%s%s" % \
+ (alphalayer, map, suffix, tmpmapname, suffix))
+ if alphalayer:
+ # Use alpha channel for nulls: problem: I've seen a map
+ # where alpha was 1-255; 1 being transparent. what to do?
+ # (Geosci Australia Gold layer, format=tiff)
+ if grass.run_command('r.mapcalc',
+ quiet = True,
+ expression = "%s%s = if(%s, %s%s, null())" % \
+ (map, sfx, alphalayer, tmpmapname, sfx)) != 0:
+ grass.fatal('r.mapcalc failed')
+ else:
+ if grass.run_command('g.copy',
+ quiet = True,
+ rast = "%s%s,%s%s" % \
+ (tmpmapname, suffix, map, suffix)) != 0:
+ grass.fatal('g.copy failed')
+
+ # copy the color tables
+ if grass.run_command('r.colors',
+ quiet = True,
+ map = map + suffix,
+ rast = tmpmapname + suffix) != 0:
+ grass.fatal('g.copy failed')
+
+ # make patch lists
+ suffix = suffix.replace('.', '_')
+ # this is a hack to make the patch lists empty:
+ if self.tiler == 0:
+ self.patches = []
+ self.patches = self.patches.append(map + suffix)
+
+ # if no suffix, processing is simple (e.g. elevation has only 1
+ # band)
+ if len(channel_list) < 1:
+ # run r.mapcalc to crop to region
+ if grass.run_command('r.mapcalc',
+ quiet = True,
+ expression = "%s = %s" % \
+ (map, tmpmapname)) != 0:
+ grass.fatal('r.mapcalc failed')
+
+ if grass.run_command('r.colors',
+ quiet = True,
+ map = map,
+ rast = tmpmapname) != 0:
+ grass.fatal('r.colors failed')
+
+ # remove the old channels
+ if grass.run_command('g.remove',
+ quiet = True,
+ rast = ','.channel_list) != 0:
+ grass.fatal('g.remove failed')
+
+ def nowarp_import(self, file, map, gdal_flags):
+ """Import raster file into GRASS"""
+ if grass.run_command('r.in.gdal',
+ quiet = True,
+ flags = 'o' + gdal_flags,
+ input = file,
+ output = map) != 0:
+ grass.fatal('r.in.gdal failed')
+
+ # get a list of channels:
+ pattern = map + '*'
+ grass.debug("pattern: %s" % ','.join(pattern))
+ mapset = grass.gisenv()['MAPSET']
+ channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)
+ grass.debug("channel list: %s" % ','.join(channel_list))
+
+ if len(channel_list) < 2:
+ # test for single band data
+ channel_suffixes = []
+ else:
+ channel_suffixes = channel_list # ???
+
+ # add to the list of all suffixes:
+ self.suffixes = self.suffixes + channel.suffixes
+ self.suffixes.sort()
+
+ for suffix in channel_suffixes:
+ # make patch lists
+ suffix = suffix.replace('.', '_')
+ # this is a hack to make the patch lists empty
+ if self.tiler == 0:
+ self.patches = []
+ self.patches = self.patches.append(map + suffix)
+
+
Property changes on: grass/trunk/scripts/r.in.wms/gdalwarp.py
___________________________________________________________________
Name: svn:mime-type
+ text/x-python
Name: svn:keywords
+ Author Date Id
Name: svn:eol-style
+ native
Modified: grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py
===================================================================
--- grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py 2009-05-08 14:33:58 UTC (rev 37052)
+++ grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py 2009-05-08 16:44:24 UTC (rev 37053)
@@ -79,337 +79,20 @@
import grass
+path = os.path.join(os.getenv('GISBASE'), 'etc', 'r.in.wms')
+sys.path.append(path)
+import gdalwarp
+
def cleanup():
if tmp:
grass.run_command('g.remove', rast = tmp, quiet = True)
-def warp_import(file, map, method,
- suffixes, tiler, patches):
- """Wrap raster file using gdalwarp before importing into GRASS"""
- global tmp
- warpfile = tmp + 'warped.geotiff'
- tmpmapname = map + '_tmp'
-
- t_srs = grass.read_command('g.proj',
- quiet = True,
- flags = 'wf')
- if not t_srs:
- grass.fatal('g.proj failed')
-
- grass.debug('gdalwarp -s_srs "%s" -t_srs "%s" "%s" "%s" %s %s' % \
- (options['s_srs'], t_srs, file,
- warpfile, options['warpoptions'], method))
-
- grass.info("Warping input file '%s'..." % file)
- ps = subprocess.Popen('gdalwarp',
- '-s_srs', options['s_srs'],
- '-t_srs', t_srs,
- file, warpfile, options['warpoptions'], method)
- if ps.wait() != 0:
- grass.fatal('gdalwarp failed')
-
- # import it into a temporary map
- grass.info('Importing temporary raster map...')
- if grass.run_command('r.in.gdal',
- quiet = True,
- flags = gdal_flags,
- input = warpfile,
- output = tmpmapname) != 0:
- grass.fatal('r.in.gdal failed')
-
- os.remove(warpfile)
-
- # get list of channels
- pattern = tmpmapfile + '*'
- grass.debug('Pattern: %s' % pattern)
- mapset = grass.gisenv()['MAPSET']
- channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)
- grass.debug('Channel list: %s' % ','.join(channel_list))
-
- if len(channel_list) < 2: # test for single band data
- channel_suffixes = []
- else:
- channel_suffixes = channel_list # ???
-
- grass.debug('Channel suffixes: %s', ','.join(channel_suffixes))
-
- # add to the list of all suffixes
- suffixes = suffixes + channel_suffixes
- suffixes.sort()
-
- # get last suffix
- if len(channel_suffixes) > 0:
- last_suffix = channel_suffixes[-1]
- else:
- last_suffix = ''
-
- # find the alpha layer
- if flags['k']:
- alphalayer = tmpmapname + last_suffix
- else:
- alphalayer = tmpmapname + '.alpha'
-
- # test to see if the alpha map exists
- if not grass.find_file(element = 'cell', file = alphalayer)['name']:
- alphalayer = ''
-
- # calculate the new maps:
- for suffix in channel_suffixes:
- grass.debug("alpha=%s MAPsfx=%s% tmpname=%s%s" % \
- (alphalayer, map, suffix, tmpmapname, suffix))
- if alphalayer:
- # Use alpha channel for nulls: problem: I've seen a map
- # where alpha was 1-255; 1 being transparent. what to do?
- # (Geosci Australia Gold layer, format=tiff)
- if grass.run_command('r.mapcalc',
- quiet = True,
- expression = "%s%s = if(%s, %s%s, null())" % \
- (map, sfx, alphalayer, tmpmapname, sfx)) != 0:
- grass.fatal('r.mapcalc failed')
- else:
- if grass.run_command('g.copy',
- quiet = True,
- rast = "%s%s,%s%s" % \
- (tmpmapname, suffix, map, suffix)) != 0:
- grass.fatal('g.copy failed')
-
- # copy the color tables
- if grass.run_command('r.colors',
- quiet = True,
- map = map + suffix,
- rast = tmpmapname + suffix) != 0:
- grass.fatal('g.copy failed')
-
- # make patch lists
- suffix = suffix.replace('.', '_')
- # this is a hack to make the patch lists empty:
- if tiler == 0:
- patches = []
- patches = patches.append(map + suffix)
-
- # if no suffix, processing is simple (e.g. elevation has only 1
- # band)
- if len(channel_list) < 1:
- # run r.mapcalc to crop to region
- if grass.run_command('r.mapcalc',
- quiet = True,
- expression = "%s = %s" % \
- (map, tmpmapname)) != 0:
- grass.fatal('r.mapcalc failed')
-
- if grass.run_command('r.colors',
- quiet = True,
- map = map,
- rast = tmpmapname) != 0:
- grass.fatal('r.colors failed')
-
- # remove the old channels
- if grass.run_command('g.remove',
- quiet = True,
- rast = ','.channel_list) != 0:
- grass.fatal('g.remove failed')
-
-def nowarp_import(file, map, gdal_flags,
- suffixes, tiler, patches):
- if grass.run_command('r.in.gdal',
- quiet = True,
- flags = 'o' + gdal_flags,
- input = file,
- output = map) != 0:
- grass.fatal('r.in.gdal failed')
-
- # get a list of channels:
- pattern = map + '*'
- grass.debug("pattern: %s" % ','.join(pattern))
- mapset = grass.gisenv()['MAPSET']
- channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)
- grass.debug("channel list: %s" % ','.join(channel_list))
-
- if len(channel_list) < 2:
- # test for single band data
- channel_suffixes = []
- else:
- channel_suffixes = channel_list # ???
-
- # add to the list of all suffixes:
- suffixes = suffixes + channel_suffixes
- suffixes.sort()
-
- for suffix in channel_suffixes:
- # make patch lists
- suffix = suffix.replace('.', '_')
- # this is a hack to make the patch lists empty
- if tiler == 0:
- patches = []
- patches = patches.append(map + suffix)
-
def main():
- if not flags['p']:
- # todo: check if gdalwarp is available
- pass
-
- # flags for r.in.gdal
- gdal_flags = ''
- if flags['e']:
- gdal_flags += 'e'
- if flags['k']:
- gdal_flags += 'k'
-
- # options for gdalwarp
- method_opt = options['method']
- if method_opt == 'nearest':
- gdal_method = '-rn'
- elif method_opt == 'bilinear':
- gdal_method = '-rb'
- elif method_opt == 'cubic':
- gdal_method = '-rc'
- elif method_opt == 'cubicspline':
- gdal_method = '-rcs'
-
- global tmp
- tmp = grass.tempfile()
-
- # list of all suffixes
- suffixes = []
- # we need a way to make sure patches are intialized correctly
- tiler = 0
- # list of maps
- maplist = []
- # list of maps to be patched
- patches = []
-
# show progress infromation and grass.info() by default
os.environ['GRASS_VERBOSE'] = '1'
-
- # import tiles
- for input in options['input'].split(','):
- tmptilename = options['output'] + '_tile_' + str(tiler)
- if not os.path.exists(input):
- grass.warning("Missing input '%s'" % input)
- continue
- grass.info('Importing tile <%s>...' % input)
- if flags['p']:
- channel_suffixes = nowarp_import(input, tmptilename, gdal_flags,
- suffixes, tiler, patches)
- else:
- warp_import(input, tmptilename, gdal_method,
- suffixes, tiler, patches)
-
- maplist.append(tmptilename)
- tiler += 1
- if tiler < 1:
- grass.message("Nothing imported")
- return 0
+ return gdalwarp.GDALWarp(flags, options).run()
- # if there's more than one tile patch them together, otherwise
- # just rename that tile.
- if tiler == 1:
- if len(channel_suffixes) > 0:
- # multi-band
- for suffix in channel_suffixes:
- # rename tile 0 to be the output
- ffile = options['output'] + '_tile_0' + suffix
- tfile = options['output'] + suffix
- if grass.run_command('g.rename',
- quiet = True,
- rast = ffile + ',' + tfile) != 0:
- grass.fatal('g.rename failed')
- else: # single-band, single-tile
- ffile = options['output'] + '_tile_0' # + sfx ?
- tfile = options['output'] # + sfx ?
- if grass.run_command('g.rename',
- quiet = True,
- rast = ffile + ',' + tfile) != 0:
- grass.fatal('g.rename failed')
- else:
- # patch together each channel
- grass.debug('suffixes: %s' % ','.join(suffixes))
- if len(suffixes) > 0:
- # multi-band data
- for suffix in suffixes:
- suffix = suffix.replace('.', '_')
- # patch these together (using nulls only)
- grass.message("Patching '%s' channel..." % suffix)
- if grass.run_command('r.patch',
- quiet = True,
- input = patches, # ???
- output = options['output'] + suffix) != 0:
- grass.fatal('r.patch failed')
-
- # remove the temporary patches we made
- if grass.run_command('g.remove',
- quiet = True,
- rast = patches) != 0:
- grass.fatal('g.remove failed')
- else:
- # single-band data
- grass.info("Patching tiles (this may take some time)...")
- grass.debug("patch list = %s" % ','.join(maplist))
-
- # HACK: for 8bit PNG, GIF all the different tiles can have
- # different color tables so patches output end up all freaky.
- # r.mapcalc r#,g#,b# manual patching + r.composite?
- # or d.out.file + r.in.png + r.region?
- # *** there is a GDAL utility to do this: gdal_merge.py
- # http://www.gdal.org/gdal_merge.html
- for color in ('r', 'g', 'b'):
- maplist_color = []
- for map in maplist:
- outmap = map + '_' + color
- maplist_color.append(outmap)
- if grass.run_command('r.mapcalc',
- quiet = True,
- expression = '%s = %s#%s' % (outmap, color, map)) != 0:
- grass.fatal('r.mapcalc failed')
- if grass.run_command('r.colors',
- quiet = True,
- map = outmap,
- color = 'grey255') != 0:
- grass.fatal('r.colors failed')
- if grass.run_command('r.null',
- quiet = True,
- map = outmap,
- setnull = 255) != 0:
- grass.fatal('r.null failed')
-
- if grass.run_command('r.patch',
- input = ','.join(maplist_color),
- output = outmap + '_all') != 0:
- grass.fatal('r.patch failed')
-
- if grass.run_command('r.composite',
- quiet = True,
- red = map + '_r_all',
- green = map + '_g_all',
- blue = map + '_b_all',
- output = options['output']) != 0:
- grass.fatal('r.composite failed')
-
- if grass.run_command('g.mremove',
- quiet = True,
- flags = 'f',
- rast = map + '*') != 0:
- grass.fatal('g.remove failed')
-
- # there's already a no suffix, can't make colors
- # can only go up from here ;)
- colors = 4
- for suffix in suffixes:
- if suffix in ('.red', '.green', '.blue'):
- colors += 1
-
- # make a composite image if asked for and colors exist
- if colors == 3 or flags['c']:
- grass.message("Building color image <%s>..." % options['output'])
- if grass.run_command('r.composite',
- quiet = True,
- red = options['output'] + '.red',
- green = options['output'] + '.green',
- blue = options['output'] + '.blue',
- output = option['output']) != 0:
- grass.fatal('r.composite failed')
-
if __name__ == "__main__":
options, flags = grass.parser()
tmp = None
Modified: grass/trunk/scripts/r.in.wms/r.in.wms.html
===================================================================
--- grass/trunk/scripts/r.in.wms/r.in.wms.html 2009-05-08 14:33:58 UTC (rev 37052)
+++ grass/trunk/scripts/r.in.wms/r.in.wms.html 2009-05-08 16:44:24 UTC (rev 37053)
@@ -20,9 +20,11 @@
capabilities request to NASA's OnEarth server</a>.
<p>
+<!--
Some brain-dead servers will only talk to certain web browsers. You can
fool these by adding <tt>--user-agent=MSIE5.5</tt> to <b>wgetoptions</b>
or for curl adding <tt>-A "MSIE5.5"</tt> to <b>curloptions</b>.
+-->
Other brain-dead servers will not accept queries in the form of POST data.
If you get an error try using the <b>-g</b> flag to force <em>r.in.wms</em>
to send an atomic GET request instead.
Modified: grass/trunk/scripts/r.in.wms/r.in.wms.py
===================================================================
--- grass/trunk/scripts/r.in.wms/r.in.wms.py 2009-05-08 14:33:58 UTC (rev 37052)
+++ grass/trunk/scripts/r.in.wms/r.in.wms.py 2009-05-08 16:44:24 UTC (rev 37053)
@@ -170,79 +170,21 @@
import sys
import tempfile
import urllib
+
import xml.sax
-import xml.sax.handler
-HandlerBase=xml.sax.handler.ContentHandler
-from xml.sax import make_parser
import grass
wmsPath = os.path.join(os.getenv('GISBASE'), 'etc', 'r.in.wms')
sys.path.append(wmsPath)
-import wms_request
-import wms_download
+try:
+ import wms_parse
+ import wms_request
+ import wms_download
+ import gdalwarp
+except ImportError:
+ pass
-class ProcessCapFile(HandlerBase):
- """
- A SAX handler for the capabilities file
- """
- def __init__(self):
- self.inTag = {}
- for tag in ('layer', 'name', 'style',
- 'title', 'srs'):
- self.inTag[tag] = False
- self.value = ''
-
- self.layers = []
-
- def startElement(self, name, attrs):
- if self.inTag.has_key(name.lower()):
- self.inTag[name.lower()] = True
-
- if name.lower() == 'layer':
- self.layers.append({})
-
- def endElement(self, name):
- if self.inTag.has_key(name.lower()):
- self.inTag[name.lower()] = False
-
- for tag in ('name', 'title', 'srs'):
- if name.lower() != tag:
- continue
- if self.inTag['style']:
- if not self.layers[-1].has_key('style'):
- self.layers[-1]['style'] = {}
- if not self.layers[-1]['style'].has_key(tag):
- self.layers[-1]['style'][tag] = []
- self.layers[-1]['style'][tag].append(self.value)
- elif self.inTag['layer']:
- self.layers[-1][tag] = self.value
-
- if name.lower() in ('name', 'title', 'srs'):
- self.value = ''
-
- def characters(self, ch):
- if self.inTag['name'] or \
- self.inTag['title'] or \
- self.inTag['srs']:
- self.value += ch
-
- def getLayers(self):
- """Print list of layers"""
- for ly in self.layers:
- if ly.has_key('name'):
- print "LAYER: " + ly['name']
- else:
- print "LAYER: ???"
- if ly.has_key('title'):
- print " Title: " + ly['title']
- if ly.has_key('srs'):
- print " SRS: " + ly['srs']
- if ly.has_key('style'):
- for idx in range(len(ly['style']['name'])):
- print " STYLE: " + ly['style']['name'][idx]
- print " Style title: " + ly['style']['title'][idx]
-
def list_layers():
"""Get list of available layers from WMS server"""
qstring = "service=WMS&request=GetCapabilities&" + options['wmsquery']
@@ -253,9 +195,9 @@
cap_file = urllib.urlopen(options['mapserver'] + '?' + qstring)
if not cap_file:
grass.fatal("Unable to get capabilities of '%s'" % options['mapserver'])
-
+
# parse file with sax
- cap_xml = ProcessCapFile()
+ cap_xml = wms_parse.ProcessCapFile()
try:
xml.sax.parse(cap_file, cap_xml)
except xml.sax.SAXParseException, err:
@@ -271,7 +213,7 @@
return 0
elif not options['output']:
grass.fatal("No output map specified")
-
+
# set directory for download
if not options['folder']:
options['folder'] = os.path.join(grass.gisenv()['GISDBASE'], 'wms_download')
@@ -297,10 +239,14 @@
for item in request.GetRequests():
files.append(item['output'])
files = ','.join(files)
+
+ # add flags for r.in.gdalwarp
+ options['input'] = files
+ flags['e'] = False
+ flags['c'] = True
+ options['warpoptions'] = ''
- gdalwarp.GDALWarp(flags, options)
-
- return 0
+ return gdalwarp.GDALWarp(flags, options).run()
if __name__ == "__main__":
options, flags = grass.parser()
Modified: grass/trunk/scripts/r.in.wms/wms_download.py
===================================================================
--- grass/trunk/scripts/r.in.wms/wms_download.py 2009-05-08 14:33:58 UTC (rev 37052)
+++ grass/trunk/scripts/r.in.wms/wms_download.py 2009-05-08 16:44:24 UTC (rev 37053)
@@ -1,11 +1,9 @@
-#!/usr/bin/env python
-
############################################################################
#
-# MODULE: r.in.wms
+# MODULE: r.in.wms / wms_download.py
#
# AUTHOR(S): Cedric Shock, 2006
-# Pythonized by Martin Landa <landa.martin gmail.com>, 2009
+# Updated for GRASS 7 by Martin Landa <landa.martin gmail.com>, 2009
#
# PURPOSE: To import data from web mapping servers
# (based on Bash script by Cedric Shock)
@@ -35,9 +33,9 @@
for item in requests:
if os.path.exists(item['output']) and \
os.path.getsize(item['output']) > 0:
- grass.message("Tile already downloaded")
+ grass.verbose("Tile already downloaded")
else:
- self.GetData(i, item['server'] + item['string'], item['output'])
+ self.GetData(i, item['server'] + '?' + item['string'], item['output'])
i += 1
def GetData(self, idx, url, output):
@@ -45,8 +43,9 @@
grass.message("Downloading data (tile %d)..." % idx)
grass.verbose("Requesting data: %s" % self.options['mapserver'])
grass.verbose(url)
-
+
if not self.flags['g']: # -> post
+ print url
try:
urllib.urlretrieve(url, output, data="POST")
except IOError:
@@ -54,13 +53,13 @@
if not os.path.exists(output):
grass.fatal("Failed while downloading the data")
-
+
# work-around for brain-dead ArcIMS servers which want POST-data as part of the GET URL
# (this is technically allowed by OGC WMS def v1.3.0 Sec6.3.4)
if os.path.getsize(output) == 0:
grass.warning("Downloaded image file is empty -- trying another method")
self.flags['g'] = True
-
+
if self.flags['g']: # -> get
try:
urllib.urlretrieve(url, output, data="GET")
@@ -69,4 +68,4 @@
if not os.path.exists(output) or os.path.getsize(output) == 0:
grass.fatal("Failed while downloading the data")
-
+
Added: grass/trunk/scripts/r.in.wms/wms_parse.py
===================================================================
--- grass/trunk/scripts/r.in.wms/wms_parse.py (rev 0)
+++ grass/trunk/scripts/r.in.wms/wms_parse.py 2009-05-08 16:44:24 UTC (rev 37053)
@@ -0,0 +1,83 @@
+############################################################################
+#
+# MODULE: r.in.wms / wms_parse
+#
+# AUTHOR(S): Cedric Shock, 2006
+# Upgraded for GRASS 7 by Martin Landa <landa.martin gmail.com>, 2009
+#
+# PURPOSE: To import data from web mapping servers
+# (based on Bash script by Cedric Shock)
+#
+# COPYRIGHT: (C) 2009 Martin Landa, and 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.
+#
+#############################################################################
+
+import xml.sax
+import xml.sax.handler
+HandlerBase=xml.sax.handler.ContentHandler
+from xml.sax import make_parser
+
+class ProcessCapFile(HandlerBase):
+ """
+ A SAX handler for the capabilities file
+ """
+ def __init__(self):
+ self.inTag = {}
+ for tag in ('layer', 'name', 'style',
+ 'title', 'srs'):
+ self.inTag[tag] = False
+ self.value = ''
+
+ self.layers = []
+
+ def startElement(self, name, attrs):
+ if self.inTag.has_key(name.lower()):
+ self.inTag[name.lower()] = True
+
+ if name.lower() == 'layer':
+ self.layers.append({})
+
+ def endElement(self, name):
+ if self.inTag.has_key(name.lower()):
+ self.inTag[name.lower()] = False
+
+ for tag in ('name', 'title', 'srs'):
+ if name.lower() != tag:
+ continue
+ if self.inTag['style']:
+ if not self.layers[-1].has_key('style'):
+ self.layers[-1]['style'] = {}
+ if not self.layers[-1]['style'].has_key(tag):
+ self.layers[-1]['style'][tag] = []
+ self.layers[-1]['style'][tag].append(self.value)
+ elif self.inTag['layer']:
+ self.layers[-1][tag] = self.value
+
+ if name.lower() in ('name', 'title', 'srs'):
+ self.value = ''
+
+ def characters(self, ch):
+ if self.inTag['name'] or \
+ self.inTag['title'] or \
+ self.inTag['srs']:
+ self.value += ch
+
+ def getLayers(self):
+ """Print list of layers"""
+ for ly in self.layers:
+ if ly.has_key('name'):
+ print "LAYER: " + ly['name']
+ else:
+ print "LAYER: ???"
+ if ly.has_key('title'):
+ print " Title: " + ly['title']
+ if ly.has_key('srs'):
+ print " SRS: " + ly['srs']
+ if ly.has_key('style'):
+ for idx in range(len(ly['style']['name'])):
+ print " STYLE: " + ly['style']['name'][idx]
+ print " Style title: " + ly['style']['title'][idx]
Property changes on: grass/trunk/scripts/r.in.wms/wms_parse.py
___________________________________________________________________
Name: svn:mime-type
+ text/x-python
Name: svn:keywords
+ Author Date Id
Name: svn:eol-style
+ native
Modified: grass/trunk/scripts/r.in.wms/wms_request.py
===================================================================
--- grass/trunk/scripts/r.in.wms/wms_request.py 2009-05-08 14:33:58 UTC (rev 37052)
+++ grass/trunk/scripts/r.in.wms/wms_request.py 2009-05-08 16:44:24 UTC (rev 37053)
@@ -1,11 +1,9 @@
-#!/usr/bin/env python
-
############################################################################
#
-# MODULE: r.in.wms
+# MODULE: r.in.wms / wms_request.py
#
# AUTHOR(S): Cedric Shock, 2006
-# Pythonized by Martin Landa <landa.martin gmail.com>, 2009
+# Update for GRASS 7 by Martin Landa <landa.martin gmail.com>, 2009
#
# PURPOSE: To import data from web mapping servers
# (based on Bash script by Cedric Shock)
@@ -26,20 +24,22 @@
def __init__(self, flags, options):
self.flags = flags
self.options = options
-
+
self.__set_options()
def __set_options(self):
- # If the user asserts that this projection is the same as the
+ # if the user asserts that this projection is the same as the
# source use this projection as the source to get a trivial
# tiling from r.tileset
if self.flags['p']:
self.proj_srs = grass.read_command('g.proj', flags='j')
+ if not self.proj_srs:
+ grass.fatal('g.proj failed')
self.srs_scale = int(grass.parse_key_val(proj_srs['+to_meter']))
else:
self.proj_srs = '+init=%s' % self.options['srs'].lower()
self.srs_scale = 1
-
+
# options for r.tileset
self.tileset_options = grass.parse_key_val(self.options['tileoptions'])
if self.options['region']:
@@ -50,7 +50,7 @@
self.transparency = "transparent=FALSE"
else:
self.transparency = "transparent=FALSE"
-
+
# image format
format_opt = self.options['format']
if format_opt == "geotiff":
@@ -75,17 +75,20 @@
self.file_extent = ".gif"
else:
grass.fatal("Uknown image format '%s'" % format_opt)
-
+
# create download directory
if not os.path.exists(self.options['folder']):
os.mkdir(self.options['folder'])
+ odir = os.path.join(self.options['folder'], self.options['output'])
+ if not os.path.exists(odir):
+ os.mkdir(odir)
+ self._tdir = os.path.join(self.options['folder'], self.options['output'],
+ self.options['region'])
+ if not os.path.exists(self._tdir):
+ os.mkdir(self._tdir)
- # clean files
- self._tdir = os.path.join(self.options['folder'], self.options['output'],
- self.options['region'])
-
self.request_file = os.path.join(self._tdir, 'request')
-
+
def GetRequestFile(self):
return self.request_file
@@ -97,9 +100,9 @@
ret.append(grass.parse_key_val(line, vsep = ';'))
finally:
rf.close()
-
+
return ret
-
+
def GetTiles(self):
grass.message("Calculating tiles...")
tiles = grass.read_command('r.tileset',
@@ -114,7 +117,6 @@
if not tiles:
grass.fatal("r.tileset failed")
tiles = tiles.splitlines()
- grass.message("Requesting %d tiles" % len(tiles))
if self.flags['c']:
rmfiles = os.path.join(self._tdir, '*')
@@ -124,7 +126,7 @@
os.rmdir(file)
else:
os.remove(file)
-
+
rf = open(self.request_file, 'w')
i = 0
for tile in tiles:
@@ -137,7 +139,7 @@
w = float(dtile['w'])
nr = int(dtile['rows'])
nc = int(dtile['cols'])
-
+
size = "bbox=%f,%f,%f,%f&width=%d&height=%d" % \
(w, s, e, n, nr, nc)
xres = (e - w) / nc
@@ -145,7 +147,7 @@
# center of top left cell
top_left_cell_center_x = w + xres / 2
top_left_cell_center_y = n + yres / 2
-
+
# write the world file
wf = open(worldfile, 'w')
try:
@@ -153,7 +155,7 @@
(xres, yres, top_left_cell_center_x, top_left_cell_center_y))
finally:
wf.close()
-
+
# request for data
string = "request=GetMap&layers=%s&srs=%s&%s&format=%s&%s&%s" % \
(self.options['layers'], self.options['srs'],
@@ -163,5 +165,6 @@
rf.write('output=%s;server=%s;string=%s\n' % \
(outputfile, self.options['mapserver'], string))
i += 1
-
+
rf.close()
+ grass.message("Done: requesting %d tiles" % len(tiles))
More information about the grass-commit
mailing list