
Imports

    >>> from tests.utils import resource_file
    >>> from owslib.wmts import WebMapTileService

Fake a request to a WMTS Server using saved doc from
http://map1b.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi

    >>> xml = open(resource_file('eosdis-wmts-cap.xml'), 'rb').read()
    >>> wmts = WebMapTileService('url', version='1.0.0', xml=xml)

Test capabilities
-----------------
    >>> wmts.identification.type
    'OGC WMTS'
    >>> wmts.identification.version
    '1.0.0'
    >>> wmts.identification.title
    'NASA Global Image Browse Services for EOSDIS'
    >>> str.strip(wmts.identification.abstract)
    'Near real time imagery from multiple NASA instruments'
    >>> sorted(wmts.identification.keywords)
    ['Global', 'World']
    >>> wmts.identification.accessconstraints
    'none'
    >>> wmts.identification.fees
    'none'

Service Provider:

    >>> wmts.provider.name
    'National Aeronautics and Space Administration'

    >>> wmts.provider.url
    'http://earthdata.nasa.gov/'

Check contact info
    >>> wmts.provider.contact.name
    'Jeff Schmaltz'
    >>> wmts.provider.contact.position
    'Principal Support Scientist'

    # TODO: check if address can be duplicated
    >>> wmts.provider.contact.address
    'Goddard Space Flight Center'
    >>> wmts.provider.contact.region
    'Maryland'
    >>> wmts.provider.contact.email
    'support@earthdata.nasa.gov'
    >>> wmts.provider.contact.city
    'Greenbelt'
    >>> wmts.provider.contact.postcode
    '20771'
    >>> wmts.provider.contact.country
    'United States of America'

Test available content layers

    >>> sorted(list(wmts.contents))
    ['AIRS_CO_Total_Column_Day', 'AIRS_CO_Total_Column_Night', 'AIRS_Dust_Score', 'AIRS_Prata_SO2_Index_Day', 'AIRS_Prata_SO2_Index_Night', 'AIRS_Precipitation_Day', 'AIRS_Precipitation_Night', 'MODIS_Aqua_Aerosol', 'MODIS_Aqua_Brightness_Temp_Band31_Day', 'MODIS_Aqua_Brightness_Temp_Band31_Night', 'MODIS_Aqua_Cloud_Top_Pressure_Day', 'MODIS_Aqua_Cloud_Top_Pressure_Night', 'MODIS_Aqua_Cloud_Top_Temp_Day', 'MODIS_Aqua_Cloud_Top_Temp_Night', 'MODIS_Aqua_CorrectedReflectance_Bands721', 'MODIS_Aqua_CorrectedReflectance_TrueColor', 'MODIS_Aqua_Data_No_Data', 'MODIS_Aqua_Land_Surface_Temp_Day', 'MODIS_Aqua_Land_Surface_Temp_Night', 'MODIS_Aqua_Sea_Ice', 'MODIS_Aqua_Snow_Cover', 'MODIS_Aqua_SurfaceReflectance_Bands121', 'MODIS_Aqua_SurfaceReflectance_Bands143', 'MODIS_Aqua_SurfaceReflectance_Bands721', 'MODIS_Aqua_Water_Vapor_5km_Day', 'MODIS_Aqua_Water_Vapor_5km_Night', 'MODIS_Land_Water_Mask', 'MODIS_Terra_Aerosol', 'MODIS_Terra_Brightness_Temp_Band31_Day', 'MODIS_Terra_Brightness_Temp_Band31_Night', 'MODIS_Terra_Cloud_Top_Pressure_Day', 'MODIS_Terra_Cloud_Top_Pressure_Night', 'MODIS_Terra_Cloud_Top_Temp_Day', 'MODIS_Terra_Cloud_Top_Temp_Night', 'MODIS_Terra_CorrectedReflectance_Bands367', 'MODIS_Terra_CorrectedReflectance_Bands721', 'MODIS_Terra_CorrectedReflectance_TrueColor', 'MODIS_Terra_Data_No_Data', 'MODIS_Terra_Land_Surface_Temp_Day', 'MODIS_Terra_Land_Surface_Temp_Night', 'MODIS_Terra_Sea_Ice', 'MODIS_Terra_Snow_Cover', 'MODIS_Terra_SurfaceReflectance_Bands121', 'MODIS_Terra_SurfaceReflectance_Bands143', 'MODIS_Terra_SurfaceReflectance_Bands721', 'MODIS_Terra_Water_Vapor_5km_Day', 'MODIS_Terra_Water_Vapor_5km_Night', 'OMI_Absorbing_Aerosol_Optical_Depth', 'OMI_Aerosol_Index', 'OMI_Aerosol_Optical_Depth', 'OMI_Cloud_Pressure', 'OMI_SO2_Lower_Troposphere', 'OMI_SO2_Middle_Troposphere', 'OMI_SO2_Planetary_Boundary_Layer', 'OMI_SO2_Upper_Troposphere_and_Stratosphere']

