Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WMTS backgroudLayer with API key #586

Open
ZedeN1 opened this issue Oct 22, 2024 · 7 comments
Open

WMTS backgroudLayer with API key #586

ZedeN1 opened this issue Oct 22, 2024 · 7 comments

Comments

@ZedeN1
Copy link

ZedeN1 commented Oct 22, 2024

I would like to add UK OS Maps WMTS layer as a background map. However, my request needs to include an API key as documented here.

This is the correct format of the get request that I am trying to achieve
https://api.os.uk/maps/raster/v1/wmts?key=xxxxxx&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=Light_3857&STYLE=default&FORMAT=image/png&TILEMATRIXSET=EPSG:3857&TILEMATRIX=EPSG:3857:16&TILEROW=21779&TILECOL=32721

Instead, I am getting:
https://api.os.uk/maps/raster/v1/wmts?layer=Light_3857&style=default&tilematrixset=EPSG:3857&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/png&TileMatrix=EPSG:3857:16&TileCol=32721&TileRow=21779

Is there a way to add another custom parameter to the request URL?

My themesConfig.json backgroundLayers entry:

      {
        "name": "Light_3857",
        "type": "wmts",
        "url": "https://api.os.uk/maps/raster/v1/wmts?key=xxxxxx&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=Light_3857&STYLE=default&FORMAT=image/png&TILEMATRIXSET={TileMatrixSet}&TILEMATRIX=EPSG:3857:{TileMatrix}&TILEROW={TileRow}&TILECOL={TileCol}",
        "format": "image/png",
        "requestEncoding": "KVP",
        "tileMatrixPrefix": "EPSG:3857:",
        "tileMatrixSet": "EPSG:3857",
        "originX": -20037508.342787,
        "originY": 20037508.342787,
        "projection": "EPSG:3857",
        "tileSize": [
          256,
          256
        ],
        "style": "default",
        "bbox": {
          "crs": "EPSG:4326",
          "bounds": [
            -10.764179999999964,
            49.52842300000003,
            1.9134115552,
            61.3311510086
          ]
        },
        "resolutions": [
          1222.9924523698928,
          611.4962261850814,
          305.7481130924087,
          152.87405654633633,
          76.43702827303586,
          38.21851413651793,
          19.109257068258966,
          9.554628534129483,
          4.777314267197037,
          2.3886571335985134,
          1.1943285666669667,
          0.5971642834657753,
          0.2985821416005962,
          0.1492910708002979
        ],
        "thumbnail": "Light_3857.png"
      },
@manisandro
Copy link
Member

Can you try whether not truncating the URL here [1] works? Possibly the code should also be adapted to remove know query paramters (REQUEST, VERSION, etc) from the URL and keep the others.

[1] https://github.com/qgis/qwc2/blob/f8fd74eeb8834a72614dbbb6669d3ff5f85d7cb9/components/map/layers/WMTSLayer.js#L16

@ZedeN1
Copy link
Author

ZedeN1 commented Oct 22, 2024

I am using qwc-docker, how can I find the file?

@manisandro
Copy link
Member

You'll need to compile a custom viewer, see https://qwc-services.github.io/master/configuration/ViewerConfiguration/#custom-viewer

@ZedeN1
Copy link
Author

ZedeN1 commented Oct 22, 2024

Thank you @manisandro

Managed to get it to work by adding "key": "xxxxx" to themesConfig.json and changing WMTSLayer.js to be:

function createWMTSSource(options) {
    const urls = getWMSURLs(Array.isArray(options.url) ? options.url : [options.url]).map((url) => {
        // Initialize the base URL
        let updatedUrl = url;

        // Append rev parameter if it exists
        if (options.rev) {
            updatedUrl += `?${options.rev}`; // Start with a question mark for rev
        }

        // Check if a key is provided and append it to the URL
        if (options.key) {
            // If rev is present, append key using "&"; otherwise, use "?"
            updatedUrl += options.rev ? `&key=${options.key}` : `?key=${options.key}`;
        }

        return updatedUrl;
    });

Had an additional issue with the resolutions, as wmts_config_generator.py could not handle the minimum zoom starting from EPSG:3857:7 instead of EPSG:3857:1.

Also removed the high zooms that require subscription.

      {
        "name": "Light_3857",
        "type": "wmts",
        "url": "https://api.os.uk/maps/raster/v1/wmts?key=xxxxxx&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=Light_3857&STYLE=default&FORMAT=image/png&TILEMATRIXSET={TileMatrixSet}&TILEMATRIX=EPSG:3857:{TileMatrix}&TILEROW={TileRow}&TILECOL={TileCol}",
        "key": "xxxxxx",
// trimmed for clarity
        "resolutions": [
          156543.03390334628,
          78271.51695167314,
          39135.75847583657,
          19567.879237918284,
          9783.939618959142,
          4891.969809479571,
          2445.9849047397856,
          1222.9924523698928,
          611.4962261850814,
          305.7481130924087,
          152.87405654633633,
          76.43702827303586,
          38.21851413651793,
          19.109257068258966,
          9.554628534129483,
          4.777314267197037,
          2.3886571335985134
        ],
        "thumbnail": "Light_3857.png"
      },

Not sure if this can be implemented in https://github.com/qgis/qwc2/ ?

@manisandro
Copy link
Member

Can you elaborate on

wmts_config_generator.py could not handle the minimum zoom starting from EPSG:3857:7 instead of EPSG:3857:1.

?

@ZedeN1
Copy link
Author

ZedeN1 commented Oct 23, 2024

The Ordnance Survey EPSG:3857 Tile Matrix Set starts at Zoom level 7 and not 0. Attached at the bottom is the relevant snippet from getcapabilities.

Running:
python3 wmts_config_generator.py "https://api.os.uk/maps/raster/v1/wmts?service=WMTS&request=getcapabilities&key=xxxxxx" "Light_3857" "EPSG:3857"

Produces:

  "resolutions": [
    1222.9924523698928,
    611.4962261850814,
    305.7481130924087,
    152.87405654633633,
    76.43702827303586,
    38.21851413651793,
    19.109257068258966,
    9.554628534129483,
    4.777314267197037,
    2.3886571335985134,
    1.1943285666669667,
    0.5971642834657753,
    0.2985821416005962,
    0.1492910708002979
  ],

If I use those resolutions in my themesConfig.json as backgroundLayers. All the TileMatrix=EPSG:3857:{TileMatrix} requests would be offset by 7. Ie instead of requesting TileMatrix=EPSG:3857:16, I would be requesting TileMatrix=EPSG:3857:9.

I need to extrapolate and add additional resolutions for zoom levels 0-6 even if those are not available in getcapabilities:

        "resolutions": [
          156543.03390334628,
          78271.51695167314,
          39135.75847583657,
          19567.879237918284,
          9783.939618959142,
          4891.969809479571,
          2445.9849047397856,
          1222.9924523698928,
          611.4962261850814,
\\ etc...

I hope this makes sense.


Relevant parts from getcapabilities TileMatrixSet below.

TileMatrixSet from getcapabilities
        <TileMatrixSet>
            <ows:Title>TileMatrix for EPSG:3857 using 0.28mm</ows:Title>
            <ows:Abstract>The tile matrix set that has scale values calculated based on the dpi defined by OGC specification (dpi assumes 0.28mm as the physical distance of a pixel).</ows:Abstract>
            <ows:Identifier>EPSG:3857</ows:Identifier>
            <ows:SupportedCRS>urn:ogc:def:crs:EPSG::3857</ows:SupportedCRS>
            <ows:BoundingBox crs="urn:ogc:def:crs:EPSG::3857">
                <ows:LowerCorner>-1198263.0364071354 6365004.037965424</ows:LowerCorner>
                <ows:UpperCorner>213000.0 8702260.01</ows:UpperCorner>
            </ows:BoundingBox>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:7</ows:Identifier>
                <ScaleDenominator>4367830.1870353315</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>65</MatrixWidth>
                <MatrixHeight>44</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:8</ows:Identifier>
                <ScaleDenominator>2183915.0935181477</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>130</MatrixWidth>
                <MatrixHeight>88</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:9</ows:Identifier>
                <ScaleDenominator>1091957.5467586026</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>260</MatrixWidth>
                <MatrixHeight>175</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:10</ows:Identifier>
                <ScaleDenominator>545978.7733797727</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>519</MatrixWidth>
                <MatrixHeight>350</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:11</ows:Identifier>
                <ScaleDenominator>272989.3866894138</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>1037</MatrixWidth>
                <MatrixHeight>699</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:12</ows:Identifier>
                <ScaleDenominator>136494.6933447069</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>2073</MatrixWidth>
                <MatrixHeight>1398</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:13</ows:Identifier>
                <ScaleDenominator>68247.34667235345</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>4145</MatrixWidth>
                <MatrixHeight>2795</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:14</ows:Identifier>
                <ScaleDenominator>34123.673336176726</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>8290</MatrixWidth>
                <MatrixHeight>5590</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:15</ows:Identifier>
                <ScaleDenominator>17061.836668560845</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>16580</MatrixWidth>
                <MatrixHeight>11180</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:16</ows:Identifier>
                <ScaleDenominator>8530.918334280406</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>33159</MatrixWidth>
                <MatrixHeight>22360</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:17</ows:Identifier>
                <ScaleDenominator>4265.459166667739</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>66317</MatrixWidth>
                <MatrixHeight>44719</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:18</ows:Identifier>
                <ScaleDenominator>2132.7295838063405</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>132634</MatrixWidth>
                <MatrixHeight>89437</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:19</ows:Identifier>
                <ScaleDenominator>1066.3647914307007</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>265267</MatrixWidth>
                <MatrixHeight>178873</MatrixHeight>
            </TileMatrix>
            <TileMatrix>
                <ows:Identifier>EPSG:3857:20</ows:Identifier>
                <ScaleDenominator>533.1823957153497</ScaleDenominator>
                <TopLeftCorner>-2.0037508342787E7 2.0037508342787E7</TopLeftCorner>
                <TileWidth>256</TileWidth>
                <TileHeight>256</TileHeight>
                <MatrixWidth>530533</MatrixWidth>
                <MatrixHeight>357746</MatrixHeight>
            </TileMatrix>
        </TileMatrixSet>

@manisandro
Copy link
Member

Could you propose a PR to address this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants