2

I'm using a WMTS service to serve image tiles, and I want to read the pixel values of these tiles over some extent.

I am able to do this with a QgsRasterLayer of a local file (.tif) without an issue, using the following:

import numpy as np
from qgis.core import *

layer = QgsProject.instance().mapLayersByName('MyLocalTiff')[0] 
provider = layer.dataProvider()
extent = provider.extent()
height, width = layer.height(), layer.width()

img = np.zeros((height, width, 3), np.uint8)

for band in range(3):
    block = provider.block(band, extent, width, height)

    for y in range(0, height):
        for x in range(0, width):
            img[y, x, band] = block.value(y, x)

However, when I use the same technique with a WMTS RasterLayer, every pixel value is NaN. QGIS is able to display the layer just fine and all the image data is displayed on the canvas without issue.

How do I access these pixel values?

1 Answer 1

1

The WMTS server is providing you with a picture of your data, most likely as a png or jpeg file. So, it is unlikely that there are 3 bands or that the values are stored as unsigned ints.

There is no point in looking at the pixel values as they will have been modified by the rendering and compression process. If you want to examine the values of remote rasters then you need to use the Web Coverage Service (WCS) protocol.

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.