[GRASS-SVN] r51700 - grass-addons/grass7/raster/r.in.wms2

svn_grass at osgeo.org svn_grass at osgeo.org
Thu May 24 06:56:36 EDT 2012


Author: turek
Date: 2012-05-24 03:56:36 -0700 (Thu, 24 May 2012)
New Revision: 51700

Modified:
   grass-addons/grass7/raster/r.in.wms2/r.in.wms2.html
   grass-addons/grass7/raster/r.in.wms2/wms_base.py
   grass-addons/grass7/raster/r.in.wms2/wms_drv.py
   grass-addons/grass7/raster/r.in.wms2/wms_gdal_drv.py
Log:
Improved support of WMS 1.3.0 (flip of geograph. coords) Added examples into manual.

Modified: grass-addons/grass7/raster/r.in.wms2/r.in.wms2.html
===================================================================
--- grass-addons/grass7/raster/r.in.wms2/r.in.wms2.html	2012-05-24 10:44:38 UTC (rev 51699)
+++ grass-addons/grass7/raster/r.in.wms2/r.in.wms2.html	2012-05-24 10:56:36 UTC (rev 51700)
@@ -30,14 +30,31 @@
 
 <h3>CUZK download</h3>
 
+
+World extend data:
+
 <div class="code"><pre>
+r.in.wms2 mapserver=http://iceds.ge.ucl.ac.uk/cgi-bin/icedswms layers=bluemarble,landsat_1_01 styles=default,default output=landsat srs=4326 format=png 
+</pre></div>
+* Server supports only WMS 1.1.1 <br>
+
+<div class="code"><pre>
+r.in.wms2 mapserver=http://132.156.97.59/cgi-bin/worldmin_en-ca_ows layers=GSC:WORLD_PrecambrianDomains output=pokus srs=4326 format=jpeg 
+</pre></div>
+* Server supports only WMS 1.1.1 
+<br>
+<br>
+
+Data in extend of Czech Republic:
+
+<div class="code"><pre>
 r.in.wms2 output=kn mapserver=http://wms.cuzk.cz/wms.asp layers=prehledka_kraju-linie srs=4326 format=png
 </pre></div>
 
+
 <h2>TODO</h2>
 
 <ul>
-  <li>Add more examples...</li>
   <li>Implement Tiled WMS</li>
 </ul>
 

Modified: grass-addons/grass7/raster/r.in.wms2/wms_base.py
===================================================================
--- grass-addons/grass7/raster/r.in.wms2/wms_base.py	2012-05-24 10:44:38 UTC (rev 51699)
+++ grass-addons/grass7/raster/r.in.wms2/wms_base.py	2012-05-24 10:56:36 UTC (rev 51700)
@@ -193,24 +193,28 @@
         
         cap_lines = cap.readlines()
         for line in cap_lines: 
-            grass.message(line) 
+            print line 
         
     def _computeBbox(self):
         """!Get region extent for WMS query (bbox)
         """
         self._debug("_computeBbox", "started")
         
-        bbox = {'n' : None, 's' : None, 'e' : None, 'w' : None}
-        
+        bbox_region_items = {'maxy' : 'n', 'miny' : 's', 'maxx' : 'e', 'minx' : 'w'}  
+        bbox = {}
+
         if self.proj_srs == self.proj_location: # TODO: do it better
-            for param in bbox:
-                bbox[param] = self.region[param]
+            for bbox_item, region_item in bbox_region_items.iteritems():
+                bbox[bbox_item] = self.region[region_item]
         
         # if location projection and wms query projection are
         # different, corner points of region are transformed into wms
         # projection and then bbox is created from extreme coordinates
         # of the transformed points
         else:
+            for bbox_item, region_item  in bbox_region_items.iteritems():
+                bbox[bbox_item] = None
+
             temp_region = self._tempfile()
             
             try:
@@ -239,25 +243,36 @@
             
             for point in points:
                 point = map(float, point.split("|"))
-                if not bbox['n']:
-                    bbox['n'] = point[1]
-                    bbox['s'] = point[1]
-                    bbox['e'] = point[0]
-                    bbox['w'] = point[0]
+                if not bbox['maxy']:
+                    bbox['maxy'] = point[1]
+                    bbox['miny'] = point[1]
+                    bbox['maxx'] = point[0]
+                    bbox['minx'] = point[0]
                     continue
                 
-                if   bbox['n'] < point[1]:
-                    bbox['n'] = point[1]
-                elif bbox['s'] > point[1]:
-                    bbox['s'] = point[1]
+                if   bbox['maxy'] < point[1]:
+                    bbox['maxy'] = point[1]
+                elif bbox['miny'] > point[1]:
+                    bbox['miny'] = point[1]
                 
