[prev in list] [next in list] [prev in thread] [next in thread] 

List:       postgis-users
Subject:    [postgis-users] RES: Clipping a raster and a polygon using ST_Intersection
From:       <liglio.pessoal () nexxa ! com ! br>
Date:       2022-11-30 14:37:02
Message-ID: !&!AAAAAAAAAAAuAAAAAAAAACaPdoM7XC5DsX4JrwXUdYwBAMO2jhD3dRHOtM0AqgC7tuYAAAAAAA4AABAAAAA7gD50laYJSKH/QHs/rcS5AQAAAAA= () nexxa ! com ! br
[Download RAW message or body]

This is a multipart message in MIME format.

[Attachment #2 (multipart/alternative)]


Regina,

 

Thanks for your replay. 

 

Your solution is fast like using ST_Clip, but I still have the problem that
my pixels are 25km square (low definition) and the image look like this:
(image b1.jpg)

 

And I want to produce something like this: (image b2.jpg)

 

I know that the second image cannot be reproduced as just raster with big
pixels, the pixels cannot be cutted.

 

There is a way to export this image and associate a value for each square
(polygon) to be colored accordingly ?

 

Regards,

 

Liglio

 

De: postgis-users <postgis-users-bounces@lists.osgeo.org
<mailto:postgis-users-bounces@lists.osgeo.org> > Em nome de Regina Obe
Enviada em: quinta-feira, 24 de novembro de 2022 12:19
Para: 'PostGIS Users Discussion' <postgis-users@lists.osgeo.org
<mailto:postgis-users@lists.osgeo.org> >
Assunto: Re: [postgis-users] Clipping a raster and a polygon using
ST_Intersection

 

I’m not quite sure what you mean by edges aren’t smooth.  Rasters are never
really smooth. Vectors are smooth.  Rasters may look smooth at the edges,
but that’s more because pixels are square and the pixels in each tile line
up with the pixels.

 

If you want it to look a little smoother, maybe expand your envelop ever so
slightly so it lines up with a pixel in a raster.  So each edge pixel is
guaranteed to be fully inside the raster and no edge partly outside the
raster or do the operation in raster space which I will explain shortly.

 

ST_Intersection yes is known to be very slow and as you noticed does not
give you a raster back.  

Is ST_Clip + ST_Intersects performance sufficient for your needs? 

 

ST_Clip really is ST_Intersection in raster space for raster/geometry.  So
you could do the intersection in pure raster space by converting your
geometry to a raster (making sure to align your new raster with your
reference raster).  That should make the edges smooth.

 

Like so – not tested so might have some syntax errors and also I haven’t
used this in a very long time so may be wrong about what it returns.

 

    SELECT ST_Intersection(r.rast, r2.rast) AS new_rast

    FROM tb_densitysurface  AS r, country, 

-- create a raster to represent the country that has the same alignment as
our density raster (pixels use the same grid)

                ST_AsRaster(  country.area_country, r.rast, '1BB', 0) AS
r2(rast)

  WHERE country.gid = 19 AND ST_Intersects(r.rast, r2.rast) 

) As foo

 

 

As far as tiling goes, yah that would improve the smoothness of the inner
tiles but not most outer ones.

You’d also need to ST_Union them all up to get back to a single tile which
will add perhaps more overhead than just keeping as  single tile.

 

If you want to tile your raster, you can use this function -
https://postgis.net/docs/manual-2.5/RT_ST_Tile.html

 

Hope that helps,

Regina

 

 

 

 

 

From: postgis-users [mailto:postgis-users-bounces@lists.osgeo.org] On Behalf
Of liglio.pessoal@nexxa.com.br <mailto:liglio.pessoal@nexxa.com.br> 
Sent: Tuesday, November 22, 2022 2:00 PM
To: postgis-users@lists.osgeo.org <mailto:postgis-users@lists.osgeo.org> 
Subject: [postgis-users] Clipping a raster and a polygon using
ST_Intersection

 

Hi,

 

I created a retangular raster representing number of lightnings in a square.
So I have this matrix raster created using ST_MapAlgebraFct and
ST_MakeEmptyRaster. 

This raster is not a tiled raster (tb_densitysurface), and when I Clip this
raster using a polygon, the query below, the performance is not good.

