Changeset 526
- Timestamp:
- 10/18/06 20:44:59
- Files:
-
- OWSLib/trunk/owslib/wms.py (modified) (12 diffs)
- OWSLib/trunk/tests/runalltests.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
OWSLib/trunk/owslib/wms.py
r513 r526 1 1 # -*- coding: ISO-8859-15 -*- 2 2 # ============================================================================= 3 # Copyright (c) 2004 Sean C. Gillies3 # Copyright (c) 2004, 2006 Sean C. Gillies 4 4 # Copyright (c) 2005 Nuxeo SARL <http://nuxeo.com> 5 5 # … … 24 24 # ============================================================================= 25 25 26 """ 27 API for Web Map Service (WMS) methods and metadata. 28 29 Currently supports only version 1.1.1 of the WMS protocol. 30 """ 31 26 32 import cgi 27 import sys 28 from urllib import urlencode, urlopen 29 30 from owslib.etree import etree 31 32 class WMSError(Exception): 33 """Base class for WMS module errors 34 """ 35 36 def __init__(self, message): 37 """Initialize a WMS Error""" 38 self.message = message 39 40 def toxml(self): 41 """Serialize into a WMS Service Exception XML 42 """ 43 preamble = '<?xml version="1.0" ?>' 44 report_elem = etree.Element('ServiceExceptionReport') 45 report_elem.attrib['version'] = '1.1.1' 46 # Service Exception 47 exception_elem = etree.Element('ServiceException') 48 exception_elem.text = self.message 49 report_elem.append(exception_elem) 50 return preamble + etree.tostring(report_elem) 33 from urllib import urlencode 34 from urllib2 import urlopen 35 36 from etree import etree 51 37 52 38 … … 70 56 self.version = version 71 57 self._capabilities = None 58 # initialize from saved capability document 72 59 if xml: 73 60 reader = WMSCapabilitiesReader(self.version) … … 82 69 83 70 def getcapabilities(self): 84 """Request and return capabilities document from the WMS.""" 71 """Request and return capabilities document from the WMS as a 72 file-like object.""" 85 73 reader = WMSCapabilitiesReader(self.version) 86 74 u = urlopen(reader.capabilities_url(self.url)) … … 97 85 exceptions='application/vnd.ogc.se_xml', 98 86 method='Get'): 99 """Request and return an image from the WMS.""" 87 """Request and return an image from the WMS as a file-like object. 88 89 Parameters 90 ---------- 91 layers : list 92 List of content layer names. 93 styles : list 94 Optional list of named styles, must be the same length as the 95 layers list. 96 srs : string 97 A spatial reference system identifier. 98 bbox : tuple 99 (left, bottom, right, top) in srs units. 100 format : string 101 Output image format such as 'image/jpeg'. 102 size : tuple 103 (width, height) in pixels. 104 transparent : bool 105 Optional. Transparent background if True. 106 bgcolor : string 107 Optional. Image background color. 108 method : string 109 Optional. HTTP DCP method name: Get or Post. 110 111 Example 112 ------- 113 >>> img = wms.getmap(layers=['global_mosaic'], 114 ... styles=['visual'], 115 ... srs='EPSG:4326', 116 ... bbox=(-112,36,-106,41), 117 ... format='image/jpeg', 118 ... size=(300,250), 119 ... transparent=True, 120 ... ) 121 >>> out = open('example.jpg', 'wb') 122 >>> out.write(img.read()) 123 >>> out.close() 124 125 """ 100 126 md = self.capabilities 101 127 base_url = md.getOperationByName('GetMap').methods[method]['url'] … … 129 155 130 156 # check for service exceptions, and return 131 if u.info() .gettype()== 'application/vnd.ogc.se_xml':157 if u.info()['Content-Type'] == 'application/vnd.ogc.se_xml': 132 158 se_xml = u.read() 133 159 se_tree = etree.fromstring(se_xml) … … 136 162 return u 137 163 164 def getfeatureinfo(self): 165 raise NotImplementedError 166 138 167 139 168 class ServiceMetadata(object): … … 146 175 """Initialize from an element tree.""" 147 176 self._root = infoset.getroot() 148 #print >> sys.stderr, self._root149 177 # properties 150 178 self.service = self._root.find('Service/Name').text … … 196 224 197 225 def __init__(self, elem, parent): 198 """ ."""226 """Initialize.""" 199 227 self.name = elem.find('Name').text 200 228 self.title = elem.find('Title').text … … 231 259 232 260 # styles 233 self.styles = dict([(s.find('Name').text, {'title': s.find('Title').text}) for s in elem.findall('Style')]) 261 self.styles = dict([(s.find('Name').text, 262 {'title': s.find('Title').text}) \ 263 for s in elem.findall('Style')] 264 ) 265 234 266 235 267 class OperationMetadata: … … 248 280 methods.append((verb.tag, {'url': url})) 249 281 self.methods = dict(methods) 250 282 283 284 # Deprecated classes follow 285 # TODO: remove 286 251 287 class WMSCapabilitiesInfoset: 252 288 """High-level container for WMS Capabilities based on lxml.etree … … 390 426 raise ValueError("String must be of type string, not %s" % type(st)) 391 427 return WMSCapabilitiesInfoset(etree.fromstring(st)) 392 428 429 430 class WMSError(Exception): 431 """Base class for WMS module errors 432 """ 433 434 def __init__(self, message): 435 """Initialize a WMS Error""" 436 self.message = message 437 438 def toxml(self): 439 """Serialize into a WMS Service Exception XML 440 """ 441 preamble = '<?xml version="1.0" ?>' 442 report_elem = etree.Element('ServiceExceptionReport') 443 report_elem.attrib['version'] = '1.1.1' 444 # Service Exception 445 exception_elem = etree.Element('ServiceException') 446 exception_elem.text = self.message 447 report_elem.append(exception_elem) 448 return preamble + etree.tostring(report_elem) 449 450 451 OWSLib/trunk/tests/runalltests.py
r480 r526 21 21 # ============================================================================= 22 22 23 import doctest 24 import glob 23 25 import os, sys 24 26 import unittest … … 43 45 suite.addTest(load(m)) 44 46 47 # Add doctests 48 for file in glob.glob('*.txt'): 49 suite.addTest(doctest.DocFileSuite(file)) 50 45 51 # ============================================================================= 46 52 # Run