Test TileMatrixSet variants
    # TODO: check individual values
    >>> sorted(wmts.tilematrixsets.keys())
    ['EPSG4326_1km', 'EPSG4326_250m', 'EPSG4326_2km', 'EPSG4326_500m']
    >>> wmts.tilematrixsets['EPSG4326_2km'].identifier
    'EPSG4326_2km'
    >>> wmts.tilematrixsets['EPSG4326_2km'].crs
    'urn:ogc:def:crs:OGC:1.3:CRS84'
    >>> sorted(wmts.tilematrixsets['EPSG4326_2km'].tilematrix.keys())
    ['0', '1', '2', '3', '4', '5']
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['0'].identifier
    '0'
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['0'].scaledenominator
    223382916.91738197
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['0'].topleftcorner
    (-180.0, 90.0)
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['0'].tilewidth
    512
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['0'].tileheight
    512
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['0'].matrixwidth
    2
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['0'].matrixheight
    1
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['5'].identifier
    '5'
    >>> int(wmts.tilematrixsets['EPSG4326_2km'].tilematrix['5'].scaledenominator)
    6980716
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['5'].topleftcorner
    (-180.0, 90.0)
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['5'].tilewidth
    512
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['5'].tileheight
    512
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['5'].matrixwidth
    40
    >>> wmts.tilematrixsets['EPSG4326_2km'].tilematrix['5'].matrixheight
    20
    >>> wmts.tilematrixsets['EPSG4326_250m'].identifier
    'EPSG4326_250m'
    >>> wmts.tilematrixsets['EPSG4326_250m'].crs
    'urn:ogc:def:crs:OGC:1.3:CRS84'
    >>> sorted(wmts.tilematrixsets['EPSG4326_250m'].tilematrix.keys())
    ['0', '1', '2', '3', '4', '5', '6', '7', '8']
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['0'].identifier
    '0'
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['0'].topleftcorner
    (-180.0, 90.0)
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['0'].tilewidth
    512
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['0'].tileheight
    512
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['0'].matrixwidth
    2
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['0'].matrixheight
    1
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['8'].identifier
    '8'
    >>> int(wmts.tilematrixsets['EPSG4326_250m'].tilematrix['8'].scaledenominator)
    872589
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['8'].topleftcorner
    (-180.0, 90.0)
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['8'].tilewidth
    512
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['8'].tileheight
    512
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['8'].matrixwidth
    320
    >>> wmts.tilematrixsets['EPSG4326_250m'].tilematrix['8'].matrixheight
    160

Test single item accessor

    >>> wmts['MODIS_Aqua_SurfaceReflectance_Bands121'].title
    'MODIS_Aqua_SurfaceReflectance_Bands121'

    # check something in the middle
    >>> wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].title
    'MODIS_Aqua_Cloud_Top_Temp_Night'
    >>> wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].boundingBoxWGS84
    (-180.0, -90.0, 180.0, 90.0)
    >>> wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].id
    'MODIS_Aqua_Cloud_Top_Temp_Night'
    >>> x = wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].styles
    >>> x == {'default': {'isDefault': True, 'title': 'default'}}
    True
    >>> wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].formats
    ['image/png']
    >>> wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].tilematrixsets
    ['EPSG4326_2km']
    >>> list(wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].tilematrixsetlinks.keys())
    ['EPSG4326_2km']
    >>> wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].tilematrixsetlinks['EPSG4326_2km']
    <TileMatrixSetLink: EPSG4326_2km, tilematrixlimits={...}>
    >>> wmts['MODIS_Aqua_Cloud_Top_Temp_Night'].tilematrixsetlinks['EPSG4326_2km'].tilematrixlimits
    {}

    >>> wmts['MODIS_Aqua_Water_Vapor_5km_Night'].title
    'MODIS_Aqua_Water_Vapor_5km_Night'

Expect a KeyError for invalid names

    >>> wmts['utterly bogus'].title
    Traceback (most recent call last):
    ...
    KeyError: 'No content named utterly bogus'

Test operations

    >>> sorted([op.name for op in wmts.operations])
    ['GetCapabilities', 'GetTile']

    >>> x = wmts.getOperationByName('GetTile').methods[0]
    >>> x.get('type')
    'Get'
    >>> x.get('url')
    'http://map1b.vis.earthdata.nasa.gov/wmts-geo/wmts.cgi?'
    >>> len(x.get('constraints'))
    1
    >>> cst = x.get('constraints')[0]
    >>> cst.name
    'GetEncoding'
    >>> cst.values
    ['KVP']

Test the gettile methods

    # TODO - more checks
    >>> try:
    ...     from PIL import Image
    ...     import cStringIO
    ... except:
    ...     pass
    ... else:
    ...     rq = wmts.buildTileRequest(layer='MODIS_Aqua_SurfaceReflectance_Bands121', tilematrix='0', row=0, column=0)
    ...     assert rq == 'SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=MODIS_Aqua_SurfaceReflectance_Bands121&STYLE=default&TILEMATRIXSET=EPSG4326_250m&TILEMATRIX=0&TILEROW=0&TILECOL=0&FORMAT=image%2Fjpeg'
    ...     tile000 = wmts.gettile(layer='MODIS_Aqua_SurfaceReflectance_Bands121', tilematrix='0', row=0, column=0)
    ...     im = cStringIO.StringIO(tile000.read())
    ...     image = Image.open(im)
    ...     assert image.size == (512, 512)
