Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@uprel
Forked from atlefren/WMTS.md
Created July 10, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uprel/7474854d84593cca8e12e9d74ec40920 to your computer and use it in GitHub Desktop.
Save uprel/7474854d84593cca8e12e9d74ec40920 to your computer and use it in GitHub Desktop.

How to calculate MaxResolution for WMTS given info in GetCapabilities

For using an arbitrary WMTS-server in OpenLayers you need two values:

  • The MaxResolution
  • A bounds object

Given a WMTS response with a TileMatrixSet defined as:

...
<TileMatrixSet>
    <ows:Identifier>EPSG:3575</ows:Identifier>
    <ows:SupportedCRS>urn:ogc:def:crs:EPSG::3575</ows:SupportedCRS>
    <TileMatrix>
        <ows:Abstract>The grid was not well-defined, the scale therefore assumes 1m per map unit.</ows:Abstract>
        <ows:Identifier>EPSG:3575:0</ows:Identifier>
        <ScaleDenominator>5.0601127250000006E8</ScaleDenominator>
        <TopLeftCorner>-1.8133594E7 1.07763E7</TopLeftCorner>
        <TileWidth>256</TileWidth>
        <TileHeight>256</TileHeight>
        <MatrixWidth>1</MatrixWidth>
        <MatrixHeight>1</MatrixHeight>
    </TileMatrix>
    <TileMatrix>
        ..

And the constant:

StandardizedRenderingPixelSize = 0.00028

We are able to calculate these properties, as follows:

###Getting initial values

From the first TileMatrix, we get the following properties:

ScaleDenominator (SD) = 5.0601127250000006E8

TopLeftCorner:

top  =  1.07763E7
left = -1.8133594E7

TileInfo:

TileWidth: 256
MatrixWidth: 1
TileHeight: 256
MatrixHeight: 1

###Formulas We know the following formulas:

WidthRealWorld = rightRW - leftRW

HeightRealWorld = top RW - bottom RW

WidthPixel = TileWidth * MatrixWidth

HeightPixel = TileHeight * MatrixHeight

MaxResolution = WidthRealWorld / WidthPixel 

ScaleDenominator = PixelWith / StandardizedRenderingPixelSize

PixelWith = WidthRealWorld / WidthPixel

###Deriving more formulas This gives:

ScaleDenominator = (WidthRealWorld / WidthPixel) / StandardizedRenderingPixelSize

Solve this for WidthRealWorld:

WidthRealWorld = ScaleDenominator * WidthPixel * StandardizedRenderingPixelSize

Solve this for right RW:

rightRW = WidthRealWorld + leftRW

Combine:

rightRW = (ScaleDenominator * WidthPixel * StandardizedRenderingPixelSize) +  leftRW

For bottom RW we have the same:

bottomRW = topRW - (ScaleDenominator * HeightPixel * StandardizedRenderingPixelSize) 

And for MaxScale

MaxResolution = (rightRW - leftRW) / WidthPixel = (topRW - bottomRW) / HeightPixel

In JavaScript:

function getBoundsAndMaxResForWMTS(scaleDenominator, topLeftCorner, tileWidth, matrixWidth, tileHeight, matrixHeight) {

    var standardizedRenderingPixelSize = 0.00028;

    var widthPixel = tileWidth * matrixWidth;
    var heightPixel = tileHeight * matrixHeight;

    var right = (scaleDenominator * widthPixel * standardizedRenderingPixelSize) +  topLeftCorner.x;
    var bottom = topLeftCorner.y - (scaleDenominator * heightPixel * standardizedRenderingPixelSize) ;
    
    var bounds = new OpenLayers.Bounds(topLeftCorner.x, bottom, right, topLeftCorner.y);
    var maxResolutionW = Math.round((right - topLeftCorner.x) / widthPixel);
    var maxResolutionH = Math.round((topLeftCorner.y - bottom) / heightPixel);

    if (maxResolutionW !== maxResolutionH) {
        throw new Error('Could not calculate ');
    }

    return {
        bounds: bounds,
        maxResolution: maxResolutionW
    };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment