<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=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@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:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:890459248;
        mso-list-type:hybrid;
        mso-list-template-ids:-1170161942 845979414 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;}
@list l0:level1
        {mso-level-start-at:0;
        mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Wingdings;
        mso-fareast-font-family:Calibri;
        mso-bidi-font-family:"Times New Roman";}
@list l0:level2
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:"Courier New";}
@list l0:level3
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Wingdings;}
@list l0:level4
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Symbol;}
@list l0:level5
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:"Courier New";}
@list l0:level6
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Wingdings;}
@list l0:level7
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Symbol;}
@list l0:level8
        {mso-level-number-format:bullet;
        mso-level-text:o;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:"Courier New";}
@list l0:level9
        {mso-level-number-format:bullet;
        mso-level-text:;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;
        font-family:Wingdings;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></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=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p style='margin-left:.5in;text-indent:-.25in;mso-list:l0 level1 lfo1'><![if !supportLists]><span style='font-family:Wingdings'><span style='mso-list:Ignore'>Ø<span style='font:7.0pt "Times New Roman"'>  </span></span></span><![endif]>First question: how can one recode <b>gbr100x100d </b>so that all pixels with value 0 are NODATA?<o:p></o:p></p><p class=MsoListParagraph><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Use ST_SetBandNoDataValue <a href="https://postgis.net/docs/manual-3.1/RT_ST_SetBandNoDataValue.html">https://postgis.net/docs/manual-3.1/RT_ST_SetBandNoDataValue.html</a><o:p></o:p></span></p><p class=MsoListParagraph><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>UPDATE gr100x100d SET ST_SetBandNoDataValue(rast,1,0);<o:p></o:p></span></p><p class=MsoListParagraph><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoListParagraph><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>General side note, using ST_Reclass for what you are doing below should be much faster than ST_MapAlgebra<o:p></o:p></span></p><p class=MsoListParagraph><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoListParagraph style='text-indent:-.25in;mso-list:l0 level1 lfo1'><![if !supportLists]><span style='font-size:11.0pt;font-family:Wingdings;color:#1F497D'><span style='mso-list:Ignore'>Ø<span style='font:7.0pt "Times New Roman"'>  </span></span></span><![endif]><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'>Second question:  I haven’t had a chance to look that closely.  What is the purpose of the ST_SnapToGrid?<o:p></o:p></span></p><p class=MsoListParagraph><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'>From:</span></b><span style='font-size:11.0pt;font-family:"Calibri",sans-serif'> postgis-users [mailto:postgis-users-bounces@lists.osgeo.org] <b>On Behalf Of </b>Simon SPDBA Greener<br><b>Sent:</b> Tuesday, March 16, 2021 8:15 PM<br><b>To:</b> postgis-users@lists.osgeo.org<br><b>Subject:</b> [postgis-users] PostGIS Raster: Two Questions<o:p></o:p></span></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><p>I have a 1 band raster, srid 4326, (0.001 x 0.001 roughly 100m x 100m) , called <b>gbr100x100d</b>, whose pixels are coded 0 and 1 where 1 is water deeper than 100m and 0 water shallower than 100m. <o:p></o:p></p><p>It was created as follows:<o:p></o:p></p><p><b><span style='font-family:"Courier New"'>create table gbr100x100d<br>as<br>SELECT row_number() over (order by 1) as rid,<br>       ST_SetSrid(ST_MapAlgebra(a.rast, 1, NULL, '([rast.val]<-100.0)::integer'),4326) as rast<br>  from gbr100x100c as a;</span></b><o:p></o:p></p><p>First question: how can one recode <b>gbr100x100d </b>so that all pixels with value 0 are NODATA?<o:p></o:p></p><p>Next. <o:p></o:p></p><p>Polygons of size 0.02x 0.01 need to be extracted from the 1 valued 0.001 pixels.<o:p></o:p></p><p>I think this should be done as follows.<o:p></o:p></p><p>First we resample to get a raster with cells of size 0.02 x 0.01:<o:p></o:p></p><p><b><span style='font-family:"Courier New"'>create table gbr2kmx1kmd1<br>as<br>SELECT row_number() over (order by 1) as rid,<br>       ST_SetSrid(ST_Rescale(a.rast,0.02,-0.01,'Bilinear',0.125),4326) as rast<br>  from gbr100x100d as a;</span></b><o:p></o:p></p><p>Then we extract the polygons of these 0.02 x 0.01 pixels.<o:p></o:p></p><p><b><span style='font-family:"Courier New"'>create table gbr2kmx1kmd1p<br>as <br>SELECT row_number() over (order by 1) as id, (gv).val, ST_SnapToGrid((gv).geom,0.001)::geography as geog<br>  FROM (SELECT ST_PixelAsPolygons(a.rast,1,true) as gv <br>          FROM gbr2kmx1kmd1 as a<br>       ) gv<br> WHERE (gv).val = 1;</span></b><o:p></o:p></p><p>The result is close as the polygons are 0.02 x 0.01 but they are organised as a set of striped polygons where the columns don't touch. <o:p></o:p></p><p>See attached.<o:p></o:p></p><p>What was expected was a complete coverage of abutting polygons. <o:p></o:p></p><p class=MsoNormal>Can anyone spot what I am doing wrong? <o:p></o:p></p><p>regards<o:p></o:p></p><p>Simon<o:p></o:p></p></div><p class=MsoNormal><br>2021-03-17_10-27-06.png <o:p></o:p></p><div><p class=MsoNormal><img border=0 width=484 height=421 id="_x0000_i1025" src="cid:image001.png@01D71AA7.9D0E2710"><o:p></o:p></p></div></div></div></body></html>