I stored Sentinel-2 images as tiled in the PostgreSQL DB. Now I am trying to clip it with geometry and it works fine, also converting raster to geoms (ST_PixelAsPolygons)is also working fine. Everything is working fine with this code:
with selected as (
select ST_Clip(rast, ST_GeomFromText('POLYGON ((539800.0 5449880.0, 539800.0 5455000.0, 544920.0 5455000.0, 544920.0 5449880.0, 539800.0 5449880.0))',32632),-9999, true) as rast
from sentinel_2.t32unv where rast && ST_GeomFromText('POLYGON ((539800.0 5449880.0, 539800.0 5455000.0, 544920.0 5455000.0, 544920.0 5449880.0, 539800.0 5449880.0))', 32632) and acquisition_date = '2022-09-23'
)
select
(ST_PixelAsPolygons(rast, 1)).x as x,
(ST_PixelAsPolygons(rast, 1)).y as y,
(ST_PixelAsPolygons(rast, 1)).geom as geom,
(ST_PixelAsPolygons(rast, 1)).val as band1,
(ST_PixelAsPolygons(rast, 2)).val as band2,
(ST_PixelAsPolygons(rast, 3)).val as band3,
(ST_PixelAsPolygons(rast, 4)).val as band4,
(ST_PixelAsPolygons(rast, 5)).val as band5,
(ST_PixelAsPolygons(rast, 6)).val as band6,
(ST_PixelAsPolygons(rast, 7)).val as band7,
(ST_PixelAsPolygons(rast, 8)).val as band8,
(ST_PixelAsPolygons(rast, 9)).val as band9,
(ST_PixelAsPolygons(rast, 10)).val as band10,
(ST_PixelAsPolygons(rast, 11)).val as band11,
(ST_PixelAspoints(rast, 12)).val as band12
from selected
except coordinates. Because of tiling, if clipping geometry covers multiple tiles then for each tile x, y coordinates starts from the beginning, while I want to have not duplicate x,y coordinates. For this reason, I tried to following query with ST_Union
with selected as (
select ST_Union(ST_Clip(rast, ST_GeomFromText('POLYGON ((539800.0 5449880.0, 539800.0 5455000.0, 544920.0 5455000.0, 544920.0 5449880.0, 539800.0 5449880.0))',32632),-9999, true)) as rast
from sentinel_2.t32unv where rast && ST_GeomFromText('POLYGON ((539800.0 5449880.0, 539800.0 5455000.0, 544920.0 5455000.0, 544920.0 5449880.0, 539800.0 5449880.0))', 32632) and acquisition_date = '2022-09-23'
)
select
(ST_PixelAsPolygons(rast, 1)).x as x,
(ST_PixelAsPolygons(rast, 1)).y as y,
(ST_PixelAsPolygons(rast, 1)).geom as geom,
(ST_PixelAsPolygons(rast, 1)).val as band1,
(ST_PixelAsPolygons(rast, 2)).val as band2,
(ST_PixelAsPolygons(rast, 3)).val as band3,
(ST_PixelAsPolygons(rast, 4)).val as band4,
(ST_PixelAsPolygons(rast, 5)).val as band5,
(ST_PixelAsPolygons(rast, 6)).val as band6,
(ST_PixelAsPolygons(rast, 7)).val as band7,
(ST_PixelAsPolygons(rast, 8)).val as band8,
(ST_PixelAsPolygons(rast, 9)).val as band9,
(ST_PixelAsPolygons(rast, 10)).val as band10,
(ST_PixelAsPolygons(rast, 11)).val as band11,
(ST_PixelAspoints(rast, 12)).val as band12
from selected
this query solved my problem with x and y coordinates, so I have nonduplicate x and y coors. However, this created a new problem which is No pixels were found for band 12
, so for the last two bands I am getting random or NULL values. What is a problem? How can I solve it?
the results after st_union: