2

I am running QGIS 3.12.3 București on Windows 10 x64; I am building a simple model to reclassify a series of rasters with a 2x3 table, and then count each unique reclassified value within different polygons in a shapefile.

enter image description here

My input rasters follow a naming convention with area_type_year_satellite, and I have rasters for each year over a long time span.

Now, since the input raster for the zonal histogram has to be a single entry, I am running the model as a batch process. I am successfully saving the output shapefiles with unique names according to the year in the filename of each input rasters, as suggested here, using an expression similar to:

'C:/Users/user/Desktop/' ||  @inputsitespolygon || '_' || substr(@RastertoReclassify, 10, 4) || '.shp'

My issue is: the Zonal Histogram algorithm allows for predefining the name of each column of the output shapefile, and I am trying to obtain unique column names depending on the input raster filename, with a similar approach to the above, using either:

substr(@RastertoReclassify, 10, 4) || '_'

or

concat(substr(@RastertoReclassify, 10, 4), '_')

zonal histogram interface

However, any expression I used seems to fail, and the columns in the output keep the default name (HISTO_) or register only the last part of the concat function:

default name concat output

My question is: is there a way to programmatically name the output columns based on the input raster filename?

Ideally, they should be something like 1986_0 | 1986_1 for one output; 1987_0 | 1987_1 for another, and so on. I have to say I am not familiar with Python, and I don't know how to build a custom processing script. Not sure if I am just missing something, but I am a bit clueless on how to achieve this.

  • 1
    Andrea, I have kind of similiar question with an answer for you. Does this help you? You are looking for a prefix. gis.stackexchange.com/questions/138549/… – GISGUILD May 24 at 18:05
  • 1
    @GISGUILD thanks! it has indeed helped a lot. I was able to solve my problem: at first, I tried modifying the python script generated by the model by adapting the example you posted. This led me also to the solution using QGIS expressions only. I'll answer the question quoting your comment and adding details :) – Andrea Titolo May 24 at 22:14
3

Thanks to the comment by @GISGUILD:

Andrea, I have kind of similiar question with an answer for you. Does this help you? You are looking for a prefix. Batch add column to layers and add layer prefix in PyQGIS

I was able to solve my problem. At first, I modified the python script generated by the QGIS Modeler, adapting the solution linked by @GISGUILD. The solution was to add parameters['RastertoReclassify'].split("_")[2] + '_', to the 'COLUMN_PREFIX' parameter of the zonal histogram inside the script:

edited script

This led also to the solution using QGIS expression only:
Adding substr( parameter('RastertoReclassify'), 10, 5) correctly result in naming the output columns programmatically, depending on the input raster filename.

columns1 columns2

| improve this answer | |
  • 1
    Well done Andrea, This should do the trick indeed. If you are satisfied about your own answer, you can check it so everybody knows your question has been answerd by your liking. – GISGUILD May 25 at 7:16
  • @GISGUILD thanks for the tip, it seems I will have to wait until tomorrow to mark my own answer as correct, but I will do it as soon as I can. – Andrea Titolo May 25 at 7:21

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.