How to properly rotate a raster

Eloi Ribeiro mail at eloiribeiro.eu
Tue Mar 19 03:55:52 PDT 2024


Hi all,

Here an example with the code.
If you compare rast_rotated_from_raster with rast_rotated_from_vector, the latest is rotated as expected and the first is not.
I guess, I must be doing something wrong when creating rast_rotated_from_raster.
Any guess?

-- non rotated raster
DROPTABLEIFEXISTS public.rast_not_rotated;
CREATETABLEpublic.rast_not_rotated AS
SELECT ST_AsRaster(t.geom,1000,1000,'8BUI',1,0) AS rast
FROM (SELECT ST_GeomFromText('POLYGON((166989.36462303242 505764.77048559673,
166996.25788353998 505576.476318614,
167265.54134611748 505585.94966528506,
167258.67088940175 505774.2438322678,
166989.36462303242 505764.77048559673))',28992) geom) t;
ALTERTABLE public.rast_not_rotated ADD COLUMN rid SERIALPRIMARY KEY;
CREATEINDEXrast_not_rotated_rast_gistON public.rast_not_rotated USING gist (st_convexhull(rast));
SELECT AddRasterConstraints('public'::name, 'rast_not_rotated'::name,'rast'::name);

-- rotate previous raster
DROPTABLEIFEXISTS public.rast_rotated_from_raster;
CREATETABLEpublic.rast_rotated_from_raster ASSELECT ST_SetRotation(rast, -0.035162353342492736) rast FROM public.rast_not_rotated;
ALTERTABLE public.rast_rotated_from_raster ADD COLUMN rid SERIALPRIMARY KEY;
CREATEINDEXrast_rotated_from_raster_rast_gistON public.rast_rotated_from_raster USING gist (st_convexhull(rast));
SELECT AddRasterConstraints('public'::name, 'rast_rotated_from_raster'::name,'rast'::name);

-- rotate polygon and then save as raster
DROPTABLEIFEXISTS public.rast_rotated_from_vector;
CREATETABLEpublic.rast_rotated_from_vector AS
SELECT ST_AsRaster(t.geom,1000,1000,'8BUI',1,0) AS rast
FROM (SELECT ST_Rotate(
ST_GeomFromText('POLYGON((166989.36462303242 505764.77048559673,
166996.25788353998 505576.476318614,
167265.54134611748 505585.94966528506,
167258.67088940175 505774.2438322678,
166989.36462303242 505764.77048559673))',28992)
,-0.035162353342492736, 166989.36462303242, 505764.77048559673) geom) t;
ALTERTABLE public.rast_rotated_from_vector ADD COLUMN rid SERIALPRIMARY KEY;
CREATEINDEXrast_rotated_from_vector_rast_gistON public.rast_rotated_from_vector USING gist (st_convexhull(rast));
SELECT AddRasterConstraints('public'::name, 'rast_rotated_from_vector'::name,'rast'::name);

Eloi

On Monday, March 18th, 2024 at 19:11, Eloi Ribeiro <mail at eloiribeiro.eu> wrote:

> Hi Regina and all,
>
> Elaborating a bit more. I have a vector layer consisting of a rectangle (farm plot), and this rectangle is subdivided into several (polygons) stripes parallel to the outer edge of the rectangle. I want to produce a raster where the rectangle and stripes long edges are parallel to the latitude line.
>
> Using ST_Rotate(geom, -0.035, x, y) in all vector layers together with ST_AsRaster(), I managed to get what I want. Good.
> Nevertheless, if I first produce the raster and then try to rotated it using ST_SetRotation(rast, -0.035), I barely see any rotation. In this step, I must be doing something wrong.
>
> I would like to first rasterize vector and then rotate the raster. Instead of rasterize non rotated vector and then, again rasterize rotated vector.
>
> The change in size (width and height), I mentioned in my first message, was while testing with 0.5 rad, not with -0.035 rad.
>
> -- input raster
> SELECT ST_Width(rast), ST_Height(rast), ST_SkewX(rast) FROM plot_3;
> -- st_width | st_height | st_skewx
> -- ----------+-----------+----------
> -- 1000 | 1000 | 0
>
> -- output raster of ST_SetRotation(rast, -0.035162353342492736)
> SELECT ST_Width(rast), ST_Height(rast), ST_SkewX(rast) FROM plot_3_rotated_from_raster;
> -- st_width | st_height | st_skewx
> -- ----------+-----------+----------------------
> -- 1000 | 1000 | 0.006952538312005269
>
> -- output raster of ST_SetRotation(rast, -0.035162353342492736)
> SELECT ST_Width(rast), ST_Height(rast), ST_SkewX(rast) FROM plot_3_rotated_from_vect;
> -- st_width | st_height | st_skewx
> -- ----------+-----------+----------
> -- 977 | 964 | 0
>
> -- output raster of ST_SetRotation(rast, -0.5)
> SELECT ST_Width(rast), ST_Height(rast), ST_SkewX(rast) FROM plot_3_rotated_from_raster_05;
> -- st_width | st_height | st_skewx
> -- ----------+-----------+---------------------
> -- 1000 | 1000 | 0.09481479675190897
>
> Cheers,
> Eloi
>
> On Monday, March 18th, 2024 at 14:01, Regina Obe <lr at pcorp.us> wrote:
>
>> By size, what do you mean exactly? It would change the width and height.
>>
>> What are you expecting rotation to do. Perhaps you can give example out of the below and some sample
>>
>> ST_Width, ST_Height, ST_SkewX before and after and what you were expecting.
>>
>> https://postgis.net/docs/en/RT_ST_SetRotation.html
>>
>> From: Eloi Ribeiro <mail at eloiribeiro.eu>
>> Sent: Monday, March 18, 2024 4:57 AM
>> To: PostGIS Users Discussion <postgis-users at lists.osgeo.org>
>> Subject: How to properly rotate a raster
>>
>> Hi all,
>>
>> I need to rotate a raster, for that I'm using the function ST_SetRotation, like so:
>>
>> CREATE TABLE plot_3_rotated AS
>> SELECT ST_SetRotation(rast, 0.03516235334249185) rast
>> FROM plot_3;
>>
>> But I see that is not producing the expected results and is changing size of the raster. What am I doing wrong?
>>
>> Cheers,
>>
>> Eloi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20240319/26a16969/attachment.htm>


More information about the postgis-users mailing list