[GRASS-SVN] r37057 - grass/trunk/scripts/r.in.wms

svn_grass at osgeo.org svn_grass at osgeo.org
Fri May 8 16:07:52 EDT 2009


Author: martinl
Date: 2009-05-08 16:07:52 -0400 (Fri, 08 May 2009)
New Revision: 37057

Modified:
   grass/trunk/scripts/r.in.wms/gdalwarp.py
   grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py
   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 (fix downloading data)


Modified: grass/trunk/scripts/r.in.wms/gdalwarp.py
===================================================================
--- grass/trunk/scripts/r.in.wms/gdalwarp.py	2009-05-08 19:18:36 UTC (rev 37056)
+++ grass/trunk/scripts/r.in.wms/gdalwarp.py	2009-05-08 20:07:52 UTC (rev 37057)
@@ -43,18 +43,7 @@
             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
@@ -63,25 +52,25 @@
             if not os.path.exists(input):
                 grass.warning("Missing input '%s'" % input)
                 continue
-            grass.info('Importing tile <%s>...' % os.path.basename(input))
+            grass.info("Importing tile '%s'..." % os.path.basename(input))
             if self.flags['p']:
-                self.nowarp_import(input, tmptilename, self.gdal_flags)
+                self.nowarp_import(input, tmptilename)
             else:
-                self.warp_import(input, tmptilename, self.gdal_method)
+                self.warp_import(input, tmptilename)
             
-            maplist.append(tmptilename)
+            self.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:
+            if len(self.channel_suffixes) > 0:
                 # multi-band
-                for suffix in channel_suffixes:
+                for suffix in self.channel_suffixes:
                     # rename tile 0 to be the output
                     ffile = self.options['output'] + '_tile_0' + suffix
                     tfile = self.options['output'] + suffix
@@ -168,25 +157,25 @@
 
         # there's already a no suffix, can't make colors
         # can only go up from here ;)
-        colors = 4 # ???
-        for suffix in suffixes:
+        colors = 0
+        for suffix in self.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']:
+        if colors == 3 and 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:
+                                 output = self.options['output']) != 0:
                 grass.fatal('r.composite failed')
         
         return 0
     
-    def warp_import(self, file, map, method):
+    def warp_import(self, file, map):
         """Wrap raster file using gdalwarp and import wrapped file
         into GRASS"""
         warpfile = self.tmp + 'warped.geotiff'
@@ -194,55 +183,64 @@
 
         t_srs = grass.read_command('g.proj',
                                    quiet = True,
-                                   flags = 'wf')
+                                   flags = 'jf').rstrip('\n')
         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])
+        grass.debug("gdalwarp -s_srs '%s' -t_srs '%s' -r %s %s %s %s" % \
+                        (self.options['srs'], t_srs,
+                         self.options['method'], self.options['warpoptions'],
+                         file, warpfile))
+        grass.verbose("Warping input file '%s'..." % os.path.basename(file))
+        if self.options['warpoptions']:
+            ps = subprocess.Popen(['gdalwarp',
+                                   '-s_srs', '%s' % self.options['srs'],
+                                   '-t_srs', '%s' % t_srs,
+                                   '-r', self.options['method'],
+                                   self.options['warpoptions'],
+                                   file, warpfile])
+        else:
+            ps = subprocess.Popen(['gdalwarp',
+                                   '-s_srs', '%s' % self.options['srs'],
+                                   '-t_srs', '%s' % t_srs,
+                                   '-r', self.options['method'],
+                                   file, warpfile])
+            
         if ps.wait() != 0:
             grass.fatal('gdalwarp failed')
     
         # import it into a temporary map
-        grass.info('Importing temporary raster map...')
+        grass.info('Importing raster map...')
         if grass.run_command('r.in.gdal',
                              quiet = True,
-                             flags = gdal_flags,
+                             flags = self.gdal_flags,
                              input = warpfile,
                              output = tmpmapname) != 0:
             grass.fatal('r.in.gdal failed')
-    
+        
         os.remove(warpfile)
 
         # get list of channels
-        pattern = tmpmapfile + '*'
+        pattern = tmpmapname + '*'
         grass.debug('Pattern: %s' % pattern)
         mapset = grass.gisenv()['MAPSET']
-        channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)
+        channel_list = grass.mlist_grouped(type = 'rast', pattern = pattern, mapset = mapset)[mapset]
         grass.debug('Channel list: %s' % ','.join(channel_list))
         
         if len(channel_list) < 2: # test for single band data
-            channel_suffixes = []
+            self.channel_suffixes = []
         else:
-            channel_suffixes = channel_list # ???
-
-        grass.debug('Channel suffixes: %s', ','.join(channel_suffixes))
-
+            self.channel_suffixes = channel_list # ???
+        
+        grass.debug('Channel suffixes: %s' % ','.join(self.channel_suffixes))
+        
         # add to the list of all suffixes
-        self.suffixes = self.suffixes + channel_suffixes
+        self.suffixes = self.suffixes + self.channel_suffixes
         self.suffixes.sort()
         
         # get last suffix
-        if len(channel_suffixes) > 0:
-            last_suffix = channel_suffixes[-1]
+        if len(self.channel_suffixes) > 0:
+            last_suffix = self.channel_suffixes[-1]
         else:
             last_suffix = ''
 