-                if   bbox['e'] < point[0]:
-                    bbox['e'] = point[0]
-                elif bbox['w'] > point[0]:
-                    bbox['w'] = point[0]  
+                if   bbox['maxx'] < point[0]:
+                    bbox['maxx'] = point[0]
+                elif bbox['minx'] > point[0]:
+                    bbox['minx'] = point[0]  
         
         self._debug("_computeBbox", "finished -> %s" % bbox)
-        
+
+        # Ordering of coordinates axis of geographic coordinate
+        # systems in WMS 1.3.0 is fliped. If self.flip_coords is 
+        # True, coords in bbox need to be flipped in WMS query.
+
+        self.flip_coords = False  
+        hasLongLat = self.proj_srs.find("+proj=longlat")   
+        hasLatLong = self.proj_srs.find("+proj=latlong")   
+
+        if (hasLongLat != -1 or hasLatLong != -1) and self.o_wms_version == "1.3.0":
+            self.flip_coords = True
+
         return bbox
 
     def _createOutputMap(self): 
@@ -346,6 +361,25 @@
         grass.try_remove(temp_warpmap)
         grass.try_remove(self.temp_map) 
 
+    def _flipBbox(self, bbox):
+        """ 
+        flips items in dictionary 
+        value flips between this keys:
+        maxy -> maxx
+        maxx -> maxy
+        miny -> minx
+        minx -> miny
+        @return copy of bbox with fliped cordinates
+        """  
+        temp_bbox = dict(bbox)
+        new_bbox = {}
+        new_bbox['maxy'] = temp_bbox['maxx']
+        new_bbox['miny'] = temp_bbox['minx']
+        new_bbox['maxx'] = temp_bbox['maxy']
+        new_bbox['minx'] = temp_bbox['miny']
+
+        return new_bbox
+
     def _tempfile(self):
         """!Create temp_file and append list self.temp_files_to_cleanup 
             with path of file 

Modified: grass-addons/grass7/raster/r.in.wms2/wms_drv.py
===================================================================
--- grass-addons/grass7/raster/r.in.wms2/wms_drv.py	2012-05-24 10:44:38 UTC (rev 51699)
+++ grass-addons/grass7/raster/r.in.wms2/wms_drv.py	2012-05-24 10:56:36 UTC (rev 51700)
@@ -21,6 +21,7 @@
         """ 
         grass.message(_("Downloading data from WMS server..."))
         
+
         proj = self.projection_name + "=EPSG:"+ str(self.o_srs)
         url = self.o_mapserver_url + "REQUEST=GetMap&VERSION=%s&LAYERS=%s&WIDTH=%s&HEIGHT=%s&STYLES=%s&BGCOLOR=%s&TRANSPARENT=%s" %\
                   (self.o_wms_version, self.o_layers, self.tile_cols, self.tile_rows, self.o_styles, self.o_bgcolor, self.transparent)
@@ -35,7 +36,7 @@
         # computes parameters of tiles 
         num_tiles_x = cols / self.tile_cols 
         last_tile_x_size = cols % self.tile_cols
-        tile_x_length =  float(self.tile_cols) / float(cols ) * (self.bbox['e'] - self.bbox['w']) 
+        tile_x_length =  float(self.tile_cols) / float(cols ) * (self.bbox['maxx'] - self.bbox['minx']) 
         
         last_tile_x = False
         if last_tile_x_size != 0:
@@ -44,7 +45,7 @@
         
         num_tiles_y = rows / self.tile_rows 
         last_tile_y_size = rows % self.tile_rows
-        tile_y_length =  float(self.tile_rows) / float(rows) * (self.bbox['n'] - self.bbox['s']) 
+        tile_y_length =  float(self.tile_rows) / float(rows) * (self.bbox['maxy'] - self.bbox['miny']) 
         
         last_tile_y = False
         if last_tile_y_size != 0:
@@ -53,34 +54,40 @@
         
         # each tile is downloaded and written into temp_map 
         tile_bbox = dict(self.bbox)
-        tile_bbox['e'] = self.bbox['w']  + tile_x_length
+        tile_bbox['maxx'] = self.bbox['minx']  + tile_x_length
         
         tile_to_temp_map_size_x = self.tile_cols
         for i_x in range(num_tiles_x):
             # set bbox for tile i_x,i_y (E, W)
             if i_x != 0:
-                tile_bbox['e'] += tile_x_length 
-                tile_bbox['w'] += tile_x_length            
+                tile_bbox['maxx'] += tile_x_length 
+                tile_bbox['minx'] += tile_x_length            
             
             if i_x == num_tiles_x - 1 and last_tile_x:
                 tile_to_temp_map_size_x = last_tile_x_size 
             
