<div dir="ltr">
<div>Hi all,</div><div><br></div><div>I am wondering why ST_AsRaster returns pixels (with NoData) which are outside of bounding box of geometry being rasterized.</div><div><br></div><div>Self-contained
 demo code is here<br></div><div></div><div>
<pre class="gmail-code gmail-highlight" lang="sql"><span id="gmail-LC1" class="gmail-line" lang="sql"><span class="gmail-k">drop</span> <span class="gmail-k">view</span> <span class="gmail-n">if</span> <span class="gmail-k">exists</span> <span class="gmail-n">v_rasterization</span><span class="gmail-p">;</span></span>
<span id="gmail-LC2" class="gmail-line" lang="sql"><span class="gmail-k">create</span> <span class="gmail-k">view</span> <span class="gmail-n">v_rasterization</span> <span class="gmail-k">as</span></span>
<span id="gmail-LC3" class="gmail-line" lang="sql"><span class="gmail-k">with</span> <span class="gmail-n">w_aoi</span> <span class="gmail-k">as</span> <span class="gmail-p">(</span></span>
<span id="gmail-LC4" class="gmail-line" lang="sql">    <span class="gmail-k">select</span> <span class="gmail-n">st_setsrid</span><span class="gmail-p">(</span></span>
<span id="gmail-LC5" class="gmail-line" lang="sql">        <span class="gmail-n">ST_GeomFromText</span><span class="gmail-p">(</span></span>
<span id="gmail-LC6" class="gmail-line" lang="sql">            <span class="gmail-s1">'POLYGON((-491000 -1146394,-491000  -1140708, -480724 -1140708, -480724 -1146394, -491000 -1146394))'</span><span class="gmail-p">)</span></span>
<span id="gmail-LC7" class="gmail-line" lang="sql">        <span class="gmail-p">,</span> <span class="gmail-mi">5221</span><span class="gmail-p">)</span> <span class="gmail-k">as</span> <span class="gmail-n">aoi</span></span>
<span id="gmail-LC8" class="gmail-line" lang="sql"><span class="gmail-p">)</span></span>
<span id="gmail-LC9" class="gmail-line" lang="sql"><span class="gmail-p">,</span> <span class="gmail-n">w_rast</span> <span class="gmail-k">as</span> <span class="gmail-p">(</span></span>
<span id="gmail-LC10" class="gmail-line" lang="sql">    <span class="gmail-k">select</span> <span class="gmail-s1">'1km'</span> <span class="gmail-k">as</span> <span class="gmail-n">dscr</span><span class="gmail-p">,</span> <span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-n">st_asraster</span><span class="gmail-p">(</span><span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-mi">1000</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">1000</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">0</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">0</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-s1">'8BUI'</span><span class="gmail-p">)</span> <span class="gmail-k">as</span> <span class="gmail-n">rast</span> <span class="gmail-k">from</span> <span class="gmail-n">w_aoi</span> <span class="gmail-k">union</span> <span class="gmail-k">all</span></span>
<span id="gmail-LC11" class="gmail-line" lang="sql">    <span class="gmail-k">select</span> <span class="gmail-s1">'5km'</span> <span class="gmail-k">as</span> <span class="gmail-n">dscr</span><span class="gmail-p">,</span> <span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-n">st_asraster</span><span class="gmail-p">(</span><span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-mi">5000</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">5000</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">0</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">0</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-s1">'8BUI'</span><span class="gmail-p">)</span> <span class="gmail-k">as</span> <span class="gmail-n">rast</span> <span class="gmail-k">from</span> <span class="gmail-n">w_aoi</span> <span class="gmail-k">union</span> <span class="gmail-k">all</span></span>
<span id="gmail-LC12" class="gmail-line" lang="sql">    <span class="gmail-k">select</span> <span class="gmail-s1">'10km'</span> <span class="gmail-k">as</span> <span class="gmail-n">dscr</span><span class="gmail-p">,</span> <span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-n">st_asraster</span><span class="gmail-p">(</span><span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-mi">10000</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">10000</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">0</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-mi">0</span><span class="gmail-p">.</span><span class="gmail-mi">0</span><span class="gmail-p">,</span> <span class="gmail-s1">'8BUI'</span><span class="gmail-p">)</span> <span class="gmail-k">as</span> <span class="gmail-n">rast</span> <span class="gmail-k">from</span> <span class="gmail-n">w_aoi</span></span>
<span id="gmail-LC13" class="gmail-line" lang="sql"><span class="gmail-p">)</span></span>
<span id="gmail-LC14" class="gmail-line" lang="sql"><span class="gmail-p">,</span> <span class="gmail-n">w_out</span> <span class="gmail-k">as</span> <span class="gmail-p">(</span></span>
<span id="gmail-LC15" class="gmail-line" lang="sql">    <span class="gmail-k">select</span> </span>
<span id="gmail-LC16" class="gmail-line" lang="sql">        <span class="gmail-n">dscr</span><span class="gmail-p">,</span> </span>
<span id="gmail-LC17" class="gmail-line" lang="sql">        <span class="gmail-n">aoi</span><span class="gmail-p">,</span> </span>
<span id="gmail-LC18" class="gmail-line" lang="sql">        <span class="gmail-n">rast</span><span class="gmail-p">,</span> </span>
<span id="gmail-LC19" class="gmail-line" lang="sql">        <span class="gmail-n">st_convexhull</span><span class="gmail-p">(</span><span class="gmail-n">rast</span><span class="gmail-p">)</span> <span class="gmail-k">as</span> <span class="gmail-n">c_h_geom</span><span class="gmail-p">,</span> </span>
<span id="gmail-LC20" class="gmail-line" lang="sql">        <span class="gmail-p">(</span><span class="gmail-n">ST_PixelAsPolygons</span><span class="gmail-p">(</span><span class="gmail-n">rast</span><span class="gmail-p">,</span> <span class="gmail-mi">1</span><span class="gmail-p">,</span> <span class="gmail-k">false</span><span class="gmail-p">)).</span><span class="gmail-n">geom</span> <span class="gmail-k">as</span> <span class="gmail-n">pix_geom</span><span class="gmail-p">,</span> </span>
<span id="gmail-LC21" class="gmail-line" lang="sql">        <span class="gmail-p">(</span><span class="gmail-n">ST_PixelAsPolygons</span><span class="gmail-p">(</span><span class="gmail-n">rast</span><span class="gmail-p">,</span> <span class="gmail-mi">1</span><span class="gmail-p">,</span> <span class="gmail-k">true</span><span class="gmail-p">)).</span><span class="gmail-n">geom</span> <span class="gmail-k">as</span> <span class="gmail-n">pix_exclND_geom</span></span>
<span id="gmail-LC22" class="gmail-line" lang="sql">    <span class="gmail-k">from</span> <span class="gmail-n">w_rast</span></span>
<span id="gmail-LC23" class="gmail-line" lang="sql"><span class="gmail-p">)</span></span>
<span id="gmail-LC24" class="gmail-line" lang="sql"><span class="gmail-k">select</span> <span class="gmail-n">row_number</span><span class="gmail-p">()</span> <span class="gmail-n">over</span><span class="gmail-p">()</span> <span class="gmail-k">as</span> <span class="gmail-n">gid</span><span class="gmail-p">,</span> <span class="gmail-n">dscr</span><span class="gmail-p">,</span> <span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-n">rast</span><span class="gmail-p">,</span> <span class="gmail-n">c_h_geom</span><span class="gmail-p">,</span> </span>
<span id="gmail-LC25" class="gmail-line" lang="sql">    <span class="gmail-n">st_collect</span><span class="gmail-p">(</span><span class="gmail-n">pix_geom</span><span class="gmail-p">)</span> <span class="gmail-k">as</span> <span class="gmail-n">pixs_geom</span><span class="gmail-p">,</span></span>
<span id="gmail-LC26" class="gmail-line" lang="sql">    <span class="gmail-n">st_collect</span><span class="gmail-p">(</span><span class="gmail-n">pix_exclND_geom</span><span class="gmail-p">)</span> <span class="gmail-k">as</span> <span class="gmail-n">pixs_exclN_geom</span></span>
<span id="gmail-LC27" class="gmail-line" lang="sql"><span class="gmail-k">from</span> <span class="gmail-n">w_out</span> </span>
<span id="gmail-LC28" class="gmail-line" lang="sql"><span class="gmail-k">group</span> <span class="gmail-k">by</span> <span class="gmail-n">dscr</span><span class="gmail-p">,</span> <span class="gmail-n">aoi</span><span class="gmail-p">,</span> <span class="gmail-n">rast</span><span class="gmail-p">,</span> <span class="gmail-n">c_h_geom</span></span>
<span id="gmail-LC29" class="gmail-line" lang="sql"><span class="gmail-p">;</span></span>
</pre>

</div><div>and can be found also in [1]. It will create view showing 
geometry being rasterizd (aoi), raster, convex hull of raster and 
vectorized raster using two variants of ST_PixelAsPolygons (with and without NoData pixels).<div><div><br></div><div>
<div>Given by ConvexHull and two lower pixels in <span class="gmail-n">pixs_geom</span><span class="gmail-p"></span> (image included in 
 [1]) it seems, that raster contains two pixels with 
NoData value, which doesn't intersect with bounding box of vectorized 
polygon (dashed line). How is determined extent of the raster? Is it
described somewhere in docs? <br></div><div><br></div><div>Thank you, with best regards, Jiří Fejfar</div><div><br></div><div>[1] 
<a href="https://gitlab.com/-/snippets/2071456">https://gitlab.com/-/snippets/2071456</a>

</div></div></div></div>

</div>