/[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 2158 by joey, Sun Apr 11 17:38:10 2004 UTC revision 2603 by joey, Tue Apr 26 15:04:22 2005 UTC
# Line 1  Line 1 
1  # Copyright (C) 2003, 2004 by Intevation GmbH  # Copyright (C) 2003, 2004 by Intevation GmbH
2  # Authors:  # Authors:
3  # Jan-Oliver Wagner <[email protected]>  # Jan-Oliver Wagner <[email protected]> (2003, 2004)
4    # Bernhard Herzog <[email protected]> (2004)
5    # Martin Schulze <[email protected]> (2004)
6  #  #
7  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
8  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 13  It just demonstrates how to add a specia Line 15  It just demonstrates how to add a specia
15  layer into Thuban via an extension.  layer into Thuban via an extension.
16  Some things are not wired, so be prepared for Exceptions  Some things are not wired, so be prepared for Exceptions
17  everywhere.  everywhere.
   
 You will need PyOGCLib 0.1.0, see  
   http://pyogclib.sourceforge.net/  
 Set the PYTHONPATH to the PyOGCLib directory before  
 starting Thuban.  
18  """  """
19    
20  __version__ = "$Revision$"  __version__ = "$Revision$"
21    # $Source$
22    # $Id$
23    
24  import os, sys  import os, sys
25  import xml.dom.minidom  import xml.dom.minidom
# Line 28  import tempfile Line 27  import tempfile
27    
28  from wxPython.wx import *  from wxPython.wx import *
29    
 from ogclib.WMSClient import WMSClient  
   
 from Thuban.Model.layer import BaseLayer  
30  from Thuban.Model.proj import Projection  from Thuban.Model.proj import Projection
31  from Thuban.Model.extension import Extension  from Thuban.Model.extension import Extension
 from Thuban.Model.resource import get_system_proj_file, EPSG_PROJ_FILE, \  
      EPSG_DEPRECATED_PROJ_FILE  
32  from Thuban.UI.command import registry, Command  from Thuban.UI.command import registry, Command
33  import Thuban.UI.mainwindow  from Thuban.UI.mainwindow import main_menu, layer_properties_dialogs
 from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor  
34  from Thuban import _  from Thuban import _
35  import Thuban.UI.baserenderer  import Thuban.UI.baserenderer
36    
37  from capabilities import WMSCapabilities  from layer import WMSLayer
   
 def epsg_code_to_projection(epsg):  
     """Find the projection for the given epsg code.  
38    
     epsg -- EPSG code as string  
     """  
     proj_file, warnings = get_system_proj_file(EPSG_PROJ_FILE)  
   
     for proj in proj_file.GetProjections():  
         if proj.EPSGCode() == epsg:  
             return proj  
     proj_file, warnings = get_system_proj_file(EPSG_DEPRECATED_PROJ_FILE)  
     for proj in proj_file.GetProjections():  
         if proj.EPSGCode() == epsg:  
             return proj  
     return None  
39    
40  class WMSExtension(Extension):  class WMSExtension(Extension):
41      def TreeInfo(self):      def TreeInfo(self):
42          return (_("Extension: %s") % self.title,          return (_("Extension: %s") % self.title,
43                  [ object.TreeInfo() for object in self.objects ])                  [ object.TreeInfo() for object in self.objects ])
44    
 class WMSLayer(BaseLayer):  
   
     def __init__(self, title, url):  
         """Initializes the WMSLayer.  
   
         title -- Title of this layer.  
         url -- URL of the WMS-Server wich must contain '?'  
   
         If an error occured, self.error_msg is a string describing  
         the problem(s). Else, self.error_msg is None.  
         """  
         BaseLayer.__init__(self, title, visible = True, projection = None)  
         self.url = url  
         self.bbox = None  
         self.latlonbbox = None  
         self.error_msg = None  
         self.layer_name = None  
         self.capabilities = None  
   
         # Change the cursor to demonstrate that we're busy but working  
         ThubanBeginBusyCursor()  
         self.capabilities = WMSCapabilities(url)  
         ThubanEndBusyCursor()  
   
         # name of the top layer of the remote map  
         foo = self.capabilities.getLayers()  
         if len(foo) == 0:  
             self.error_msg = _('No layers found in remote resource:\n'\  
                                '%s') % url  
             return  
         top_layer = foo[0]  
         self.layer_name = top_layer  
   
         # first projection of the top layer  
         foo = self.capabilities.getLayerSRS(top_layer)  
         if len(foo) == 0:  
             self.error_msg = _('No LatLonBoundingBox found for top layer %s')\  
                              % top_layer  
             return  
         top_srs = foo[0]  
   
         # BoundingBox of the top layer  
         bbox = self.capabilities.getLayerBBox(top_layer, top_srs)  
         if len(bbox) == 0:  
             self.error_msg = _('No BoundingBox found for layer %s and EPSG:')\  
                              % (top_layer, top_srs)  
             return  
         self.bbox = (float(bbox['minx']),  
                      float(bbox['miny']),  
                      float(bbox['maxx']),  
                      float(bbox['maxy']))  
   
         # LatLonBox of the top layer  
         bbox = self.capabilities.getLayerLatLonBBox(top_layer)  
         self.latlonbbox = (float(bbox['minx']),  
                            float(bbox['miny']),  
                            float(bbox['maxx']),  
                            float(bbox['maxy']))  
   
         # get projection  
         p = epsg_code_to_projection(top_srs)  
         self.SetProjection(p)  
   
         if p is None:  
             self.error_msg = _('EPSG projection code %s not found!\n'\  
                                'Setting projection to "None".\n'\  
                                'Please set an appropriate projection yourself.'\  
                                % epsg_id)  
   
         # pre-determine the used format  
         self.wmsformat, self.format = \  
             self.calcFormat(self.capabilities.getFormats())  
         if self.wmsformat is None:  
             self.error_msg = \  
                 _('No supported image format found in remote resource')  
             return  
   
         # get and set the title  
         self.SetTitle(self.capabilities.getTitle().encode('latin1', 'replace'))  
   
   
     def LatLongBoundingBox(self):  
         """Return the layer's bounding box in lat-lon.  
         """  
         return self.latlonbbox  
   
     def BoundingBox(self):  
         """Return the layer's bounding box in the intrinsic coordinate system.  
         """  
         return self.bbox  
   
   
     def getFormat(self, format):  
         """  
         Return the image format for the render engine  
   
         format -- format as returned by the WMS server  
   
         If no mapping was found, None is returned  
   
         An exception rule is implemented in order to not accept  
         image/wbmp or WBMP which refers to WAP bitmap format and is  
         not supported by the included render engine.  
         """  
         fmap = {'png' : "PNG",  
                 'jpeg': "JPEG",  
                 'jpg' : "JPEG",  
                 'tif' : "TIFF",  
                 'gif' : "GIF",  
                 'wbmp': None,  
                 'bmp' : "BMP"}  
   
         for f in fmap.keys():  
             if format.lower().find(f) > -1:  
                     return fmap[f]  
         return None  
   
           
     def calcFormat(self, formats):  
         """  
         Calculate the preferred image format  
   
         formats -- list of formates as returned by the WMS server  
   
         The following priority is used:  
         - PNG  
         - JPEG  
         - TIFF  
         - GIF  
         - BMP  
   
         If no matching format was found, None, None will be returned.  
   
         An exception rule is implemented in order to not accept  
         image/wbmp or WBMP which refers to WAP bitmap format and is  
         not supported by the included render engine.  
         """  
         prio = ['png', 'jpeg', 'jpg', 'tif', 'gif', 'bmp']  
         for p in prio:  
             for f in formats:  
                 if f.lower().find(p) > -1:  
                     if f.lower().find('wbmp') == -1:  
                         return f, self.getFormat(f)  
         return None, None  
           
   
     def GetMapImg(self, width, height, bbox):  
         bbox_dict = { 'minx': bbox[0], 'miny': bbox[1],  
                       'maxx': bbox[2], 'maxy': bbox[3] }  
   
         # Change the cursor to demonstrate that we're busy but working  
         ThubanBeginBusyCursor()  
   
         wmsclient = WMSClient()  
   
         epsg_id = int(self.GetProjection().EPSGCode())  
   
         wms_response = wmsclient.getMap(self.url, self.wmsformat, width, height,  
                                    epsg_id, bbox_dict,  
                                    [self.layer_name], version = self.capabilities.getVersion())  
         ThubanEndBusyCursor()  
         return wms_response, self.format  
   
45    
46  def render_wms_layer(renderer, layer):  def render_wms_layer(renderer, layer):
47      offx, offy = renderer.offset      offx, offy = renderer.offset
# Line 239  def render_wms_layer(renderer, layer): Line 54  def render_wms_layer(renderer, layer):
54      ymax = (offy - 0) / scale      ymax = (offy - 0) / scale
55    
56      img, format = layer.GetMapImg(width, height, (xmin, ymin, xmax, ymax))      img, format = layer.GetMapImg(width, height, (xmin, ymin, xmax, ymax))
57      renderer.draw_raster_data(img, format)  
58        data = (width, height, (img, None, None))
59    
60        renderer.draw_raster_data(layer, 0,0, data, format)
61    
62      return ()      return ()
63    
64  Thuban.UI.baserenderer.add_renderer_extension(WMSLayer, render_wms_layer)  Thuban.UI.baserenderer.add_renderer_extension(WMSLayer, render_wms_layer)
65    from properties import wmsProperties
66    layer_properties_dialogs.add(WMSLayer, wmsProperties)
67    
68    
69  class SelectWMSServer(wxDialog):  class SelectWMSServer(wxDialog):
# Line 301  def wms_dialog(context): Line 121  def wms_dialog(context):
121    
122      if dialog.ShowModal() == wxID_OK:      if dialog.ShowModal() == wxID_OK:
123          url = dialog.url          url = dialog.url
124            if len(url) == 0:
125                url = None
126      else:      else:
127          url = None          url = None
128      dialog.Destroy()      dialog.Destroy()
# Line 330  registry.Add(Command('wms', _('Add WMS l Line 152  registry.Add(Command('wms', _('Add WMS l
152                           helptext = _('Add a WMS Layer')))                           helptext = _('Add a WMS Layer')))
153    
154  # find the experimental menu (create it anew if not found)  # find the experimental menu (create it anew if not found)
155  main_menu = Thuban.UI.mainwindow.main_menu  experimental_menu = main_menu.FindOrInsertMenu('experimental',
156  experimental_menu = main_menu.find_menu('experimental')                                                 _('Experimenta&l'))
 if experimental_menu is None:  
     experimental_menu = main_menu.InsertMenu('experimental', _('Experimenta&l'))  
157    
158  # finally add the new entry to the experimental menu  # finally add the new entry to the experimental menu
159  experimental_menu.InsertItem('wms')  experimental_menu.InsertItem('wms')

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26