20

I need to perform multiple calculations on a set of rasters based on values in a CSV metadata file.

I'm using the QGIS graphical modeler and can call a specific field in a CSV with the Table and Table Field inputs.

Since I have multiple rasters, and each raster has a corresponding value in the CSV, how do I call the specific row? (similar to a lookup for instance).

I don't want to add a script if I don't have to for ease of use.

  • 1
    It would probably be easier to approach this by converting the rasters to point files. From there it would be easier to make the necessary linkages between the two files. webhelp.esri.com/arcgisdesktop/9.3/… – Jamie2483 Jan 6 '17 at 16:32
  • 3
    Make a list (pseudocode) of your task. This will tell you if you need to resort to a script to do your job. Iterations or conditional procedures are indicators of you having to resort to scripts. Done with structure scripts are your friends. – ragnvald Apr 19 '17 at 20:12
2

Really old post but I guess a possible solution is to, unfortunately for your case, use a script in your modeler which:

  • Retrieves the names all loaded raster layers.
  • Iterate through the metadata which contains a column with raster names.
  • Matches the loaded raster names with those in the metadata.
  • If any matches are found, the raster's corresponding value is extracted.

  1. You can create a script from:

    Processing Toolbox > Scripts > Tools > Create new script
    

    Then use something like the following which:

    ##Example=name
    ##Table=table
    ##Field=Field Table
    ##Output_value=output Number
    
    from qgis.core import QgsMapLayer, QgsMapLayerRegistry
    import csv
    
    raster_list = [layer.name() for layer in QgsMapLayerRegistry.instance().mapLayers().values() if layer.type() == QgsMapLayer.RasterLayer]
    with open(Table) as f:
        reader = csv.reader(f)
        for row in reader:
            for raster in raster_list:
                if raster in row[0]:
                    Output_value = row[1]
                    print Output_value
    

  1. Then add the script into your modeler (I made a simple example with just two inputs):

    Modeler

    If I run this model using the script above, it simply prints the output value from the metadata which corresponds to the name of the raster:

    Result


  1. Depending on how your model is contructed and possibly tweaking the script, you could perform your multiple raster calculations.

Your Answer

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

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