2

WMTS layers have been rendering in OpenLayers 6.5.1 flawlessly in from my code for some time now, but it has recently stopped working.

I can't actually think of anything that I've recently changed in the OpenLayers part of the app, so I've cut it down to a simple OpenLayers-only HMTL+JavaScript and it is still not rendering a WMTS layer. So surely I've changed something and forgotten about it???

But I can't figure out why this is not working. No errors are produced, and the console log message looks good. Additionally, (to answer the question in comments), there are no requests to the WMTS service in the browser debugger (only to OSM). Furthermore, if I edit the tileURLFunction to include a console.log() message, nothing gets logged from there. So it appears that it never even attempts to load any tiles.

Why is this WMTS layer not working?

JSFiddle: https://jsfiddle.net/nsands/m78qygjn/4/

Full code is below.

test.js:

function newLayerWMTS(url, layerName, matrixSetID, projCode, projString) {
    var capabilitiesURL = url + '?request=GetCapabilities';
    var options;
    var request = new XMLHttpRequest();
    request.open("GET", capabilitiesURL, false);
    request.onload = function (e) {
        if ( request.readyState === 4 ) {
            if ( request.status === 200 ) {
                parsedCapabilities = new ol.format.WMTSCapabilities().read(request.responseText)
                options = ol.source.WMTS.optionsFromCapabilities(parsedCapabilities, {
                    layer: layerName,
                    matrixSet: matrixSetID,
                });
            } else {
                console.log("REQUEST STATUS:  " + request.status);
                console.error(request.statusText);
            }
        } else {
            console.log("ONLOAD READY STATE IS NOT 4");
        }
    };
    request.onerror = function (e) {
        console.error("REQUEST ERROR " + request.status);
        console.error(request.statusText);
    };
    
    request.send();
    
    proj4.defs(projCode, projString);
    ol.proj.proj4.register(proj4);

    options['projection'] = projCode;
    
    console.log(options)
    
    return new ol.layer.Tile({ source: new ol.source.WMTS(options) });
}

var map = new ol.Map({
    layers: [],
    target: 'map',
    controls: [
       new ol.control.Attribution({collapsible: false}),
       new ol.control.ScaleLine(),
    ],
    view: new ol.View(),
});

map.getView().fit([56322178.5436955, -5207263.87320154, 56358296.78939585, -5129055.83184506], map.getSize(), {constrainResolution: false});
   
layer = new ol.layer.Tile({ source: new ol.source.OSM() });
map.addLayer(layer);

layer = newLayerWMTS('https://services.thelist.tas.gov.au/arcgis/rest/services/Basemaps/Orthophoto/MapServer/WMTS', 'Basemaps_Orthophoto', 'default028mm', 'EPSG:3857', '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs');

map.addLayer(layer);

test.html

<!doctype html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="ol/ol.css" type="text/css" />
        <script src="ol/ol.js" type="text/javascript"></script>
        <script src="proj4/proj4.js" type="text/javascript"></script>
    </head>
    <body>
        <div id="map" style="position: absolute;top: 0px;left: 0px;height: 100%;width: 100%;margin: 0px;padding: 0px;"></div>
        <script src="test.js" type="text/javascript"></script>
    </body>
</html>
2
  • 2
    Have you had a look in the network section of the browser debugger? What are the WMST requests and what is the response?
    – TomazicM
    Commented Jul 27, 2021 at 7:53
  • Sorry I should have mentioned that it appears to be making no requests at all (apart from OSM). Additionally, if I modify the tileURLFunction to log stuff… nothing is logged from there. I have updated the question to include this information now. Commented Jul 27, 2021 at 9:24

1 Answer 1

4

Problem is in your view fit bounding box coordinates. You have shifted map to the left once around the world. Center of Tasmania is about at [16324503, -5166231], your X coordinates are about 40.000.000 higher. If you pan you map to the right once around the world, your WMTS layer will appear.

Solution is either to use correct fit bounding box or set layer source option wrapX to true:

options['wrapX'] = true;
1
  • Verified now... options['wrapx'] = true; in my app's code solves it, as it does in the JSFiddle. Panning around the world (Westwards) also finds the 'missing' layer. Thank you. Commented Jul 28, 2021 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.