<?xml version="1.0"?>
<Capabilities xmlns="http://www.opengis.net/wmts/1.0" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gml="http://www.opengis.net/gml" xsi:schemaLocation="http://www.opengis.net/wmts/1.0 http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd" version="1.0.0">
  <ows:ServiceIdentification>
    <ows:Title>{{service.title}}</ows:Title>
    <ows:Abstract>{{service.abstract}}</ows:Abstract>
    {{if service.keyword_list and len(service.keyword_list) > 0}}
    <ows:Keywords>
    {{for list in service.keyword_list}}
      {{py: kw=bunch(default='', **list)}}
      {{for keyword in kw.keywords}}
        <ows:Keyword{{if kw.vocabulary}} vocabulary="{{kw.vocabulary}}"{{endif}}>{{keyword}}</ows:Keyword>
      {{endfor}}
    {{endfor}}
    </ows:Keywords>
    {{endif}}
    <ows:ServiceType>OGC WMTS</ows:ServiceType>
    <ows:ServiceTypeVersion>1.0.0</ows:ServiceTypeVersion>
    <ows:Fees>{{service.get('fees', 'none')}}</ows:Fees>
    <ows:AccessConstraints>{{service.get('access_constraints', 'none')}}</ows:AccessConstraints>
  </ows:ServiceIdentification>
{{if service.contact}}
{{py:service.contact = bunch(default='', **service.contact)}}
  <ows:ServiceProvider>
    <ows:ProviderName>{{service.contact.organization}}</ows:ProviderName>
    <ows:ProviderSite xlink:href="{{service.online_resource}}"/>
    <ows:ServiceContact>
      <ows:IndividualName>{{service.contact.person}}</ows:IndividualName>
      <ows:PositionName>{{service.contact.position}}</ows:PositionName>
      <ows:ContactInfo>
        <ows:Phone>
          <ows:Voice>{{service.contact.phone}}</ows:Voice>
          <ows:Facsimile>{{service.contact.fax}}</ows:Facsimile>
        </ows:Phone>
        <ows:Address>
          <ows:DeliveryPoint>{{service.contact.address}}</ows:DeliveryPoint>
          <ows:City>{{service.contact.city}}</ows:City>
          <ows:AdministrativeArea>{{service.contact.state}}</ows:AdministrativeArea>
          <ows:PostalCode>{{service.contact.postcode}}</ows:PostalCode>
          <ows:Country>{{service.contact.country}}</ows:Country>
          <ows:ElectronicMailAddress>{{service.contact.email}}</ows:ElectronicMailAddress>
        </ows:Address>
      </ows:ContactInfo>
    </ows:ServiceContact>
  </ows:ServiceProvider>
{{endif}}
  <ows:OperationsMetadata>
    <ows:Operation name="GetCapabilities">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:href="{{service.url}}?">
            <ows:Constraint name="GetEncoding">
              <ows:AllowedValues>
                <ows:Value>{{if restful}}RESTful{{else}}KVP{{endif}}</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Get>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
    <ows:Operation name="GetTile">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:href="{{service.url}}?">
            <ows:Constraint name="GetEncoding">
              <ows:AllowedValues>
                <ows:Value>{{if restful}}RESTful{{else}}KVP{{endif}}</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Get>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
    <ows:Operation name="GetFeatureInfo">
      <ows:DCP>
        <ows:HTTP>
          <ows:Get xlink:href="{{service.url}}?">
            <ows:Constraint name="GetEncoding">
              <ows:AllowedValues>
                <ows:Value>{{if restful}}RESTful{{else}}KVP{{endif}}</ows:Value>
              </ows:AllowedValues>
            </ows:Constraint>
          </ows:Get>
        </ows:HTTP>
      </ows:DCP>
    </ows:Operation>
  </ows:OperationsMetadata>
  <Contents>
{{for layer in layers}}
    <Layer>
      <ows:Title>{{layer.title}}</ows:Title>
      <ows:Abstract></ows:Abstract>
      <ows:WGS84BoundingBox>
        <ows:LowerCorner>{{layer.md['extent'].llbbox[0]}} {{layer.md['extent'].llbbox[1]}}</ows:LowerCorner>
        <ows:UpperCorner>{{layer.md['extent'].llbbox[2]}} {{layer.md['extent'].llbbox[3]}}</ows:UpperCorner>
      </ows:WGS84BoundingBox>
      <ows:Identifier>{{layer.name}}</ows:Identifier>
      <Style isDefault="true">
        <ows:Identifier>default</ows:Identifier>
        {{if legendurls[layer.name]}}
        <LegendURL
          format="image/png"
          xlink:href="{{legendurls[layer.name]}}"
        />
        {{endif}}
      </Style>
      <Format>image/{{layer.format}}</Format>
      {{if layer.queryable}}
      {{for format in info_formats.values()}}
      <InfoFormat>{{ format }}</InfoFormat>
      {{endfor}}
      {{endif}}
      {{for dimension in layer.dimensions}}
      <Dimension>
        <ows:Identifier>{{ dimension_keys[dimension] }}</ows:Identifier>
        <Default>{{ layer.dimensions[dimension].default }}</Default>
        {{for value in layer.dimensions[dimension]}}
        <Value>{{ value }}</Value>
        {{endfor}}
      </Dimension>
      {{endfor}}
      {{for grid in layer.grids}}
      <TileMatrixSetLink>
        <TileMatrixSet>{{grid.name}}</TileMatrixSet>
      </TileMatrixSetLink>
      {{endfor}}
      {{if restful}}
      <ResourceURL format="image/{{layer.format}}" resourceType="tile"
        template="{{format_resource_template(layer, resource_template, service)}}"/>

      {{if layer.queryable}}
      {{for suffix, mimetype in info_formats.items()}}
      <ResourceURL format="{{mimetype}}" resourceType="FeatureInfo" template="{{format_info_resource_template(layer, fi_resource_template, suffix, service)}}"/>
      {{endfor}}
      {{endif}}

      {{endif}}
    </Layer>
{{endfor}}
{{for tile_matrix_set in tile_matrix_sets}}
    <TileMatrixSet>
      <ows:Identifier>{{tile_matrix_set.name}}</ows:Identifier>
      <ows:SupportedCRS>urn:ogc:def:crs:{{tile_matrix_set.srs_name}}</ows:SupportedCRS>
  {{for matrix in tile_matrix_set}}
      <TileMatrix>
        <ows:Identifier>{{matrix.identifier}}</ows:Identifier>
        <ScaleDenominator>{{matrix.scale_denom}}</ScaleDenominator>
        <TopLeftCorner>{{matrix.topleft[0]}} {{matrix.topleft[1]}}</TopLeftCorner>
        <TileWidth>{{matrix.tile_size[0]}}</TileWidth>
        <TileHeight>{{matrix.tile_size[1]}}</TileHeight>
        <MatrixWidth>{{matrix.grid_size[0]}}</MatrixWidth>
        <MatrixHeight>{{matrix.grid_size[1]}}</MatrixHeight>
      </TileMatrix>
  {{endfor}}
    </TileMatrixSet>
{{endfor}}
  </Contents>
{{if restful}}
  <ServiceMetadataURL xlink:href="{{service.url}}/1.0.0/WMTSCapabilities.xml"/>
{{endif}}
</Capabilities>
