Sean Gillies (Posts about masks)
https://sgillies.net/tags/masks.atom
2023-12-31T01:26:20Z
Sean Gillies
Nikola
Rasterio windows and masks
https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html
2013-12-21T00:00:00-07:00
2013-12-21T00:00:00-07:00
Sean Gillies
<p>Rasterio 0.4 is up on PyPI: <a class="reference external" href="https://pypi.python.org/pypi/rasterio/0.4">https://pypi.python.org/pypi/rasterio/0.4</a>. The new
features since the last time I blogged about rasterio are <a class="reference external" href="https://github.com/sgillies/rasterio/blob/master/docs/windowed-rw.rst">windowed</a> and
<a class="reference external" href="https://github.com/sgillies/rasterio/blob/master/docs/nodata-masks.rst">masked</a>
reads and writes. Here's an example that combines both features and demonstrates
a good pattern for reading raster data.</p>
<div class="code"><pre class="code python"><a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-1" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-1" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-1"></a><span class="kn">import</span> <span class="nn">numpy</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-2" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-2" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-2"></a><span class="kn">import</span> <span class="nn">rasterio</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-3" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-3" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-3"></a>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-4" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-4" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-4"></a><span class="k">with</span> <span class="n">rasterio</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'rasterio/tests/data/RGB.byte.tif'</span><span class="p">)</span> <span class="k">as</span> <span class="n">src</span><span class="p">:</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-5" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-5" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-5"></a>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-6" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-6" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-6"></a> <span class="c1"># The following asserts that all bands have the same block/stripe structure.</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-7" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-7" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-7"></a> <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">src</span><span class="o">.</span><span class="n">block_shapes</span><span class="p">))</span> <span class="o">==</span> <span class="mi">1</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-8" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-8" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-8"></a>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-9" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-9" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-9"></a> <span class="c1"># As they do, we can iterate over the first band's block windows</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-10" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-10" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-10"></a> <span class="c1"># and apply to all other bands.</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-11" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-11" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-11"></a> <span class="k">for</span> <span class="n">ji</span><span class="p">,</span> <span class="n">window</span> <span class="ow">in</span> <span class="n">src</span><span class="o">.</span><span class="n">block_windows</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-12" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-12" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-12"></a>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-13" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-13" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-13"></a> <span class="c1"># Get data in this window for each band as a Numpy masked array.</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-14" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-14" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-14"></a> <span class="n">bands</span> <span class="o">=</span> <span class="p">[</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-15" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-15" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-15"></a> <span class="n">numpy</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">masked_equal</span><span class="p">(</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-16" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-16" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-16"></a> <span class="n">src</span><span class="o">.</span><span class="n">read_band</span><span class="p">(</span><span class="n">src</span><span class="o">.</span><span class="n">indexes</span><span class="p">[</span><span class="n">i</span><span class="p">]),</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-17" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-17" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-17"></a> <span class="n">src</span><span class="o">.</span><span class="n">nodatavals</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-18" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-18" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-18"></a> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">src</span><span class="o">.</span><span class="n">count</span><span class="p">)]</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-19" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-19" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-19"></a>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-20" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-20" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-20"></a> <span class="c1"># Break out after the first block in this example.</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-21" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-21" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-21"></a> <span class="k">break</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-22" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-22" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-22"></a>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-23" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-23" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-23"></a><span class="c1"># Let's look at the first, blue band.</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-24" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-24" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-24"></a><span class="n">b</span> <span class="o">=</span> <span class="n">bands</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-25" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-25" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-25"></a><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-26" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-26" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-26"></a><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">mask</span><span class="p">)</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-27" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-27" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-27"></a><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">fill_value</span><span class="p">)</span>
<a id="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-28" name="rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-28" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_0d05dcbbfbb64cdcb6d8e317ccd19b56-28"></a><span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">min</span><span class="p">(),</span> <span class="n">b</span><span class="o">.</span><span class="n">max</span><span class="p">(),</span> <span class="n">b</span><span class="o">.</span><span class="n">mean</span><span class="p">())</span>
</pre></div>
<p>The output:</p>
<div class="code"><pre class="code console"><a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-1" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-1" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-1"></a><span class="go">[[-- -- -- ..., -- -- --]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-2" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-2" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-2"></a><span class="go"> [-- -- -- ..., -- -- --]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-3" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-3" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-3"></a><span class="go"> [-- -- -- ..., -- -- --]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-4" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-4" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-4"></a><span class="go"> ...,</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-5" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-5" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-5"></a><span class="go"> [-- -- -- ..., -- -- --]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-6" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-6" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-6"></a><span class="go"> [-- -- -- ..., -- -- --]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-7" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-7" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-7"></a><span class="go"> [-- -- -- ..., -- -- --]]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-8" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-8" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-8"></a><span class="go">[[ True True True ..., True True True]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-9" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-9" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-9"></a><span class="go"> [ True True True ..., True True True]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-10" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-10" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-10"></a><span class="go"> [ True True True ..., True True True]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-11" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-11" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-11"></a><span class="go"> ...,</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-12" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-12" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-12"></a><span class="go"> [ True True True ..., True True True]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-13" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-13" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-13"></a><span class="go"> [ True True True ..., True True True]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-14" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-14" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-14"></a><span class="go"> [ True True True ..., True True True]]</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-15" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-15" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-15"></a><span class="go">0</span>
<a id="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-16" name="rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-16" href="https://sgillies.net/2013/12/21/rasterio-windows-and-masks.html#rest_code_34149ca7b1de40cc969aa9e79e9a3ae7-16"></a><span class="gp gp-VirtualEnv">(1, 255, 44.434478650699106)</span>
</pre></div>
<p>You can see that all four corners of the test image are masked.</p>