@@ -253,11 +251,11 @@
             alphalayer = tmpmapname + '.alpha'
         
         # test to see if the alpha map exists
-        if not grass.find_file(element = 'cell', file = alphalayer)['name']:
+        if not grass.find_file(element = 'cell', name = alphalayer)['name']:
             alphalayer = ''
         
         # calculate the new maps:
-        for suffix in channel_suffixes:
+        for suffix in self.channel_suffixes:
             grass.debug("alpha=%s MAPsfx=%s% tmpname=%s%s" % \
                             (alphalayer, map, suffix, tmpmapname, suffix))
             if alphalayer:
@@ -292,7 +290,7 @@
     
         # if no suffix, processing is simple (e.g. elevation has only 1
         # band)
-        if len(channel_list) < 1:
+        if len(channel_list) < 2:
             # run r.mapcalc to crop to region
             if grass.run_command('r.mapcalc',
                                  quiet = True,
@@ -309,14 +307,14 @@
         # remove the old channels
         if grass.run_command('g.remove',
                              quiet = True,
-                             rast = ','.channel_list) != 0:
+                             rast = ','.join(channel_list)) != 0:
             grass.fatal('g.remove failed')
         
-    def nowarp_import(self, file, map, gdal_flags):
+    def nowarp_import(self, file, map):
         """Import raster file into GRASS"""
         if grass.run_command('r.in.gdal',
                              quiet = True,
-                             flags = 'o' + gdal_flags,
+                             flags = 'o' + self.gdal_flags,
                              input = file,
                              output = map) != 0:
             grass.fatal('r.in.gdal failed')
@@ -330,15 +328,15 @@
 
         if len(channel_list) < 2:
             # test for single band data
-            channel_suffixes = []
+            self.channel_suffixes = []
         else:
-            channel_suffixes = channel_list # ???
+            self.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:
+        for suffix in self.channel_suffixes:
             # make patch lists
             suffix = suffix.replace('.', '_')
             # this is a hack to make the patch lists empty

Modified: grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py
===================================================================
--- grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py	2009-05-08 19:18:36 UTC (rev 37056)
+++ grass/trunk/scripts/r.in.wms/r.in.gdalwarp.py	2009-05-08 20:07:52 UTC (rev 37057)
@@ -62,8 +62,8 @@
 #% key: method
 #% type: string
 #% description: Reprojection method to use
-#% options:nearest,bilinear,cubic,cubicspline
-#% answer:nearest
+#% options:near,bilinear,cubic,cubicspline
+#% answer:near
 #% required: yes
 #%end
 #%option

Modified: grass/trunk/scripts/r.in.wms/r.in.wms.py
===================================================================
--- grass/trunk/scripts/r.in.wms/r.in.wms.py	2009-05-08 19:18:36 UTC (rev 37056)
+++ grass/trunk/scripts/r.in.wms/r.in.wms.py	2009-05-08 20:07:52 UTC (rev 37057)
@@ -152,8 +152,8 @@
 #% key: method
 #% type: string
 #% description: Reprojection method to use
-#% options:nearest,bilinear,cubic,cubicspline
-#% answer:nearest
+#% options:near,bilinear,cubic,cubicspline
+#% answer:near
 #% required: yes
 #% guisection: Import
 #%end

Modified: grass/trunk/scripts/r.in.wms/wms_download.py
===================================================================
--- grass/trunk/scripts/r.in.wms/wms_download.py	2009-05-08 19:18:36 UTC (rev 37056)
+++ grass/trunk/scripts/r.in.wms/wms_download.py	2009-05-08 20:07:52 UTC (rev 37057)
@@ -35,19 +35,17 @@
                     os.path.getsize(item['output']) > 0:
                 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):
+    def GetData(self, idx, server, query, output):
         """Download data"""
         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")
+                urllib.urlretrieve(server, output, data = query)
             except IOError:
                 grass.fatal("Failed while downloading the data")
             

Modified: grass/trunk/scripts/r.in.wms/wms_request.py
===================================================================
--- grass/trunk/scripts/r.in.wms/wms_request.py	2009-05-08 19:18:36 UTC (rev 37056)
+++ grass/trunk/scripts/r.in.wms/wms_request.py	2009-05-08 20:07:52 UTC (rev 37057)
@@ -141,7 +141,7 @@
             nc = int(dtile['cols'])
             
             size = "bbox=%f,%f,%f,%f&width=%d&height=%d" % \
-                (w, s, e, n, nr, nc)
+                (w, s, e, n, nc, nr)
             xres = (e - w) / nc
             yres = (s - n) / nr
             # center of top left cell
@@ -157,11 +157,9 @@
                 wf.close()
             
             # request for data
-            string = "request=GetMap&layers=%s&srs=%s&%s&format=%s&%s&%s" % \
-                (self.options['layers'], self.options['srs'],
+            string = "service=WMS&request=GetMap&layers=%s&styles=%s&srs=%s&%s&format=%s&%s&%s" % \
+                (self.options['layers'], self.options['styles'], self.options['srs'],
                  size, self.format, self.transparency, self.options['wmsquery'])
-            if self.options['styles']:
-                string += "&styles=%s" % self.options['styles']
             rf.write('output=%s;server=%s;string=%s\n' % \
                          (outputfile, self.options['mapserver'], string))
             i += 1



More information about the grass-commit mailing list