I have a table in PostGIS with several rasters, which have the same spatial reference, but the tiffs are from different dates. Now I am trying to access the column "rast" to detect changes between rows. My aim is to subtract the pixel value of the first row from the second and then from the third row's pixel values, and so on.
How can I iterate over the rows and subtract the pixel values of each row from the following row?
[enter image description here][1]
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
conn = None
conn = psycopg2.connect(database="postgres", user="postgres",host="localhost", password="password")
cur = conn.cursor()
cur.execute('SELECT * from my_table')
while True:
row = cur.fetchone()
if row == None:
break
rast_col = row[1]
I imported several rasters, which have the same spatial area but diffrent dates via following command:
C:\Program Files\PostgreSQL\9.6\bin>raster2pgsql -s 4326 -F -I "C:\User\Desktop\Data\*.tif" public.all_data|psql -U User -h localhost -p 5432
This is the table that was created in postgresql after importing the data [1]: https://i.stack.imgur.com/uBHX3.jpg
Each row is representing one raster image in "TIFF" format. The column "rast" contains the pixel values. My aim is to calculate the diffrence between the adjacent rows...same like the lag windows function, but it does not work on raster column type...
The only thing, that i fixed was calculating the diffrence between two raster images. For that I had to create for each row a separate table. U can see it below:
CREATE TABLE table1 AS SELECT * FROM my_tabke WHERE rid=1;
CREATE TABLE table2 AS SELECT * FROM my_table WHERE rid=2;
And then I did a simple MapAlgebra Operation on both tables like this:
SELECT ST_MapAlgebra(t1.rast,t2.rast, '([rast1]-[rast2])') AS rast INTO diffrence FROM table1 t1, table2 t2;
but this is just the diffrence between two rasters, and for the MapAlgebra operation I had to create extra tables for each raster images. But I have more the 40 raster images in one table, and I want to detect the change of all adjacent rows between my table.