/[thuban]/branches/WIP-pyshapelib-bramz/Extensions/wms/wms.py
ViewVC logotype

Diff of /branches/WIP-pyshapelib-bramz/Extensions/wms/wms.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1944 by jan, Wed Nov 12 08:26:56 2003 UTC revision 2158 by joey, Sun Apr 11 17:38:10 2004 UTC
# Line 1  Line 1 
1  # Copyright (C) 2003 by Intevation GmbH  # Copyright (C) 2003, 2004 by Intevation GmbH
2  # Authors:  # Authors:
3  # Jan-Oliver Wagner <[email protected]>  # Jan-Oliver Wagner <[email protected]>
4  #  #
# Line 33  from ogclib.WMSClient import WMSClient Line 33  from ogclib.WMSClient import WMSClient
33  from Thuban.Model.layer import BaseLayer  from Thuban.Model.layer import BaseLayer
34  from Thuban.Model.proj import Projection  from Thuban.Model.proj import Projection
35  from Thuban.Model.extension import Extension  from Thuban.Model.extension import Extension
36  from Thuban.Model.resource import read_proj_file  from Thuban.Model.resource import get_system_proj_file, EPSG_PROJ_FILE, \
37         EPSG_DEPRECATED_PROJ_FILE
38  from Thuban.UI.command import registry, Command  from Thuban.UI.command import registry, Command
39  import Thuban.UI.mainwindow  import Thuban.UI.mainwindow
40    from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor
41  from Thuban import _  from Thuban import _
42  import Thuban.UI.baserenderer  import Thuban.UI.baserenderer
43    
44    from capabilities import WMSCapabilities
45    
46  def epsg_code_to_projection(epsg):  def epsg_code_to_projection(epsg):
47      """Find the projection for the given epsg code.      """Find the projection for the given epsg code.
48    
49      epsg -- EPSG code as string      epsg -- EPSG code as string
50      """      """
51      proj_file, warnings = read_proj_file("Resources/Projections/epsg.proj")      proj_file, warnings = get_system_proj_file(EPSG_PROJ_FILE)
52    
53      for proj in proj_file.GetProjections():      for proj in proj_file.GetProjections():
54          if proj.EPSGCode() == epsg:          if proj.EPSGCode() == epsg:
55              return proj              return proj
56      proj_file, warnings = read_proj_file("Resources/Projections/epsg-deprecated.proj")      proj_file, warnings = get_system_proj_file(EPSG_DEPRECATED_PROJ_FILE)
57      for proj in proj_file.GetProjections():      for proj in proj_file.GetProjections():
58          if proj.EPSGCode() == epsg:          if proj.EPSGCode() == epsg:
59              return proj              return proj
# Line 60  class WMSExtension(Extension): Line 64  class WMSExtension(Extension):
64          return (_("Extension: %s") % self.title,          return (_("Extension: %s") % self.title,
65                  [ object.TreeInfo() for object in self.objects ])                  [ object.TreeInfo() for object in self.objects ])
66    
67  class WMSLayer(BaseLayer, WMSClient):  class WMSLayer(BaseLayer):
68    
69      def __init__(self, title, url):      def __init__(self, title, url):
70          """Initializes the WMSLayer.          """Initializes the WMSLayer.
# Line 77  class WMSLayer(BaseLayer, WMSClient): Line 81  class WMSLayer(BaseLayer, WMSClient):
81          self.latlonbbox = None          self.latlonbbox = None
82          self.error_msg = None          self.error_msg = None
83          self.layer_name = None          self.layer_name = None
84            self.capabilities = None
85    
86          wms_response = self.getCapabilities(self.url, '1.0')          # Change the cursor to demonstrate that we're busy but working
87          self._capa_dom = xml.dom.minidom.parseString(wms_response)          ThubanBeginBusyCursor()
88            self.capabilities = WMSCapabilities(url)
89          root = self._capa_dom.documentElement          ThubanEndBusyCursor()
90          cap = root.getElementsByTagName('Capability')[0]  
91          layer = cap.getElementsByTagName('Layer')[0]          # name of the top layer of the remote map
92            foo = self.capabilities.getLayers()
93          # get projected bounding box and latlon bounding box          if len(foo) == 0:
94          for node in layer.childNodes:              self.error_msg = _('No layers found in remote resource:\n'\
95              if node.nodeName == 'BoundingBox':                                 '%s') % url
96                  minx = node.attributes.get('minx').nodeValue              return
97                  miny = node.attributes.get('miny').nodeValue          top_layer = foo[0]
98                  maxx = node.attributes.get('maxx').nodeValue          self.layer_name = top_layer
99                  maxy = node.attributes.get('maxy').nodeValue  
100                  self.bbox = (float(minx), float(miny), float(maxx), float(maxy))          # first projection of the top layer
101          bbox = layer.getElementsByTagName('LatLonBoundingBox')[0]          foo = self.capabilities.getLayerSRS(top_layer)
102          self.layer_name = layer.getElementsByTagName('Name')[0].childNodes[0].data          if len(foo) == 0:
103          minx = bbox.attributes.get('minx').nodeValue              self.error_msg = _('No LatLonBoundingBox found for top layer %s')\
104          miny = bbox.attributes.get('miny').nodeValue                               % top_layer
105          maxx = bbox.attributes.get('maxx').nodeValue              return
106          maxy = bbox.attributes.get('maxy').nodeValue          top_srs = foo[0]
107          self.latlonbbox = (float(minx), float(miny), float(maxx), float(maxy))  
108            # BoundingBox of the top layer
109            bbox = self.capabilities.getLayerBBox(top_layer, top_srs)
110            if len(bbox) == 0:
111                self.error_msg = _('No BoundingBox found for layer %s and EPSG:')\
112                                 % (top_layer, top_srs)
113                return
114            self.bbox = (float(bbox['minx']),
115                         float(bbox['miny']),
116                         float(bbox['maxx']),
117                         float(bbox['maxy']))
118    
119            # LatLonBox of the top layer
120            bbox = self.capabilities.getLayerLatLonBBox(top_layer)
121            self.latlonbbox = (float(bbox['minx']),
122                               float(bbox['miny']),
123                               float(bbox['maxx']),
124                               float(bbox['maxy']))
125    
126          # get projection          # get projection
127          srs = layer.getElementsByTagName('SRS')[0].childNodes[0].data          p = epsg_code_to_projection(top_srs)
         if len(srs.split(':')) == 1:  
             epsg_id = srs  
         else:  
             epsg_id = srs.split(':')[1]  
   
         p = epsg_code_to_projection(epsg_id)  
128          self.SetProjection(p)          self.SetProjection(p)
129    
130          if p is None:          if p is None:
# Line 117  class WMSLayer(BaseLayer, WMSClient): Line 133  class WMSLayer(BaseLayer, WMSClient):
133                                 'Please set an appropriate projection yourself.'\                                 'Please set an appropriate projection yourself.'\
134                                 % epsg_id)                                 % epsg_id)
135    
136          # get title          # pre-determine the used format
137          title = layer.getElementsByTagName('Title')[0].childNodes[0].data          self.wmsformat, self.format = \
138          self.SetTitle(title.encode('latin1', 'replace'))              self.calcFormat(self.capabilities.getFormats())
139            if self.wmsformat is None:
140                self.error_msg = \
141                    _('No supported image format found in remote resource')
142                return
143    
144            # get and set the title
145            self.SetTitle(self.capabilities.getTitle().encode('latin1', 'replace'))
146    
         self._capa_dom.unlink()  
147    
148      def LatLongBoundingBox(self):      def LatLongBoundingBox(self):
149          """Return the layer's bounding box in lat-lon.          """Return the layer's bounding box in lat-lon.
# Line 133  class WMSLayer(BaseLayer, WMSClient): Line 155  class WMSLayer(BaseLayer, WMSClient):
155          """          """
156          return self.bbox          return self.bbox
157    
158    
159        def getFormat(self, format):
160            """
161            Return the image format for the render engine
162    
163            format -- format as returned by the WMS server
164    
165            If no mapping was found, None is returned
166    
167            An exception rule is implemented in order to not accept
168            image/wbmp or WBMP which refers to WAP bitmap format and is
169            not supported by the included render engine.
170            """
171            fmap = {'png' : "PNG",
172                    'jpeg': "JPEG",
173                    'jpg' : "JPEG",
174                    'tif' : "TIFF",
175                    'gif' : "GIF",
176                    'wbmp': None,
177                    'bmp' : "BMP"}
178    
179            for f in fmap.keys():
180                if format.lower().find(f) > -1:
181                        return fmap[f]
182            return None
183    
184            
185        def calcFormat(self, formats):
186            """
187            Calculate the preferred image format
188    
189            formats -- list of formates as returned by the WMS server
190    
191            The following priority is used:
192            - PNG
193            - JPEG
194            - TIFF
195            - GIF
196            - BMP
197    
198            If no matching format was found, None, None will be returned.
199    
200            An exception rule is implemented in order to not accept
201            image/wbmp or WBMP which refers to WAP bitmap format and is
202            not supported by the included render engine.
203            """
204            prio = ['png', 'jpeg', 'jpg', 'tif', 'gif', 'bmp']
205            for p in prio:
206                for f in formats:
207                    if f.lower().find(p) > -1:
208                        if f.lower().find('wbmp') == -1:
209                            return f, self.getFormat(f)
210            return None, None
211            
212    
213      def GetMapImg(self, width, height, bbox):      def GetMapImg(self, width, height, bbox):
214          bbox_dict = { 'minx': bbox[0], 'miny': bbox[1],          bbox_dict = { 'minx': bbox[0], 'miny': bbox[1],
215                        'maxx': bbox[2], 'maxy': bbox[3] }                        'maxx': bbox[2], 'maxy': bbox[3] }
216    
217            # Change the cursor to demonstrate that we're busy but working
218            ThubanBeginBusyCursor()
219    
220            wmsclient = WMSClient()
221    
222          epsg_id = int(self.GetProjection().EPSGCode())          epsg_id = int(self.GetProjection().EPSGCode())
         wms_response = self.getMap(self.url, 'JPEG', width, height,  
                                    epsg_id, bbox_dict,  
                                    [self.layer_name], version = '1.0')  
         return wms_response  
223    
224            wms_response = wmsclient.getMap(self.url, self.wmsformat, width, height,
225                                       epsg_id, bbox_dict,
226                                       [self.layer_name], version = self.capabilities.getVersion())
227            ThubanEndBusyCursor()
228            return wms_response, self.format
229    
230    
231  def render_wms_layer(renderer, layer):  def render_wms_layer(renderer, layer):
# Line 154  def render_wms_layer(renderer, layer): Line 238  def render_wms_layer(renderer, layer):
238      xmax = (width - offx) / scale      xmax = (width - offx) / scale
239      ymax = (offy - 0) / scale      ymax = (offy - 0) / scale
240    
241      img = layer.GetMapImg(width, height, (xmin, ymin, xmax, ymax))      img, format = layer.GetMapImg(width, height, (xmin, ymin, xmax, ymax))
242      renderer.draw_raster_data(img, "JPEG")      renderer.draw_raster_data(img, format)
243    
244      return ()      return ()
245    

Legend:
Removed from v.1944  
changed lines
  Added in v.2158

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26