More

How to classify a raster using QGIS Plugin interacting with PostGIS?

How to classify a raster using QGIS Plugin interacting with PostGIS?


I'm developing a QGIS plugin where users can enter some parameters (like distance, depth, area… ) and by submiting the plugin interact with a PostGIS database and extract informations form vector and raster layers.

I want to classify a DEM raster by substituting the pixel values, for example:

If 200 < val < 210 Then val = 205

If 211 < val < 220 Then val = 215

This classification is based on the value entered by users using a TextBox or LineEdit

I'm using the query bellow and it's working but manually not automatically:

This query took 2 seconds for a DEM raster with (X:6000 , Y:5000) and (MIN(val):200,MAX(val):2400)

So each time I need to specify the gap.

Is there any way to allow to users to specify the interval they want to be applied for all values of raster?


if you have a constant interval, then you do not need a look up table. You can use the following formula

(int([email protected]) / interval) * interval + interval/2

of course, / must be an integer division (rounding to the integer below)


Based on the great idea of @radouxju's responce I resolve this issues.

Instead of using Reclass function I used MapAlgebra function so the query is :

SELECT ST_MapAlgebra(a.rast,b.rast, '(ceil([rast1]/10)*10)+5') AS newrast from my_raster1 a, my_raster2 b

I used 'ceil' funtion to get the smallest integer not less than argument see this link

And this is how I used it into my QGIS plugin code :

reclass_val = float(self.dlg.lineEdit.text()) cur.execute("create table example as SELECT ST_MapAlgebra(a.rast,b.rast, '(ceil([rast1]/%s)*%s)+(%s/2)') AS newrast from my_raster1 a, my_raster2 b",(reclass_val ,reclass_val ,reclass_val ))

I hope this will help


Watch the video: Tool Import Raster to PostgreSQLPostGIS and load in QGIS