Skip to content

Commit

Permalink
add lib to generate WMTS GetCapability
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Mar 29, 2012
1 parent b7b23f4 commit b432400
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tilecloud/lib/wmts.py
@@ -0,0 +1,69 @@
from math import ceil
from os.path import dirname, join

from bottle import jinja2_template

from tilecloud.lib.wmts_get_capabilities_template import wmts_get_capabilities_template

METERS_PER_UNIT = {
"feet": 3.28084,
"meters": 1,
"degrees": 111118.752,
"inch": 39.3700787
}


def to_wsg84(srs, x, y):
return transform(Proj(init=srs.lower()),
Proj(proj="latlong", datum="WGS84"), x, y)


def matrix_sets(tile_matrix_set):
sets = {}
tile_size = int(tile_matrix_set['tile_size'])
units = tile_matrix_set['units']
matrix_set = {"crs": tile_matrix_set['srs'].replace(':', '::'), "matrices": []}
for i, resolution in enumerate(tile_matrix_set['resolutions']):
col = int(ceil(((tile_matrix_set['bbox'][2] - tile_matrix_set['bbox'][0]) / tile_size) / resolution))
row = int(ceil(((tile_matrix_set['bbox'][3] - tile_matrix_set['bbox'][1]) / tile_size) / resolution))
if hasattr(tile_matrix_set, 'yorigin') and tile_matrix_set.yorigin == 'top':
maxy = tile_matrix_set['bbox'][1]
else:
maxy = tile_matrix_set['bbox'][1] + (row * tile_size * resolution)

matrix_set["matrices"].append({
"id": i,
"tilewidth": tile_size,
"tileheight": tile_size,
"matrixwidth": col,
"matrixheight": row,
"resolution": resolution,
# 0.000028 corresponds to 0.28 mm per pixel
"scale": resolution * METERS_PER_UNIT[units] / 0.00028,
"topleft": "%f %f" % (tile_matrix_set['bbox'][0], maxy)
})
sets[tile_matrix_set['name']] = matrix_set

return sets


def get_capabilities(layers, tile_matrix_set, wmts_gettile):
"""
layers is an array of dict that contains:
extension: the tiles extension like 'png'
dimension_key: the used dimension key
dimension_default: the default dimension value
dimension_values: the possible dimension value
matrix_set: the matrix set name
tile_matrix_set a dict that contants the tile matrix set definition:
name: the name of the tile matrix set
srs: projection like 'EPSG:21781'
units: units like 'meters'
resolutions: array of floats for used resolutions
bbox: array of floats, the use bbox where the tiles is generated
tiles_size: the sise of the tiles (int)
yorigin: 'top' if the tiles origin is at top
"""
return jinja2_template(wmts_get_capabilities_template,
layers=layers, matrix_sets=matrix_sets(tile_matrix_set),
wmts_gettile=wmts_gettile, tile_matrix_set=tile_matrix_set['name'])
60 changes: 60 additions & 0 deletions tilecloud/lib/wmts_get_capabilities_template.py
@@ -0,0 +1,60 @@
wmts_get_capabilities_template = """<?xml version="1.0" encoding="UTF-8"?>
<Capabilities version="1.0.0" 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://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd">
<ows:ServiceIdentification> </ows:ServiceIdentification>
<ows:ServiceProvider> </ows:ServiceProvider>
<ows:OperationsMetadata>
<ows:Operation name="GetTile">
<ows:DCP>
<ows:HTTP><ows:Get xlink:href="{{wmts_gettile}}" /></ows:HTTP>
</ows:DCP>
</ows:Operation>
</ows:OperationsMetadata>
<!-- <ServiceMetadataURL xlink:href="" /> -->
<Contents>
{% for layer in layers %}
<Layer>
<ows:Title>{{layer['name']}}</ows:Title>
<ows:Identifier>{{layer['name']}}</ows:Identifier>
<Style isDefault="true">
<ows:Identifier>default</ows:Identifier>
</Style>
<Format>{{layer['format']}}</Format>
<Dimension>
<ows:Identifier>{{layer["dimension_key"]}}</ows:Identifier>
<Default>{{layer["dimension_default"]}}</Default>
{% for value in layers["dimension_values"] %}
<Value>{{value}}</Value>
{% endfor %}
</Dimension>
<ResourceURL format="{{layer['mime_type']}}" resourceType="tile"
template="{{wmts_gettile}}/1.0.0/{{layer['name']}}/{style}/{{'{'}}{{layer['dimension_key']}}{{'}'}}/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.{{layer['extension']}}" />
<TileMatrixSetLink>
<TileMatrixSet>{{layer["metadata_matrix_set"]}}</TileMatrixSet>
</TileMatrixSetLink>
</Layer>
{% endfor %}
{% for key, matrix_set in matrix_sets.iteritems() %}
<TileMatrixSet>
<ows:Identifier>{{key}}</ows:Identifier>
<ows:SupportedCRS>urn:ogc:def:crs:{{matrix_set["crs"]}}</ows:SupportedCRS>
{% for matrix in matrix_set["matrices"] %}
<TileMatrix>
<ows:Identifier>{{matrix["id"]}}</ows:Identifier>
<ScaleDenominator>{{matrix["scale"]}}</ScaleDenominator> <!-- resolution: {{matrix["resolution"]}} -->
<TopLeftCorner>{{matrix["topleft"]}}</TopLeftCorner>
<TileWidth>{{matrix["tilewidth"]}}</TileWidth>
<TileHeight>{{matrix["tileheight"]}}</TileHeight>
<MatrixWidth>{{matrix["matrixwidth"]}}</MatrixWidth>
<MatrixHeight>{{matrix["matrixheight"]}}</MatrixHeight>
</TileMatrix>
{% endfor %}
</TileMatrixSet>
{% endfor %}
</Contents>
</Capabilities>
"""

0 comments on commit b432400

Please sign in to comment.