3

I'm currently developing a python plugin for QGIS 3. In short, the code conducts among others a viewshed algorithm, turns the output in a numpy array and calculates some spatial metrics the user can choose.

The code below -and plugin- works perfectly IF every iteration I save the process to a .tif file first, but since the algorithm is a large loop, I want to try and save the temporary output in memory for the sake of performance. Alas, when saved to memory, the "Raster.GetRasterBand(1)" won't work because it extracts a None value as output (see commented out file path which works).

AttributeError: 'NoneType' object has no attribute 'GetRasterBand'

Is there another way to save the output in memory and transform it into a numpy array? I've searched similar topics on GIS-SE but none really helped.

EDIT: Read here that memory layers can't be read in GDAL processing tools, hence my question.

Example of algorithm:

QVS = processing.run("grass7:r.viewshed", 
                    {'input': DEM,
                     'coordinates': "{0}, {1}".format(j, i),
                     'observer_elevation': VIEW_ELEV,
                     'max_distance': MAX_DIST,
                     'memory': MEMORY,
                     '-c': False,
                     '-r': False,
                     '-b': True,
                     '-e': True,
                     'GRASS_REGION_PARAMETER': "{0}, {1}, {2}, {3}".format(j - HALF_MAX,
                                                                    j + HALF_MAX,
                                                                    i - HALF_MAX,
                                                                    i + HALF_MAX),
                     'GRASS_REGION_CELLSIZE_PARAMETER': 0,
                     'output': "memory:tempVS"}) #"D:/Users/KPAENEN/Documents/TestRoom/THSS/VS.tif"})

To turn it into an array:

def rst(self, QRast):
    # transform QRaster Object to Numpy array
    Raster = gdal.Open(QRast)
    Band = Raster.GetRasterBand(1)
    Array = Band.ReadAsArray()
    return Array

and

VS = dlg.rst(QVS['output'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.