I am looking for a way to convert the values contained in a QgsRasterLayer object to a numpy array and vice versa without using any GDAL methods. To obtain the numpy array I go through QgsRasterDataProvider to QgsRasterBlock and then read the values into a numpy array through the QgsRasterBlock.value(i,j) method. This is not very elegant, but runs in an acceptable time. Analogously - after working with the numpy array - I change the values in the QgsRasterBlock through the QgsRasterBlock.setValue(i, j, value) method, which takes forever and is not acceptable for my application... so I am looking for an alternative!

I've read that handling the pixel values in the QgsRasterBlock object as QByteArrays and creating numpy arrays directly from the QByteArrays is possible and efficient (as compared to the method described above).

So my first question: Is this true?

My second question: What does the numpy function to create an array from QByteArray look like? And which function can I use to go back again?

Browse other questions tagged or ask your own question.