Changeset 526

Show
Ignore:
Timestamp:
10/18/06 20:44:59
Author:
seang
Message:

add example to WebMapService?.getmap docstring, and add doctests into the master test runner

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OWSLib/trunk/owslib/wms.py

    r513 r526  
    11# -*- coding: ISO-8859-15 -*- 
    22# ============================================================================= 
    3 # Copyright (c) 2004 Sean C. Gillies 
     3# Copyright (c) 2004, 2006 Sean C. Gillies 
    44# Copyright (c) 2005 Nuxeo SARL <http://nuxeo.com> 
    55# 
     
    2424# ============================================================================= 
    2525 
     26""" 
     27API for Web Map Service (WMS) methods and metadata. 
     28 
     29Currently supports only version 1.1.1 of the WMS protocol. 
     30""" 
     31 
    2632import 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) 
     33from urllib import urlencode 
     34from urllib2 import urlopen 
     35 
     36from etree import etree 
    5137 
    5238 
     
    7056        self.version = version 
    7157        self._capabilities = None 
     58        # initialize from saved capability document 
    7259        if xml: 
    7360            reader = WMSCapabilitiesReader(self.version) 
     
    8269             
    8370    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.""" 
    8573        reader = WMSCapabilitiesReader(self.version) 
    8674        u = urlopen(reader.capabilities_url(self.url)) 
     
    9785               exceptions='application/vnd.ogc.se_xml', 
    9886               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        """ 
    100126        md = self.capabilities 
    101127        base_url = md.getOperationByName('GetMap').methods[method]['url'] 
     
    129155 
    130156        # 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': 
    132158            se_xml = u.read() 
    133159            se_tree = etree.fromstring(se_xml) 
     
    136162        return u 
    137163 
     164    def getfeatureinfo(self): 
     165        raise NotImplementedError 
     166         
    138167         
    139168class ServiceMetadata(object): 
     
    146175        """Initialize from an element tree.""" 
    147176        self._root = infoset.getroot() 
    148         #print >> sys.stderr, self._root 
    149177        # properties 
    150178        self.service = self._root.find('Service/Name').text 
     
    196224 
    197225    def __init__(self, elem, parent): 
    198         """.""" 
     226        """Initialize.""" 
    199227        self.name = elem.find('Name').text 
    200228        self.title = elem.find('Title').text 
     
    231259 
    232260        # 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 
    234266 
    235267class OperationMetadata: 
     
    248280            methods.append((verb.tag, {'url': url})) 
    249281        self.methods = dict(methods) 
    250          
     282        
     283 
     284# Deprecated classes follow 
     285# TODO: remove 
     286 
    251287class WMSCapabilitiesInfoset: 
    252288    """High-level container for WMS Capabilities based on lxml.etree 
     
    390426            raise ValueError("String must be of type string, not %s" % type(st)) 
    391427        return WMSCapabilitiesInfoset(etree.fromstring(st)) 
    392      
     428 
     429 
     430class 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  
    2121# ============================================================================= 
    2222 
     23import doctest 
     24import glob 
    2325import os, sys 
    2426import unittest 
     
    4345    suite.addTest(load(m)) 
    4446 
     47# Add doctests 
     48for file in glob.glob('*.txt'): 
     49    suite.addTest(doctest.DocFileSuite(file)) 
     50     
    4551# ============================================================================= 
    4652# Run