
Imports

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

Fake a request to a WMTS Server using saved doc from
http://server.caris.com/spatialfusionserver/services/ows/wmts/World/1.0.0/WMTSCapabilities.xml

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

Test capabilities
-----------------
    >>> wmts.identification.type
    'WMTS'
    >>> wmts.identification.version
    '1.0.0'
    >>> wmts.identification.title

    >>> wmts.identification.abstract

    >>> wmts.identification.keywords
    []
    >>> wmts.identification.accessconstraints
    'None'
    >>> wmts.identification.fees
    'conditions unknown'

Test service metadata URL (RESTful API)
    >>> wmts.serviceMetadataURL
    'http://server.caris.com/spatialfusionserver/services/ows/wmts/World/1.0.0/WMTSCapabilities.xml'

Service Provider:

    >>> wmts.provider.name
    'CARIS'

    >>> wmts.provider.url

Check contact info
    >>> wmts.provider.contact.name
    'Andy Hoggarth'
    >>> wmts.provider.contact.position
    'Marketing & Sales Manager'
    >>> wmts.provider.contact.phone
    '1 (506) 459-3849'
    >>> wmts.provider.contact.address
    '115 Waggoners Lane'
    >>> wmts.provider.contact.region
    'New Brunswick'
    >>> wmts.provider.contact.email
    'info@caris.com'
    >>> wmts.provider.contact.city
    'Fredericton'
    >>> wmts.provider.contact.postcode
    'E3B 2L4'
    >>> wmts.provider.contact.country
    'Canada'

Test available content layers

    >>> sorted(list(wmts.contents))
    ['Ocean', 'World']

Test TileMatrixSet variants

    >>> sorted(wmts.tilematrixsets.keys())
    ['GlobalCRS84Scale', 'GoogleMapsCompatible']
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].identifier
    'GoogleMapsCompatible'
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].crs
    'urn:ogc:def:crs:EPSG:6.18:3:3857'
    >>> sorted(wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix.keys())
    ['0', '1', '10', '11', '12', '13', '14', '15', '16', '17', '2', '3', '4', '5', '6', '7', '8', '9']
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['0'].identifier
    '0'
    >>> int(wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['0'].scaledenominator)
    559082264
    >>> cast_tuple_int_list(wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['0'].topleftcorner)
    [-20037508, -20037508]
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['0'].tilewidth
    256
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['0'].tileheight
    256
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['0'].matrixwidth
    1
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['0'].matrixheight
    1
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['17'].identifier
    '17'
    >>> int(wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['17'].scaledenominator)
    4265
    >>> cast_tuple_int_list(wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['17'].topleftcorner)
    [-20037508, -20037508]
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['17'].tilewidth
    256
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['17'].tileheight
    256
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['17'].matrixwidth
    131072
    >>> wmts.tilematrixsets['GoogleMapsCompatible'].tilematrix['17'].matrixheight
    131072

    >>> wmts.tilematrixsets['GlobalCRS84Scale'].identifier
    'GlobalCRS84Scale'
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].crs
    'urn:ogc:def:crs:OGC:1.3:CRS84'
    >>> sorted(wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix.keys())
    ['0', '1', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '2', '20', '3', '4', '5', '6', '7', '8', '9']
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['0'].identifier
    '0'
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['0'].scaledenominator
    500000000.0
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['0'].topleftcorner
    (90.0, -180.0)
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['0'].tilewidth
    256
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['0'].tileheight
    256
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['0'].matrixwidth
    2
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['0'].matrixheight
    1
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['20'].identifier
    '20'
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['20'].scaledenominator
    100.0
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['20'].topleftcorner
    (90.0, -180.0)
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['20'].tilewidth
    256
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['20'].tileheight
    256
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['20'].matrixwidth
    2097152
    >>> wmts.tilematrixsets['GlobalCRS84Scale'].tilematrix['20'].matrixheight
    1048576