Maybe because the raster is not tiled. So how do I tile this raster ? And I
want the result to be a raster (not gval) to be exported as a file, and then
colored using GDAL.

It worked with a different query only using ST_Clip (this returns a rast
column and is very fast), but the edges aren´t smooth as using
ST_Insersection. I am using Postgis 2.5.

 

SELECT

    (gval).val

    (gval).geom

FROM (

    SELECT ST_Intersection(

        ST_Clip(rast,ST_Envelope(area_country)),

        1,

       area_country

    ) As gval

    FROM country, tb_densitysurface WHERE ST_Intersects(rast, area_country)
and country.gid = 19

) As foo

 

 

Regards,

 

Liglio

 


[Attachment #5 (text/html)]

<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type \
content="text/html; charset=iso-8859-1"><meta name=Generator content="Microsoft Word \
15 (filtered medium)"><style><!-- /* Font Definitions */
@font-face
	{font-family:"Cambria Math";
	panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0cm;
	font-size:11.0pt;
	font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:#0563C1;
	text-decoration:underline;}
span.EstiloDeEmail21
	{mso-style-type:personal-reply;
	font-family:"Calibri",sans-serif;
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-size:10.0pt;}
@page WordSection1
	{size:612.0pt 792.0pt;
	margin:70.85pt 3.0cm 70.85pt 3.0cm;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=PT-BR link="#0563C1" \
vlink="#954F72" style='word-wrap:break-word'><div class=WordSection1><p \
class=MsoNormal><span \
style='mso-fareast-language:EN-US'>Regina,<o:p></o:p></span></p><p \
class=MsoNormal><span \
style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Thanks for your \
replay. <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>Your solution is \
fast like using ST_Clip, but I still have the problem that my pixels are 25km square \
(low definition) and the image look like this: (image b1.jpg)<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US \
style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>And I want to \
produce something like this: (image b2.jpg)<o:p></o:p></span></p><p \
class=MsoNormal><b><span lang=EN-US \
style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></b></p><p \
class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>I know that the \
second image cannot be reproduced as just raster with big pixels, the pixels cannot \
be cutted.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='mso-fareast-language:EN-US'>There is a way to \
export this image and associate a value for each square (polygon) to be colored \
accordingly ?<o:p></o:p></span></p><div><p class=MsoNormal><span \
lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span \
lang=EN-US>Regards,<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span \
lang=EN-US>Liglio<o:p></o:p></span></p></div><p class=MsoNormal><span lang=EN-US \
style='mso-fareast-language:EN-US'><o:p>&nbsp;</o:p></span></p><div><div \
style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p \
class=MsoNormal><b>De:</b> postgis-users &lt;<a \
href="mailto:postgis-users-bounces@lists.osgeo.org">postgis-users-bounces@lists.osgeo.org</a>&gt; \
<b>Em nome de </b>Regina Obe<br><b>Enviada em:</b> quinta-feira, 24 de novembro de \
2022 12:19<br><b>Para:</b> 'PostGIS Users Discussion' &lt;<a \
href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a>&gt;<br><b>Assunto:</b> \
Re: [postgis-users] Clipping a raster and a polygon using \
ST_Intersection<o:p></o:p></p></div></div><p class=MsoNormal><o:p>&nbsp;</o:p></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'>I&#8217;m not quite sure what \
you mean by edges aren&#8217;t smooth.&nbsp; Rasters are never really smooth. Vectors \
are smooth.&nbsp; Rasters may look smooth at the edges, but that&#8217;s more because \
pixels are square and the pixels in each tile line up with the \
pixels.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'>If you want it to look a little smoother, maybe expand your \
envelop ever so slightly so it lines up with a pixel in a raster.&nbsp; So each edge \
pixel is guaranteed to be fully inside the raster and no edge partly outside the \
raster or do the operation in raster space which I will explain \
shortly.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'>ST_Intersection yes is known to be very slow and as you noticed \
does not give you a raster back.&nbsp; <o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US style='color:#1F497D'>Is ST_Clip + ST_Intersects performance sufficient \
for your needs? <o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'>ST_Clip really is ST_Intersection in raster space for \
raster/geometry. &nbsp;So you could do the intersection in pure raster space by \
converting your geometry to a raster (making sure to align your new raster with your \
reference raster).&nbsp; That should make the edges smooth.<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'>Like so &#8211; not tested so \
might have some syntax errors and also I haven&#8217;t used this in a very long time \
so may be wrong about what it returns.<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span \
lang=EN-US>&nbsp;&nbsp;&nbsp; SELECT ST_Intersection(r.rast, r2.rast) AS \
new_rast<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp; \
FROM tb_densitysurface &nbsp;AS r, country, <o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US>-- create a raster to represent the country that has \
the same alignment as our density raster (pixels use the same \
grid)<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
ST_AsRaster(&nbsp; country.area_country, r.rast, <var><span \
style='font-family:"Calibri",sans-serif'>'1BB', 0</span></var>) AS \
r2(rast)<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>&nbsp; WHERE \
country.gid = 19 AND ST_Intersects(r.rast, r2.rast) <o:p></o:p></span></p><p \
class=MsoNormal>) As foo<o:p></o:p></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'>As far as tiling goes, yah that would improve the smoothness of \
the inner tiles but not most outer ones.<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'>You&#8217;d also need to \
ST_Union them all up to get back to a single tile which will add perhaps more \
overhead than just keeping as&nbsp; single tile.<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'>If you want to tile your \
raster, you can use this function - <a \
href="https://postgis.net/docs/manual-2.5/RT_ST_Tile.html">https://postgis.net/docs/manual-2.5/RT_ST_Tile.html</a><o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p \
class=MsoNormal><span lang=EN-US style='color:#1F497D'>Hope that \
helps,<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'>Regina<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US \
style='color:#1F497D'><o:p>&nbsp;</o:p></span></p><div \
style='border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt'><div><div \
style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p \
class=MsoNormal><b><span lang=EN-US>From:</span></b><span lang=EN-US> postgis-users \
[<a href="mailto:postgis-users-bounces@lists.osgeo.org">mailto:postgis-users-bounces@lists.osgeo.org</a>] \
<b>On Behalf Of </b><a \
href="mailto:liglio.pessoal@nexxa.com.br">liglio.pessoal@nexxa.com.br</a><br><b>Sent:</b> \
Tuesday, November 22, 2022 2:00 PM<br><b>To:</b> <a \
href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br><b>Subject:</b> \
[postgis-users] Clipping a raster and a polygon using \
ST_Intersection<o:p></o:p></span></p></div></div><p class=MsoNormal><span \
lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span \
lang=EN-US>Hi,<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span lang=EN-US>I created \
a retangular raster representing number of lightnings in a square. So I have this \
matrix raster created using ST_MapAlgebraFct and ST_MakeEmptyRaster. \
<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>This raster is not a tiled \
raster (tb_densitysurface), and when I Clip this raster using a polygon, the query \
below, the performance is not good.<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US>Maybe because the raster is not tiled. So how do I tile this raster ? And \
I want the result to be a raster (not gval) to be exported as a file, and then \
colored using GDAL.<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>It \
worked with a different query only using ST_Clip (this returns a rast column and is \
very fast), but the edges aren´t smooth as using ST_Insersection. I am using Postgis \
2.5.<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US><o:p>&nbsp;</o:p></span></p><p class=MsoNormal><span \
lang=EN-US>SELECT<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US>&nbsp;&nbsp;&nbsp; (gval).val<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp; \
(gval).geom<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>FROM \
(<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp; SELECT \
ST_Intersection(<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
ST_Clip(rast,ST_Envelope(area_country)),<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
1,<o:p></o:p></span></p><p class=MsoNormal><span \
lang=EN-US>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;area_country<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp; ) As gval<o:p></o:p></span></p><p \
class=MsoNormal><span lang=EN-US>&nbsp;&nbsp;&nbsp; FROM country, tb_densitysurface \
WHERE ST_Intersects(rast, area_country) and country.gid = 19<o:p></o:p></span></p><p \
class=MsoNormal>) As foo<o:p></o:p></p><p class=MsoNormal><o:p>&nbsp;</o:p></p><p \
class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>Regards,<o:p></o:p></p><p \
class=MsoNormal><o:p>&nbsp;</o:p></p><p class=MsoNormal>Liglio<o:p></o:p></p><p \
class=MsoNormal><o:p>&nbsp;</o:p></p></div></div></body></html>


["b1.jpg" (image/jpeg)]
["b2.jpg" (image/jpeg)]

_______________________________________________
postgis-users mailing list
postgis-users@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/postgis-users


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic