Geographic Information Systems Asked by JBihin on July 8, 2021
I would like to generate a tool that reclassifies rasters in 10 classes with equal intervals. Since QGIS reclassifies tools does not allow to automatically create classes with such methods, I was wondering if I could do it myself inside the graphical modeller this way:
I know that the last step is possible, but I’m not sure about the other ones… Can we process data and informations like this inside the graphical modeller? I know it’s possible with Python but for a teaching purpose, I would like to know if it’s possible with the graphical modeller specifically.
I have created a model you can download here from my github repo.
Thats how it looks:
Where:
And:
The Expression as explanation (uses different variables than in the actual model above, better to explain this way):
-- CREATE VARIABLES
with_variable('rmin',1,( -- Equals to result of Raster layer statistics MIN result
with_variable('rmax',100,( -- Equals to result of Raster layer statistics MAX result
with_variable('nclasses',10, -- Equals to model input NClasses
-- for copy paste into your own model, only copy below, and change variables
-- NEST ARRAYS
array_to_string(array_foreach(
-- ADD LOWER CLASS VALUES
array_remove_at(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),@nclasses)
,@element
-- ADD UPPER CLASS VALUES
-- concat
||','||
-- array of upper classes
array_get(
array_filter(
array_foreach(
generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses)
,
array_get(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),
array_find(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),@element)+1)
),@element is not null)
,
-- get the index of initial array to insert at this position
array_find(
array_remove_at(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),@nclasses)
,@element))
-- ADD CATEGORY VALUES
-- concat
||','||
-- array of class values
array_get(generate_series(1,@nclasses),
-- get the index of initial array to insert at this position
array_find(
array_remove_at(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),@nclasses)
,@element))
)) -- closing array nesting
-- for copy paste into your own model, only copy above, and change variables
))))) -- closing with variable
*Only tested in QGIS 3.16.1
Further explanation on the expression:
The expression basically consists of four parts:
lowerclass1value,upperclass1value,class1reclassvalue,lowerclass2value,upperclass2value,class2reclassvalue,...
#1 is done by:
array_to_string(array_foreach(...),THE_THREE_ARRAYS)
just some complicated nesting, I cant really explain...
#2 is done by:
array_remove_at(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),@nclasses),@element
Generating a series from raster min value to raster max value in steps by raster range devided by nclasses-1. This will result in n equal classes. Since we only want the lower borders, we remove the last entry (raster max) from the array. The last @element is for nesting.
#3 is done by:
array_filter(
array_foreach(
generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses)
,
array_get(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),
array_find(generate_series(@rmin,@rmax,(@rmax-@rmin)/@nclasses),@element)+1)
),@element is not null)
Generating a series from raster min value to raster max value in steps by raster range devided by nclasses-1. This will result in n equal classes. Since we only want the upper borders here, we remove the first entry (raster min) from the array.
#4 is done by:
generate_series(1,@nclasses)
To generate class values from 1 to the maximum of classes. Step 1.
Cant really explain better.... Maybe best to try out yourself and stitch the parts together.
Proof:
Answered by MrXsquared on July 8, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP