I am creating a script to run some processing tools on pairs of rasters - in this case VelocityX and VelocityY raster grids. The files are called .Vx and .Vy and are uniquely identified by names in this format:

  • StandardBaseName_0001.Vx
  • StandardBaseName_0001.Vy
  • StandardBaseName_0002.Vx
  • StandardBaseName_0002.Vy
  • etc etc

This question has some code to loop over a folder of rasters - can this be adapted for identifying each 'pair' of rasters and feeding their names into variables that I can give to the processing algorithms?

def findRasters (path, filter):
    for root, dirs, files in os.walk(path):
        for file in fnmatch.filter(files, filter):
            yield file

for raster in findRasters(INPUT_FOLDER, '*.tif'):

I was thinking that matching the name (without extension) of the .Vx raster to the name of the .Vy raster might work but I'm not sure how to do it.


1 Answer 1


After much trial and error I've managed to solve this one I think. You can do it either with files in a folder (answer 01 below) or with loaded files in the QGIS TOC (answer 02 below). I suspect these aren't that elegant but they do seem to work; any suggestions on improvements are welcome.

Answer 01 - this takes an input folder and iterates over the rasters in it.

    ##Lisflood - Calculate Velocity Vectors=name

    #import the os and processing modules
    import os
    import processing

    #iterate through all the files in the input folder
    for filename in os.listdir(inputfolder):
        #Compare the file extensions - this is what we're using the select the files
        if filename.endswith(".Vx"):
            #Set the inputvx and inputvy parameters - you need to specify the full filepath
            inputvx = inputfolder + "/" + filename
            inputvy = inputfolder + "/" + filename [:-3] + ".Vy"
            #Specify an output name - this goes into the final processing tool
            OutputName = outputfolder + '/' + filename [:-3] + '_VelocityVectors.shp' 

#Run whatever tools you want - in this case I'm calculating directional vectors from Vx and Vy velocity grids.          outputs_SAGARASTERCALCULATOR_1=processing.runalg('saga:rastercalculator', inputvx,[inputvy],'sqrt(a^2 + b^2)',True,7,None)
            outputs_SAGARASTERCALCULATOR_3=processing.runalg('saga:rastercalculator', inputvx,[inputvy],'(atan2(a,b)) * (180/pi())',True,7,None)
            outputs_SAGAGRIDVALUESTOPOINTS_1=processing.runalg('saga:gridvaluestopoints', [outputs_SAGARASTERCALCULATOR_3['RESULT']],None,True,0,None)
            outputs_SAGAADDGRIDVALUESTOPOINTS_1=processing.runalg('saga:addgridvaluestopoints', outputs_SAGAGRIDVALUESTOPOINTS_1['SHAPES'],[outputs_SAGARASTERCALCULATOR_1['RESULT']],1,OutputName)

Answer 02 - this one is the same but this one uses layers loaded into the QGIS TOC instead of an input folder so you don't need :

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    layerfilepath = layer.dataProvider().dataSourceUri()
    NameStub = layer.name()
    if layerfilepath [-3:] == '.Vy':
        layer.setLayerName(NameStub + '.Vy')
    elif layerfilepath [-3:] == '.Vx':
        layer.setLayerName(NameStub + '.Vx')
    layerfilepath = layer.dataProvider().dataSourceUri()
    NameStub = layer.name()
    if NameStub [-3:] == '.Vy':
        inputvy = layerfilepath
        print inputvy
        inputvx = layerfilepath [:-3] + '.Vx'
        print inputvx
        OutputName = outputfolder + '/' + layerfilepath [:-3] + '_VelocityVectors'

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.