-            tile_bbox['n'] = self.bbox['n']                    
-            tile_bbox['s'] = self.bbox['n'] - tile_y_length 
+            tile_bbox['maxy'] = self.bbox['maxy']                    
+            tile_bbox['miny'] = self.bbox['maxy'] - tile_y_length 
             tile_to_temp_map_size_y = self.tile_rows       
             
             for i_y in range(num_tiles_y):
                 # set bbox for tile i_x,i_y (N, S)
                 if i_y != 0:
-                    tile_bbox['s'] -= tile_y_length 
-                    tile_bbox['n'] -= tile_y_length
+                    tile_bbox['miny'] -= tile_y_length 
+                    tile_bbox['maxy'] -= tile_y_length
                 
                 if i_y == num_tiles_y - 1 and last_tile_y:
                     tile_to_temp_map_size_y = last_tile_y_size 
                 
-                # bbox for tile defined
-                query_url = url + "&" + "BBOX=%s,%s,%s,%s" % (tile_bbox['w'], tile_bbox['s'], tile_bbox['e'], tile_bbox['n'])
-                
+                if self.flip_coords:
+                    # flips coordinates if WMS strandard is 1.3.0 and 
+                    # projection is geographic (see:wms_base.py _computeBbox)
+                    query_bbox = dict(self._flipBbox(tile_bbox))
+                else:
+                    query_bbox = tile_bbox
+
+                query_url = url + "&" + "BBOX=%s,%s,%s,%s" % ( query_bbox['minx'],  query_bbox['miny'],  query_bbox['maxx'],  query_bbox['maxy'])
+                grass.debug(query_url)
                 try: 
                     wms_data = urlopen(query_url)
                 except IOError:
@@ -158,9 +165,11 @@
                                         epsg =self.o_srs).rstrip('\n')
         temp_map_dataset.SetProjection(projection)
         
-        pixel_x_length = (self.bbox['e'] - self.bbox['w']) / int(cols)
-        pixel_y_length = (self.bbox['s'] - self.bbox['n']) / int(rows)
-        geo_transform = [ self.bbox['w'] , pixel_x_length  , 0.0 ,  self.bbox['n'] , 0.0 , pixel_y_length ] 
+
+
+        pixel_x_length = (self.bbox['maxx'] - self.bbox['minx']) / int(cols)
+        pixel_y_length = (self.bbox['miny'] - self.bbox['maxy']) / int(rows)
+        geo_transform = [ self.bbox['minx'] , pixel_x_length  , 0.0 ,  self.bbox['maxy'] , 0.0 , pixel_y_length ] 
         temp_map_dataset.SetGeoTransform(geo_transform )
         temp_map_dataset = None
         

Modified: grass-addons/grass7/raster/r.in.wms2/wms_gdal_drv.py
===================================================================
--- grass-addons/grass7/raster/r.in.wms2/wms_gdal_drv.py	2012-05-24 10:44:38 UTC (rev 51699)
+++ grass-addons/grass7/raster/r.in.wms2/wms_gdal_drv.py	2012-05-24 10:56:36 UTC (rev 51700)
@@ -53,16 +53,16 @@
         data_window = etree.SubElement(gdal_wms, "DataWindow")
         
         upper_left_x = etree.SubElement(data_window, "UpperLeftX")
-        upper_left_x.text = str(self.bbox['w']) 
+        upper_left_x.text = str(self.bbox['minx']) 
         
         upper_left_y = etree.SubElement(data_window, "UpperLeftY")
-        upper_left_y.text = str(self.bbox['n']) 
+        upper_left_y.text = str(self.bbox['maxy']) 
         
         lower_right_x = etree.SubElement(data_window, "LowerRightX")
-        lower_right_x.text = str(self.bbox['e']) 
+        lower_right_x.text = str(self.bbox['maxx']) 
         
         lower_right_y = etree.SubElement(data_window, "LowerRightY")
-        lower_right_y.text = str(self.bbox['s'])
+        lower_right_y.text = str(self.bbox['miny'])
         
         size_x = etree.SubElement(data_window, "SizeX")
         size_x.text = str(self.region['cols']) 
@@ -95,10 +95,17 @@
         @return temp_map with stored downloaded data
         """
         grass.message("Downloading data from WMS server...")
+
+        # GDAL WMS driver does not flip geographic coordinates 
+        # according to WMS standard 1.3.0.
+        if self.flip_coords and self.o_wms_version == "1.3.0":
+            grass.warning(_("If module will not be able to fetch the data in this\
+                           geographic projection, \n try flag -d or use WMS version 1.1.1."))
+
         self._debug("_download", "started")
         
-        temp_map = self._tempfile()
-        
+        temp_map = self._tempfile()        
+
         xml_file = self._createXML()
         wms_dataset = gdal.Open(xml_file, gdal.GA_ReadOnly)
         grass.try_remove(xml_file)



More information about the grass-commit mailing list