3

I would like to calculate zonal histograms (number of raster cells of each type contained in a polygon) using the RQGIS3 package in R. The problem is that I can't seem to find the algorithm name for zonal histograms. For example, if I want zonal statistics, I might do something like:

args <- get_args_man(alg="qgis:zonalstatistics")
args$INPUT_RASTER <- someraster
args$INPUT_VECTOR <- somevector
args$output <- file.path(tempdir(), "out.shp")
out<- run_qgis(alg = "qgis:zonalstatistics", params = args,
        load_output = TRUE,
        show_output_paths = FALSE,
        qgis_env = qgis_env)

But for zonal histograms, I don't know the name of the algorithm to specify, or if I can do this at all from R (it is easily done from within QGIS 3.4).

If I run the following in the Python console in QGIS, I can't seem to find an algorithm name for zonal statistics:

for alg in QgsApplication.processingRegistry().algorithms():
        print(alg.id(), "->", alg.displayName())

Can I do zonal histograms from within R, using the RQGIS3 package?

My motivation is that I want to be able to generate various polygon grids in R and calculate the zonal histograms on the fly. I could do it in R (without QGIS, but QGIS seems much faster than the native R functions.

  • Have you tried find_algorithms()? – pat-s Feb 14 at 7:18
  • I hadn't, but I tried it now, and it produces the same output that the python script does, that I showed above (but in a more user-friendly way). But your comment inspired me to try find_algorithms()[grep("histogram",find_algorithms()] to sort through the very long list of algorithms. And then I found it easily - it is native:zonalhistogram rather than qgis:zonalhistogram. I'll provide this as an answer below. – Glenn Stauffer Feb 14 at 16:05
1

I was able to find the algorithm name using find_algorithms()[grep("histogram",find_algorithms()] which led me to native:zonalhistogram instead of the expected qgis:zonalhistogram. And, it appears than a raster extraction that took 52 minutes using R functions, took only 94 seconds with zonalhistogram!

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.