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.

mgri
  • 16,159
  • 6
  • 47
  • 80
HDunn
  • 8,626
  • 3
  • 40
  • 71
  • 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. http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=1439&pid=1436&topicname=Raster_to_Point_(Conversion) – Jamie Jan 06 '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

1 Answers1

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.
Joseph
  • 75,746
  • 7
  • 171
  • 282