I have the URL of a WMTS server which provides map tiles in EPSG:25832 (ETRS89 / UTM zone 32N; I'm sorry I cannot post it as it contains user credentials).
If I connect to this server with QGIS I can select a tileset (ADV_25832_Quad) and it is displayed properly. The image below (left) shows the result with a vector layer (red border) for verification.
Now I tried to use this WMTS server in OpenLayers by combining these two examples:
- WMTS example: https://openlayers.org/en/latest/examples/wmts.html
- Custom projection example: https://maps.omniscale.com/de/examples/openlayers3_25832
In OpenLayers the WMTS layer is not displayed properly (see image below on the right).
I think is has to to with the projection extent or the resolution of the tilegrid (I got the projection extent from https://epsg.io/25832).
But how does QGIS get these values and how can I get them for OpenLayers?
Here's my code:
var projectionName = 'EPSG:25832';
proj4.defs(projectionName, '+proj=utm +zone=32 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
var projection = ol.proj.get(projectionName);
var projectionExtent = [ -1877994.66, 3932281.56, 836715.13, 9440581.95 ];
var size = ol.extent.getWidth(projectionExtent) / 256;
var resolutions = new Array(14);
var matrixIds = new Array(14);
for (var z = 0; z < 14; ++z) {
resolutions[z] = size / Math.pow(2, z);
matrixIds[z] = "ADV_25832_Quad:" + z;
}
var map = new ol.Map({
layers: [
new ol.layer.Tile({
opacity: 1,
source: new ol.source.WMTS({
attributions: '...',
url: 'https://...',
layer: "DOP_20_C",
"matrixSet": "ADV_25832_Quad",
projection: projection,
tileGrid: new ol.tilegrid.WMTS({
origin: ol.extent.getTopLeft(projectionExtent),
resolutions: resolutions,
matrixIds: matrixIds
}),
style: 'default',
wrapX: true,
})
}),
new ol.layer.Vector({
source: new ol.source.Vector({
url: 'bw25832.txt',
format: new ol.format.GeoJSON()
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 2
})
})
})
],
target: 'map',
view: new ol.View({
projection: projection,
center: [506102,5384251],
zoom: 8
})
});
EDIT
Here are the top 2 levels of the tile matrix from the getCapabilities response:
<TileMatrixSet>
<ows:Identifier>ADV_25832_Quad</ows:Identifier>
<ows:SupportedCRS>urn:ogc:def:crs:EPSG::25832</ows:SupportedCRS>
<TileMatrix>
<ows:Identifier>ADV_25832_Quad:0</ows:Identifier>
<ScaleDenominator>1.747132075089743E7</ScaleDenominator>
<TopLeftCorner>-46133.17 6301219.54</TopLeftCorner>
<TileWidth>256</TileWidth>
<TileHeight>256</TileHeight>
<MatrixWidth>1</MatrixWidth>
<MatrixHeight>1</MatrixHeight>
</TileMatrix>
<TileMatrix>
<ows:Identifier>ADV_25832_Quad:1</ows:Identifier>
<ScaleDenominator>8735660.375448715</ScaleDenominator>
<TopLeftCorner>-46133.17 6301219.54</TopLeftCorner>
<TileWidth>256</TileWidth>
<TileHeight>256</TileHeight>
<MatrixWidth>2</MatrixWidth>
<MatrixHeight>2</MatrixHeight>
</TileMatrix>