OGC Web Service utility library: OWSLib
Connect to ORNL DAAC WMS service:
>>> from owslib.wms import WebMapService
>>> wms=WebMapService('http://webmap.ornl.gov/ogcbroker/wms', version='1.1.1')
>>> wms.identification.type
'OGC:WMS'
>>> wms.identification.title
'ORNL DAAC WMS Server'
List available layers:
>>> list(wms.contents)
['909_3329',
'909_3328',
...
'417_42',
'930_18']
Find details of a WMS layer:
% retrieve the layer object by id: '10006_1'
>>> layer = wms['10006_1']
% descriptive title of this layer
>>> layer.title
'AVHRR Landcover'
% bounding box (in WGS84) of this layer
>>>layer.boundingBoxWGS84
(-180.0, -90.0, 180.0, 90.0)
% coordinate reference systems (CRS) supported by this layer
>>> layer.crsOptions
['EPSG:54003', 'EPSG:4326', 'EPSG:54008', 'EPSG:54009', 'EPSG:9822']
% list styles supported by this layer
>>> layer.styles
{'default': {'legend': 'http://webmap.ornl.gov/ogcbroker/wms?version=1.1.1&service=WMS&request=GetLegendGraphic&layer=10006_1&format=image/png&STYLE=default',
'title': 'default'}}
Operations, their URLs, and available formats
% list all operations supported by ORNL DAAC WMS
>>> [op.name for op in wms.operations]
['GetCapabilities',
'GetMap',
'GetFeatureInfo',
'DescribeLayer',
'GetLegendGraphic',
'GetStyles']
% get GetMap operation object
>>> op = wms.getOperationByName('GetMap')
% methods supported by GetMap operation
>>> op.methods
{'Get': {'url': 'http://webmap.ornl.gov/ogcbroker/wms?'},
'Post': {'url': 'http://webmap.ornl.gov/ogcbroker/wms?'}}
% image formats supported by GetMap operation
>>>op.formatOptions
['image/png',
'image/gif',
'image/png; mode=24bit',
'image/jpeg',
'image/vnd.wap.wbmp',
'image/tiff',
'image/svg+xml']
Make a request for imagery
% send the request
>>> img = wms.getmap(
layers=['10006_1'],
styles=['default'],
srs='EPSG:4326',
bbox=(-180, -90, 180, 90),
size=(600, 300),
format='image/png',
transparent=True)
% save image in a local file
>>> out = open('/tmp/avhrr.png', 'wb')
>>> out.write(img.read())
>>> out.close()
% result
Make a request for legend image
% Note: OWSLib currently (as of 03/29/2013) doesn't fully support the request for layer legend image. Below is a walkaround in Python.
% get legend image URL
>>> legend_url = layer.styles['default']['legend']
% retrieve legend image
>>> import urllib
>>> urllib.urlretrieve(legend_url, '/tmp/avhrr_legend.png')
% result