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

Contents of /branches/WIP-pyshapelib-bramz/Extensions/wms/layer.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2175 - (show annotations)
Thu Apr 15 07:13:31 2004 UTC (20 years, 10 months ago) by joey
Original Path: trunk/thuban/Extensions/wms/layer.py
File MIME type: text/x-python
File size: 7417 byte(s)
Moved the WMS layer into a file on its own in order to establish a
clean structure.

1 # Copyright (c) 2003, 2004 by Intevation GmbH
2 # Authors:
3 # Jan-Oliver Wagner <[email protected]>
4 # Martin Schulze <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 """
21 Graphic Layer via OGC WMS.
22
23 class WMSLayer:
24 __init__()
25
26
27 Requirements:
28 - PyOGCLib <http://www.sourceforge.net/projects/pyogclib>
29
30 Requires the ogclib installed regularily on the system or checked out
31 next to the Thuban checkout. Or set the PYTHONPATH to the PyOGCLib
32 directory before starting Thuban.
33
34 """
35
36 __version__ = "$Revision$"
37 # $Source$
38 # $Id$
39
40
41 from Thuban.Model.layer import BaseLayer
42 from Thuban.Model.resource import get_system_proj_file, EPSG_PROJ_FILE, \
43 EPSG_DEPRECATED_PROJ_FILE
44 from Thuban.UI.common import ThubanBeginBusyCursor, ThubanEndBusyCursor
45
46 from capabilities import WMSCapabilities
47
48 from ogclib.WMSClient import WMSClient
49
50
51 def epsg_code_to_projection(epsg):
52 """Find the projection for the given epsg code.
53
54 epsg -- EPSG code as string
55 """
56 proj_file, warnings = get_system_proj_file(EPSG_PROJ_FILE)
57
58 for proj in proj_file.GetProjections():
59 if proj.EPSGCode() == epsg:
60 return proj
61 proj_file, warnings = get_system_proj_file(EPSG_DEPRECATED_PROJ_FILE)
62 for proj in proj_file.GetProjections():
63 if proj.EPSGCode() == epsg:
64 return proj
65 return None
66
67
68 class WMSLayer(BaseLayer):
69
70 def __init__(self, title, url):
71 """Initializes the WMSLayer.
72
73 title -- Title of this layer.
74 url -- URL of the WMS-Server wich must contain '?'
75
76 If an error occured, self.error_msg is a string describing
77 the problem(s). Else, self.error_msg is None.
78 """
79 BaseLayer.__init__(self, title, visible = True, projection = None)
80 self.url = url
81 self.bbox = None
82 self.latlonbbox = None
83 self.error_msg = None
84 self.layer_name = None
85 self.capabilities = None
86
87 # Change the cursor to demonstrate that we're busy but working
88 ThubanBeginBusyCursor()
89 self.capabilities = WMSCapabilities(url)
90 ThubanEndBusyCursor()
91
92 # name of the top layer of the remote map
93 foo = self.capabilities.getLayers()
94 if len(foo) == 0:
95 self.error_msg = _('No layers found in remote resource:\n'\
96 '%s') % url
97 return
98 top_layer = foo[0]
99 self.layer_name = top_layer
100
101 # first projection of the top layer
102 foo = self.capabilities.getLayerSRS(top_layer)
103 if len(foo) == 0:
104 self.error_msg = _('No LatLonBoundingBox found for top layer %s')\
105 % top_layer
106 return
107 top_srs = foo[0]
108
109 # BoundingBox of the top layer
110 bbox = self.capabilities.getLayerBBox(top_layer, top_srs)
111 if len(bbox) == 0:
112 self.error_msg = _('No BoundingBox found for layer %s and EPSG:')\
113 % (top_layer, top_srs)
114 return
115 self.bbox = (float(bbox['minx']),
116 float(bbox['miny']),
117 float(bbox['maxx']),
118 float(bbox['maxy']))
119
120 # LatLonBox of the top layer
121 bbox = self.capabilities.getLayerLatLonBBox(top_layer)
122 self.latlonbbox = (float(bbox['minx']),
123 float(bbox['miny']),
124 float(bbox['maxx']),
125 float(bbox['maxy']))
126
127 # get projection
128 p = epsg_code_to_projection(top_srs)
129 self.SetProjection(p)
130
131 if p is None:
132 self.error_msg = _('EPSG projection code %s not found!\n'\
133 'Setting projection to "None".\n'\
134 'Please set an appropriate projection yourself.'\
135 % top_srs)
136
137 # pre-determine the used format
138 self.wmsformat, self.format = \
139 self.calcFormat(self.capabilities.getFormats())
140 if self.wmsformat is None:
141 self.error_msg = \
142 _('No supported image format found in remote resource')
143 return
144
145 # get and set the title
146 self.SetTitle(self.capabilities.getTitle().encode('latin1', 'replace'))
147
148
149 def LatLongBoundingBox(self):
150 """Return the layer's bounding box in lat-lon.
151 """
152 return self.latlonbbox
153
154 def BoundingBox(self):
155 """Return the layer's bounding box in the intrinsic coordinate system.
156 """
157 return self.bbox
158
159
160 def getFormat(self, format):
161 """
162 Return the image format for the render engine
163
164 format -- format as returned by the WMS server
165
166 If no mapping was found, None is returned
167
168 An exception rule is implemented in order to not accept
169 image/wbmp or WBMP which refers to WAP bitmap format and is
170 not supported by the included render engine.
171 """
172 fmap = {'png' : "PNG",
173 'jpeg': "JPEG",
174 'jpg' : "JPEG",
175 'tif' : "TIFF",
176 'gif' : "GIF",
177 'wbmp': None,
178 'bmp' : "BMP"}
179
180 for f in fmap.keys():
181 if format.lower().find(f) > -1:
182 return fmap[f]
183 return None
184
185
186 def calcFormat(self, formats):
187 """
188 Calculate the preferred image format
189
190 formats -- list of formates as returned by the WMS server
191
192 The following priority is used:
193 - PNG
194 - JPEG
195 - TIFF
196 - GIF
197 - BMP
198
199 If no matching format was found, None, None will be returned.
200
201 An exception rule is implemented in order to not accept
202 image/wbmp or WBMP which refers to WAP bitmap format and is
203 not supported by the included render engine.
204 """
205 prio = ['jpeg', 'bmp']
206 for p in prio:
207 for f in formats:
208 if f.lower().find(p) > -1:
209 if f.lower().find('wbmp') == -1:
210 return f, self.getFormat(f)
211 return None, None
212
213
214 def GetMapImg(self, width, height, bbox):
215 bbox_dict = { 'minx': bbox[0], 'miny': bbox[1],
216 'maxx': bbox[2], 'maxy': bbox[3] }
217
218 # Change the cursor to demonstrate that we're busy but working
219 ThubanBeginBusyCursor()
220
221 wmsclient = WMSClient()
222
223 epsg_id = int(self.GetProjection().EPSGCode())
224
225 wms_response = wmsclient.getMap(self.url, self.wmsformat, width, height,
226 epsg_id, bbox_dict,
227 [self.layer_name], version = self.capabilities.getVersion())
228 ThubanEndBusyCursor()
229 return wms_response, self.format

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26