Test single item accessor

    >>> wmts['World'].title
    'World'
    >>> cast_tuple_int_list(wmts['World'].boundingBoxWGS84)
    [-179, -90, 179, 83]
    >>> wmts['World'].id
    'World'
    >>> wmts['World'].styles
    {'default': {'isDefault': True}}
    >>> wmts['World'].formats
    ['image/png']
    >>> sorted(wmts['World'].tilematrixsets)
    ['GlobalCRS84Scale', 'GoogleMapsCompatible']
    >>> sorted(wmts['World'].tilematrixsetlinks.keys())
    ['GlobalCRS84Scale', 'GoogleMapsCompatible']
    >>> wmts['World'].infoformats
    ['application/gml+xml; version=3.1']
    >>> wmts['World'].resourceURLs[0] == {'resourceType': 'tile', 'template': 'http://server.caris.com/spatialfusionserver/services/ows/wmts/World/World/default/{tileMatrixSet}/{tileMatrix}/{tileRow}/{tileCol}.png', 'format': 'image/png'}
    True
    >>> wmts['World'].resourceURLs[1] == {'resourceType': 'FeatureInfo', 'template': 'http://server.caris.com/spatialfusionserver/services/ows/wmts/World/World/default/{tileMatrixSet}/{tileMatrix}/{tileRow}/{tileCol}/{j}/{i}.xml', 'format': 'application/gml+xml; version=3.1'}
    True

    >>> wmts['Ocean'].title
    'Ocean'
    >>> cast_tuple_int_list(wmts['Ocean'].boundingBoxWGS84)
    [-179, -85, 179, 89]
    >>> wmts['Ocean'].id
    'Ocean'
    >>> wmts['Ocean'].styles
    {'default': {'isDefault': True}}
    >>> wmts['Ocean'].formats
    ['image/png']
    >>> sorted(wmts['Ocean'].tilematrixsets)
    ['GlobalCRS84Scale', 'GoogleMapsCompatible']
    >>> sorted(wmts['Ocean'].tilematrixsetlinks.keys())
    ['GlobalCRS84Scale', 'GoogleMapsCompatible']
    >>> wmts['Ocean'].infoformats
    ['application/gml+xml; version=3.1']
    >>> wmts['Ocean'].resourceURLs[0] == {'resourceType': 'tile', 'template': 'http://server.caris.com/spatialfusionserver/services/ows/wmts/World/Ocean/default/{tileMatrixSet}/{tileMatrix}/{tileRow}/{tileCol}.png', 'format': 'image/png'}
    True
    >>> wmts['Ocean'].resourceURLs[1] == {'resourceType': 'FeatureInfo', 'template': 'http://server.caris.com/spatialfusionserver/services/ows/wmts/World/Ocean/default/{tileMatrixSet}/{tileMatrix}/{tileRow}/{tileCol}/{j}/{i}.xml', 'format': 'application/gml+xml; version=3.1'}
    True

Test themes
    >>> list(wmts.themes.keys())
    ['Earth']
    >>> wmts.themes['Earth'].title
    'Earth'
    >>> wmts.themes['Earth'].identifier
    'Earth'
    >>> wmts.themes['Earth'].abstract

    >>> sorted(wmts.themes['Earth'].layerRefs)
    ['Ocean', 'World']



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', 'GetFeatureInfo', 'GetTile']

    >>> x = wmts.getOperationByName('GetFeatureInfo').methods
    >>> x == [{'type': 'Get', 'url': 'http://server.caris.com/spatialfusionserver/services/ows/wmts/World', 'constraints': []}]
    True

    >>> x = wmts.getOperationByName('GetTile').methods
    >>> x == [{'type' : 'Get', 'url': 'http://server.caris.com/spatialfusionserver/services/ows/wmts/World', 'constraints': []}]
    True

    >>> x = wmts.getOperationByName('GetCapabilities').methods
    >>> x == [{'type' : 'Get', 'url': 'http://server.caris.com/spatialfusionserver/services/ows/wmts/World', 'constraints': []}]
    True


Test the gettile methods
    >>> try:
    ...     from io import BytesIO as ImageIO
    ... except ImportError:
    ...     from cStringIO import StringIO as ImageIO
    >>> try:
    ...     from PIL import Image
    ... except:
    ...     pass
    ... else:
    ...     rq = wmts.buildTileRequest(layer='World', tilematrix='0', row=0, column=0)
    ...     assert rq == 'SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=World&STYLE=default&TILEMATRIXSET=GlobalCRS84Scale&TILEMATRIX=0&TILEROW=0&TILECOL=0&FORMAT=image%2Fpng'
    ...     tile000 = wmts.gettile(layer='World', tilematrix='0', row=0, column=0)
    ...     im = ImageIO(tile000.read())
    ...     image = Image.open(im)
    ...     assert image.size == (256, 256)

