<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Hendesson</title>
<link>https://hendesson.github.io/en/articles/</link>
<atom:link href="https://hendesson.github.io/en/articles/index.xml" rel="self" type="application/rss+xml"/>
<description>Hands-on articles with public data from INPE, MMA, IBAMA and SICAR (Brazil). Each article has a version for each program, with tested code.</description>
<image>
<url>https://hendesson.github.io/en/../imagens/artigos/capa_artigos.png</url>
<title>Hendesson</title>
<link>https://hendesson.github.io/en/articles/</link>
</image>
<generator>quarto-1.10.19</generator>
<lastBuildDate>Thu, 29 Oct 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>From ArcMap to QGIS: the same tool in every program</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/01-equivalences.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Beginner <span class="rotulo">Reading</span> 6 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>Many monitoring scripts were written for <strong>ArcMap</strong> (Python 2 and <code>arcpy.mapping</code>) and later migrated in a hurry to <strong>ArcGIS Pro</strong>. Anyone who leaves a government agency, or wants to run the same analysis without a license, needs to know <strong>which tool does the same thing</strong> in another program.</p>
<p>This article is the reference for the others: an equivalence table and the most common changes when you update an old script.</p>
<div class="faixa-icones">
<p><span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"> <img src="https://hendesson.github.io/en/imagens/ferramentas/pandas.svg" class="img-fluid" alt="pandas"></p>
</div>
<section id="the-tools-side-by-side" class="level2">
<h2 class="anchored" data-anchor-id="the-tools-side-by-side">The tools side by side</h2>
<p>The columns show the tool name (ArcGIS), the algorithm ID in QGIS (the same one used in <code>processing.run</code>), the PostGIS function and the geopandas method.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 16%">
<col style="width: 16%">
<col style="width: 16%">
<col style="width: 16%">
<col style="width: 16%">
<col style="width: 16%">
</colgroup>
<thead>
<tr class="header">
<th>Operation</th>
<th>ArcMap 10.x</th>
<th>ArcGIS Pro (<code>arcpy</code>)</th>
<th>QGIS (Processing)</th>
<th>PostGIS</th>
<th>Python (geopandas)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Select by attribute</td>
<td><code>Select_analysis</code></td>
<td><code>analysis.Select</code></td>
<td><code>native:extractbyexpression</code></td>
<td><code>WHERE</code></td>
<td><code>gdf[gdf.year == 2025]</code></td>
</tr>
<tr class="even">
<td>Select by location</td>
<td><code>SelectLayerByLocation_management</code></td>
<td><code>management.SelectLayerByLocation</code></td>
<td><code>native:extractbylocation</code></td>
<td><code>WHERE EXISTS (… ST_Intersects …)</code></td>
<td><code>gpd.sjoin(…, predicate="intersects")</code></td>
</tr>
<tr class="odd">
<td>Clip</td>
<td><code>Clip_analysis</code></td>
<td><code>analysis.PairwiseClip</code></td>
<td><code>native:clip</code></td>
<td><code>ST_Intersection</code></td>
<td><code>gpd.clip</code></td>
</tr>
<tr class="even">
<td>Intersect (keeping attributes of both layers)</td>
<td><code>Intersect_analysis</code></td>
<td><code>analysis.PairwiseIntersect</code></td>
<td><code>native:intersection</code></td>
<td><code>ST_Intersection</code> + <code>JOIN</code></td>
<td><code>gpd.overlay(how="intersection")</code></td>
</tr>
<tr class="odd">
<td>Erase (keep what is outside)</td>
<td><code>Erase_analysis</code> (Advanced license only)</td>
<td><code>analysis.PairwiseErase</code></td>
<td><code>native:difference</code></td>
<td><code>ST_Difference</code></td>
<td><code>gpd.overlay(how="difference")</code></td>
</tr>
<tr class="even">
<td>Buffer</td>
<td><code>Buffer_analysis</code></td>
<td><code>analysis.PairwiseBuffer</code></td>
<td><code>native:buffer</code></td>
<td><code>ST_Buffer(geom::geography, m)</code></td>
<td><code>gdf.to_crs(5880).buffer(m)</code></td>
</tr>
<tr class="odd">
<td>Dissolve</td>
<td><code>Dissolve_management</code></td>
<td><code>analysis.PairwiseDissolve</code></td>
<td><code>native:dissolve</code></td>
<td><code>ST_Union</code> + <code>GROUP BY</code></td>
<td><code>gdf.dissolve(by=…)</code></td>
</tr>
<tr class="even">
<td>Group nearby polygons</td>
<td><code>AggregatePolygons_cartography</code></td>
<td><code>cartography.AggregatePolygons</code></td>
<td><code>native:buffer</code> + <code>native:dissolve</code></td>
<td><code>ST_ClusterDBSCAN</code>, <code>ST_ClusterWithin</code></td>
<td>buffer + <code>unary_union</code></td>
</tr>
<tr class="odd">
<td>Join a table by field</td>
<td><code>JoinField_management</code></td>
<td><code>management.JoinField</code></td>
<td><code>native:joinattributestable</code></td>
<td><code>JOIN … USING (cnuc)</code></td>
<td><code>df.merge(…)</code></td>
</tr>
<tr class="even">
<td>Spatial join</td>
<td><code>SpatialJoin_analysis</code></td>
<td><code>analysis.SpatialJoin</code></td>
<td><code>native:joinattributesbylocation</code></td>
<td><code>JOIN … ON ST_Intersects(…)</code></td>
<td><code>gpd.sjoin</code></td>
</tr>
<tr class="odd">
<td>Summary table</td>
<td><code>Frequency_analysis</code></td>
<td><code>analysis.Statistics</code></td>
<td><code>qgis:statisticsbycategories</code></td>
<td><code>GROUP BY</code></td>
<td><code>groupby().agg()</code></td>
</tr>
<tr class="even">
<td>Area in hectares</td>
<td><code>!shape.geodesicArea@hectares!</code></td>
<td><code>CalculateGeometryAttributes</code> (<code>AREA_GEODESIC</code>)</td>
<td><code>$area / 10000</code> (ellipsoidal)</td>
<td><code>ST_Area(geom::geography) / 10000</code></td>
<td><code>to_crs("ESRI:102033").area / 10000</code></td>
</tr>
<tr class="odd">
<td>Polygon → point</td>
<td><code>FeatureToPoint_management</code></td>
<td><code>management.FeatureToPoint</code> (<code>INSIDE</code>)</td>
<td><code>native:pointonsurface</code></td>
<td><code>ST_PointOnSurface</code></td>
<td><code>representative_point()</code></td>
</tr>
<tr class="even">
<td>Repair geometry</td>
<td><code>RepairGeometry_management</code></td>
<td><code>management.RepairGeometry</code></td>
<td><code>native:fixgeometries</code></td>
<td><code>ST_MakeValid</code></td>
<td><code>make_valid()</code></td>
</tr>
<tr class="odd">
<td>Kernel density</td>
<td><code>sa.KernelDensity</code></td>
<td><code>sa.KernelDensity</code></td>
<td><code>native:heatmapkerneldensityestimation</code></td>
<td>grid + weighted sum</td>
<td><code>numpy</code></td>
</tr>
<tr class="even">
<td>Zonal statistics</td>
<td><code>sa.ZonalStatisticsAsTable</code></td>
<td><code>sa.ZonalStatisticsAsTable</code></td>
<td><code>native:zonalstatisticsfb</code></td>
<td><code>ST_SummaryStats</code> (<code>postgis_raster</code>)</td>
<td><code>rasterstats.zonal_stats</code></td>
</tr>
<tr class="odd">
<td>Delete and append records</td>
<td><code>DeleteFeatures</code> + <code>Append</code></td>
<td><code>DeleteFeatures</code> + <code>Append</code></td>
<td>save over the layer</td>
<td><code>DELETE</code> + <code>INSERT</code>, <code>ON CONFLICT</code></td>
<td><code>to_postgis(if_exists=…)</code></td>
</tr>
<tr class="even">
<td>Map series</td>
<td><code>arcpy.mapping</code> (<code>.mxd</code>)</td>
<td><code>arcpy.mp</code> (<code>.aprx</code>), map series</td>
<td>Atlas</td>
<td>not applicable</td>
<td>loop with <code>matplotlib</code></td>
</tr>
</tbody>
</table>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Why “Pairwise”?
</div>
</div>
<div class="callout-body-container callout-body">
<p>In ArcGIS Pro, the <code>Pairwise…</code> tools (Clip, Intersect, Erase, Dissolve, Buffer) use parallel processing and are usually much faster than the classic ones. For the overlays in these articles, the result is the same.</p>
</div>
</div>
</section>
<section id="each-program-in-one-sentence" class="level2">
<h2 class="anchored" data-anchor-id="each-program-in-one-sentence">Each program in one sentence</h2>
<dl>
<dt><span class="selo-texto">ArcGIS Pro</span></dt>
<dd>
Paid. <code>arcpy</code> comes with it and runs in ArcGIS’s own Python. Good when the institution already has a license and publishes to ArcGIS Online.
</dd>
<dt>QGIS</dt>
<dd>
Free. Everything you do in the menus is a <strong>Processing</strong> algorithm that can be called from the Python console (<code>processing.run</code>), from graphical models or from the terminal (<code>qgis_process</code>).
</dd>
<dt>PostGIS</dt>
<dd>
Free. Best when the data is large, lives in a database and the analysis is repeated every week. The result is ready for Power BI.
</dd>
<dt>Python (geopandas)</dt>
<dd>
Free. Good for automating without opening any program and for making charts and maps. For very large data, leave the heavy overlays to PostGIS.
</dd>
</dl>
</section>
<section id="updating-an-old-arcmap-script" class="level2">
<h2 class="anchored" data-anchor-id="updating-an-old-arcmap-script">Updating an old ArcMap script</h2>
<p>ArcMap scripts use <strong>Python 2.7</strong>. ArcGIS Pro uses <strong>Python 3</strong>. The changes that show up most often:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>In the old script</th>
<th>In ArcGIS Pro</th>
<th>Why</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>print "text"</code></td>
<td><code>print("text")</code></td>
<td>Python 3</td>
</tr>
<tr class="even">
<td><code>arcpy.SearchCursor(fc)</code> and <code>row.getValue("field")</code></td>
<td><code>arcpy.da.SearchCursor(fc, ["field"])</code></td>
<td><code>da</code> cursors are much faster</td>
</tr>
<tr class="odd">
<td><code>arcpy.mapping.MapDocument("x.mxd")</code></td>
<td><code>arcpy.mp.ArcGISProject("x.aprx")</code></td>
<td><code>.mxd</code> does not exist in Pro (import it with <strong>Insert → Import Map</strong>)</td>
</tr>
<tr class="even">
<td><code>arcpy.mapping.ExportToJPEG(mxd, …)</code></td>
<td><code>layout.exportToJPEG(…)</code></td>
<td>exporting is now done by the layout</td>
</tr>
<tr class="odd">
<td><code>"PYTHON_9.3"</code> in <code>CalculateField</code></td>
<td><code>"PYTHON3"</code></td>
<td>the expression type changed</td>
</tr>
<tr class="even">
<td><code>"in_memory\\x"</code></td>
<td><code>"memory\\x"</code></td>
<td>Pro’s in-memory workspace</td>
</tr>
<tr class="odd">
<td><code>.tbx</code> toolbox</td>
<td><code>.atbx</code></td>
<td>Pro still opens <code>.tbx</code> but saves <code>.atbx</code></td>
</tr>
<tr class="even">
<td>files with accents in ISO-8859-1</td>
<td>save as UTF-8</td>
<td>Python 3 reads UTF-8 by default</td>
</tr>
</tbody>
</table>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Three pitfalls I found in old scripts
</div>
</div>
<div class="callout-body-container callout-body">
<ol type="1">
<li><strong>Functions named <code>max</code> and <code>min</code></strong> that override Python’s own and sort the whole list on every row read. With 100,000 rows, the script grinds to a halt. Use Python’s built-in <code>max()</code> over the cursor, or the <strong>Statistics</strong> tool.</li>
<li><strong>Comma as decimal separator</strong> in text parameters (<code>"0,0015"</code>) mixed with a period (<code>"0.0015"</code>) in the same script. The result changes with the Windows language. Pass numbers as numbers, not text.</li>
<li><strong>Network paths and passwords written in the code.</strong> Read paths from tool parameters (<code>GetParameterAsText</code>) and passwords from environment variables or a file outside the script. <a href="https://hendesson.github.io/guia-espacial/curso/06-python.html">Module 6</a> (in Portuguese) shows how.</li>
</ol>
</div>
</div>
</section>
<section id="from-an-arcgis-script-tool-to-a-qgis-script" class="level2">
<h2 class="anchored" data-anchor-id="from-an-arcgis-script-tool-to-a-qgis-script">From an ArcGIS script tool to a QGIS script</h2>
<p>In ArcGIS, a script becomes a tool by reading parameters with <code>arcpy.GetParameterAsText(0)</code>. In QGIS, the equivalent is a <strong>Processing script</strong>, which shows up in the Toolbox with its own form. Variable names in the code are in Portuguese, as in the original scripts (<code>entrada</code> = input, <code>saida</code> = output, <code>ucs</code> = protected areas).</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"></span>
<span id="cb1-3">entrada <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.GetParameterAsText(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alerts layer</span></span>
<span id="cb1-4">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.GetParameterAsText(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># protected area boundaries</span></span>
<span id="cb1-5">saida <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.GetParameterAsText(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># output layer</span></span>
<span id="cb1-6"></span>
<span id="cb1-7">arcpy.analysis.PairwiseIntersect([entrada, ucs], saida)</span>
<span id="cb1-8">arcpy.AddMessage(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Done."</span>)</span></code></pre></div></div>
<p>In the toolbox: right-click → <strong>New → Script Tool</strong>, and declare the three parameters.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>Save it in <strong>Toolbox → Scripts → Create New Script</strong>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> qgis.processing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> alg</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@alg</span>(name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alertas_em_ucs"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alerts in protected areas"</span>, group<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"monitoramento"</span>,</span>
<span id="cb2-5">     group_label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monitoring"</span>)</span>
<span id="cb2-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@alg.input</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alg.SOURCE, name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ENTRADA"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alerts layer"</span>)</span>
<span id="cb2-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@alg.input</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alg.SOURCE, name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UCS"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Protected area boundaries"</span>)</span>
<span id="cb2-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">@alg.input</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alg.SINK, name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SAIDA"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alerts in protected areas"</span>)</span>
<span id="cb2-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> alertas_em_ucs(instance, parameters, context, feedback, inputs):</span>
<span id="cb2-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Intersects the alerts with the protected areas, keeping the attributes of both layers."""</span></span>
<span id="cb2-11">    resultado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> processing.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:intersection"</span>, {</span>
<span id="cb2-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT"</span>: parameters[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ENTRADA"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OVERLAY"</span>: parameters[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UCS"</span>],</span>
<span id="cb2-13">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>: parameters[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SAIDA"</span>]}, context<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>context, feedback<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>feedback)</span>
<span id="cb2-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SAIDA"</span>: resultado[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]}</span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>In the database, the “tool” is a query saved as a <strong>view</strong> (or a SQL function):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OR</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">REPLACE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VIEW</span> alertas_em_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> a.<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>, u.cnuc, u.nome_uc,</span>
<span id="cb3-3">       ST_Intersection(a.geom, u.geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom_uc</span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter a</span>
<span id="cb3-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.ucs u <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> ST_Intersects(a.geom, u.geom);</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<p>A script with command-line arguments:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sys</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-3"></span>
<span id="cb4-4">entrada, ucs, saida <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sys.argv[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb4-5">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(entrada)</span>
<span id="cb4-6">limites <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(ucs).to_crs(alertas.crs)</span>
<span id="cb4-7">gpd.overlay(alertas, limites, how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intersection"</span>).to_file(saida)</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> alertas_em_ucs.py deter.gpkg ucs.gpkg alertas_ucs.gpkg</span></code></pre></div></div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The tabs above are <strong>synchronized</strong> across all articles: pick your program once and the other articles open on it.</p>
</div>
</div>
</section>
<section id="next-articles" class="level2">
<h2 class="anchored" data-anchor-id="next-articles">Next articles</h2>
<p>The following articles put this table into practice, always with the four versions. Start with <a href="../articles/02-inside-and-around.html">Inside and around protected areas</a>.</p>
<div class="navegacao-livro">
<p><a href="../articles/02-inside-and-around.html" class="proximo"><span class="direcao">Next →</span> Inside and around protected areas: the overlay every report uses</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/01-equivalencias.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {From {ArcMap} to {QGIS:} The Same Tool in Every Program},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/01-equivalences.html},
  doi = {10.5281/zenodo.23780245},
  langid = {en},
  abstract = {Still running ArcMap scripts, or want to stop depending on
    a license? This article gives the equivalence table between ArcMap,
    ArcGIS Pro, QGIS, PostGIS and geopandas for the most common
    geoprocessing tools, plus the changes almost every old script needs
    to run today.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“From ArcMap to QGIS: The Same Tool in
Every Program.”</span> In <em>Geoprocessing Articles for Environmental
Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780245">https://doi.org/10.5281/zenodo.23780245</a>.
</div></div></section></div> ]]></description>
  <category>Fundamentals</category>
  <category>ArcMap</category>
  <guid>https://hendesson.github.io/en/articles/01-equivalences.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/equivalencias.png" medium="image" type="image/png" height="69" width="144"/>
</item>
<item>
  <title>Inside and around protected areas: the overlay every report uses</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/02-inside-and-around.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Beginner <span class="rotulo">Reading</span> 9 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>Almost every monitoring report answers the same question: <strong>how much of the deforestation (or fire) happened inside the protected area, and how much right next to it?</strong> The surroundings matter because that is where the pressure comes from.</p>
<p>In Brazil, protected areas are called <em>Unidades de Conservação</em> (UCs). In the scripts I used, this was done with a protected-area layer that already included the surrounding band, a <code>0/1</code> field and an ArcGIS <code>Intersect</code>. Here you will build that layer from scratch and run the overlay in four programs.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/interior_entorno_zonas.png" class="img-fluid figure-img" alt="Map of the Novo Progresso region with protected areas in green, a yellow 3 km band around them and DETER alerts in blue inside and in orange in the surroundings. Where two protected areas touch, there is no yellow band."></p>
<figcaption>Inside (green) and 3 km surrounding band (yellow) of the protected areas in the study area, with DETER alerts since 2024.</figcaption>
</figure>
</div>
<section id="what-you-will-do" class="level2">
<h2 class="anchored" data-anchor-id="what-you-will-do">What you will do</h2>
<div class="cell" data-eval="true" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
    A["Protected&lt;br/&gt;areas"] --&gt; B["3 km buffer&lt;br/&gt;(in meters)"]
    B --&gt; C["Erase all&lt;br/&gt;protected areas"]
    C --&gt; D["Surroundings"]
    A --&gt; E["Inside"]
    D --&gt; F["Merge&lt;br/&gt;'local' field"]
    E --&gt; F
    F --&gt; G["Intersect&lt;br/&gt;with alerts"]
    G --&gt; H["Area in ha&lt;br/&gt;and summary"]
</pre>
</div>
<p></p><figcaption> Steps of the overlay</figcaption> </figure><p></p>
</div>
</div>
</div>
<ol start="0" type="1">
<li><strong>Repair the geometries.</strong> 830 of the 7,823 alerts in the study area (11%) have invalid geometry, and some programs stop in the middle of the overlay because of it.</li>
<li><strong>Surroundings:</strong> a 3 km buffer around each protected area, minus the area of <strong>all</strong> protected areas. That way, an alert inside a neighboring area counts as inside that one, not as the surroundings of the other.</li>
<li><strong>Zones:</strong> merge inside and surroundings into a single layer with a <code>local</code> field (<code>interior</code> or <code>entorno</code>).</li>
<li><strong>Intersect:</strong> cut each alert by the zones. An alert crossing the boundary becomes two pieces.</li>
<li><strong>Area and summary:</strong> area in hectares of each piece, summed by protected area and zone.</li>
</ol>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>Use the articles’ GeoPackage (layers <code>ucs</code> and <code>deter</code>). See how to download it in <a href="../articles/about.html#data">Articles data</a>.</p>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Variable and field names are in Portuguese, as in the data: <code>ucs</code> = protected areas, <code>entorno</code> = surroundings, <code>faixa</code> = band, <code>zonas</code> = zones, <code>cruzado</code> = overlaid, <code>resumo</code> = summary.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"></span>
<span id="cb1-3">arcpy.env.overwriteOutput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-4">gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg"</span></span>
<span id="cb1-5">gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb"</span>          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create it first with CreateFileGDB</span></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0. Copy to memory and repair the geometries</span></span>
<span id="cb1-8">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao = 'Amazonia'"</span>)</span>
<span id="cb1-9">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.CopyFeatures(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">deter"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter"</span>)</span>
<span id="cb1-10">arcpy.management.RepairGeometry(ucs)</span>
<span id="cb1-11">arcpy.management.RepairGeometry(deter)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Surroundings: 3 km geodesic buffer minus all protected areas</span></span>
<span id="cb1-14">faixa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseBuffer(ucs, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aixa"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3000 Meters"</span>, method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GEODESIC"</span>)</span>
<span id="cb1-15">entorno <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseErase(faixa, ucs, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">entorno"</span>)</span>
<span id="cb1-16"></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. "local" field and merge of the two layers</span></span>
<span id="cb1-18">arcpy.management.CalculateField(ucs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'interior'"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>, field_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>)</span>
<span id="cb1-19">arcpy.management.CalculateField(entorno, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'entorno'"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>, field_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>)</span>
<span id="cb1-20">zonas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.Merge([ucs, entorno], <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\z</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">onas"</span>)</span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Intersect the alerts with the zones</span></span>
<span id="cb1-23">cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseIntersect([deter, zonas], gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter_interior_entorno"</span>)</span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Geodesic area in hectares and summary table</span></span>
<span id="cb1-26">arcpy.management.CalculateGeometryAttributes(</span>
<span id="cb1-27">    cruzado, [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AREA_GEODESIC"</span>]], area_unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HECTARES"</span>)</span>
<span id="cb1-28">arcpy.analysis.Statistics(cruzado, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esumo_interior_entorno"</span>,</span>
<span id="cb1-29">                          [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUM"</span>], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COUNT"</span>]], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>])</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>In <strong>ArcMap</strong>, the <code>Erase</code> tool is only available with an Advanced license. With a Basic license, run a <code>Union</code> of the band with the protected areas and delete the parts that have a protected area. In ArcGIS Pro, use <code>PairwiseErase</code>.</p>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p><strong>From the menus</strong> (Processing Toolbox, <code>Ctrl+Alt+T</code>):</p>
<ol type="1">
<li><strong>Fix geometries</strong> on the <code>ucs</code> and <code>deter</code> layers.</li>
<li><strong>Reproject layer</strong> <code>ucs</code> to EPSG:5880 (in meters).</li>
<li><strong>Buffer</strong> of 3000 m, without dissolving.</li>
<li><strong>Difference</strong>: input = buffer, overlay = protected areas. The result is the surroundings.</li>
<li><strong>Field calculator</strong> on both layers: new field <code>local</code> with <code>'interior'</code> or <code>'entorno'</code>.</li>
<li><strong>Merge vector layers</strong> and <strong>Fix geometries</strong> again.</li>
<li><strong>Intersection</strong>: input = alerts, overlay = zones.</li>
<li><strong>Field calculator</strong>: <code>area_ha</code> = <code>area(transform($geometry, 'EPSG:4674', 'ESRI:102033')) / 10000</code>.</li>
<li><strong>Statistics by categories</strong>: field <code>area_ha</code>, categories <code>nome_uc</code> and <code>local</code>.</li>
</ol>
<p><strong>In the Python console</strong> (<code>Ctrl+Alt+P</code>), the same steps:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-2">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> rodar(alg, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>p):</span>
<span id="cb2-5">    p.setdefault(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>)</span>
<span id="cb2-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> processing.run(alg, p)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0 and 1. Amazon protected areas in meters, 3 km buffer and difference</span></span>
<span id="cb2-9">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs"</span>,</span>
<span id="cb2-10">            EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao = 'Amazonia'"</span>)</span>
<span id="cb2-11">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fixgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs)</span>
<span id="cb2-12">ucs_m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:reprojectlayer"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs, TARGET_CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:5880"</span>)</span>
<span id="cb2-13">faixa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:buffer"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs_m, DISTANCE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>, SEGMENTS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, DISSOLVE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb2-14">entorno <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:difference"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>faixa, OVERLAY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs_m)</span>
<span id="cb2-15"></span>
<span id="cb2-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. "local" field and merge</span></span>
<span id="cb2-17">interior <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs_m, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>,</span>
<span id="cb2-18">                 FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, FIELD_LENGTH<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'interior'"</span>)</span>
<span id="cb2-19">entorno <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>entorno, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>,</span>
<span id="cb2-20">                FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, FIELD_LENGTH<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'entorno'"</span>)</span>
<span id="cb2-21">zonas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:mergevectorlayers"</span>, LAYERS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[interior, entorno], CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:4674"</span>)</span>
<span id="cb2-22">zonas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fixgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>zonas)</span>
<span id="cb2-23"></span>
<span id="cb2-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Repaired alerts × zones</span></span>
<span id="cb2-25">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fixgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=deter"</span>)</span>
<span id="cb2-26">cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:intersection"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>deter, OVERLAY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>zonas,</span>
<span id="cb2-27">                OVERLAY_FIELDS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>])</span>
<span id="cb2-28">cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:deletecolumn"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cruzado, COLUMN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fid"</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># see pitfall 3</span></span>
<span id="cb2-29"></span>
<span id="cb2-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Area in hectares and summary</span></span>
<span id="cb2-31">cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cruzado, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb2-32">                FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-33">                FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area(transform($geometry, 'EPSG:4674', 'ESRI:102033')) / 10000"</span>,</span>
<span id="cb2-34">                OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/deter_interior_entorno.gpkg"</span>)</span>
<span id="cb2-35">resumo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qgis:statisticsbycategories"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cruzado, VALUES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb2-36">               CATEGORIES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>])</span>
<span id="cb2-37">QgsProject.instance().addMapLayers([zonas, resumo])</span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 1 and 2. Zones: inside (the protected area itself) and surroundings (3 km minus all areas)</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.zonas_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> cnuc, nome_uc, ST_MakeValid(geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.ucs</span>
<span id="cb3-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> regiao <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazonia'</span></span>
<span id="cb3-7">),</span>
<span id="cb3-8">todas <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> ST_Union(geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> ucs</span>
<span id="cb3-10">)</span>
<span id="cb3-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> cnuc, nome_uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'interior'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span>, geom</span>
<span id="cb3-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> ucs</span>
<span id="cb3-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UNION</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALL</span></span>
<span id="cb3-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> u.cnuc, u.nome_uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'entorno'</span>,</span>
<span id="cb3-15">       ST_Multi(ST_Difference(ST_Buffer(u.geom:<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geometry</span>, t.geom))</span>
<span id="cb3-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> ucs u, todas t;</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INDEX</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> artigos.zonas_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> gist (geom);</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 0 and 3. Repaired alerts × zones</span></span>
<span id="cb3-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.deter_interior_entorno <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> deter <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> gid, classname, view_date, municipality,</span>
<span id="cb3-24">           ST_CollectionExtract(ST_MakeValid(geom), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 3 = polygons only</span></span>
<span id="cb3-25">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter</span>
<span id="cb3-26">)</span>
<span id="cb3-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> d.gid, d.classname, d.view_date, d.municipality, z.cnuc, z.nome_uc, z.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span>,</span>
<span id="cb3-28">       ST_Intersection(d.geom, z.geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> deter d</span>
<span id="cb3-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.zonas_ucs z <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> ST_Intersects(d.geom, z.geom)</span>
<span id="cb3-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> ST_IsEmpty(d.geom);</span>
<span id="cb3-32"></span>
<span id="cb3-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 4. Area in hectares and summary</span></span>
<span id="cb3-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.deter_interior_entorno <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COLUMN</span> area_ha <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>;</span>
<span id="cb3-35"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> artigos.deter_interior_entorno <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> area_ha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ST_Area(geom:<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>;</span>
<span id="cb3-36"></span>
<span id="cb3-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> nome_uc, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> alertas, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(area_ha), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha</span>
<span id="cb3-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno</span>
<span id="cb3-39"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> nome_uc, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span></span>
<span id="cb3-40"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> nome_uc, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span>;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb4-3"></span>
<span id="cb4-4">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span></span>
<span id="cb4-5">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>).query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao == 'Amazonia'"</span>)</span>
<span id="cb4-6">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0. Repair geometries, dropping pieces that collapse into lines or points</span></span>
<span id="cb4-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> corrigir(gdf):</span>
<span id="cb4-10">    gdf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gdf.make_valid(method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"structure"</span>, keep_collapsed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> gdf[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>gdf.is_empty]</span>
<span id="cb4-12"></span>
<span id="cb4-13">ucs, deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corrigir(ucs), corrigir(deter)</span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Surroundings: 3 km band (in meters, EPSG:5880) minus all protected areas</span></span>
<span id="cb4-16">ucs_m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ucs.to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb4-17">entorno <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.GeoDataFrame(ucs_m[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>]],</span>
<span id="cb4-18">                           geometry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs_m.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>).difference(ucs_m.union_all()),</span>
<span id="cb4-19">                           crs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb4-20"></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Zones with the "local" field</span></span>
<span id="cb4-22">zonas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([ucs_m[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>]].assign(local<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interior"</span>),</span>
<span id="cb4-23">                   entorno.assign(local<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"entorno"</span>)])</span>
<span id="cb4-24">zonas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.GeoDataFrame(zonas, crs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>).to_crs(deter.crs)</span>
<span id="cb4-25"></span>
<span id="cb4-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Intersection and area in hectares (equal-area projection)</span></span>
<span id="cb4-27">cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.overlay(deter, zonas, how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intersection"</span>, keep_geom_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-28">cruzado[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cruzado.to_crs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ESRI:102033"</span>).area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span></span>
<span id="cb4-29">cruzado.to_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/deter_interior_entorno.gpkg"</span>)</span>
<span id="cb4-30"></span>
<span id="cb4-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Summary table</span></span>
<span id="cb4-32">resumo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (cruzado.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>])</span>
<span id="cb4-33">          .agg(alertas<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>), area_ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>))</span>
<span id="cb4-34">          .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).reset_index())</span>
<span id="cb4-35"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(resumo)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>I ran the QGIS, PostGIS and Python versions on the same data (DETER alerts from January 2024 to October 2026). QGIS and Python gave <strong>identical</strong> numbers. PostGIS came within 0.2%, because it computes the buffer and the area on the ellipsoid, not on a projection.</p>
<table class="table-striped caption-top table">
<caption>Area of DETER alerts (all classes, including burn scars and degradation) by protected area and zone.</caption>
<thead>
<tr class="header">
<th>Protected area</th>
<th style="text-align: right;">Inside (ha)</th>
<th style="text-align: right;">Surroundings (ha)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Jamanxim National Forest</td>
<td style="text-align: right;">154,419</td>
<td style="text-align: right;">13,165</td>
</tr>
<tr class="even">
<td>Altamira National Forest</td>
<td style="text-align: right;">61,256</td>
<td style="text-align: right;">5,055</td>
</tr>
<tr class="odd">
<td>Jamanxim National Park</td>
<td style="text-align: right;">28,058</td>
<td style="text-align: right;">5,228</td>
</tr>
<tr class="even">
<td>Tapajós Environmental Protection Area</td>
<td style="text-align: right;">16,750</td>
<td style="text-align: right;">6,106</td>
</tr>
<tr class="odd">
<td>Rio Novo National Park</td>
<td style="text-align: right;">10,924</td>
<td style="text-align: right;">0.1 (see pitfall 2)</td>
</tr>
<tr class="even">
<td>Iriri State Forest</td>
<td style="text-align: right;">467</td>
<td style="text-align: right;">—</td>
</tr>
</tbody>
</table>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/interior_entorno_barras.png" class="img-fluid figure-img" alt="Horizontal bar chart with the area of alerts inside (green) and around (orange) each protected area. Jamanxim National Forest has by far the largest area, with more than 150,000 hectares inside."></p>
<figcaption>Area of alerts inside and around each protected area.</figcaption>
</figure>
</div>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>1. Buffer in degrees
</div>
</div>
<div class="callout-body-container callout-body">
<p>SIRGAS 2000 (EPSG:4674) is in <strong>degrees</strong>. A buffer of <code>3000</code> in this coordinate system creates a band 3,000 degrees wide, covering the whole planet. Reproject to meters (EPSG:5880), use <code>::geography</code> in PostGIS or <code>method="GEODESIC"</code> in ArcGIS Pro.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>2. Protected area at the edge of the study area (and slivers)
</div>
</div>
<div class="callout-body-container callout-body">
<p>Rio Novo National Park shows only <strong>0.1 ha of alerts in its surroundings</strong>. Checking, 58% of its surrounding band lies <strong>outside the area we downloaded</strong>: the park is at the edge of the study area, and the alerts there were not downloaded. When downloading data, extend the area by at least the width of the surrounding band.</p>
<p>The 21 remaining pieces are <strong>slivers</strong>: edges of alerts that lie almost entirely inside a neighboring protected area (the largest is 0.03 ha). For a report, drop pieces smaller than 0.01 ha.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>3. Duplicate <code>fid</code> in QGIS
</div>
</div>
<div class="callout-body-container callout-body">
<p>When saving the intersection to a GeoPackage, QGIS raises <code>UNIQUE constraint failed: fid</code>. An alert cut into two pieces carries the same <code>fid</code> to both. Delete the <code>fid</code> field before saving (<code>native:deletecolumn</code>).</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>4. Overlapping protected areas
</div>
</div>
<div class="callout-body-container callout-body">
<p>Some protected areas overlap (an Environmental Protection Area may contain a National Forest). In that case the same alert counts as inside both, and the sum over all areas is larger than the deforested area. For an overall total, dissolve the protected areas first, or sum the alert area without the overlay.</p>
</div>
</div>
</section>
<section id="variations" class="level2">
<h2 class="anchored" data-anchor-id="variations">Variations</h2>
<ul>
<li><strong>Outside any protected area, with no distance limit:</strong> instead of the buffer, use <strong>Erase/Difference</strong> between the alerts and the protected areas. That is how I separated burned area “inside” and “outside” protected areas.</li>
<li><strong>Other distances:</strong> 10 km is common for buffer zones of protected areas without a management plan in Brazil (Conama Resolution 428/2010).</li>
</ul>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/03-prodes-protected-areas.html">Annual PRODES deforestation in protected areas of the Amazon and the Cerrado</a></li>
<li><a href="https://hendesson.github.io/guia-espacial/curso/03-postgis.html">Course module 3: PostGIS</a> (in Portuguese), with inside and surroundings in SQL, step by step.</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/01-equivalences.html" class="anterior"><span class="direcao">← Previous</span> From ArcMap to QGIS: the same tool in every program</a> <a href="../articles/03-prodes-protected-areas.html" class="proximo"><span class="direcao">Next →</span> Annual PRODES deforestation in protected areas of the Amazon and the Cerrado</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/02-interior-entorno.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Inside and Around Protected Areas: The Overlay Every Report
    Uses},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/02-inside-and-around.html},
  doi = {10.5281/zenodo.23780248},
  langid = {en},
  abstract = {How to separate deforestation inside a protected area from
    deforestation in the 3 km band around it, without double counting
    neighboring areas. Using DETER alerts from 2024 to 2026, the article
    walks through the steps in four programs and four pitfalls that
    showed up with the real data.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Inside and Around Protected Areas: The
Overlay Every Report Uses.”</span> In <em>Geoprocessing Articles for
Environmental Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780248">https://doi.org/10.5281/zenodo.23780248</a>.
</div></div></section></div> ]]></description>
  <category>Protected areas</category>
  <category>DETER</category>
  <guid>https://hendesson.github.io/en/articles/02-inside-and-around.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/interior_entorno_zonas.png" medium="image" type="image/png" height="176" width="144"/>
</item>
<item>
  <title>Annual PRODES deforestation in protected areas of the Amazon and the Cerrado</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/03-prodes-protected-areas.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Beginner <span class="rotulo">Reading</span> 9 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p><strong>PRODES</strong> is Brazil’s official deforestation rate, produced by the National Institute for Space Research (INPE). It is released once a year for each biome, and the “PRODES year” runs from August to July. When INPE publishes a new year, the question is always the same: <strong>how much was cleared inside each protected area?</strong></p>
<p>I had one script for the Amazon and an almost identical one for the Cerrado. Here it is <strong>a single script</strong>: the PRODES layer and the set of protected areas become parameters.</p>
<section id="what-you-will-do" class="level2">
<h2 class="anchored" data-anchor-id="what-you-will-do">What you will do</h2>
<ol type="1">
<li><strong>Filter</strong> the deforestation polygons and repair the geometries.</li>
<li><strong>Overlay</strong> them with the protected areas of each biome (intersection).</li>
<li><strong>Measure</strong> the area in hectares.</li>
<li><strong>Summarize:</strong> a table of the latest year by protected area, and the time series.</li>
<li><strong>Update</strong> when a new year is released, without duplicating it.</li>
</ol>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Data
</div>
</div>
<div class="callout-body-container callout-body">
<p>Layers <code>prodes_amz</code>, <code>prodes_cerrado</code> and <code>ucs</code> of the articles’ GeoPackage (<a href="../articles/about.html#data">how to download</a>). Both PRODES layers have the same fields (<code>year</code>, <code>main_class</code>, <code>area_km</code>), so the same code works for both. The WFS services are:</p>
<ul>
<li>Amazon: <code>https://terrabrasilis.dpi.inpe.br/geoserver/prodes-legal-amz/ows</code>, layer <code>prodes-legal-amz:yearly_deforestation</code></li>
<li>Cerrado: <code>https://terrabrasilis.dpi.inpe.br/geoserver/prodes-cerrado-nb/ows</code>, layer <code>prodes-cerrado-nb:yearly_deforestation</code></li>
</ul>
</div>
</div>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Names in Portuguese: <code>bioma</code> = biome, <code>camada</code> = layer, <code>regiao</code> = region, <code>partes</code> = parts, <code>ultimo</code> = latest, <code>ano</code> = year, <code>grupo</code> = protected area group (strict protection or sustainable use).</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"></span>
<span id="cb1-3">arcpy.env.overwriteOutput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-4">gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg"</span></span>
<span id="cb1-5">gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb"</span></span>
<span id="cb1-6"></span>
<span id="cb1-7">biomas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amazônia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"main.prodes_amz"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amazonia"</span>),</span>
<span id="cb1-8">          (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cerrado"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"main.prodes_cerrado"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cerrado"</span>)]</span>
<span id="cb1-9">partes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> bioma, camada, regiao <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> biomas:</span>
<span id="cb1-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Deforestation (the field comes in upper and lower case) and repaired geometries</span></span>
<span id="cb1-13">    prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> camada, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">prodes_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>regiao<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb1-14">                                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UPPER(main_class) = 'DESMATAMENTO'"</span>)</span>
<span id="cb1-15">    arcpy.management.RepairGeometry(prodes)</span>
<span id="cb1-16">    ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>regiao<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb1-17">                                <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"regiao = '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>regiao<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb1-18"></span>
<span id="cb1-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 and 3. Intersection and geodesic area</span></span>
<span id="cb1-20">    cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseIntersect([prodes, ucs], <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">cruzado_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>regiao<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-21">    arcpy.management.CalculateGeometryAttributes(</span>
<span id="cb1-22">        cruzado, [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AREA_GEODESIC"</span>]], area_unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HECTARES"</span>)</span>
<span id="cb1-23">    arcpy.management.CalculateField(cruzado, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bioma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>,</span>
<span id="cb1-24">                                    field_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>)</span>
<span id="cb1-25">    partes.append(cruzado)</span>
<span id="cb1-26"></span>
<span id="cb1-27">prodes_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.Merge(partes, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">prodes_ucs"</span>)</span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Time series by biome and year, and table by protected area</span></span>
<span id="cb1-30">arcpy.analysis.Statistics(prodes_ucs, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">prodes_serie"</span>,</span>
<span id="cb1-31">                          [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUM"</span>]], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>])</span>
<span id="cb1-32">arcpy.analysis.Statistics(prodes_ucs, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">prodes_por_uc"</span>,</span>
<span id="cb1-33">                          [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUM"</span>]], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>])</span>
<span id="cb1-34"></span>
<span id="cb1-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Latest year of each biome</span></span>
<span id="cb1-36"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> bioma, _, _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> biomas:</span>
<span id="cb1-37">    anos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [r[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(prodes_ucs, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>], <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"bioma = '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bioma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)]</span>
<span id="cb1-38">    arcpy.AddMessage(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bioma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: latest year = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(anos)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-2">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> rodar(alg, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>p):</span>
<span id="cb2-5">    p.setdefault(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>)</span>
<span id="cb2-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> processing.run(alg, p)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-7"></span>
<span id="cb2-8">partes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> bioma, camada, regiao <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> [(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amazônia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prodes_amz"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amazonia"</span>),</span>
<span id="cb2-10">                              (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cerrado"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prodes_cerrado"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cerrado"</span>)]:</span>
<span id="cb2-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Deforestation and repaired geometries</span></span>
<span id="cb2-12">    prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>camada<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb2-13">                   EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper(main_class) = 'DESMATAMENTO'"</span>)</span>
<span id="cb2-14">    prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fixgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prodes)</span>
<span id="cb2-15">    ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs"</span>,</span>
<span id="cb2-16">                EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"regiao = '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>regiao<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb2-17"></span>
<span id="cb2-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 and 3. Intersection, area in hectares and biome</span></span>
<span id="cb2-19">    cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:intersection"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prodes, OVERLAY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs,</span>
<span id="cb2-20">                    INPUT_FIELDS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>], OVERLAY_FIELDS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>])</span>
<span id="cb2-21">    cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cruzado, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb2-22">                    FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-23">                    FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area(transform($geometry, 'EPSG:4674', 'ESRI:102033')) / 10000"</span>)</span>
<span id="cb2-24">    cruzado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cruzado, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>,</span>
<span id="cb2-25">                    FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, FIELD_LENGTH<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bioma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb2-26">    partes.append(cruzado)</span>
<span id="cb2-27"></span>
<span id="cb2-28">prodes_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:mergevectorlayers"</span>, LAYERS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>partes, CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:4674"</span>)</span>
<span id="cb2-29"></span>
<span id="cb2-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Sum by biome, year and protected area; then only the latest year of each biome</span></span>
<span id="cb2-31">por_uc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qgis:statisticsbycategories"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prodes_ucs, VALUES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb2-32">               CATEGORIES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>])</span>
<span id="cb2-33">ultimo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>por_uc,</span>
<span id="cb2-34">               EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"year" = maximum("year", group_by:="bioma")'</span>)</span>
<span id="cb2-35"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ultimo.getFeatures():</span>
<span id="cb2-36">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
<p>The trick is in the last expression: <code>maximum("year", group_by:="bioma")</code> finds the latest year <strong>of each biome</strong>, and the same idea works in the Field Calculator and in layer filters.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 1 to 3. Both biomes in a single table</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> artigos.prodes_ucs;</span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.prodes_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> prodes <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazônia'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> bioma, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazonia'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> regiao, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ano,</span>
<span id="cb3-6">           ST_CollectionExtract(ST_MakeValid(geom), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_amz</span>
<span id="cb3-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">upper</span>(main_class) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO'</span></span>
<span id="cb3-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UNION</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALL</span></span>
<span id="cb3-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Cerrado'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Cerrado'</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span>,</span>
<span id="cb3-11">           ST_CollectionExtract(ST_MakeValid(geom), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb3-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_cerrado</span>
<span id="cb3-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">upper</span>(main_class) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO'</span></span>
<span id="cb3-14">)</span>
<span id="cb3-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> p.bioma, p.ano, u.cnuc, u.nome_uc, u.grupo,</span>
<span id="cb3-16">       ST_Intersection(p.geom, u.geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> prodes p</span>
<span id="cb3-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.ucs u <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> u.regiao <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p.regiao <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> ST_Intersects(p.geom, u.geom)</span>
<span id="cb3-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> ST_IsEmpty(p.geom);</span>
<span id="cb3-20"></span>
<span id="cb3-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.prodes_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COLUMN</span> area_ha <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>;</span>
<span id="cb3-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> artigos.prodes_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> area_ha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ST_Area(geom:<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>;</span>
<span id="cb3-23"></span>
<span id="cb3-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 4a. Latest year of each biome, by protected area</span></span>
<span id="cb3-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> bioma, ano, nome_uc, grupo, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> poligonos, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(area_ha), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha</span>
<span id="cb3-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_ucs p</span>
<span id="cb3-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> ano <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(ano) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_ucs q <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> q.bioma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p.bioma)</span>
<span id="cb3-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> bioma, ano, nome_uc, grupo</span>
<span id="cb3-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> bioma, area_ha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span>
<span id="cb3-30"></span>
<span id="cb3-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 4b. Time series</span></span>
<span id="cb3-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> ano,</span>
<span id="cb3-33">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(area_ha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> bioma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazônia'</span>)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> amazonia_ha,</span>
<span id="cb3-34">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(area_ha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> bioma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Cerrado'</span>)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> cerrado_ha</span>
<span id="cb3-35"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_ucs</span>
<span id="cb3-36"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> ano</span>
<span id="cb3-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> ano;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb4-3"></span>
<span id="cb4-4">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span></span>
<span id="cb4-5">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>)</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> corrigir(gdf):</span>
<span id="cb4-8">    gdf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gdf.make_valid(method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"structure"</span>, keep_collapsed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> gdf[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>gdf.is_empty]</span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> prodes_em_ucs(camada, regiao, bioma):</span>
<span id="cb4-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Overlays a PRODES layer with the protected areas of a region and measures in hectares."""</span></span>
<span id="cb4-13">    prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>camada)</span>
<span id="cb4-14">    prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corrigir(prodes[prodes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"main_class"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.upper() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO"</span>])</span>
<span id="cb4-15">    u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ucs[ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> regiao][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>]]</span>
<span id="cb4-16">    x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.overlay(prodes[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>]], u, how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intersection"</span>, keep_geom_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-17">    x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x.to_crs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ESRI:102033"</span>).area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span></span>
<span id="cb4-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x.assign(bioma<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>bioma).rename(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>})</span>
<span id="cb4-19"></span>
<span id="cb4-20">prodes_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([prodes_em_ucs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prodes_amz"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amazonia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amazônia"</span>),</span>
<span id="cb4-21">                        prodes_em_ucs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prodes_cerrado"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cerrado"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cerrado"</span>)])</span>
<span id="cb4-22"></span>
<span id="cb4-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4a. Latest year of each biome, by protected area</span></span>
<span id="cb4-24">ultimo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> prodes_ucs[prodes_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> prodes_ucs.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>].transform(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>)]</span>
<span id="cb4-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(ultimo.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>])</span>
<span id="cb4-26">            .agg(poligonos<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>), area_ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>))</span>
<span id="cb4-27">            .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>))</span>
<span id="cb4-28"></span>
<span id="cb4-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4b. Time series</span></span>
<span id="cb4-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(prodes_ucs.pivot_table(index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>, values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb4-31">                             aggfunc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>())</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The three tested versions (QGIS, PostGIS and Python) gave the same totals, with differences below 0.1%.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/prodes_biomas.png" class="img-fluid figure-img" alt="Two bar charts side by side. On the left, Amazon: deforestation inside the protected areas of the study area exceeds 20,000 hectares in 2020 and 2021 and falls below 5,000 in 2025. On the right, Cerrado: smaller values, between 130 and 2,500 hectares per year, peaking in 2022."></p>
<figcaption>PRODES deforestation inside the protected areas of the two study areas, by year.</figcaption>
</figure>
</div>
<table class="table-striped caption-top table">
<caption>Latest PRODES year available for each biome (queried in October 2026). PRODES Cerrado 2026 had already been published; the Amazon one had not.</caption>
<colgroup>
<col style="width: 15%">
<col style="width: 15%">
<col style="width: 15%">
<col style="width: 15%">
<col style="width: 20%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="header">
<th>Biome</th>
<th>PRODES year</th>
<th>Protected area</th>
<th>Group</th>
<th style="text-align: right;">Polygons</th>
<th style="text-align: right;">Area (ha)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Amazon</td>
<td>2025</td>
<td>Jamanxim National Forest</td>
<td>Sustainable use</td>
<td style="text-align: right;">120</td>
<td style="text-align: right;">1,569.7</td>
</tr>
<tr class="even">
<td>Amazon</td>
<td>2025</td>
<td>Tapajós Environmental Protection Area</td>
<td>Sustainable use</td>
<td style="text-align: right;">69</td>
<td style="text-align: right;">1,251.6</td>
</tr>
<tr class="odd">
<td>Amazon</td>
<td>2025</td>
<td>Altamira National Forest</td>
<td>Sustainable use</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">269.5</td>
</tr>
<tr class="even">
<td>Amazon</td>
<td>2025</td>
<td>Iriri State Forest</td>
<td>Sustainable use</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">59.4</td>
</tr>
<tr class="odd">
<td>Amazon</td>
<td>2025</td>
<td>Jamanxim National Park</td>
<td>Strict protection</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">27.0</td>
</tr>
<tr class="even">
<td>Amazon</td>
<td>2025</td>
<td>Rio Novo National Park</td>
<td>Strict protection</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">16.7</td>
</tr>
<tr class="odd">
<td>Cerrado</td>
<td>2026</td>
<td>Pouso Alto Environmental Protection Area</td>
<td>Sustainable use</td>
<td style="text-align: right;">37</td>
<td style="text-align: right;">129.7</td>
</tr>
</tbody>
</table>
<p>Chapada dos Veadeiros National Park had no PRODES deforestation in 2026. Note also that, in both biomes, almost all deforestation inside protected areas is in <strong>sustainable-use</strong> areas, where people live and some activities are allowed.</p>
</section>
<section id="updating-when-a-new-year-is-released" class="level2">
<h2 class="anchored" data-anchor-id="updating-when-a-new-year-is-released">Updating when a new year is released</h2>
<p>INPE sometimes <strong>republishes</strong> a year (with corrections). If your script only appends rows, the same year appears twice and the area doubles. The rule is: <strong>delete the year and insert it again</strong>, in a transaction.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-3" aria-controls="tabset-2-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-4" aria-controls="tabset-2-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">ano <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span></span>
<span id="cb5-2">tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">prodes_ucs_historico"</span></span>
<span id="cb5-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> arcpy.da.UpdateCursor(tabela, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>], <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"year = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ano<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> AND bioma = 'Amazônia'"</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> cur:</span>
<span id="cb5-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cur:</span>
<span id="cb5-5">        cur.deleteRow()</span>
<span id="cb5-6">novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(prodes_ucs, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ovos"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"year = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ano<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> AND bioma = 'Amazônia'"</span>)</span>
<span id="cb5-7">arcpy.management.Append(novos, tabela, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NO_TEST"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<p>In QGIS, the safest option is to <strong>rewrite the whole layer</strong> at each update (the overlay takes seconds). To edit an existing table, select the year with <strong>Select by expression</strong> (<code>"year" = 2025</code>), delete the selected features and use <strong>Edit → Paste features</strong> with the new data.</p>
</div>
<div id="tabset-2-3" class="tab-pane" aria-labelledby="tabset-2-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb6-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BEGIN</span>;</span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DELETE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> bioma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazônia'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> ano <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>;</span>
<span id="cb6-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INTO</span> artigos.prodes_ucs (bioma, ano, cnuc, nome_uc, grupo, geom, area_ha)</span>
<span id="cb6-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> bioma, ano, cnuc, nome_uc, grupo, geom, area_ha</span>
<span id="cb6-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_ucs_novo            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- the same overlay, only with the new year</span></span>
<span id="cb6-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> bioma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazônia'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> ano <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>;</span>
<span id="cb6-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COMMIT</span>;</span></code></pre></div></div>
<p>If something goes wrong between the <code>DELETE</code> and the <code>INSERT</code>, <code>ROLLBACK</code> undoes everything and the table is never left half-done.</p>
</div>
<div id="tabset-2-4" class="tab-pane" aria-labelledby="tabset-2-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">historico <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/prodes_ucs_historico.gpkg"</span>)</span>
<span id="cb7-2">ano, bioma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Amazônia"</span></span>
<span id="cb7-3">novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> prodes_ucs[(prodes_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> ano) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (prodes_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> bioma)]</span>
<span id="cb7-4">historico <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([historico[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>((historico[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> ano) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (historico[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bioma"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> bioma))],</span>
<span id="cb7-5">                       novos])</span>
<span id="cb7-6">historico.to_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/prodes_ucs_historico.gpkg"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
<p>The article <a href="../articles/13-updating-layers.html">Updating layers without duplicates</a> covers this pattern in detail.</p>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Upper and lower case
</div>
</div>
<div class="callout-body-container callout-body">
<p>In the Amazon study area, 3 polygons from 2024 come with <code>main_class = 'desmatamento'</code> (lower case) and 1,635 with <code>'DESMATAMENTO'</code>. A filter <code>main_class = 'DESMATAMENTO'</code> misses the three. Always compare with <code>UPPER(...)</code>.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Invalid geometries in PRODES
</div>
</div>
<div class="callout-body-container callout-body">
<p>2,263 of the 11,126 PRODES Amazon polygons in the study area (20%) are invalid. The old script had the <code>RepairGeometry</code> line commented out and worked by luck. Always repair before the overlay.</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span><code>area_km</code> is already in the table, why recompute it?
</div>
</div>
<div class="callout-body-container callout-body">
<p>INPE’s <code>area_km</code> is the area of the <strong>whole</strong> polygon. After the intersection, a polygon that crosses the protected area boundary keeps only the part inside, and the original <code>area_km</code> is wrong. Always recompute after cutting.</p>
</div>
</div>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/02-inside-and-around.html">Inside and around protected areas</a></li>
<li><a href="../articles/06-monthly-cumulative-table.html">Monthly cumulative table</a>, for DETER, which is updated daily</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/02-inside-and-around.html" class="anterior"><span class="direcao">← Previous</span> Inside and around protected areas: the overlay every report uses</a> <a href="../articles/04-fire-hotspots.html" class="proximo"><span class="direcao">Next →</span> Fire hotspots in protected areas over the last 24 and 48 hours</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/03-prodes-ucs.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Annual {PRODES} Deforestation in Protected Areas of the
    {Amazon} and the {Cerrado}},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/03-prodes-protected-areas.html},
  doi = {10.5281/zenodo.23780250},
  langid = {en},
  abstract = {One script to overlay PRODES for the Amazon and the
    Cerrado with protected areas, build the table by protected area and
    the time series, and add a new year without duplicating it. In both
    biomes, almost all deforestation inside protected areas happens in
    sustainable-use areas.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Annual PRODES Deforestation in Protected
Areas of the Amazon and the Cerrado.”</span> In <em>Geoprocessing
Articles for Environmental Monitoring</em>. Preprint, Hendesson Alves,
October 29. <a href="https://doi.org/10.5281/zenodo.23780250">https://doi.org/10.5281/zenodo.23780250</a>.
</div></div></section></div> ]]></description>
  <category>Protected areas</category>
  <category>PRODES</category>
  <guid>https://hendesson.github.io/en/articles/03-prodes-protected-areas.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/prodes_biomas.png" medium="image" type="image/png" height="56" width="144"/>
</item>
<item>
  <title>Fire hotspots in protected areas over the last 24 and 48 hours</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/04-fire-hotspots.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Beginner <span class="rotulo">Reading</span> 9 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>In the dry season, fire brigades start the day with one question: <strong>where has fire appeared in the protected areas since yesterday?</strong> INPE publishes the fire hotspots detected by several satellites many times an hour, in open files.</p>
<p>The script I used read the hotspots from the ArcGIS Online <em>Living Atlas</em> and only worked with an institutional account. Here the hotspots come straight from <strong>INPE’s server</strong>, with no login.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/focos_48h.png" class="img-fluid figure-img" alt="Map of the Novo Progresso region with protected areas in light green, the surrounding band in yellow and fire hotspots as points: red in the last 24 hours and orange between 24 and 48 hours. The largest concentration is in the Tapajós Environmental Protection Area."></p>
<figcaption>Fire hotspots of the last 48 hours in the example area (queried on 2026-10-29).</figcaption>
</figure>
</div>
<section id="where-the-hotspots-come-from" class="level2">
<h2 class="anchored" data-anchor-id="where-the-hotspots-come-from">Where the hotspots come from</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Source</th>
<th>Address</th>
<th>Update</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>INPE, daily file</strong></td>
<td><code>https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/diario/Brasil/focos_diario_br_YYYYMMDD.csv</code></td>
<td>today’s file grows during the day</td>
</tr>
<tr class="even">
<td>INPE, every 10 minutes</td>
<td><code>https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/10min/</code></td>
<td>10 minutes</td>
</tr>
<tr class="odd">
<td>NASA FIRMS (VIIRS and MODIS)</td>
<td><code>https://firms.modaps.eosdis.nasa.gov/data/active_fire/suomi-npp-viirs-c2/csv/SUOMI_VIIRS_C2_South_America_48h.csv</code></td>
<td>a few hours; <code>_24h</code> and <code>_7d</code> files also exist</td>
</tr>
</tbody>
</table>
<p>The INPE file has one row per hotspot, with <code>lat</code>, <code>lon</code>, <code>data_hora_gmt</code> (date and time in UTC), <code>satelite</code>, <code>municipio</code>, <code>estado</code>, <code>bioma</code> and other fields.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>A hotspot is not burned area
</div>
</div>
<div class="callout-body-container callout-body">
<p>A hotspot is a <strong>hot pixel</strong> seen by a satellite at one moment. The same fire shows up as dozens of hotspots, and the same hotspot can be seen by several satellites. Use hotspots to <strong>raise alerts</strong> and to compare periods with the <strong>reference satellite</strong> (<code>AQUA_M-T</code>). For burned area, use DETER burn scars or MapBiomas Fire.</p>
</div>
</div>
</section>
<section id="what-you-will-do" class="level2">
<h2 class="anchored" data-anchor-id="what-you-will-do">What you will do</h2>
<ol type="1">
<li>Download the files for <strong>today, yesterday and the day before</strong> (to cover a full 48 hours).</li>
<li>Turn <code>lon</code> and <code>lat</code> into <strong>points</strong> (SIRGAS 2000).</li>
<li>Filter by <strong>time</strong> (in UTC, as in the file).</li>
<li>Find out <strong>which protected area</strong> (or surroundings) each hotspot fell in.</li>
<li>Count by protected area, time window and satellite.</li>
</ol>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Names in Portuguese: <code>focos</code> = hotspots, <code>pasta</code> = folder, <code>agora</code> = now, <code>dia</code> = day, <code>corte</code> = cutoff, <code>janela</code> = time window, <code>ultimos</code> = latest.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> urllib.request</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime, timedelta, timezone</span>
<span id="cb1-4"></span>
<span id="cb1-5">arcpy.env.overwriteOutput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-6">gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg"</span></span>
<span id="cb1-7">pasta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocos"</span></span>
<span id="cb1-8">url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/diario/"</span></span>
<span id="cb1-9">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brasil/focos_diario_br_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.csv"</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 and 2. Download the three days and create the points</span></span>
<span id="cb1-12">agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now(timezone.utc)</span>
<span id="cb1-13">pontos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb1-14"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>):</span>
<span id="cb1-15">    dia <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i)).strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y%m</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-16">    csv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pasta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocos_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dia<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">csv"</span></span>
<span id="cb1-17">    urllib.request.urlretrieve(url.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(dia), csv)</span>
<span id="cb1-18">    pontos.append(arcpy.management.XYTableToPoint(</span>
<span id="cb1-19">        csv, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocos_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dia<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lon"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span>,</span>
<span id="cb1-20">        coordinate_system<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>arcpy.SpatialReference(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>)))</span>
<span id="cb1-21">focos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.Merge(pontos, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocos"</span>)</span>
<span id="cb1-22"></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 and 4. Protected area of each hotspot and filter for the last 48 hours</span></span>
<span id="cb1-24">focos_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseIntersect([focos, gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>], <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocos_ucs"</span>)</span>
<span id="cb1-25">corte_48h <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(hours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span>)).strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> %H:%M:%S"</span>)</span>
<span id="cb1-26">ultimos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(focos_ucs, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocos_ucs_48h"</span>,</span>
<span id="cb1-27">                                <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"data_hora_gmt &gt;= timestamp '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>corte_48h<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Count by protected area and satellite</span></span>
<span id="cb1-30">arcpy.analysis.Statistics(ultimos, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocos_por_uc"</span>,</span>
<span id="cb1-31">                          [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COUNT"</span>]], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"satelite"</span>])</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>XYTableToPoint</code> reads <code>data_hora_gmt</code> as text or as a date, depending on the ArcGIS Pro version. If <code>Select</code> finds nothing, check the field type and change the filter to <code>data_hora_gmt &gt;= '{corte_48h}'</code>, which compares the text.</p>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os, tempfile, urllib.request</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime, timedelta, timezone</span>
<span id="cb2-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-4"></span>
<span id="cb2-5">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb2-6">url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/diario/"</span></span>
<span id="cb2-7">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brasil/focos_diario_br_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.csv"</span>)</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> rodar(alg, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>p):</span>
<span id="cb2-10">    p.setdefault(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>)</span>
<span id="cb2-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> processing.run(alg, p)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 and 2. Download the three days and open them as point layers (delimited text)</span></span>
<span id="cb2-14">agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now(timezone.utc)</span>
<span id="cb2-15">camadas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-16"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>):</span>
<span id="cb2-17">    dia <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i)).strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y%m</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb2-18">    csv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.path.join(tempfile.gettempdir(), <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"focos_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dia<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.csv"</span>)</span>
<span id="cb2-19">    urllib.request.urlretrieve(url.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(dia), csv)</span>
<span id="cb2-20">    uri <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"file:///</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>csv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">?delimiter=,&amp;xField=lon&amp;yField=lat&amp;crs=EPSG:4674"</span></span>
<span id="cb2-21">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&amp;field=data_hora_gmt:text"</span>)</span>
<span id="cb2-22">    camadas.append(QgsVectorLayer(uri, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"focos_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dia<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delimitedtext"</span>))</span>
<span id="cb2-23">focos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:mergevectorlayers"</span>, LAYERS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>camadas, CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:4674"</span>)</span>
<span id="cb2-24"></span>
<span id="cb2-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Last 48 hours (text in YYYY-MM-DD HH:MM:SS format compares like a date)</span></span>
<span id="cb2-26">corte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(hours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span>)).strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> %H:%M:%S"</span>)</span>
<span id="cb2-27">focos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>focos,</span>
<span id="cb2-28">              EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">data_hora_gmt</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> &gt;= '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>corte<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb2-29"></span>
<span id="cb2-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Protected area of each hotspot (predicate 5 = "within")</span></span>
<span id="cb2-31">focos_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:joinattributesbylocation"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>focos,</span>
<span id="cb2-32">                  JOIN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs"</span>, PREDICATE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>], JOIN_FIELDS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>],</span>
<span id="cb2-33">                  METHOD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, DISCARD_NONMATCHING<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb2-34"></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Count by protected area and satellite</span></span>
<span id="cb2-36">resumo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qgis:statisticsbycategories"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>focos_ucs,</span>
<span id="cb2-37">               CATEGORIES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"satelite"</span>])</span>
<span id="cb2-38"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> resumo.getFeatures():</span>
<span id="cb2-39">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"satelite"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>])</span>
<span id="cb2-40">QgsProject.instance().addMapLayer(focos_ucs)</span></code></pre></div></div>
<p>The <code>&amp;field=data_hora_gmt:text</code> in the layer address forces QGIS to read the date as text, so the filter works the same in any system language.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 1. Table for the hotspots and loading of the three files (\copy runs on your computer)</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> artigos.focos;</span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.focos (</span>
<span id="cb3-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">id</span> text, lat <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, lon <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, data_hora_gmt <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">timestamp</span>,</span>
<span id="cb3-5">    satelite text, municipio text, estado text, pais text,</span>
<span id="cb3-6">    municipio_id bigint, estado_id <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span>, pais_id <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span>,</span>
<span id="cb3-7">    numero_dias_sem_chuva <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, precipitacao <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, risco_fogo <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>,</span>
<span id="cb3-8">    bioma text, frp <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span></span>
<span id="cb3-9">);</span>
<span id="cb3-10">\<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">copy</span> artigos.focos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> PROGRAM <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'curl -s https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/diario/Brasil/focos_diario_br_$(date -u +%Y%m%d).csv'</span> CSV <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">HEADER</span></span>
<span id="cb3-11">\<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">copy</span> artigos.focos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> PROGRAM <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'curl -s https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/diario/Brasil/focos_diario_br_$(date -u -d yesterday +%Y%m%d).csv'</span> CSV <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">HEADER</span></span>
<span id="cb3-12">\<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">copy</span> artigos.focos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> PROGRAM <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'curl -s https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/diario/Brasil/focos_diario_br_$(date -u -d "2 days ago" +%Y%m%d).csv'</span> CSV <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">HEADER</span></span>
<span id="cb3-13"></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 2. Points</span></span>
<span id="cb3-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.focos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COLUMN</span> geom geometry(Point, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>);</span>
<span id="cb3-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> artigos.focos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> geom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ST_SetSRID(ST_MakePoint(lon, lat), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>);</span>
<span id="cb3-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INDEX</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> artigos.focos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> gist (geom);</span>
<span id="cb3-18"></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 3 to 5. Hotspots in protected areas in the last 24 and 48 hours (time in UTC)</span></span>
<span id="cb3-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> u.nome_uc,</span>
<span id="cb3-21">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> f.data_hora_gmt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> (now() <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AT</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">TIME</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ZONE</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'UTC'</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interval</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'24 hours'</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> focos_24h,</span>
<span id="cb3-22">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> focos_48h,</span>
<span id="cb3-23">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> f.satelite <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'AQUA_M-T'</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> focos_48h_referencia</span>
<span id="cb3-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.focos f</span>
<span id="cb3-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.ucs u <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> ST_Within(f.geom, u.geom)</span>
<span id="cb3-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> f.data_hora_gmt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> (now() <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AT</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">TIME</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ZONE</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'UTC'</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interval</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'48 hours'</span></span>
<span id="cb3-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> u.nome_uc</span>
<span id="cb3-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> focos_48h <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>\copy … FROM PROGRAM</code> needs <code>curl</code>, and on Windows replace <code>$(date …)</code> with the date written by hand. Another option is to download the files first and use <code>\copy artigos.focos FROM 'focos.csv' CSV HEADER</code>.</p>
</div>
</div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime, timedelta, timezone</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb4-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-4"></span>
<span id="cb4-5">url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://dataserver-coids.inpe.br/queimadas/queimadas/focos/csv/diario/"</span></span>
<span id="cb4-6">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brasil/focos_diario_br_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">.csv"</span>)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Download today, yesterday and the day before</span></span>
<span id="cb4-9">agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now(timezone.utc)</span>
<span id="cb4-10">dias <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>i)).strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y%m</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)]</span>
<span id="cb4-11">focos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([pd.read_csv(url.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(d)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> dias], ignore_index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-12">focos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_hora_gmt"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(focos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_hora_gmt"</span>], utc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-13"></span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Points and 3. last 48 hours</span></span>
<span id="cb4-15">focos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.GeoDataFrame(focos, geometry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>gpd.points_from_xy(focos.lon, focos.lat), crs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>)</span>
<span id="cb4-16">focos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> focos[focos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_hora_gmt"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(hours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">48</span>)]</span>
<span id="cb4-17">focos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"janela"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (focos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data_hora_gmt"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> agora <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(hours<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</span>)).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(</span>
<span id="cb4-18">    {<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last 24 h"</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"24 to 48 h"</span>})</span>
<span id="cb4-19"></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Which protected area (or surroundings) each hotspot fell in</span></span>
<span id="cb4-21">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span></span>
<span id="cb4-22">zonas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat([</span>
<span id="cb4-23">    gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>]].assign(local<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interior"</span>),</span>
<span id="cb4-24">    gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs_entorno"</span>)[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>]].assign(local<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"entorno"</span>),</span>
<span id="cb4-25">])</span>
<span id="cb4-26">focos_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.sjoin(focos, zonas, predicate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"within"</span>)</span>
<span id="cb4-27"></span>
<span id="cb4-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Count</span></span>
<span id="cb4-29"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(pd.crosstab([focos_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], focos_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>]], focos_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"janela"</span>],</span>
<span id="cb4-30">                  margins<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, margins_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total"</span>))</span>
<span id="cb4-31"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(focos_ucs[focos_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"satelite"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AQUA_M-T"</span>].groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>).size())</span>
<span id="cb4-32">focos_ucs.to_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/focos_ucs_48h.gpkg"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The numbers change every run. On the afternoon of 2026-10-29, the three tested versions found the same hotspots inside the protected areas of the study area:</p>
<table class="table-striped caption-top table">
<caption>Fire hotspots inside and around protected areas, queried on 2026-10-29 at 4:40 pm (Brasília time).</caption>
<colgroup>
<col style="width: 15%">
<col style="width: 21%">
<col style="width: 21%">
<col style="width: 21%">
<col style="width: 21%">
</colgroup>
<thead>
<tr class="header">
<th>Protected area</th>
<th style="text-align: right;">Last 24 h</th>
<th style="text-align: right;">Last 48 h</th>
<th style="text-align: right;">48 h, <code>AQUA_M-T</code> only</th>
<th style="text-align: right;">Surroundings, 48 h</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Tapajós Environmental Protection Area</td>
<td style="text-align: right;">17</td>
<td style="text-align: right;">34</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">41</td>
</tr>
<tr class="even">
<td>Altamira National Forest</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td>Jamanxim National Forest</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="even">
<td>Iriri State Forest</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td>Pouso Alto Environmental Protection Area (Cerrado)</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
</tr>
</tbody>
</table>
<p>The same 52 hotspots inside the protected areas in all three versions. The difference between the all-satellites column (34) and the reference-satellite column (5) for the Tapajós Environmental Protection Area shows why you should not add up satellites to compare periods.</p>
</section>
<section id="running-it-on-its-own-every-day" class="level2">
<h2 class="anchored" data-anchor-id="running-it-on-its-own-every-day">Running it on its own every day</h2>
<p>This is the kind of script worth <strong>scheduling</strong>:</p>
<ul>
<li><strong>Windows:</strong> Task Scheduler, calling <code>propy.bat script.py</code> (ArcGIS Pro) or <code>python script.py</code>.</li>
<li><strong>ArcGIS Pro 3.x:</strong> right-click the script tool → <strong>Schedule</strong>.</li>
<li><strong>Linux/Mac:</strong> <code>crontab -e</code> and a line such as <code>0 7 * * * cd /dados &amp;&amp; python focos.py</code>.</li>
</ul>
<p>To avoid piling up duplicate rows at each run, see <a href="../articles/13-updating-layers.html">Updating layers without duplicates</a>.</p>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Time in UTC
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>data_hora_gmt</code> field is in <strong>UTC</strong> (Brasília = UTC−3). If you filter “last 24 hours” using the computer clock, you lose or gain 3 hours of hotspots. Compute the cutoff in UTC, as in the code above.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Counting hotspots from all satellites
</div>
</div>
<div class="callout-body-container callout-body">
<p>On the same day, the study area had hotspots from 7 satellites. Adding them all gives a larger number that changes with the satellites in operation. To compare years or months, use only <code>AQUA_M-T</code> (the reference satellite). To alert the brigade, use all of them.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Today’s file is incomplete
</div>
</div>
<div class="callout-body-container callout-body">
<p>The current day’s file grows during the day. That is why the code downloads <strong>three</strong> files: only then is the last 48 hours complete at any time of day.</p>
</div>
</div>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/02-inside-and-around.html">Inside and around protected areas</a>, to build the surroundings layer</li>
<li><a href="https://hendesson.github.io/guia-espacial/guia/03-fontes-de-dados.html">Data sources</a> (in Portuguese), with BDQueimadas and MapBiomas Fire</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/03-prodes-protected-areas.html" class="anterior"><span class="direcao">← Previous</span> Annual PRODES deforestation in protected areas of the Amazon and the Cerrado</a> <a href="../articles/05-date-fields.html" class="proximo"><span class="direcao">Next →</span> Year, month and PRODES year: date fields in every program</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/04-focos-calor.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Fire Hotspots in Protected Areas over the Last 24 and 48
    Hours},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/04-fire-hotspots.html},
  doi = {10.5281/zenodo.23780252},
  langid = {en},
  abstract = {INPE fire hotspots straight from the public server, no
    login needed: download the daily files, turn them into points,
    filter the last 24 and 48 hours in UTC and count by protected area.
    And why adding up hotspots from several satellites is misleading.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Fire Hotspots in Protected Areas over the
Last 24 and 48 Hours.”</span> In <em>Geoprocessing Articles for
Environmental Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780252">https://doi.org/10.5281/zenodo.23780252</a>.
</div></div></section></div> ]]></description>
  <category>Fire</category>
  <category>Protected areas</category>
  <guid>https://hendesson.github.io/en/articles/04-fire-hotspots.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/focos_48h.png" medium="image" type="image/png" height="175" width="144"/>
</item>
<item>
  <title>Year, month and PRODES year: date fields in every program</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/05-date-fields.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Beginner <span class="rotulo">Reading</span> 6 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"> <img src="https://hendesson.github.io/en/imagens/ferramentas/powerbi.svg" class="img-fluid" alt="Power BI"></p>
</div>
<p>Every monitoring dashboard needs to break dates into parts: <strong>year</strong>, <strong>month</strong>, <strong>month name</strong> and, for deforestation, the <strong>PRODES year</strong>, which runs from <strong>August 1 to July 31</strong>. “PRODES 2025” starts in August 2024.</p>
<p>In the old scripts, each part was a function with an <code>UpdateCursor</code>, repeated across several files. Here each one becomes <strong>a single line</strong> in each program.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/calendario_prodes.png" class="img-fluid figure-img" alt="Two month rulers. On top, the calendar year from January to December. Below, the PRODES year 2025, from August 2024 (month 1) to July 2025 (month 12), with arrows linking the corresponding months."></p>
<figcaption>The PRODES year starts in August: August is month 1 and July is month 12.</figcaption>
</figure>
</div>
<section id="the-rules" class="level2">
<h2 class="anchored" data-anchor-id="the-rules">The rules</h2>
<p>The field names are in Portuguese, as in the dashboards: <code>ano</code> = year, <code>mes</code> = month, <code>mes_nome</code> = month name, <code>ano_prodes</code> = PRODES year, <code>mes_prodes</code> = PRODES month.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Field</th>
<th>Rule</th>
<th>2025-03-15</th>
<th>2025-09-20</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>ano</code></td>
<td>year of the date</td>
<td>2025</td>
<td>2025</td>
</tr>
<tr class="even">
<td><code>mes</code></td>
<td>month of the date</td>
<td>3</td>
<td>9</td>
</tr>
<tr class="odd">
<td><code>mes_nome</code></td>
<td>month name, from a fixed list</td>
<td>March</td>
<td>September</td>
</tr>
<tr class="even">
<td><code>ano_prodes</code></td>
<td>year + 1 if the month is August or later</td>
<td>2025</td>
<td><strong>2026</strong></td>
</tr>
<tr class="odd">
<td><code>mes_prodes</code></td>
<td>August = 1 … July = 12, or <code>(mes + 4) % 12 + 1</code></td>
<td>8</td>
<td>2</td>
</tr>
</tbody>
</table>
<p><code>%</code> is the <strong>remainder of the division</strong>: <code>(9 + 4) % 12 + 1</code> = <code>13 % 12 + 1</code> = <code>1 + 1</code> = 2.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Month name from the computer’s language
</div>
</div>
<div class="callout-body-container callout-body">
<p>The old script used <code>strftime("%B")</code>, which returns the month name <strong>in the system language</strong>: “março” on a computer in Portuguese, “March” on a server in English. In a dashboard, that mixes languages. Use a <strong>fixed list</strong> of names, as in the code below.</p>
</div>
</div>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>The examples use the <code>deter</code> layer (field <code>view_date</code>) of the articles’ GeoPackage (<a href="../articles/about.html#data">how to download</a>).</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false" href="">Power BI</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"></span>
<span id="cb1-3">fc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter"</span></span>
<span id="cb1-4">arcpy.management.CopyFeatures(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">deter"</span>, fc)</span>
<span id="cb1-5"></span>
<span id="cb1-6">meses <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"['January','February','March','April','May','June','July',"</span></span>
<span id="cb1-7">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'August','September','October','November','December']"</span>)</span>
<span id="cb1-8"></span>
<span id="cb1-9">arcpy.management.CalculateFields(fc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>, [</span>
<span id="cb1-10">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>,        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"!view_date!.year"</span>],</span>
<span id="cb1-11">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes"</span>,        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"!view_date!.month"</span>],</span>
<span id="cb1-12">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_nome"</span>,   <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>meses<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">[!view_date!.month - 1]"</span>],</span>
<span id="cb1-13">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"!view_date!.year + (1 if !view_date!.month &gt;= 8 else 0)"</span>],</span>
<span id="cb1-14">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(!view_date!.month + 4) % 12 + 1"</span>],</span>
<span id="cb1-15">])</span></code></pre></div></div>
<p><code>CalculateFields</code> (plural) computes several fields at once and creates the ones that do not exist. In ArcMap, use one <code>CalculateField_management</code> per field, with <code>"PYTHON_9.3"</code>.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>In the <strong>Field Calculator</strong>, create each field with the expression:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Field</th>
<th>Type</th>
<th>Expression</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>ano</code></td>
<td>integer</td>
<td><code>year("view_date")</code></td>
</tr>
<tr class="even">
<td><code>mes</code></td>
<td>integer</td>
<td><code>month("view_date")</code></td>
</tr>
<tr class="odd">
<td><code>mes_nome</code></td>
<td>text</td>
<td><code>array_get(array('January','February','March','April','May','June','July','August','September','October','November','December'), month("view_date") - 1)</code></td>
</tr>
<tr class="even">
<td><code>ano_prodes</code></td>
<td>integer</td>
<td><code>year("view_date") + if(month("view_date") &gt;= 8, 1, 0)</code></td>
</tr>
<tr class="odd">
<td><code>mes_prodes</code></td>
<td>integer</td>
<td><code>(month("view_date") + 4) % 12 + 1</code></td>
</tr>
</tbody>
</table>
<p>In the Python console, the same five fields in sequence:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-2">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb2-3"></span>
<span id="cb2-4">campos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb2-5">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'year("view_date")'</span>),</span>
<span id="cb2-6">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'month("view_date")'</span>),</span>
<span id="cb2-7">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_nome"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"array_get(array('January','February','March','April','May','June',"</span></span>
<span id="cb2-8">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'July','August','September','October','November','December'), "</span></span>
<span id="cb2-9">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'month("view_date") - 1)'</span>),</span>
<span id="cb2-10">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'year("view_date") + if(month("view_date") &gt;= 8, 1, 0)'</span>),</span>
<span id="cb2-11">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(month("view_date") + 4) % 12 + 1'</span>),</span>
<span id="cb2-12">]</span>
<span id="cb2-13">camada <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=deter"</span></span>
<span id="cb2-14"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> nome, tipo, formula <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> campos:          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># type: 1 = integer, 2 = text</span></span>
<span id="cb2-15">    camada <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> processing.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, {</span>
<span id="cb2-16">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT"</span>: camada, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FIELD_NAME"</span>: nome, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FIELD_TYPE"</span>: tipo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FIELD_LENGTH"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,</span>
<span id="cb2-17">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FORMULA"</span>: formula, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>})[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-18"></span>
<span id="cb2-19">resumo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> processing.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qgis:statisticsbycategories"</span>, {</span>
<span id="cb2-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT"</span>: camada, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CATEGORIES_FIELD_NAME"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>})[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> resumo.getFeatures():</span>
<span id="cb2-22">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>])</span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> view_date,</span>
<span id="cb3-2">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span>                                   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ano,</span>
<span id="cb3-3">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">month</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span>                                  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> mes,</span>
<span id="cb3-4">       (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ARRAY</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'January'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'February'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'March'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'April'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'May'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'June'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'July'</span>,</span>
<span id="cb3-5">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'August'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'September'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'October'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'November'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'December'</span>])</span>
<span id="cb3-6">           [<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">month</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span>]                            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> mes_nome,</span>
<span id="cb3-7">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interval</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'5 months'</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span>             <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ano_prodes,</span>
<span id="cb3-8">       (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">month</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) % <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>                   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> mes_prodes</span>
<span id="cb3-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter</span>
<span id="cb3-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">LIMIT</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>;</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Check: alerts by PRODES year</span></span>
<span id="cb3-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interval</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'5 months'</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ano_prodes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>)</span>
<span id="cb3-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter</span>
<span id="cb3-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span></code></pre></div></div>
<p>The <code>+ interval '5 months'</code> trick: adding 5 months to any date from August to December pushes it into the next year, while dates from January to July stay in the same year. That is exactly the PRODES year rule.</p>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-2"></span>
<span id="cb4-3">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span>, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>)</span>
<span id="cb4-4">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>].astype(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"datetime64[ns]"</span>)</span>
<span id="cb4-5"></span>
<span id="cb4-6">MESES <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"January"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"February"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"March"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"April"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"May"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"June"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"July"</span>,</span>
<span id="cb4-7">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"August"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"September"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"October"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"November"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"December"</span>]</span>
<span id="cb4-8"></span>
<span id="cb4-9">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.dt.year</span>
<span id="cb4-10">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.dt.month</span>
<span id="cb4-11">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_nome"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> m: MESES[m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb4-12">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>).astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb4-13">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-14"></span>
<span id="cb4-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(deter[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_nome"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>]].head())</span>
<span id="cb4-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(deter.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>).size())</span></code></pre></div></div>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<p>In <strong>Power Query</strong> (Home → Transform data), use <strong>Add Column → Custom Column</strong>:</p>
<pre><code>// ano_prodes
Date.Year([view_date]) + (if Date.Month([view_date]) &gt;= 8 then 1 else 0)

// mes_prodes
Number.Mod(Date.Month([view_date]) + 4, 12) + 1

// mes_nome (fixed list, independent of the language)
{"January","February","March","April","May","June","July",
 "August","September","October","November","December"}{Date.Month([view_date]) - 1}</code></pre>
<p>Or in <strong>DAX</strong>, as a calculated column of the calendar table:</p>
<pre><code>PRODES year = YEAR ( dim_calendario[data] ) + IF ( MONTH ( dim_calendario[data] ) &gt;= 8, 1, 0 )
PRODES month = MOD ( MONTH ( dim_calendario[data] ) + 4, 12 ) + 1</code></pre>
<p>To sort months in PRODES-year order in charts, select the <code>mes_nome</code> column and use <strong>Sort by column → mes_prodes</strong>.</p>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The QGIS, PostGIS and Python versions gave the same count of alerts by PRODES year:</p>
<table class="table-striped caption-top table">
<caption>DETER alerts by PRODES year, all classes.</caption>
<thead>
<tr class="header">
<th>PRODES year</th>
<th>Period</th>
<th style="text-align: right;">DETER alerts in the study area</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>2024</td>
<td>Jan to Jul 2024 (the download starts in 2024)</td>
<td style="text-align: right;">639</td>
</tr>
<tr class="even">
<td>2025</td>
<td>Aug 2024 to Jul 2025</td>
<td style="text-align: right;">5,787</td>
</tr>
<tr class="odd">
<td>2026</td>
<td>Aug 2025 to Jul 2026</td>
<td style="text-align: right;">595</td>
</tr>
<tr class="even">
<td>2027</td>
<td>Aug to Oct 2026 (ongoing)</td>
<td style="text-align: right;">802</td>
</tr>
</tbody>
</table>
<p>The 2025 peak is <strong>5,127 burn scars</strong> from the 2024 drought. Separating by class, clear-cut deforestation alerts (<code>DESMATAMENTO_CR</code>) numbered 431 in 2025 and 300 in 2026. That is why dashboards almost always separate <strong>deforestation</strong> from <strong>fire</strong>.</p>
</section>
<section id="first-and-last-day-of-the-period" class="level2">
<h2 class="anchored" data-anchor-id="first-and-last-day-of-the-period">First and last day of the period</h2>
<p>Two other common questions: “what are the first and last alerts in the layer?” (for the report title) and “what is the last day of the month?” (to know whether the month is closed).</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th></th>
<th>First and last date</th>
<th>Last day of the month</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>ArcGIS Pro</td>
<td><code>min()</code>/<code>max()</code> over <code>arcpy.da.SearchCursor(fc, ["view_date"])</code></td>
<td><code>calendar.monthrange(year, month)[1]</code></td>
</tr>
<tr class="even">
<td>QGIS</td>
<td><code>minimum("view_date")</code>, <code>maximum("view_date")</code></td>
<td><code>make_date(year(d), month(d), 1) + '1 month' - '1 day'</code></td>
</tr>
<tr class="odd">
<td>PostGIS</td>
<td><code>min(view_date)</code>, <code>max(view_date)</code></td>
<td><code>(date_trunc('month', d) + interval '1 month - 1 day')::date</code></td>
</tr>
<tr class="even">
<td>Python</td>
<td><code>deter["view_date"].min()</code></td>
<td><code>date + pd.offsets.MonthEnd(0)</code></td>
</tr>
<tr class="odd">
<td>Power BI (DAX)</td>
<td><code>MIN()</code>, <code>MAX()</code></td>
<td><code>EOMONTH ( date, 0 )</code></td>
</tr>
</tbody>
</table>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/06-monthly-cumulative-table.html">Monthly cumulative table</a>, which uses these fields</li>
<li><a href="https://hendesson.github.io/guia-espacial/curso/07-power-bi.html">Course module 7: Power BI</a> (in Portuguese), with the calendar table</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/04-fire-hotspots.html" class="anterior"><span class="direcao">← Previous</span> Fire hotspots in protected areas over the last 24 and 48 hours</a> <a href="../articles/06-monthly-cumulative-table.html" class="proximo"><span class="direcao">Next →</span> DETER monthly cumulative table, with zero-filled months</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/05-campos-de-data.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Year, Month and {PRODES} Year: Date Fields in Every Program},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/05-date-fields.html},
  doi = {10.5281/zenodo.23780254},
  langid = {en},
  abstract = {Year, month, month name, PRODES year (August to July) and
    PRODES month in one line per program, without depending on the
    computer’s language. Includes Power Query and DAX for those who
    publish in Power BI.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Year, Month and PRODES Year: Date Fields
in Every Program.”</span> In <em>Geoprocessing Articles for
Environmental Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780254">https://doi.org/10.5281/zenodo.23780254</a>.
</div></div></section></div> ]]></description>
  <category>Dates</category>
  <category>Power BI</category>
  <guid>https://hendesson.github.io/en/articles/05-date-fields.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/calendario_prodes.png" medium="image" type="image/png" height="43" width="144"/>
</item>
<item>
  <title>DETER monthly cumulative table, with zero-filled months</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/06-monthly-cumulative-table.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Intermediate <span class="rotulo">Reading</span> 9 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"> <img src="https://hendesson.github.io/en/imagens/ferramentas/powerbi.svg" class="img-fluid" alt="Power BI"></p>
</div>
<p>The most requested chart in a deforestation dashboard is the <strong>cumulative curve</strong>: how much has been cleared since the start of the PRODES year, month by month, compared with previous years.</p>
<p>To draw that curve, the table needs <strong>one row per protected area, year and month</strong>, including months <strong>without any alert</strong> (with zero). Without them, the line skips months and the cumulative total is wrong. The script I used did this with pandas <code>itertools.product</code>, <code>merge</code> and <code>cumsum</code>, after several <code>arcpy</code> steps.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/acumulado_mensal.png" class="img-fluid figure-img" alt="Line chart with three PRODES years. The horizontal axis runs from August to July. The 2025 line ends at 757 hectares and the 2026 line at 821. The 2027 line only goes up to October, at 587 hectares, because the following months have not happened yet."></p>
<figcaption>Cumulative area of deforestation alerts inside Jamanxim National Forest, by PRODES year.</figcaption>
</figure>
</div>
<section id="what-you-will-do" class="level2">
<h2 class="anchored" data-anchor-id="what-you-will-do">What you will do</h2>
<ol type="1">
<li><strong>Filter</strong> deforestation alerts (clear-cut, with vegetation, and mining), inside protected areas, of at least 6.25 ha.</li>
<li>Compute the <strong>PRODES year</strong> and <strong>PRODES month</strong> (see <a href="../articles/05-date-fields.html">Date fields</a>).</li>
<li>Build the <strong>complete grid</strong>: all protected areas × all years × 12 months.</li>
<li><strong>Sum</strong> the area of each month and fill the gaps with <strong>zero</strong>.</li>
<li><strong>Accumulate</strong> within each protected area and year.</li>
<li>Flag as <strong>not valid</strong> the months after the latest data (so the chart does not draw a flat line up to July).</li>
</ol>
<div class="cell" data-eval="true" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
    A["Filtered&lt;br/&gt;alerts"] --&gt; B["Sum by&lt;br/&gt;area, year, month"]
    G["Grid:&lt;br/&gt;area × year × 12 months"] --&gt; C["Join&lt;br/&gt;(gaps = 0)"]
    B --&gt; C
    C --&gt; D["Cumulative&lt;br/&gt;within the year"]
    D --&gt; E["valid = false&lt;br/&gt;after the last month"]
</pre>
</div>
<p></p><figcaption> From the monthly sum to the complete table</figcaption> </figure><p></p>
</div>
</div>
</div>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>Use the <strong><code>deter_interior_entorno</code></strong> layer created in the article <a href="../articles/02-inside-and-around.html">Inside and around protected areas</a> (it already has <code>nome_uc</code>, <code>local</code> and <code>area_ha</code>).</p>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Names in Portuguese: <code>soma</code> = sum, <code>grade</code> = grid, <code>tabela</code> = table, <code>acumulado_ha</code> = cumulative hectares, <code>valido</code> = valid, <code>ultimo</code> = latest, <code>mensal</code> = monthly.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false" href="">Power BI</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p><code>arcpy</code> has no tool to “complete the grid”. The solution is the same as in the original script: sum with <code>arcpy</code> and complete with pandas, which ships with ArcGIS Pro’s Python.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-4"></span>
<span id="cb1-5">gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb"</span></span>
<span id="cb1-6">fc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter_interior_entorno"</span></span>
<span id="cb1-7">filtro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname IN ('DESMATAMENTO_CR', 'DESMATAMENTO_VEG', 'MINERACAO') "</span></span>
<span id="cb1-8">          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AND local = 'interior' AND area_ha &gt;= 6.25"</span>)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 and 2. Read the filtered table into pandas</span></span>
<span id="cb1-11">linhas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.da.SearchCursor(fc, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>], filtro)</span>
<span id="cb1-12">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(linhas), columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>])</span>
<span id="cb1-13">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>])</span>
<span id="cb1-14">d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.dt.year <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (data.dt.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb1-15">d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (data.dt.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-16"></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 and 4. Complete grid and monthly sum (gaps = 0)</span></span>
<span id="cb1-18">soma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>])[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb1-19">grade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.MultiIndex.from_product(</span>
<span id="cb1-20">    [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>].unique()), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>].unique()), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>)],</span>
<span id="cb1-21">    names<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>soma.index.names)</span>
<span id="cb1-22">tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soma.reindex(grade, fill_value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>).reset_index()</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Cumulative</span></span>
<span id="cb1-25">tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acumulado_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tabela.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>])[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>].cumsum()</span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 6. Months after the latest data</span></span>
<span id="cb1-28">ultimo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(r[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(fc, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>]))</span>
<span id="cb1-29">ano_u, mes_u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ultimo.year <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (ultimo.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), (ultimo.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-30">tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"valido"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>((tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> ano_u) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> mes_u))</span>
<span id="cb1-31"></span>
<span id="cb1-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save as a geodatabase table</span></span>
<span id="cb1-33">arr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.rec.fromrecords(tabela.astype({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U120"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"valido"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"int16"</span>}).values,</span>
<span id="cb1-34">                         names<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(tabela.columns))</span>
<span id="cb1-35">arcpy.da.NumPyArrayToTable(arr, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter_mensal_acumulado"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>In QGIS, the most direct path is a <strong>virtual layer</strong>: a SQL (SQLite) query over the open layers, with no database. Open <strong>Layer → Add Layer → Add/Edit Virtual Layer</strong>, import the <code>deter_interior_entorno</code> layer and paste the query. Or, in the console:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">consulta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">WITH alertas AS (</span></span>
<span id="cb2-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT nome_uc,</span></span>
<span id="cb2-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">           CAST(strftime('%Y', view_date) AS INTEGER)</span></span>
<span id="cb2-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">             + (CAST(strftime('%m', view_date) AS INTEGER) &gt;= 8)          AS ano_prodes,</span></span>
<span id="cb2-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">           (CAST(strftime('%m', view_date) AS INTEGER) + 4) % 12 + 1      AS mes_prodes,</span></span>
<span id="cb2-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">           area_ha</span></span>
<span id="cb2-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM deter</span></span>
<span id="cb2-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    WHERE classname IN ('DESMATAMENTO_CR', 'DESMATAMENTO_VEG', 'MINERACAO')</span></span>
<span id="cb2-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      AND local = 'interior' AND area_ha &gt;= 6.25</span></span>
<span id="cb2-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span></span>
<span id="cb2-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">meses(mes_prodes) AS (SELECT 1 UNION ALL SELECT mes_prodes + 1 FROM meses WHERE mes_prodes &lt; 12),</span></span>
<span id="cb2-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">grade AS (</span></span>
<span id="cb2-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT u.nome_uc, a.ano_prodes, m.mes_prodes</span></span>
<span id="cb2-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM (SELECT DISTINCT nome_uc FROM alertas) u,</span></span>
<span id="cb2-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">         (SELECT DISTINCT ano_prodes FROM alertas) a, meses m</span></span>
<span id="cb2-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span></span>
<span id="cb2-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">mensal AS (</span></span>
<span id="cb2-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT g.nome_uc, g.ano_prodes, g.mes_prodes, coalesce(sum(a.area_ha), 0) AS area_ha</span></span>
<span id="cb2-20"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM grade g</span></span>
<span id="cb2-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    LEFT JOIN alertas a USING (nome_uc, ano_prodes, mes_prodes)</span></span>
<span id="cb2-22"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    GROUP BY g.nome_uc, g.ano_prodes, g.mes_prodes</span></span>
<span id="cb2-23"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">),</span></span>
<span id="cb2-24"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ultimo AS (</span></span>
<span id="cb2-25"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    SELECT CAST(strftime('%Y', max(view_date)) AS INTEGER)</span></span>
<span id="cb2-26"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">             + (CAST(strftime('%m', max(view_date)) AS INTEGER) &gt;= 8)      AS ano,</span></span>
<span id="cb2-27"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">           (CAST(strftime('%m', max(view_date)) AS INTEGER) + 4) % 12 + 1 AS mes</span></span>
<span id="cb2-28"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    FROM deter</span></span>
<span id="cb2-29"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb2-30"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">SELECT m.*,</span></span>
<span id="cb2-31"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">       sum(m.area_ha) OVER (PARTITION BY m.nome_uc, m.ano_prodes</span></span>
<span id="cb2-32"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">                            ORDER BY m.mes_prodes) AS acumulado_ha,</span></span>
<span id="cb2-33"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">       NOT (m.ano_prodes = u.ano AND m.mes_prodes &gt; u.mes) AS valido</span></span>
<span id="cb2-34"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">FROM mensal m, ultimo u</span></span>
<span id="cb2-35"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">ORDER BY m.nome_uc, m.ano_prodes, m.mes_prodes</span></span>
<span id="cb2-36"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-37">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/deter_interior_entorno.gpkg"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb2-38">QgsProject.instance().addMapLayer(deter)</span>
<span id="cb2-39">tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"?query=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>consulta<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter_mensal_acumulado"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"virtual"</span>)</span>
<span id="cb2-40">QgsProject.instance().addMapLayer(tabela)</span>
<span id="cb2-41"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(tabela.featureCount(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rows"</span>)</span></code></pre></div></div>
<p>The virtual layer is recomputed whenever the source layer changes, and can be exported as CSV or GeoPackage (<strong>right-click → Export</strong>).</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OR</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">REPLACE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VIEW</span> artigos.deter_mensal_acumulado <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> alertas <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-3">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> nome_uc,</span>
<span id="cb3-4">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interval</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'5 months'</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ano_prodes,</span>
<span id="cb3-5">           (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">month</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> view_date):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) % <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> mes_prodes,</span>
<span id="cb3-6">           area_ha</span>
<span id="cb3-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno</span>
<span id="cb3-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> classname <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IN</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO_CR'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO_VEG'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MINERACAO'</span>)</span>
<span id="cb3-9">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'interior'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> area_ha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.25</span></span>
<span id="cb3-10">),</span>
<span id="cb3-11">grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> u.nome_uc, a.ano_prodes, m.mes_prodes</span>
<span id="cb3-13">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DISTINCT</span> nome_uc <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> alertas) u</span>
<span id="cb3-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CROSS</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DISTINCT</span> ano_prodes <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> alertas) a</span>
<span id="cb3-15">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CROSS</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> generate_series(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> m(mes_prodes)</span>
<span id="cb3-16">),</span>
<span id="cb3-17">mensal <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-18">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> g.nome_uc, g.ano_prodes, g.mes_prodes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coalesce</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(a.area_ha), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha</span>
<span id="cb3-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> grade g</span>
<span id="cb3-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">LEFT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> alertas a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> (nome_uc, ano_prodes, mes_prodes)</span>
<span id="cb3-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> g.nome_uc, g.ano_prodes, g.mes_prodes</span>
<span id="cb3-22">),</span>
<span id="cb3-23">ultimo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(view_date) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">interval</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'5 months'</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ano,</span>
<span id="cb3-25">           (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">month</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(view_date)):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:int</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) % <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> mes</span>
<span id="cb3-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno</span>
<span id="cb3-27">)</span>
<span id="cb3-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> m.nome_uc, m.ano_prodes, m.mes_prodes, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(m.area_ha, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha,</span>
<span id="cb3-29">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(m.area_ha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OVER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">PARTITION</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> m.nome_uc, m.ano_prodes</span>
<span id="cb3-30">                                  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> m.mes_prodes), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> acumulado_ha,</span>
<span id="cb3-31">       <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> (m.ano_prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u.ano <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> m.mes_prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> u.mes) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> valido</span>
<span id="cb3-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> mensal m, ultimo u;</span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_mensal_acumulado</span>
<span id="cb3-35"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> nome_uc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'FLORESTA NACIONAL DO JAMANXIM'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> ano_prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2027</span></span>
<span id="cb3-36"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> mes_prodes;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb4-3"></span>
<span id="cb4-4">todos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/deter_interior_entorno.gpkg"</span>)</span>
<span id="cb4-5">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> todos[todos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>].isin([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_CR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_VEG"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MINERACAO"</span>])</span>
<span id="cb4-6">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (todos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interior"</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (todos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">6.25</span>)].copy()</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. PRODES year and month</span></span>
<span id="cb4-9">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>])</span>
<span id="cb4-10">d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> data.dt.year <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (data.dt.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb4-11">d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (data.dt.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-12"></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3 and 4. Complete grid and monthly sum (gaps = 0)</span></span>
<span id="cb4-14">soma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>])[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb4-15">grade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.MultiIndex.from_product(</span>
<span id="cb4-16">    [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>].unique()), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>].unique()), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>)],</span>
<span id="cb4-17">    names<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>soma.index.names)</span>
<span id="cb4-18">tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soma.reindex(grade, fill_value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>).reset_index()</span>
<span id="cb4-19"></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Cumulative over the PRODES year</span></span>
<span id="cb4-21">tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"acumulado_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tabela.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>])[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>].cumsum()</span>
<span id="cb4-22"></span>
<span id="cb4-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 6. Months after the latest data</span></span>
<span id="cb4-24">ultimo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(todos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb4-25">ano_u, mes_u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ultimo.year <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (ultimo.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), (ultimo.month <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-26">tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"valido"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>((tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ano_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> ano_u) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (tabela[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mes_prodes"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> mes_u))</span>
<span id="cb4-27"></span>
<span id="cb4-28">tabela.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>).to_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/deter_mensal_acumulado.csv"</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-29"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(tabela.query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc == 'FLORESTA NACIONAL DO JAMANXIM' and ano_prodes == 2027"</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div></div>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<p>In Power BI, the complete grid and the cumulative total come from the <strong>model</strong>, with no prebuilt table: all you need is a calendar table (<code>dim_calendario</code>) related to the alerts by date.</p>
<pre><code>Area (ha) = SUM ( fato_deter[area_ha] ) + 0

Cumulative area in the PRODES year (ha) =
CALCULATE ( [Area (ha)], DATESYTD ( dim_calendario[data], "07-31" ) )

Cumulative area (months with data only) =
IF ( MAX ( dim_calendario[data] ) &lt;= MAX ( fato_deter[view_date] ),
     [Cumulative area in the PRODES year (ha)] )</code></pre>
<ul>
<li>The <code>+ 0</code> makes months without alerts show up as zero.</li>
<li>The <code>"07-31"</code> tells <code>DATESYTD</code> that the year ends on <strong>July 31</strong>: that is the PRODES year.</li>
<li>The third measure returns blank after the latest alert, the equivalent of the <code>valido</code> column.</li>
</ul>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The QGIS (virtual layer), PostGIS and Python versions produced the same table: 288 rows with the same values. PRODES year 2027 in Jamanxim National Forest looks like this:</p>
<table class="table-striped caption-top table">
<caption>Jamanxim National Forest, PRODES year 2027 (August 2026 to July 2027).</caption>
<colgroup>
<col style="width: 22%">
<col style="width: 16%">
<col style="width: 22%">
<col style="width: 22%">
<col style="width: 16%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: right;">PRODES month</th>
<th>Month</th>
<th style="text-align: right;">Area in month (ha)</th>
<th style="text-align: right;">Cumulative (ha)</th>
<th><code>valido</code></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td>August</td>
<td style="text-align: right;">57.7</td>
<td style="text-align: right;">57.7</td>
<td>yes</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td>September</td>
<td style="text-align: right;">414.4</td>
<td style="text-align: right;">472.1</td>
<td>yes</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td>October</td>
<td style="text-align: right;">115.3</td>
<td style="text-align: right;">587.4</td>
<td>yes</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td>November</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">587.4</td>
<td>no</td>
</tr>
<tr class="odd">
<td style="text-align: right;">…</td>
<td>…</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">587.4</td>
<td>no</td>
</tr>
<tr class="even">
<td style="text-align: right;">12</td>
<td>July</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">587.4</td>
<td>no</td>
</tr>
</tbody>
</table>
<p>In three months, the cumulative total already reached 587 ha, against 757 ha for the whole of PRODES year 2025 and 821 ha for 2026. PRODES year 2024 is incomplete in the table because the downloaded data starts in January 2024.</p>
</section>
<section id="where-the-6.25-ha-comes-from" class="level2">
<h2 class="anchored" data-anchor-id="where-the-6.25-ha-comes-from">Where the “6.25 ha” comes from</h2>
<p>The minimum-area filter comes from the original script and follows the minimum mapping area of PRODES. DETER has smaller alerts, down to 3 ha. Changing this threshold changes the curve, so state it in the dashboard.</p>
</section>
<section id="classes-as-columns" class="level2">
<h2 class="anchored" data-anchor-id="classes-as-columns">Classes as columns</h2>
<p>Another script in the folder turned DETER classes into columns (<code>DCR_AREA</code>, <code>MIN_AREA</code>, …) and replaced blanks with zero. It is the same idea in another form: a <strong>pivot table</strong>.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Program</th>
<th>How</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>ArcGIS Pro</td>
<td><code>arcpy.management.PivotTable(tabela, ["nome_uc"], "classname", "area_ha", saida)</code></td>
</tr>
<tr class="even">
<td>QGIS</td>
<td>virtual layer with <code>sum(CASE WHEN classname = 'MINERACAO' THEN area_ha ELSE 0 END) AS min_area</code></td>
</tr>
<tr class="odd">
<td>PostGIS</td>
<td><code>sum(area_ha) FILTER (WHERE classname = 'MINERACAO') AS min_area</code></td>
</tr>
<tr class="even">
<td>Python</td>
<td><code>d.pivot_table(index="nome_uc", columns="classname", values="area_ha", aggfunc="sum", fill_value=0)</code></td>
</tr>
<tr class="odd">
<td>Power BI</td>
<td>a matrix with <code>classname</code> in the columns; no need to pivot</td>
</tr>
</tbody>
</table>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/05-date-fields.html">Date fields</a></li>
<li><a href="https://hendesson.github.io/guia-espacial/curso/07-power-bi.html">Course module 7: Power BI</a> (in Portuguese)</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/05-date-fields.html" class="anterior"><span class="direcao">← Previous</span> Year, month and PRODES year: date fields in every program</a> <a href="../articles/07-joining-attributes.html" class="proximo"><span class="direcao">Next →</span> Bringing in data from another table: joins and lists of values</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/06-tabela-mensal-acumulada.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {DETER Monthly Cumulative Table, with Zero-Filled Months},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/06-monthly-cumulative-table.html},
  doi = {10.5281/zenodo.23780256},
  langid = {en},
  abstract = {The table behind the cumulative-curve chart of a
    deforestation dashboard: one row per protected area, year and month,
    with zero in months without alerts, cumulative over the PRODES year
    and a flag for months that have not happened yet. Also in DAX, with
    DATESYTD.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“DETER Monthly Cumulative Table, with
Zero-Filled Months.”</span> In <em>Geoprocessing Articles for
Environmental Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780256">https://doi.org/10.5281/zenodo.23780256</a>.
</div></div></section></div> ]]></description>
  <category>DETER</category>
  <category>Dashboards</category>
  <category>Power BI</category>
  <guid>https://hendesson.github.io/en/articles/06-monthly-cumulative-table.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/acumulado_mensal.png" medium="image" type="image/png" height="75" width="144"/>
</item>
<item>
  <title>Bringing in data from another table: joins and lists of values</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/07-joining-attributes.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Beginner <span class="rotulo">Reading</span> 6 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>Two tasks appear in almost every monitoring script:</p>
<ol type="1">
<li><strong>Join by field:</strong> the alerts layer has the protected area code (<code>cnuc</code>, from Brazil’s National Register of Protected Areas), and you want to bring in the category, group and level of government, which are in the protected areas layer.</li>
<li><strong>List of values:</strong> one row per protected area with <strong>all</strong> the municipalities where alerts occurred, separated by commas. In my script, it was the list of e-mails of the people responsible for each alert.</li>
</ol>
<p>In the old scripts, the join was done with a dictionary and two cursors, <strong>repeated for each field</strong>, and the list of values with an 80-line function. In each program, both tasks fit in a few lines.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/juncao.png" class="img-fluid figure-img" alt="Two tables. On the left, alerts with the columns gid, cnuc and area_ha. On the right, protected areas with cnuc, categoria and grupo. Arrows link the matching cnuc values, and the result below has the columns of both tables."></p>
<figcaption>The join uses the common field (cnuc) to copy columns from one table to the other.</figcaption>
</figure>
</div>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">Data</h2>
<p>The <code>deter_interior_entorno</code> layer (from the article <a href="../articles/02-inside-and-around.html">Inside and around</a>) and the <code>ucs</code> layer of the articles’ GeoPackage. Municipalities come from DETER’s <code>municipality</code> field.</p>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Names in Portuguese: <code>alertas</code> = alerts, <code>campos</code> = fields, <code>categoria</code> = category, <code>grupo</code> = group, <code>esfera</code> = level of government, <code>municipios</code> = municipalities, <code>lista</code> = list.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"></span>
<span id="cb1-3">gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg"</span></span>
<span id="cb1-4">gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb"</span></span>
<span id="cb1-5">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter_interior_entorno"</span></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Join by field: one line does it</span></span>
<span id="cb1-8">arcpy.management.JoinField(alertas, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>,</span>
<span id="cb1-9">                           [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"esfera"</span>])</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. List of municipalities by protected area: CONCATENATE statistic (ArcGIS Pro 3.2 or newer)</span></span>
<span id="cb1-12">arcpy.analysis.Statistics(alertas, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">unicipios_por_uc"</span>,</span>
<span id="cb1-13">                          [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipality"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UNIQUE"</span>], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipality"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CONCATENATE"</span>]],</span>
<span id="cb1-14">                          [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], concatenation_separator<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>)</span></code></pre></div></div>
<p>If <code>JoinField</code> is slow on large tables, the old scripts’ pattern (dictionary + cursor) is faster, but write <strong>one dictionary for all fields</strong>, not one per field:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">campos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"esfera"</span>]</span>
<span id="cb2-2">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {linha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]: linha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> linha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span></span>
<span id="cb2-3">       arcpy.da.SearchCursor(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> campos)}</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> campos:</span>
<span id="cb2-6">    arcpy.management.AddField(alertas, c, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>)</span>
<span id="cb2-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> arcpy.da.UpdateCursor(alertas, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> campos) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> cursor:</span>
<span id="cb2-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> linha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cursor:</span>
<span id="cb2-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> linha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ucs:</span>
<span id="cb2-10">            cursor.updateRow([linha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>ucs[linha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]]])</span></code></pre></div></div>
<p>In older versions of Pro (without <code>CONCATENATE</code>), build the list with a dictionary of sets:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> collections <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> defaultdict</span>
<span id="cb3-2">lista <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> defaultdict(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>)</span>
<span id="cb3-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> uc, mun <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(alertas, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipality"</span>]):</span>
<span id="cb3-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> mun:</span>
<span id="cb3-5">        lista[uc].add(mun)</span>
<span id="cb3-6">municipios <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {uc: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(m)) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> uc, m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lista.items()}</span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p><strong>From the menus:</strong> alerts layer properties → <strong>Joins</strong> → <code>+</code>, layer <code>ucs</code>, field <code>cnuc</code> on both sides. To write the join permanently, use <strong>Join attributes by field value</strong> in the Toolbox. For the list, use <strong>Aggregate</strong> with the <code>concatenate_unique</code> function.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb4-2">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Join by field</span></span>
<span id="cb4-5">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> processing.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:joinattributestable"</span>, {</span>
<span id="cb4-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/deter_interior_entorno.gpkg"</span>,</span>
<span id="cb4-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FIELD"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT_2"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FIELD_2"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>,</span>
<span id="cb4-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FIELDS_TO_COPY"</span>: [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"esfera"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"METHOD"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 = first match only</span></span>
<span id="cb4-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>})[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. One row per protected area, with the list of municipalities and the total area</span></span>
<span id="cb4-12">municipios <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> processing.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:aggregate"</span>, {</span>
<span id="cb4-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT"</span>: alertas, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GROUP_BY"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>,</span>
<span id="cb4-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGGREGATES"</span>: [</span>
<span id="cb4-15">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first_value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"nome_uc"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>,</span>
<span id="cb4-16">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delimiter"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>},</span>
<span id="cb4-17">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first_value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"categoria"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categoria"</span>,</span>
<span id="cb4-18">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delimiter"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>},</span>
<span id="cb4-19">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"concatenate_unique"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"municipality"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipios"</span>,</span>
<span id="cb4-20">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delimiter"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>},</span>
<span id="cb4-21">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"area_ha"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb4-22">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"delimiter"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>},</span>
<span id="cb4-23">    ],</span>
<span id="cb4-24">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>})[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb4-25"></span>
<span id="cb4-26"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> municipios.getFeatures():</span>
<span id="cb4-27">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span>, f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categoria"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span>, f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipios"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>]))</span></code></pre></div></div>
<p>In the Field Calculator <strong>of the protected areas layer</strong>, the same list comes from one expression (protected areas without alerts stay empty): <code>aggregate('deter_interior_entorno', 'concatenate_unique', "municipality", filter:="nome_uc" = attribute(@parent, 'nome_uc'), concatenator:=', ')</code>.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 1. Join by field</span></span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> a.gid, a.nome_uc, a.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span>, a.area_ha, u.categoria, u.grupo, u.esfera</span>
<span id="cb5-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno a</span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.ucs u <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> (cnuc)</span>
<span id="cb5-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">LIMIT</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>;</span>
<span id="cb5-6"></span>
<span id="cb5-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 2. One row per protected area, with the list of municipalities</span></span>
<span id="cb5-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> a.nome_uc, u.categoria,</span>
<span id="cb5-9">       string_agg(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DISTINCT</span> a.municipality, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">', '</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> a.municipality) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> municipios,</span>
<span id="cb5-10">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(a.area_ha)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha</span>
<span id="cb5-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno a</span>
<span id="cb5-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.ucs u <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> (cnuc)</span>
<span id="cb5-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> a.nome_uc, u.categoria</span>
<span id="cb5-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> area_ha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb6-2"></span>
<span id="cb6-3">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/deter_interior_entorno.gpkg"</span>)</span>
<span id="cb6-4">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span>, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>)</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Join by field (validate checks that each cnuc appears only once in the protected areas)</span></span>
<span id="cb6-7">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> alertas.merge(ucs[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"esfera"</span>]],</span>
<span id="cb6-8">                        on<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, validate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"many_to_one"</span>)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. One row per protected area, with the list of municipalities</span></span>
<span id="cb6-11">lista <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> s: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>.join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(s.dropna().unique()))</span>
<span id="cb6-12">municipios <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (alertas.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>)</span>
<span id="cb6-13">              .agg(categoria<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categoria"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>), municipios<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipality"</span>, lista),</span>
<span id="cb6-14">                   area_ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>))</span>
<span id="cb6-15">              .<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>).sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>))</span>
<span id="cb6-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(municipios.to_string())</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The three tested versions gave the same lists and the same area (PostGIS differs by less than 0.1%, because of how it measures, as in article 2):</p>
<table class="table-striped caption-top table">
<caption>Inside and surroundings combined, all DETER classes.</caption>
<colgroup>
<col style="width: 23%">
<col style="width: 23%">
<col style="width: 23%">
<col style="width: 30%">
</colgroup>
<thead>
<tr class="header">
<th>Protected area</th>
<th>Category</th>
<th>Municipalities with alerts</th>
<th style="text-align: right;">Alert area (ha)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Jamanxim National Forest</td>
<td>Forest</td>
<td>Itaituba, Novo Progresso</td>
<td style="text-align: right;">167,584</td>
</tr>
<tr class="even">
<td>Altamira National Forest</td>
<td>Forest</td>
<td>Altamira, Itaituba</td>
<td style="text-align: right;">66,310</td>
</tr>
<tr class="odd">
<td>Jamanxim National Park</td>
<td>Park</td>
<td>Itaituba, Trairao</td>
<td style="text-align: right;">33,286</td>
</tr>
<tr class="even">
<td>Tapajós Environmental Protection Area</td>
<td>Environmental Protection Area</td>
<td>Itaituba, Novo Progresso</td>
<td style="text-align: right;">22,855</td>
</tr>
<tr class="odd">
<td>Rio Novo National Park</td>
<td>Park</td>
<td>Itaituba, Novo Progresso</td>
<td style="text-align: right;">10,924</td>
</tr>
<tr class="even">
<td>Iriri State Forest</td>
<td>Forest</td>
<td>Altamira</td>
<td style="text-align: right;">467</td>
</tr>
</tbody>
</table>
<p>Note “Trairao”, without the tilde: that is how the name comes in DETER (the correct spelling is Trairão). One more reason to join tables by <strong>code</strong>, not by name. QGIS’s <code>concatenate_unique</code> lists the names in the order they appear; for alphabetical order, use this in the Field Calculator: <code>array_to_string(array_sort(array_distinct(array_agg("municipality", group_by:="nome_uc"))), ', ')</code>.</p>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>A repeated key multiplies rows
</div>
</div>
<div class="callout-body-container callout-body">
<p>If a <code>cnuc</code> appears twice in the protected areas layer (for example, an area split into two polygons), each alert is copied twice and the area doubles. pandas <code>validate="many_to_one"</code> raises an error; in PostGIS, check with <code>SELECT cnuc, count(*) FROM artigos.ucs GROUP BY cnuc HAVING count(*) &gt; 1</code>; in QGIS, use <code>METHOD = 1</code> (first match only).</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Joining by name instead of code
</div>
</div>
<div class="callout-body-container callout-body">
<p>The old scripts sometimes linked tables by <strong>protected area name</strong>. A different accent or capital letter (<code>Floresta Nacional</code> × <code>FLORESTA NACIONAL</code>) and the join fails silently. Always use a code (<code>cnuc</code>, IBGE municipality code, embargo number).</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Removing duplicate rows
</div>
</div>
<div class="callout-body-container callout-body">
<p>To keep <strong>one row per alert</strong> after concatenating (ArcGIS’s <code>DeleteIdentical</code>), use <code>native:removeduplicatesbyattribute</code> in QGIS, <code>DISTINCT ON (gid)</code> in PostGIS and <code>drop_duplicates("gid")</code> in pandas.</p>
</div>
</div>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/01-equivalences.html">From ArcMap to QGIS: the equivalence table</a></li>
<li><a href="https://hendesson.github.io/guia-espacial/curso/02-joins-e-agregacoes.html">Course module 2: joins and aggregations</a> (in Portuguese)</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/06-monthly-cumulative-table.html" class="anterior"><span class="direcao">← Previous</span> DETER monthly cumulative table, with zero-filled months</a> <a href="../articles/08-car-protected-areas.html" class="proximo"><span class="direcao">Next →</span> Rural properties (CAR) inside protected areas: selecting and measuring the overlap</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/07-juntar-atributos.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Bringing in Data from Another Table: Joins and Lists of
    Values},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/07-joining-attributes.html},
  doi = {10.5281/zenodo.23780258},
  langid = {en},
  abstract = {Bringing the protected area category and group to each
    alert through the CNUC code, and collapsing several values into a
    list, such as the municipalities of each protected area. The old
    scripts used dozens of lines of cursors; here it takes a few lines
    in each program.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Bringing in Data from Another Table:
Joins and Lists of Values.”</span> In <em>Geoprocessing Articles for
Environmental Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780258">https://doi.org/10.5281/zenodo.23780258</a>.
</div></div></section></div> ]]></description>
  <category>Tables</category>
  <guid>https://hendesson.github.io/en/articles/07-joining-attributes.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/juncao.png" medium="image" type="image/png" height="66" width="144"/>
</item>
<item>
  <title>Rural properties (CAR) inside protected areas: selecting and measuring the overlap</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/08-car-protected-areas.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Intermediate <span class="rotulo">Reading</span> 8 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>Brazil’s <strong>Rural Environmental Registry (CAR)</strong> is self-declared: the landholder draws the property. That is why there are properties <strong>inside</strong> protected areas, including strict-protection ones, where private property should not exist. Knowing where this happens helps prioritize land regularization and enforcement.</p>
<p>The script I used only <strong>selected</strong> the properties that touched the protected areas. Here you will also measure <strong>how much</strong> of each property and of each protected area overlaps, which separates the property that only touches the boundary from the one lying entirely inside.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/car_ucs.png" class="img-fluid figure-img" alt="Map of the Novo Progresso region. Protected areas have a green outline and CAR properties are colored polygons: active in blue, pending in orange, suspended in purple and cancelled in grey. Many properties lie inside Jamanxim National Forest and the Tapajós Environmental Protection Area."></p>
<figcaption>CAR properties overlapping the protected areas of the study area, colored by registration status.</figcaption>
</figure>
</div>
<section id="where-the-car-comes-from" class="level2">
<h2 class="anchored" data-anchor-id="where-the-car-comes-from">Where the CAR comes from</h2>
<p>SICAR publishes the properties of each state in an open <strong>WFS</strong> service:</p>
<ul>
<li>Address: <code>https://geoserver.car.gov.br/geoserver/sicar/wfs</code></li>
<li>Layers: <code>sicar:sicar_imoveis_pa</code>, <code>sicar:sicar_imoveis_mt</code> etc. (one per state)</li>
</ul>
<p>The most useful fields are <code>cod_imovel</code> (property code), <code>status_imovel</code> (<code>AT</code> active, <code>PE</code> pending, <code>SU</code> suspended, <code>CA</code> cancelled), <code>tipo_imovel</code> (<code>IRU</code> rural property, <code>AST</code> settlement, <code>PCT</code> traditional peoples and communities), <code>condicao</code> (review status) and <code>area</code> (declared area, in hectares). The articles’ script downloads the properties of the study area into the <code>car</code> layer (<a href="../articles/about.html#data">how to download</a>).</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>SIGEF</strong> (properties certified by INCRA, the land reform agency) works the same way: replace the <code>car</code> layer with the SIGEF one and the code below works unchanged. The files are in INCRA’s Land Archive (<code>https://acervofundiario.incra.gov.br</code>).</p>
</div>
</div>
</section>
<section id="what-you-will-do" class="level2">
<h2 class="anchored" data-anchor-id="what-you-will-do">What you will do</h2>
<ol type="1">
<li><strong>Select</strong> the properties that touch the protected areas (what the old script did).</li>
<li><strong>Cut</strong> each property by the protected areas and measure the overlapping area.</li>
<li>Compute the <strong>percentage</strong> of the property inside the protected area and drop those that only touch.</li>
<li>Summarize by protected area: number of properties by status and <strong>area of the protected area covered by CAR</strong>, without double counting properties that overlap each other.</li>
</ol>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Names in Portuguese: <code>toca</code> = touches, <code>dentro</code> = inside, <code>sobreposicao_ha</code> = overlap in hectares, <code>area_imovel_ha</code> = property area, <code>pct_imovel</code> = percentage of the property, <code>coberta</code> = covered.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"></span>
<span id="cb1-3">arcpy.env.overwriteOutput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-4">gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg"</span></span>
<span id="cb1-5">gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb"</span></span>
<span id="cb1-6">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao = 'Amazonia'"</span>)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Select by location (what the old script did)</span></span>
<span id="cb1-9">car <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.MakeFeatureLayer(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">car"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"car"</span>)</span>
<span id="cb1-10">arcpy.management.SelectLayerByLocation(car, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INTERSECT"</span>, ucs)</span>
<span id="cb1-11">arcpy.management.CopyFeatures(car, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">car_toca_ucs"</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 and 3. Intersection, overlapping area and percentage of the property</span></span>
<span id="cb1-14">arcpy.management.CalculateGeometryAttributes(</span>
<span id="cb1-15">    gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">car_toca_ucs"</span>, [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_imovel_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AREA_GEODESIC"</span>]], area_unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HECTARES"</span>)</span>
<span id="cb1-16">car_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseIntersect([gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">car_toca_ucs"</span>, ucs], gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">car_ucs"</span>)</span>
<span id="cb1-17">arcpy.management.CalculateGeometryAttributes(</span>
<span id="cb1-18">    car_ucs, [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobreposicao_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AREA_GEODESIC"</span>]], area_unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HECTARES"</span>)</span>
<span id="cb1-19">arcpy.management.CalculateField(car_ucs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_imovel"</span>,</span>
<span id="cb1-20">                                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"100 * !sobreposicao_ha! / !area_imovel_ha!"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>,</span>
<span id="cb1-21">                                field_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DOUBLE"</span>)</span>
<span id="cb1-22"></span>
<span id="cb1-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Summary by protected area and status (only properties with at least 1% inside)</span></span>
<span id="cb1-24">dentro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(car_ucs, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">entro"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_imovel &gt;= 1"</span>)</span>
<span id="cb1-25">arcpy.analysis.Statistics(dentro, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">car_por_uc"</span>,</span>
<span id="cb1-26">                          [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cod_imovel"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COUNT"</span>], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobreposicao_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUM"</span>]],</span>
<span id="cb1-27">                          [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status_imovel"</span>])</span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Area of the protected area covered by CAR, without counting overlaps between properties</span></span>
<span id="cb1-30">coberta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseDissolve(dentro, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">coberta"</span>, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>])</span>
<span id="cb1-31">arcpy.management.CalculateGeometryAttributes(</span>
<span id="cb1-32">    coberta, [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coberta_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AREA_GEODESIC"</span>]], area_unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HECTARES"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-2">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb2-3">AREA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area(transform($geometry, 'EPSG:4674', 'ESRI:102033')) / 10000"</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> rodar(alg, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>p):</span>
<span id="cb2-6">    p.setdefault(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>)</span>
<span id="cb2-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> processing.run(alg, p)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-8"></span>
<span id="cb2-9">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs"</span>,</span>
<span id="cb2-10">            EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao = 'Amazonia'"</span>)</span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Select by location (predicate 0 = intersects)</span></span>
<span id="cb2-13">car <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbylocation"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=car"</span>, INTERSECT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs,</span>
<span id="cb2-14">            PREDICATE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])</span>
<span id="cb2-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"properties touching protected areas:"</span>, car.featureCount())</span>
<span id="cb2-16"></span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 and 3. Property area, intersection, overlapping area and percentage</span></span>
<span id="cb2-18">car <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>car, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_imovel_ha"</span>,</span>
<span id="cb2-19">            FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>AREA)</span>
<span id="cb2-20">car_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:intersection"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>car, OVERLAY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs,</span>
<span id="cb2-21">                OVERLAY_FIELDS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>])</span>
<span id="cb2-22">car_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>car_ucs, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobreposicao_ha"</span>,</span>
<span id="cb2-23">                FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>AREA)</span>
<span id="cb2-24">car_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>car_ucs, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_imovel"</span>,</span>
<span id="cb2-25">                FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-26">                FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'100 * "sobreposicao_ha" / "area_imovel_ha"'</span>)</span>
<span id="cb2-27"></span>
<span id="cb2-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Summary by protected area and status (at least 1% inside)</span></span>
<span id="cb2-29">dentro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>car_ucs, EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"pct_imovel" &gt;= 1'</span>)</span>
<span id="cb2-30">resumo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qgis:statisticsbycategories"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dentro, VALUES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobreposicao_ha"</span>,</span>
<span id="cb2-31">               CATEGORIES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status_imovel"</span>])</span>
<span id="cb2-32"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> resumo.getFeatures():</span>
<span id="cb2-33">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status_imovel"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>]))</span>
<span id="cb2-34"></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Area of the protected area covered by CAR, without counting overlaps between properties</span></span>
<span id="cb2-36">coberta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:dissolve"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>dentro, FIELD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>])</span>
<span id="cb2-37">coberta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>coberta, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coberta_ha"</span>,</span>
<span id="cb2-38">                FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>AREA)</span>
<span id="cb2-39"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> coberta.getFeatures():</span>
<span id="cb2-40">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"covered by CAR:"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coberta_ha"</span>]), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ha"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 1 to 3. Properties touching protected areas, overlapping area and percentage of the property</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> artigos.car_ucs;</span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.car_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> c.cod_imovel, c.status_imovel, c.tipo_imovel, u.cnuc, u.nome_uc, u.grupo,</span>
<span id="cb3-5">       ST_Area(c.geom:<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>                         <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_imovel_ha,</span>
<span id="cb3-6">       ST_Area(ST_Intersection(c.geom, u.geom):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> sobreposicao_ha,</span>
<span id="cb3-7">       ST_Intersection(c.geom, u.geom)                            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.car c</span>
<span id="cb3-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.ucs u <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> u.regiao <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazonia'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> ST_Intersects(c.geom, u.geom);</span>
<span id="cb3-10"></span>
<span id="cb3-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.car_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COLUMN</span> pct_imovel <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>;</span>
<span id="cb3-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> artigos.car_ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> pct_imovel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sobreposicao_ha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> area_imovel_ha;</span>
<span id="cb3-13"></span>
<span id="cb3-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 4. Summary by protected area (only properties with at least 1% inside)</span></span>
<span id="cb3-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> nome_uc, grupo,</span>
<span id="cb3-16">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>)                                         <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> imoveis,</span>
<span id="cb3-17">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> status_imovel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'AT'</span>)     <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ativos,</span>
<span id="cb3-18">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> status_imovel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'PE'</span>)     <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> pendentes,</span>
<span id="cb3-19">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> status_imovel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SU'</span>)     <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> suspensos,</span>
<span id="cb3-20">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> status_imovel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'CA'</span>)     <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> cancelados,</span>
<span id="cb3-21">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(sobreposicao_ha))                      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> soma_sobreposicao_ha,</span>
<span id="cb3-22">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>((ST_Area(ST_Union(geom):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:numeric</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> coberta_ha</span>
<span id="cb3-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.car_ucs</span>
<span id="cb3-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> pct_imovel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> nome_uc, grupo</span>
<span id="cb3-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> coberta_ha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-2"></span>
<span id="cb4-3">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span></span>
<span id="cb4-4">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>).query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao == 'Amazonia'"</span>)</span>
<span id="cb4-5">car <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"car"</span>)</span>
<span id="cb4-6">ALBERS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ESRI:102033"</span>                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># equal-area projection</span></span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Select by location</span></span>
<span id="cb4-9">toca <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> car[car.intersects(ucs.union_all())].copy()</span>
<span id="cb4-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"properties touching protected areas:"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(toca))</span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 and 3. Intersection, overlapping area and percentage of the property</span></span>
<span id="cb4-13">toca[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_imovel_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> toca.to_crs(ALBERS).area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span></span>
<span id="cb4-14">car_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.overlay(toca, ucs[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grupo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>]],</span>
<span id="cb4-15">                      how<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intersection"</span>, keep_geom_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-16">car_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobreposicao_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> car_ucs.to_crs(ALBERS).area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span></span>
<span id="cb4-17">car_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_imovel"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> car_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobreposicao_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> car_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_imovel_ha"</span>]</span>
<span id="cb4-18"></span>
<span id="cb4-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Summary by protected area (at least 1% inside)</span></span>
<span id="cb4-20">dentro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> car_ucs[car_ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pct_imovel"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb4-21">resumo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dentro.pivot_table(index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status_imovel"</span>, values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cod_imovel"</span>,</span>
<span id="cb4-22">                            aggfunc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>, fill_value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-23">resumo[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"soma_sobreposicao_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dentro.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sobreposicao_ha"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>()</span>
<span id="cb4-24">resumo[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coberta_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (dentro.dissolve(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>).to_crs(ALBERS).area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>()</span>
<span id="cb4-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(resumo.sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coberta_ha"</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>))</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The QGIS, PostGIS and Python versions gave the same property counts and the same areas (PostGIS differs by less than 0.01%). Since the CAR was only downloaded inside the study area, the percentages refer to the part of each protected area within the study area:</p>
<table class="table-striped caption-top table">
<caption>CAR properties overlapping the protected areas of the study area (SICAR, queried in October 2026).</caption>
<colgroup>
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 15%">
<col style="width: 15%">
<col style="width: 15%">
<col style="width: 15%">
<col style="width: 15%">
</colgroup>
<thead>
<tr class="header">
<th>Protected area</th>
<th>Group</th>
<th style="text-align: right;">Properties (≥ 1% inside)</th>
<th style="text-align: right;">Cancelled</th>
<th style="text-align: right;">Area covered by CAR (ha)</th>
<th style="text-align: right;">% of the area in the study area</th>
<th style="text-align: right;">% without cancelled</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Jamanxim National Forest</td>
<td>Sustainable use</td>
<td style="text-align: right;">415</td>
<td style="text-align: right;">119</td>
<td style="text-align: right;">631,419</td>
<td style="text-align: right;">49%</td>
<td style="text-align: right;">37%</td>
</tr>
<tr class="even">
<td>Tapajós Environmental Protection Area</td>
<td>Sustainable use</td>
<td style="text-align: right;">517</td>
<td style="text-align: right;">171</td>
<td style="text-align: right;">511,783</td>
<td style="text-align: right;">72%</td>
<td style="text-align: right;">48%</td>
</tr>
<tr class="odd">
<td>Iriri State Forest</td>
<td>Sustainable use</td>
<td style="text-align: right;">70</td>
<td style="text-align: right;">63</td>
<td style="text-align: right;">146,701</td>
<td style="text-align: right;">80%</td>
<td style="text-align: right;">4%</td>
</tr>
<tr class="even">
<td>Jamanxim National Park</td>
<td>Strict protection</td>
<td style="text-align: right;">89</td>
<td style="text-align: right;">80</td>
<td style="text-align: right;">124,953</td>
<td style="text-align: right;">19%</td>
<td style="text-align: right;">2%</td>
</tr>
<tr class="odd">
<td>Rio Novo National Park</td>
<td>Strict protection</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">17</td>
<td style="text-align: right;">51,462</td>
<td style="text-align: right;">17%</td>
<td style="text-align: right;">2%</td>
</tr>
<tr class="even">
<td>Altamira National Forest</td>
<td>Sustainable use</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">29</td>
<td style="text-align: right;">29,644</td>
<td style="text-align: right;">5%</td>
<td style="text-align: right;">1%</td>
</tr>
</tbody>
</table>
<p>The “without cancelled” column changes the story: in Iriri State Forest, 63 of the 70 records have already been cancelled, and coverage drops from 80% to 4%. In the two National Parks (strict protection), <strong>12 active, pending or suspended records</strong> remain.</p>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>“Intersects” includes properties that only touch
</div>
</div>
<div class="callout-body-container callout-body">
<p>Selection by location with <strong>intersects</strong> also picks the property drawn along the protected area boundary, with 0.01% of its area inside. In the study area, 1,196 properties touch the protected areas, but only 1,135 have at least 1% of their area inside. Measure the overlap before concluding that “there are N properties in the protected area”.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Properties overlapping each other
</div>
</div>
<div class="callout-body-container callout-body">
<p>Because the CAR is self-declared, several properties cover the same piece of land. Adding up the overlap of each one gives more area than exists: compare the sum column with the covered (dissolved) area in the table above.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Cancelled records
</div>
</div>
<div class="callout-body-container callout-body">
<p>Properties with <code>status_imovel = 'CA'</code> (cancelled) remain in the service. Decide whether they enter the analysis and say so in the report. In this article, they appear in a separate column.</p>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Personal data
</div>
</div>
<div class="callout-body-container callout-body">
<p>SICAR’s public service has neither the name nor the tax ID of the landholder, only the property code. Even so, avoid publishing lists of property codes with accusations: a CAR record overlapping a protected area is a lead for analysis, not a proven violation.</p>
</div>
</div>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/02-inside-and-around.html">Inside and around protected areas</a></li>
<li><a href="https://hendesson.github.io/guia-espacial/guia/03-fontes-de-dados.html">Data sources</a> (in Portuguese), with the CAR, SIGEF and Indigenous Lands</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/07-joining-attributes.html" class="anterior"><span class="direcao">← Previous</span> Bringing in data from another table: joins and lists of values</a> <a href="../articles/09-kernel-density.html" class="proximo"><span class="direcao">Next →</span> Kernel density of deforestation: where the pressure is concentrated</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/08-car-ucs.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Rural Properties {(CAR)} Inside Protected Areas: Selecting
    and Measuring the Overlap},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/08-car-protected-areas.html},
  doi = {10.5281/zenodo.23780260},
  langid = {en},
  abstract = {Rural properties from the Rural Environmental Registry
    (CAR), downloaded from the public SICAR service and overlaid with
    protected areas, which ones only touch the boundary, which ones are
    inside, and how much of each protected area is declared as a rural
    property. Removing cancelled records changes the conclusion.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Rural Properties (CAR) Inside Protected
Areas: Selecting and Measuring the Overlap.”</span> In <em>Geoprocessing
Articles for Environmental Monitoring</em>. Preprint, Hendesson Alves,
October 29. <a href="https://doi.org/10.5281/zenodo.23780260">https://doi.org/10.5281/zenodo.23780260</a>.
</div></div></section></div> ]]></description>
  <category>CAR</category>
  <category>Protected areas</category>
  <guid>https://hendesson.github.io/en/articles/08-car-protected-areas.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/car_ucs.png" medium="image" type="image/png" height="192" width="144"/>
</item>
<item>
  <title>Kernel density of deforestation: where the pressure is concentrated</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/09-kernel-density.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Intermediate <span class="rotulo">Reading</span> 11 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>A map with 1,600 small deforestation polygons is hard to read. <strong>Kernel density</strong> spreads each polygon over a neighborhood and adds everything into a continuous surface: the result is a <strong>heat map</strong> that shows where deforestation is concentrated.</p>
<p>The script I used computed two densities, one weighted by polygon <strong>area</strong> and one by <strong>count</strong> only, normalized each between 0 and 1 and took the average. That way, both one large clearing and many small ones show up as “hot”. Then it computed the mean index inside each protected area.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/kernel.png" class="img-fluid figure-img" alt="Heat map over the Novo Progresso region, from light yellow to red. Most of the heat lies outside the protected areas, in a band east of Jamanxim National Forest. The hottest point, marked with an X, lies on the border between the Tapajós Environmental Protection Area and Jamanxim National Forest, to the west. Protected area boundaries are drawn in black."></p>
<figcaption>PRODES 2025 deforestation density index (average of the area and count densities, from 0 to 1).</figcaption>
</figure>
</div>
<section id="how-it-works" class="level2">
<h2 class="anchored" data-anchor-id="how-it-works">How it works</h2>
<div class="cell" data-eval="true" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
    A["PRODES 2025&lt;br/&gt;polygons"] --&gt; B["Point inside&lt;br/&gt;each polygon"]
    B --&gt; C["Kernel weighted&lt;br/&gt;by area"]
    B --&gt; D["Unweighted kernel&lt;br/&gt;(count)"]
    C --&gt; E["Normalize&lt;br/&gt;0 to 1"]
    D --&gt; F["Normalize&lt;br/&gt;0 to 1"]
    E --&gt; G["Average"]
    F --&gt; G
    G --&gt; H["Mean by&lt;br/&gt;protected area"]
</pre>
</div>
<p></p><figcaption> From polygon to index by protected area</figcaption> </figure><p></p>
</div>
</div>
</div>
<ul>
<li><strong>Search radius:</strong> how far each point “spreads” its influence. Here, <strong>10 km</strong>.</li>
<li><strong>Cell size:</strong> the resolution of the map. Here, <strong>1 km</strong>.</li>
<li><strong>Quartic kernel:</strong> the weight falls smoothly from the center (maximum weight) to the edge of the radius (zero). It is the default in ArcGIS and QGIS.</li>
<li><strong>Study area:</strong> we only downloaded deforestation inside a rectangle. Outside it, the density would be zero without really being zero, so the map and the means by protected area stay <strong>inside the study area</strong>.</li>
<li><strong>Normalize:</strong> <code>(value − minimum) / (maximum − minimum)</code>. Each program computes density on a different scale, but after normalizing the results are comparable.</li>
</ul>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Work in meters
</div>
</div>
<div class="callout-body-container callout-body">
<p>The old script used a radius of <code>0.777</code> and a cell of <code>0.0015</code>, in <strong>degrees</strong>. A degree of longitude is about 111 km at the Equator and less further south, so the search “circle” becomes an ellipse. Reproject to a projection in meters (here, EPSG:5880) and write radius and cell size in meters.</p>
</div>
</div>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Data: layers <code>prodes_amz</code> (year 2025) and <code>ucs</code> of the articles’ GeoPackage (<a href="../articles/about.html#data">how to download</a>). Names in Portuguese: <code>pontos</code> = points, <code>peso</code> = weight, <code>recorte</code> = study area, <code>indice</code> = index, <code>kd_qtde</code> = count density, <code>RAIO</code> = radius, <code>CEL</code> = cell.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>Requires the <strong>Spatial Analyst</strong> extension.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> arcpy.sa <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> KernelDensity</span>
<span id="cb1-3"></span>
<span id="cb1-4">arcpy.CheckOutExtension(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spatial"</span>)</span>
<span id="cb1-5">arcpy.env.overwriteOutput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-6">arcpy.env.outputCoordinateSystem <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.SpatialReference(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># meters</span></span>
<span id="cb1-7">gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg"</span></span>
<span id="cb1-8">gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb"</span></span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Downloaded area (the study area): the map and the means stay inside it</span></span>
<span id="cb1-11">recorte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.Extent(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">56.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">54.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, spatial_reference<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>arcpy.SpatialReference(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>))</span>
<span id="cb1-12">arcpy.env.extent <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> recorte.projectAs(arcpy.SpatialReference(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>))</span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. 2025 polygons, area in hectares and a point inside each one</span></span>
<span id="cb1-15">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">prodes_amz"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">prodes"</span>,</span>
<span id="cb1-16">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year = 2025 AND UPPER(main_class) = 'DESMATAMENTO'"</span>)</span>
<span id="cb1-17">arcpy.management.RepairGeometry(prodes)</span>
<span id="cb1-18">arcpy.management.CalculateGeometryAttributes(</span>
<span id="cb1-19">    prodes, [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AREA_GEODESIC"</span>]], area_unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HECTARES"</span>)</span>
<span id="cb1-20">pontos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.FeatureToPoint(prodes, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">pontos"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INSIDE"</span>)</span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Two densities: weighted by area and unweighted (1 km cell, 10 km radius)</span></span>
<span id="cb1-23">kd_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> KernelDensity(pontos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SQUARE_KILOMETERS"</span>)</span>
<span id="cb1-24">kd_qtde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> KernelDensity(pontos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NONE"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SQUARE_KILOMETERS"</span>)</span>
<span id="cb1-25"></span>
<span id="cb1-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Normalize each one between 0 and 1 and take the average</span></span>
<span id="cb1-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> normalizar(r):</span>
<span id="cb1-28">    arcpy.management.CalculateStatistics(r)</span>
<span id="cb1-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> r.minimum) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (r.maximum <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> r.minimum)</span>
<span id="cb1-30"></span>
<span id="cb1-31">indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (normalizar(kd_area) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> normalizar(kd_qtde)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-32">indice.save(gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">kernel_indice"</span>)</span>
<span id="cb1-33"></span>
<span id="cb1-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Mean and maximum of the index in each protected area, only inside the study area</span></span>
<span id="cb1-35">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao = 'Amazonia'"</span>)</span>
<span id="cb1-36">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Clip(ucs, recorte.polygon, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ucs_recorte"</span>)</span>
<span id="cb1-37">arcpy.sa.ZonalStatisticsAsTable(ucs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, indice, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">kernel_por_uc"</span>,</span>
<span id="cb1-38">                                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MEAN_STD"</span>)</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The original script normalized with <code>ZonalStatistics</code> (minimum and maximum per zone). With a single zone, the raster’s <code>r.minimum</code> and <code>r.maximum</code> give the same result in one line.</p>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-2">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb2-3">SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/"</span></span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> rodar(alg, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>p):</span>
<span id="cb2-6">    p.setdefault(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>)</span>
<span id="cb2-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> processing.run(alg, p)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. 2025 polygons, area in hectares and a point inside each one (in meters)</span></span>
<span id="cb2-10">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=prodes_amz"</span>,</span>
<span id="cb2-11">               EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year = 2025 AND upper(main_class) = 'DESMATAMENTO'"</span>)</span>
<span id="cb2-12">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fixgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prodes)</span>
<span id="cb2-13">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:removenullgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prodes, REMOVE_EMPTY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># slivers</span></span>
<span id="cb2-14">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prodes, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-15">               FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-16">               FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area(transform($geometry, 'EPSG:4674', 'ESRI:102033')) / 10000"</span>)</span>
<span id="cb2-17">pontos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:pointonsurface"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>prodes)</span>
<span id="cb2-18">pontos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:reprojectlayer"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pontos, TARGET_CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:5880"</span>)</span>
<span id="cb2-19"></span>
<span id="cb2-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Heat map (quartic kernel), weighted and unweighted</span></span>
<span id="cb2-21">kernel <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pontos, RADIUS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, PIXEL_SIZE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, KERNEL<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, OUTPUT_VALUE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-22">kd_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:heatmapkerneldensityestimation"</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kernel, WEIGHT_FIELD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb2-23">                OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kd_area.tif"</span>)</span>
<span id="cb2-24">kd_qtde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:heatmapkerneldensityestimation"</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>kernel,</span>
<span id="cb2-25">                OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kd_qtde.tif"</span>)</span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Normalize (0 to 1) and take the average</span></span>
<span id="cb2-28">norm_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:rescaleraster"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>kd_area, BAND<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, MINIMUM<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, MAXIMUM<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-29">                  OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kd_area_norm.tif"</span>)</span>
<span id="cb2-30">norm_qtde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:rescaleraster"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>kd_qtde, BAND<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, MINIMUM<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, MAXIMUM<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-31">                  OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kd_qtde_norm.tif"</span>)</span>
<span id="cb2-32">indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdal:rastercalculator"</span>, INPUT_A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>norm_area, BAND_A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, INPUT_B<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>norm_qtde,</span>
<span id="cb2-33">               BAND_B<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"(A + B) / 2"</span>, RTYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kernel_media.tif"</span>)</span>
<span id="cb2-34"></span>
<span id="cb2-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The heat map only covers the points' extent plus the radius, and writes "no data" where the</span></span>
<span id="cb2-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># density is zero: extend it to the study area and replace "no data" with zero</span></span>
<span id="cb2-37">indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdal:warpreproject"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>indice, TARGET_CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:5880"</span>,</span>
<span id="cb2-38">               TARGET_RESOLUTION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, TARGET_EXTENT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-56.5,-54.5,-8.5,-5.5"</span>,</span>
<span id="cb2-39">               TARGET_EXTENT_CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:4674"</span>, OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kernel_recorte.tif"</span>)</span>
<span id="cb2-40">indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fillnodata"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>indice, BAND<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, FILL_VALUE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-41">               OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>SAIDA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"kernel_indice.tif"</span>)</span>
<span id="cb2-42"></span>
<span id="cb2-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Mean and maximum of the index in each protected area, only inside the study area</span></span>
<span id="cb2-44">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs"</span>,</span>
<span id="cb2-45">            EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao = 'Amazonia'"</span>)</span>
<span id="cb2-46">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyextent"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs, CLIP<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb2-47">            EXTENT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-56.5,-54.5,-8.5,-5.5 [EPSG:4674]"</span>)</span>
<span id="cb2-48">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fixgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs)</span>
<span id="cb2-49">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:reprojectlayer"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs, TARGET_CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:5880"</span>)</span>
<span id="cb2-50">por_uc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:zonalstatisticsfb"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ucs, INPUT_RASTER<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>indice, RASTER_BAND<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-51">               STATISTICS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>], COLUMN_PREFIX<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_"</span>)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2 = mean, 6 = maximum</span></span>
<span id="cb2-52"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> por_uc.getFeatures():</span>
<span id="cb2-53">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_mean"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_max"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb2-54">QgsProject.instance().addMapLayer(QgsRasterLayer(indice, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kernel index"</span>))</span></code></pre></div></div>
<p><strong>Heatmap</strong> is under Processing → Interpolation (in QGIS 3, the ID is <code>qgis:heatmapkerneldensityestimation</code>). From the menus, step 3 is <strong>Rescale raster</strong> twice and a <strong>Raster calculator</strong> with <code>("norm_area@1" + "norm_qtde@1") / 2</code>.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>PostGIS computes a kernel without rasters: a <strong>grid of points</strong> every 1 km and, for each grid point, the sum of the quartic weights of the deforestation within 10 km.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 1. A point inside each 2025 polygon (in meters) and the area in hectares</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> artigos.kernel_pontos;</span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.kernel_pontos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> ST_Transform(ST_PointOnSurface(g), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom,</span>
<span id="cb3-5">       ST_Area(g:<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha</span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> ST_CollectionExtract(ST_MakeValid(geom), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> g</span>
<span id="cb3-7">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prodes_amz</span>
<span id="cb3-8">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">year</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">upper</span>(main_class) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO'</span>) p</span>
<span id="cb3-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> ST_IsEmpty(g);</span>
<span id="cb3-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INDEX</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> artigos.kernel_pontos <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> gist (geom);</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 2. 1 km grid over the points' extent (plus a 10 km margin) and the two densities</span></span>
<span id="cb3-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> artigos.kernel_grade;</span>
<span id="cb3-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.kernel_grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> ST_Centroid(c.geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-17">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> ST_SquareGrid(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> ST_SetSRID(ST_Expand(ST_Extent(geom), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geometry</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb3-18">                              <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.kernel_pontos)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> c</span>
<span id="cb3-19">),</span>
<span id="cb3-20">quartico <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- weight: (1 - (d/r)²)², from 1 at the center to 0 at 10 km</span></span>
<span id="cb3-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> g.geom, p.area_ha,</span>
<span id="cb3-22">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power</span>(ST_Distance(g.geom, p.geom) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> w</span>
<span id="cb3-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> grade g</span>
<span id="cb3-24">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.kernel_pontos p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> ST_DWithin(g.geom, p.geom, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>)</span>
<span id="cb3-25">)</span>
<span id="cb3-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> geom, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> area_ha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> kd_area, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(w) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> kd_qtde</span>
<span id="cb3-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> quartico</span>
<span id="cb3-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> geom;</span>
<span id="cb3-29"></span>
<span id="cb3-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 3. Normalize and average (cells with no deforestation nearby stay at 0)</span></span>
<span id="cb3-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.kernel_grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COLUMN</span> indice <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>;</span>
<span id="cb3-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> lim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(kd_area) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ma, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(kd_qtde) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> mq <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.kernel_grade)</span>
<span id="cb3-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> artigos.kernel_grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ((kd_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ma) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (kd_qtde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> mq)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> lim;</span>
<span id="cb3-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INDEX</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> artigos.kernel_grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> gist (geom);</span>
<span id="cb3-35"></span>
<span id="cb3-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 4. Mean and maximum of the index in each protected area, only inside the study area</span></span>
<span id="cb3-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--    (cells with zero count)</span></span>
<span id="cb3-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> ucs <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (</span>
<span id="cb3-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> nome_uc,</span>
<span id="cb3-40">           ST_Transform(ST_Intersection(geom, ST_MakeEnvelope(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">56.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">54.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>)),</span>
<span id="cb3-41">                        <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.ucs</span>
<span id="cb3-43">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> regiao <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Amazonia'</span></span>
<span id="cb3-44">)</span>
<span id="cb3-45"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> u.nome_uc,</span>
<span id="cb3-46">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">avg</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coalesce</span>(k.indice, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> idx_media,</span>
<span id="cb3-47">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coalesce</span>(k.indice, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> idx_max</span>
<span id="cb3-48"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> ucs u</span>
<span id="cb3-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CROSS</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> LATERAL ST_SquareGrid(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, u.geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> c</span>
<span id="cb3-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">LEFT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.kernel_grade k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> k.geom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ST_Centroid(c.geom)</span>
<span id="cb3-51"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> ST_Intersects(ST_Centroid(c.geom), u.geom)</span>
<span id="cb3-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> u.nome_uc</span>
<span id="cb3-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> idx_media <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span></code></pre></div></div>
<p>The minimum of each density is zero (far from any deforestation), so normalization becomes just <code>value / maximum</code>.</p>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> shapely.geometry <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> box</span>
<span id="cb4-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rasterio.transform <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> from_origin</span>
<span id="cb4-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> rasterstats <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> zonal_stats</span>
<span id="cb4-6"></span>
<span id="cb4-7">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span></span>
<span id="cb4-8">RAIO, CEL <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1_000</span>                         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># meters</span></span>
<span id="cb4-9">recorte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.GeoSeries([box(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">56.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">54.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>)], crs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>)</span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. 2025 polygons, area in hectares and a point inside each one (in meters)</span></span>
<span id="cb4-12">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prodes_amz"</span>)</span>
<span id="cb4-13">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> prodes[(prodes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2025</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (prodes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"main_class"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>.upper() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO"</span>)]</span>
<span id="cb4-14">prodes[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> prodes.make_valid(method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"structure"</span>, keep_collapsed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-15">prodes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> prodes[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>prodes.is_empty]</span>
<span id="cb4-16">peso <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> prodes.to_crs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ESRI:102033"</span>).area.values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span></span>
<span id="cb4-17">pontos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> prodes.representative_point().to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb4-18">x, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pontos.x.values, pontos.y.values</span>
<span id="cb4-19"></span>
<span id="cb4-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. 1 km grid over the study area and quartic kernel, point by point</span></span>
<span id="cb4-21">xmin, ymin, xmax, ymax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> recorte.to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>).total_bounds</span>
<span id="cb4-22">x0, y1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> xmin, ymax</span>
<span id="cb4-23">ncol <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>((xmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> CEL) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-24">nlin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>((y1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ymin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> CEL) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-25">cx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (np.arange(ncol) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> CEL           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># column centers</span></span>
<span id="cb4-26">cy <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (np.arange(nlin) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> CEL           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># row centers</span></span>
<span id="cb4-27">kd_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((nlin, ncol))</span>
<span id="cb4-28">kd_qtde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((nlin, ncol))</span>
<span id="cb4-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> xi, yi, wi <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(x, y, peso):</span>
<span id="cb4-30">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">slice</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>((xi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> RAIO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> CEL)), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>((xi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> RAIO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> CEL) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-31">    l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">slice</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>((y1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> yi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> RAIO) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> CEL)), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>((y1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> yi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> RAIO) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> CEL) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-32">    d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (cx[c][<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, :] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> xi) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (cy[l][:, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> yi) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb4-33">    k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.where(d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> RAIO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> d2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> RAIO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-34">    kd_area[l, c] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> wi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k</span>
<span id="cb4-35">    kd_qtde[l, c] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> k</span>
<span id="cb4-36"></span>
<span id="cb4-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Normalize (the minimum is zero) and take the average</span></span>
<span id="cb4-38">indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (kd_area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> kd_area.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> kd_qtde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> kd_qtde.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb4-39"></span>
<span id="cb4-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Mean and maximum of the index in each protected area, only inside the study area</span></span>
<span id="cb4-41">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>).query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao == 'Amazonia'"</span>)</span>
<span id="cb4-42">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.clip(ucs, recorte).to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb4-43">stats <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zonal_stats(ucs, indice, affine<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>from_origin(x0, y1, CEL, CEL),</span>
<span id="cb4-44">                    stats<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>], nodata<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-45">ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_media"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(s[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> stats]</span>
<span id="cb4-46">ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_max"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(s[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> stats]</span>
<span id="cb4-47"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(ucs[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_media"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_max"</span>]].sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"idx_media"</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>))</span>
<span id="cb4-48">np.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/kernel_indice.npy"</span>, indice)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The three tested versions gave the same means by protected area (differences up to 0.001). The maximum varies a little more (up to 0.02), because each program aligns the cell grid differently:</p>
<table class="table-striped caption-top table">
<caption>PRODES 2025 kernel density index (0 = no deforestation within 10 km; 1 = the hottest point in the study area).</caption>
<colgroup>
<col style="width: 30%">
<col style="width: 40%">
<col style="width: 30%">
</colgroup>
<thead>
<tr class="header">
<th>Protected area (part inside the study area)</th>
<th style="text-align: right;">Mean index</th>
<th>Maximum (QGIS / PostGIS / Python)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Jamanxim National Forest</td>
<td style="text-align: right;">0.014</td>
<td>0.36 / 0.37 / 0.37</td>
</tr>
<tr class="even">
<td>Tapajós Environmental Protection Area</td>
<td style="text-align: right;">0.013–0.014</td>
<td>0.49 / 0.47 / 0.47</td>
</tr>
<tr class="odd">
<td>Altamira National Forest</td>
<td style="text-align: right;">0.008</td>
<td>0.19 / 0.18 / 0.19</td>
</tr>
<tr class="even">
<td>Iriri State Forest</td>
<td style="text-align: right;">0.005–0.006</td>
<td>0.06 / 0.06 / 0.06</td>
</tr>
<tr class="odd">
<td>Rio Novo National Park</td>
<td style="text-align: right;">0.002</td>
<td>0.03 / 0.03 / 0.03</td>
</tr>
<tr class="even">
<td>Jamanxim National Park</td>
<td style="text-align: right;">0.001</td>
<td>0.09 / 0.08 / 0.08</td>
</tr>
<tr class="odd">
<td>Itaituba I National Forest</td>
<td style="text-align: right;">0</td>
<td>0 / 0 / 0</td>
</tr>
</tbody>
</table>
<p>The means are low because the protected areas are huge and most of each one is far from any deforestation. The <strong>maximum</strong> says more: the Tapajós Environmental Protection Area and Jamanxim National Forest have points with almost half the intensity of the hottest point in the study area. That point (the X on the map) lies <strong>outside</strong> the protected areas, on the border between the Tapajós Environmental Protection Area and Jamanxim National Forest, and most of the heat is in a band east of Jamanxim National Forest, also outside the protected areas.</p>
</section>
<section id="choosing-the-radius" class="level2">
<h2 class="anchored" data-anchor-id="choosing-the-radius">Choosing the radius</h2>
<p>The radius changes everything. With 2 km, each clearing becomes an isolated spot; with 50 km, the whole map turns lukewarm. There is no “right” radius: choose it by the scale of the decision (the working range of a field team, for example) and <strong>write the value in the legend</strong>. The article <a href="../articles/12-prioritization-arcgis-qgis.html">Prioritizing alerts in ArcGIS Pro and QGIS</a> shows another way to look at concentration, by grouping nearby polygons.</p>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/03-prodes-protected-areas.html">Annual PRODES deforestation in protected areas</a></li>
<li><a href="https://hendesson.github.io/guia-espacial/guia/01-conceitos.html">Spatial data concepts</a> (in Portuguese): raster, resolution and projections</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/08-car-protected-areas.html" class="anterior"><span class="direcao">← Previous</span> Rural properties (CAR) inside protected areas: selecting and measuring the overlap</a> <a href="../articles/10-automated-maps.html" class="proximo"><span class="direcao">Next →</span> One map per protected area, automatically: from ArcMap to QGIS and Python</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/09-densidade-kernel.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Kernel Density of Deforestation: Where the Pressure Is
    Concentrated},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/09-kernel-density.html},
  doi = {10.5281/zenodo.23780262},
  langid = {en},
  abstract = {A heat map of PRODES 2025 deforestation that combines area
    and number of polygons into an index from 0 to 1, with radius and
    cell size in meters. It also shows how to compute a kernel density
    in PostGIS without rasters, and why normalizing makes the programs
    comparable.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Kernel Density of Deforestation: Where
the Pressure Is Concentrated.”</span> In <em>Geoprocessing Articles for
Environmental Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780262">https://doi.org/10.5281/zenodo.23780262</a>.
</div></div></section></div> ]]></description>
  <category>PRODES</category>
  <category>Raster</category>
  <guid>https://hendesson.github.io/en/articles/09-kernel-density.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/kernel.png" medium="image" type="image/png" height="180" width="144"/>
</item>
<item>
  <title>One map per protected area, automatically: from ArcMap to QGIS and Python</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/10-automated-maps.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Intermediate <span class="rotulo">Reading</span> 10 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>Every week, one map per protected area with the new alerts: this is the kind of task that <strong>should not be done by hand</strong>. With 7 protected areas that is already 7 maps; with 130, nobody keeps up.</p>
<p>The scripts I used did this in <strong>ArcMap</strong>, with <code>arcpy.mapping</code>: they opened an <code>.mxd</code>, changed the layer filter, zoomed to the protected area and exported a JPEG. That module <strong>does not exist in ArcGIS Pro</strong>. Here is the same idea in each program, from the old script to the license-free version.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/mapa_atlas.png" class="img-fluid figure-img" alt="Landscape A4 map titled Parque Nacional do Jamanxim. The protected area is shown in green with a thick black outline, the 3 km surrounding band in yellow, neighboring protected areas in light grey and DETER alerts colored by class. On the right, a legend, a 0 to 40 km scale bar, a north arrow and the data source."></p>
<figcaption>Map generated by the QGIS Atlas for Jamanxim National Park (labels in Portuguese). The same layout was exported for the 7 protected areas of the study area.</figcaption>
</figure>
</div>
<section id="what-every-automated-map-needs" class="level2">
<h2 class="anchored" data-anchor-id="what-every-automated-map-needs">What every automated map needs</h2>
<ol type="1">
<li>A <strong>template</strong> (layout) with map, title, legend, scale bar, north arrow and source.</li>
<li>A <strong>layer that drives the series</strong> (the protected areas): each feature becomes a page.</li>
<li><strong>Title and filters that change</strong> with the page (the protected area’s name, only its surroundings).</li>
<li><strong>Automatic zoom</strong> to the page’s feature, with a margin.</li>
<li><strong>Export</strong> with one file name per protected area (the CNUC code is better than the name, which has accents and spaces).</li>
</ol>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th></th>
<th>Template</th>
<th>Series</th>
<th>Changing text</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>ArcMap</td>
<td><code>.mxd</code></td>
<td>Python loop with <code>arcpy.mapping</code></td>
<td><code>ListLayoutElements("TEXT_ELEMENT")</code></td>
</tr>
<tr class="even">
<td>ArcGIS Pro</td>
<td>layout in the <code>.aprx</code></td>
<td><strong>Map Series</strong></td>
<td>dynamic text <code>&lt;dyn type="page" property="name"/&gt;</code></td>
</tr>
<tr class="odd">
<td>QGIS</td>
<td>print layout</td>
<td><strong>Atlas</strong></td>
<td>expression <code>[% "nome_uc" %]</code></td>
</tr>
<tr class="even">
<td>Python</td>
<td>matplotlib figure</td>
<td><code>for</code> loop</td>
<td>f-string</td>
</tr>
</tbody>
</table>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Names in Portuguese: <code>mapa</code> = map, <code>titulo</code> = title, <code>legenda</code> = legend, <code>escala</code> = scale bar, <code>norte</code> = north arrow, <code>fonte</code> = source, <code>quadro</code> = map frame, <code>colocar</code> = place, <code>regras</code> = rules.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcMap</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>The old script, simplified. Runs only in <strong>ArcMap 10.x</strong> (Python 2.7).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># -*- coding: utf-8 -*-</span></span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-3"></span>
<span id="cb1-4">mxd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.mapping.MapDocument(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apa_uc</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">mxd"</span>)</span>
<span id="cb1-5">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.mapping.ListDataFrames(mxd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Layers"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb1-6">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.mapping.ListLayers(mxd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UCs"</span>, df)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb1-7">titulo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.mapping.ListLayoutElements(mxd, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT_ELEMENT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"titulo"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> cnuc, nome <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(ucs, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>]):</span>
<span id="cb1-10">    ucs.definitionQuery <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc = '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> cnuc</span>
<span id="cb1-11">    df.extent <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ucs.getExtent()</span>
<span id="cb1-12">    df.scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> df.scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 10% margin</span></span>
<span id="cb1-13">    titulo.text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nome</span>
<span id="cb1-14">    arcpy.mapping.ExportToPNG(mxd, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apas</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apa_%s</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">png"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> cnuc, resolution<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>)</span>
<span id="cb1-15">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Map of </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> exported"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> nome</span>
<span id="cb1-16">ucs.definitionQuery <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span>
<span id="cb1-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">del</span> mxd</span></code></pre></div></div>
<p>To bring this layout into ArcGIS Pro, open a project and use <strong>Insert → Import Map</strong> with the <code>.mxd</code>.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p><strong>Without code:</strong> in the layout, <strong>Layout → Map Series → Spatial</strong>, index layer = protected areas, name field = <code>nome_uc</code>. In the title, insert <strong>Dynamic Text → Map Series → Page Name</strong>. Then <strong>Share → Export Layout</strong> and choose “All pages” and “One file per page”.</p>
<p><strong>With code</strong> (<code>arcpy.mp</code>), for full control of each step:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb2-2"></span>
<span id="cb2-3">projeto <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.mp.ArcGISProject(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apas</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aprx"</span>)</span>
<span id="cb2-4">layout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> projeto.listLayouts(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mapa por UC"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb2-5">quadro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> layout.listElements(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MAPFRAME_ELEMENT"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb2-6">titulo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> layout.listElements(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT_ELEMENT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"titulo"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb2-7">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quadro.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>.listLayers(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UCs"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> cnuc, nome <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(ucs, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>]):</span>
<span id="cb2-10">    ucs.definitionQuery <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"cnuc = '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cnuc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span></span>
<span id="cb2-11">    quadro.camera.setExtent(quadro.getLayerExtent(ucs, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>))</span>
<span id="cb2-12">    quadro.camera.scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>                     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 10% margin</span></span>
<span id="cb2-13">    titulo.text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nome</span>
<span id="cb2-14">    layout.exportToPNG(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apas</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apa_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cnuc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">png"</span>, resolution<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>)</span>
<span id="cb2-15">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Map of </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>nome<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> exported"</span>)</span>
<span id="cb2-16">ucs.definitionQuery <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span></code></pre></div></div>
<p>If the layout already has a map series set up, exporting takes one line:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">layout.mapSeries.exportToPDF(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apas</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apas</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">pdf"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ALL"</span>, multiple_files<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PDF_SINGLE_FILE"</span>)</span></code></pre></div></div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p><strong>Without code:</strong> in the print layout, <strong>Atlas → Generate an atlas</strong> tab, coverage layer = protected areas, file name = <code>'mapa_' || "cnuc"</code>. On the map item, check <strong>Controlled by atlas</strong> with a 10% margin. In the title, use <strong>Insert expression</strong> with <code>[% "nome_uc" %]</code>. Export with <strong>Atlas → Export atlas as images</strong>.</p>
<p><strong>With code</strong> (Python console), the whole layout built from scratch, to run on any computer:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> qgis.core <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> (QgsProject, QgsVectorLayer, QgsPrintLayout, QgsLayoutItemMap,</span>
<span id="cb4-3">                       QgsLayoutItemLabel, QgsLayoutItemLegend, QgsLayoutItemScaleBar,</span>
<span id="cb4-4">                       QgsLayoutItemPicture, QgsLayoutPoint, QgsLayoutSize, QgsUnitTypes,</span>
<span id="cb4-5">                       QgsLayoutExporter, QgsCategorizedSymbolRenderer, QgsRendererCategory,</span>
<span id="cb4-6">                       QgsFillSymbol, QgsSingleSymbolRenderer, QgsCoordinateReferenceSystem,</span>
<span id="cb4-7">                       QgsCoordinateTransform, QgsRuleBasedRenderer)</span>
<span id="cb4-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> qgis.PyQt.QtGui <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> QFont</span>
<span id="cb4-9"></span>
<span id="cb4-10">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb4-11">PASTA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/mapas/"</span></span>
<span id="cb4-12">os.makedirs(PASTA, exist_ok<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-13">projeto <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsProject.instance()</span>
<span id="cb4-14">MM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsUnitTypes.LayoutMillimeters</span>
<span id="cb4-15"></span>
<span id="cb4-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> poligono(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>estilo):</span>
<span id="cb4-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> QgsFillSymbol.createSimple(estilo)</span>
<span id="cb4-18"></span>
<span id="cb4-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Layers and styles: protected areas (they drive the atlas), surroundings and alerts since Aug 2026</span></span>
<span id="cb4-20">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Protected area"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb4-21">ucs.setSubsetString(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao = 'Amazonia'"</span>)</span>
<span id="cb4-22">extensao_ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ucs.extent()</span>
<span id="cb4-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> regras(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>lista):</span>
<span id="cb4-24">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Rule-based style: (label, expression, symbol). @atlas_feature is the page's protected area."""</span></span>
<span id="cb4-25">    raiz <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsRuleBasedRenderer.Rule(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)</span>
<span id="cb4-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> rotulo, expressao, simbolo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lista:</span>
<span id="cb4-27">        raiz.appendChild(QgsRuleBasedRenderer.Rule(simbolo, filterExp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>expressao, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>rotulo))</span>
<span id="cb4-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> QgsRuleBasedRenderer(raiz)</span>
<span id="cb4-29"></span>
<span id="cb4-30">ucs.setRenderer(regras(</span>
<span id="cb4-31">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mapped protected area"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"@atlas_featureid = $id"</span>,</span>
<span id="cb4-32">     poligono(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"229,236,220"</span>, outline_color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0,0,0"</span>, outline_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.8"</span>)),</span>
<span id="cb4-33">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other protected areas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"@atlas_featureid &lt;&gt; $id"</span>,</span>
<span id="cb4-34">     poligono(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"242,242,242"</span>, outline_color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"150,150,150"</span>, outline_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.3"</span>))))</span>
<span id="cb4-35">entorno <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=ucs_entorno"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surroundings"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb4-36">entorno.setRenderer(regras(</span>
<span id="cb4-37">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 km surroundings"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">cnuc</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> = attribute(@atlas_feature, 'cnuc')"</span>,</span>
<span id="cb4-38">     poligono(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"240,228,66,140"</span>, outline_style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no"</span>))))</span>
<span id="cb4-39">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=deter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DETER alerts"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb4-40">alertas.setSubsetString(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date &gt;= '2026-08-01'"</span>)</span>
<span id="cb4-41">classes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_CR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"213,94,0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deforestation (clear-cut)"</span>),</span>
<span id="cb4-42">           (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MINERACAO"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"204,121,167"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mining"</span>),</span>
<span id="cb4-43">           (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DEGRADACAO"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"230,159,0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Degradation"</span>),</span>
<span id="cb4-44">           (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CICATRIZ_DE_QUEIMADA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"86,180,233"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Burn scar"</span>)]</span>
<span id="cb4-45">categorias <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [QgsRendererCategory(valor, poligono(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cor, outline_color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cor,</span>
<span id="cb4-46">                                                  outline_width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.15"</span>), rotulo)</span>
<span id="cb4-47">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> valor, cor, rotulo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> classes]</span>
<span id="cb4-48">categorias.append(QgsRendererCategory(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, poligono(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"120,120,120"</span>, outline_style<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no"</span>),</span>
<span id="cb4-49">                                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Other classes"</span>))</span>
<span id="cb4-50">alertas.setRenderer(QgsCategorizedSymbolRenderer(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>, categorias))</span>
<span id="cb4-51">projeto.addMapLayers([ucs, entorno, alertas])</span>
<span id="cb4-52"></span>
<span id="cb4-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Landscape A4 layout (each item is added and then placed, in millimeters)</span></span>
<span id="cb4-54">layout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsPrintLayout(projeto)</span>
<span id="cb4-55">layout.initializeDefaults()</span>
<span id="cb4-56"></span>
<span id="cb4-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> colocar(item, x, y, largura<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, altura<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>):</span>
<span id="cb4-58">    layout.addLayoutItem(item)</span>
<span id="cb4-59">    item.attemptMove(QgsLayoutPoint(x, y, MM))</span>
<span id="cb4-60">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> largura:</span>
<span id="cb4-61">        item.attemptResize(QgsLayoutSize(largura, altura, MM))</span>
<span id="cb4-62">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> item</span>
<span id="cb4-63"></span>
<span id="cb4-64">mapa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutItemMap(layout)</span>
<span id="cb4-65">crs_mapa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsCoordinateReferenceSystem(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:5880"</span>)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># in meters: correct scale</span></span>
<span id="cb4-66">mapa.setCrs(crs_mapa)</span>
<span id="cb4-67">mapa.setLayers([alertas, entorno, ucs])</span>
<span id="cb4-68">mapa.setFrameEnabled(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-69">colocar(mapa, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>)</span>
<span id="cb4-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial extent: without it, the atlas stretches the map frame on the first page</span></span>
<span id="cb4-71">para_metros <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsCoordinateTransform(ucs.crs(), crs_mapa, projeto)</span>
<span id="cb4-72">mapa.zoomToExtent(para_metros.transformBoundingBox(extensao_ucs))</span>
<span id="cb4-73"></span>
<span id="cb4-74">titulo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutItemLabel(layout)</span>
<span id="cb4-75">titulo.setText(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'[% "nome_uc" %]'</span>)                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># changes on every page</span></span>
<span id="cb4-76">titulo.setFont(QFont(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DejaVu Sans"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, QFont.Weight.Bold))</span>
<span id="cb4-77">colocar(titulo, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">280</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb4-78"></span>
<span id="cb4-79">legenda <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutItemLegend(layout)</span>
<span id="cb4-80">legenda.setLinkedMap(mapa)</span>
<span id="cb4-81">legenda.setTitle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Legend"</span>)</span>
<span id="cb4-82">colocar(legenda, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">214</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>)</span>
<span id="cb4-83"></span>
<span id="cb4-84">norte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutItemPicture(layout)</span>
<span id="cb4-85">norte.setPicturePath(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":/images/north_arrows/layout_default_north_arrow.svg"</span>)</span>
<span id="cb4-86">colocar(norte, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">270</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">138</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb4-87">norte.setLinkedMap(mapa)</span>
<span id="cb4-88"></span>
<span id="cb4-89">escala <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutItemScaleBar(layout)</span>
<span id="cb4-90">escala.setLinkedMap(mapa)</span>
<span id="cb4-91">escala.setStyle(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Single Box"</span>)</span>
<span id="cb4-92">escala.setUnits(QgsUnitTypes.DistanceKilometers)</span>
<span id="cb4-93">escala.setUnitLabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"km"</span>)</span>
<span id="cb4-94">escala.setNumberOfSegments(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-95">escala.setNumberOfSegmentsLeft(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-96">escala.setUnitsPerSegment(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb4-97">colocar(escala, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">214</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">165</span>)</span>
<span id="cb4-98"></span>
<span id="cb4-99">fonte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutItemLabel(layout)</span>
<span id="cb4-100">fonte.setText(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DETER alerts since 2026-08-01.</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Sources: INPE/TerraBrasilis, MMA (CNUC).</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-101">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SIRGAS 2000 / Brazil Polyconic (EPSG:5880)."</span>)</span>
<span id="cb4-102">colocar(fonte, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">214</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">182</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb4-103"></span>
<span id="cb4-104"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Atlas: one page per protected area, map centered on it with a 10% margin</span></span>
<span id="cb4-105">atlas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> layout.atlas()</span>
<span id="cb4-106">atlas.setCoverageLayer(ucs)</span>
<span id="cb4-107">atlas.setEnabled(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-108">atlas.setFilenameExpression(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'mapa_' || </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">cnuc</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-109">mapa.setAtlasDriven(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-110">mapa.setAtlasScalingMode(QgsLayoutItemMap.Auto)</span>
<span id="cb4-111">mapa.setAtlasMargin(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>)</span>
<span id="cb4-112"></span>
<span id="cb4-113"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. One PNG image per protected area</span></span>
<span id="cb4-114">config <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutExporter.ImageExportSettings()</span>
<span id="cb4-115">config.dpi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span></span>
<span id="cb4-116">config.generateWorldFile <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb4-117">config.exportMetadata <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb4-118">resultado, erro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutExporter.exportToImage(atlas, PASTA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mapa.png"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"png"</span>, config)</span>
<span id="cb4-119"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"export:"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ok"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> resultado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> QgsLayoutExporter.Success <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> erro)</span>
<span id="cb4-120"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sorted</span>(os.listdir(PASTA)))</span></code></pre></div></div>
<p>The two tricks that make the atlas look hand-made are in the rule-based style:</p>
<ul>
<li><code>@atlas_featureid = $id</code> highlights <strong>the page’s protected area</strong> and leaves the neighbors in grey;</li>
<li><code>"cnuc" = attribute(@atlas_feature, 'cnuc')</code> shows <strong>only the surroundings</strong> of the page’s protected area.</li>
</ul>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<p>PostGIS <strong>does not draw maps</strong>, but it can deliver ready-made layers to QGIS or ArcGIS Pro. A view with each protected area’s recent alerts keeps the map project light and always up to date:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OR</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">REPLACE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VIEW</span> artigos.mapa_alertas_recentes <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> d.gid, d.classname, d.view_date, z.cnuc, z.nome_uc, z.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span>, z.geom</span>
<span id="cb5-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno z</span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.deter d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> (gid)</span>
<span id="cb5-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> d.view_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">current_date</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>;</span></code></pre></div></div>
<p>In QGIS, connect to the database (<strong>Browser → PostgreSQL → New Connection</strong>), drag the view onto the map and use the atlas from the QGIS tab. Every time the database is updated, the atlas comes out with the new data.</p>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<p>No GIS program at all: a loop that draws and saves one PNG per protected area.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb6-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib</span>
<span id="cb6-4">matplotlib.use(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agg"</span>)</span>
<span id="cb6-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb6-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> matplotlib.patches <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Patch</span>
<span id="cb6-7"></span>
<span id="cb6-8">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span></span>
<span id="cb6-9">os.makedirs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/mapas_py"</span>, exist_ok<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb6-10">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>).query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regiao == 'Amazonia'"</span>).to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb6-11">entorno <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs_entorno"</span>).to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb6-12">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>).to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb6-13">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> alertas[alertas[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>].astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-08-01"</span>]</span>
<span id="cb6-14"></span>
<span id="cb6-15">CORES <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_CR"</span>: (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D55E00"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deforestation (clear-cut)"</span>),</span>
<span id="cb6-16">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MINERACAO"</span>: (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#CC79A7"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mining"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DEGRADACAO"</span>: (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E69F00"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Degradation"</span>),</span>
<span id="cb6-17">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CICATRIZ_DE_QUEIMADA"</span>: (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#56B4E9"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Burn scar"</span>)}</span>
<span id="cb6-18">alertas[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cor"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> alertas[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> c: CORES.get(c, (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#787878"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>))[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])</span>
<span id="cb6-19"></span>
<span id="cb6-20"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> _, uc <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ucs.iterrows():</span>
<span id="cb6-21">    fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.3</span>))                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># landscape A4</span></span>
<span id="cb6-22">    xmin, ymin, xmax, ymax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> uc.geometry.bounds</span>
<span id="cb6-23">    m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(xmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> xmin, ymax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ymin)                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 10% margin</span></span>
<span id="cb6-24">    ax.set_xlim(xmin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m, xmax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m)</span>
<span id="cb6-25">    ax.set_ylim(ymin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m, ymax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m)</span>
<span id="cb6-26"></span>
<span id="cb6-27">    ucs.plot(ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f2f2f2"</span>, edgecolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#969696"</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>)</span>
<span id="cb6-28">    entorno[entorno[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>]].plot(ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F0E442"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span>)</span>
<span id="cb6-29">    ucs[ucs[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc"</span>]].plot(ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e5ecdc"</span>, edgecolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>)</span>
<span id="cb6-30">    alertas.plot(ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alertas[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cor"</span>], linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb6-31"></span>
<span id="cb6-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 20 km scale bar, north arrow, legend, title and source</span></span>
<span id="cb6-33">    x0, y0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax.get_xlim()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, ax.get_ylim()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span></span>
<span id="cb6-34">    ax.plot([x0, x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20_000</span>], [y0, y0], color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, linewidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb6-35">    ax.text(x0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span>, y0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20 km"</span>, ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb6-36">    ax.annotate(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>, xy<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>), xytext<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.86</span>), xycoords<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"axes fraction"</span>,</span>
<span id="cb6-37">                ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span id="cb6-38">                arrowprops<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(facecolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, headwidth<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb6-39">    itens <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [Patch(facecolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e5ecdc"</span>, edgecolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mapped protected area"</span>),</span>
<span id="cb6-40">             Patch(facecolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#F0E442"</span>, alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 km surroundings"</span>)]</span>
<span id="cb6-41">    itens <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> [Patch(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>cor, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>rotulo) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> cor, rotulo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> CORES.values()]</span>
<span id="cb6-42">    ax.legend(handles<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>itens, loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper left"</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, framealpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)</span>
<span id="cb6-43">    ax.set_title(uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, fontweight<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, loc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>)</span>
<span id="cb6-44">    ax.set_axis_off()</span>
<span id="cb6-45">    fig.text(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DETER alerts since 2026-08-01. Sources: INPE/TerraBrasilis, "</span></span>
<span id="cb6-46">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MMA (CNUC). SIRGAS 2000 / Brazil Polyconic (EPSG:5880)."</span>, fontsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb6-47">    fig.savefig(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"dados/mapas_py/mapa_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cnuc'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.png"</span>, dpi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, bbox_inches<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tight"</span>)</span>
<span id="cb6-48">    plt.close(fig)</span>
<span id="cb6-49">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Map of"</span>, uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"exported"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The QGIS Atlas and the Python loop produced the <strong>7 maps</strong> of the study area, one per protected area, in a few seconds. The ArcMap and ArcGIS Pro versions follow the same logic, but were not tested here (they need a license).</p>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Map in degrees, wrong scale
</div>
</div>
<div class="callout-body-container callout-body">
<p>A map in geographic SIRGAS 2000 (degrees) has a distorted scale bar, and the protected areas look “squashed” vertically. Put the <strong>map frame</strong> in a projection in meters (here, EPSG:5880), even if the data stay in degrees.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Atlas that stretches the map frame (QGIS, from code)
</div>
</div>
<div class="callout-body-container callout-body">
<p>When building the layout in Python, if the map starts without an extent, the first atlas page <strong>changes the size of the frame</strong> and it runs off the page. Give the map an initial extent (<code>zoomToExtent</code>) before turning on the atlas, as in the code above.</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>File names
</div>
</div>
<div class="callout-body-container callout-body">
<p>Use the protected area code (<code>mapa_0000.00.0267.png</code>), not the name. Accents, spaces and slashes in file names break scripts and links, especially between Windows and Linux.</p>
</div>
</div>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/11-pdf-report.html">Automated PDF report</a>, which combines map, table and text</li>
<li><a href="https://hendesson.github.io/tutoriais/mapas-preto-e-branco.html">Black-and-white academic maps in R</a> (in Portuguese), on the elements of a good map</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/09-kernel-density.html" class="anterior"><span class="direcao">← Previous</span> Kernel density of deforestation: where the pressure is concentrated</a> <a href="../articles/11-pdf-report.html" class="proximo"><span class="direcao">Next →</span> Automated PDF report: the bulletin that writes itself</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/10-mapas-automaticos.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {One Map Per Protected Area, Automatically: From {ArcMap} to
    {QGIS} and {Python}},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/10-automated-maps.html},
  doi = {10.5281/zenodo.23780264},
  langid = {en},
  abstract = {One map per protected area, generated without clicking:
    ArcMap’s arcpy.mapping, ArcGIS Pro’s arcpy.mp and map series, the
    QGIS Atlas built from code, and a matplotlib loop.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“One Map Per Protected Area,
Automatically: From ArcMap to QGIS and Python.”</span> In
<em>Geoprocessing Articles for Environmental Monitoring</em>. Preprint,
Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780264">https://doi.org/10.5281/zenodo.23780264</a>.
</div></div></section></div> ]]></description>
  <category>Maps</category>
  <category>ArcMap</category>
  <guid>https://hendesson.github.io/en/articles/10-automated-maps.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/mapa_atlas.png" medium="image" type="image/png" height="102" width="144"/>
</item>
<item>
  <title>Automated PDF report: the bulletin that writes itself</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/11-pdf-report.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Intermediate <span class="rotulo">Reading</span> 10 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>The longest scripts in my folder did no spatial analysis at all: they built an <strong>information note</strong> in PDF, with logos, a legend, a table per protected area and the text of each occurrence. That was 1,500 lines of <code>fpdf</code>, and any layout change was a chore.</p>
<p>Here is the same kind of product, a <strong>deforestation bulletin per protected area</strong> with public data, in four ways. The easiest to maintain is usually <strong>Quarto</strong>: the layout lives in a text file and the script only changes the protected area.</p>
<div class="grid">
<div class="g-col-12 g-col-md-6">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/boletim.png" class="img-fluid figure-img" alt="A4 page with a blue band at the top titled Boletim de desmatamento em Unidades de Conservação, a paragraph with the period, a table with five protected areas and a bar chart with the area inside and around them."></p>
<figcaption>First page of the bulletin generated with fpdf2 (Portuguese version).</figcaption>
</figure>
</div>
</div>
<div class="g-col-12 g-col-md-6">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/boletim_quarto.png" class="img-fluid figure-img" alt="A4 page with the name of the Tapajós Environmental Protection Area, a sentence with the area cleared inside and around it, the map of the protected area and a table of alerts by zone and class."></p>
<figcaption>Bulletin page generated by Quarto for the Tapajós Environmental Protection Area (Portuguese version).</figcaption>
</figure>
</div>
</div>
</div>
<section id="what-goes-into-the-bulletin" class="level2">
<h2 class="anchored" data-anchor-id="what-goes-into-the-bulletin">What goes into the bulletin</h2>
<ol type="1">
<li><strong>Period</strong> and source, computed from the data itself (never typed in).</li>
<li><strong>Summary table</strong>: area of alerts inside and around each protected area.</li>
<li>A <strong>chart</strong> of the table.</li>
<li><strong>One page per protected area</strong>, with the map from the <a href="../articles/10-automated-maps.html">previous article</a>.</li>
</ol>
<p>The data come from the <code>deter_interior_entorno</code> layer (article <a href="../articles/02-inside-and-around.html">Inside and around</a>): deforestation alerts (clear-cut, with vegetation and mining) from the last 90 days.</p>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Names in Portuguese: <code>boletim</code> = bulletin, <code>relatorio</code> = report, <code>tabela</code> = table, <code>grafico</code> = chart, <code>inicio</code>/<code>fim</code> = start/end, <code>nome</code> = name.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false" href="">Quarto</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>ArcGIS Pro has <strong>Reports</strong> (Insert → Report), which build the table per protected area with header and footer, and <strong>Map Series</strong> in the layout. The script combines both into a PDF:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"></span>
<span id="cb1-3">projeto <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.mp.ArcGISProject(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\b</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">oletim</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aprx"</span>)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Table by protected area (report created in Insert → Report, grouped by nome_uc)</span></span>
<span id="cb1-6">relatorio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> projeto.listReports(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alertas por UC"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb1-7">relatorio.exportToPDF(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aida</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\t</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">abela</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">pdf"</span>)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One map per protected area (layout with a map series set up)</span></span>
<span id="cb1-10">layout <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> projeto.listLayouts(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mapa por UC"</span>)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb1-11">layout.mapSeries.exportToPDF(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aida</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apas</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">pdf"</span>)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Join everything into a single file</span></span>
<span id="cb1-14">boletim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.mp.PDFDocumentCreate(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aida</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\b</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">oletim</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">pdf"</span>)</span>
<span id="cb1-15">boletim.appendPages(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aida</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\t</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">abela</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">pdf"</span>)</span>
<span id="cb1-16">boletim.appendPages(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">aida</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">apas</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">pdf"</span>)</span>
<span id="cb1-17">boletim.saveAndClose()</span></code></pre></div></div>
<p>The <code>fpdf2</code> code from the Python tab also runs in ArcGIS Pro: in the <strong>Package Manager</strong>, clone the default environment, activate the clone and install <code>fpdf2</code>. That is how the original scripts worked.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>The QGIS Atlas exports <strong>one PDF with one page per protected area</strong>. Just add to the layout from the <a href="../articles/10-automated-maps.html">previous article</a> an <strong>attribute table</strong> filtered by the page’s protected area. Run this snippet <strong>after</strong> the code from that article:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Continues the code from the previous article (layout, atlas and layers already created)</span></span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> qgis.core <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> QgsLayoutItemAttributeTable, QgsLayoutFrame, QgsLayoutItemPage</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shrink the map to make room for the table below, in portrait</span></span>
<span id="cb2-5">layout.pageCollection().page(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>).setPageSize(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A4"</span>, QgsLayoutItemPage.Portrait)</span>
<span id="cb2-6">mapa.attemptResize(QgsLayoutSize(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">194</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, MM))</span>
<span id="cb2-7">mapa.zoomToExtent(para_metros.transformBoundingBox(extensao_ucs))</span>
<span id="cb2-8">legenda.setColumnCount(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> item, (x, y) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> [(legenda, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">176</span>)), (norte, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">188</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)), (escala, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">178</span>)),</span>
<span id="cb2-10">                     (fonte, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">145</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">192</span>))]:</span>
<span id="cb2-11">    item.attemptMove(QgsLayoutPoint(x, y, MM))</span>
<span id="cb2-12">norte.attemptResize(QgsLayoutSize(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, MM))</span>
<span id="cb2-13">fonte.attemptResize(QgsLayoutSize(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">57</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, MM))</span>
<span id="cb2-14"></span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Table with the alerts of the page's protected area (filtered by the atlas feature)</span></span>
<span id="cb2-16">tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutItemAttributeTable.create(layout)</span>
<span id="cb2-17">layout.addMultiFrame(tabela)                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the layout now owns the table</span></span>
<span id="cb2-18">tabela.setVectorLayer(alertas)</span>
<span id="cb2-19">tabela.setFilterFeatures(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb2-20">tabela.setFeatureFilter(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'intersects($geometry, @atlas_geometry)'</span>)</span>
<span id="cb2-21">tabela.setMaximumNumberOfFeatures(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-22">tabela.setDisplayedFields([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipality"</span>])</span>
<span id="cb2-23">quadro_tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutFrame(layout, tabela)</span>
<span id="cb2-24">tabela.addFrame(quadro_tabela)</span>
<span id="cb2-25">quadro_tabela.attemptMove(QgsLayoutPoint(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">230</span>, MM))</span>
<span id="cb2-26">quadro_tabela.attemptResize(QgsLayoutSize(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">194</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>, MM))</span>
<span id="cb2-27"></span>
<span id="cb2-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A single PDF, with one page per protected area</span></span>
<span id="cb2-29">config_pdf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutExporter.PdfExportSettings()</span>
<span id="cb2-30">resultado, erro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsLayoutExporter.exportToPdf(atlas, PASTA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"boletim_qgis.pdf"</span>, config_pdf)</span>
<span id="cb2-31"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PDF:"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ok"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> resultado <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> QgsLayoutExporter.Success <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> erro)</span></code></pre></div></div>
<p>The filter <code>intersects($geometry, @atlas_geometry)</code> makes the table show only the alerts of the page’s protected area. Without code, the same filter is in <strong>Item Properties → Filter with</strong>.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>The database delivers the table ready for any report generator (Quarto, Power BI, ArcGIS Pro):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OR</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">REPLACE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VIEW</span> artigos.boletim_90_dias <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> periodo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(view_date) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> fim <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno)</span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> nome_uc,</span>
<span id="cb3-4">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'interior'</span>)                 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> alertas_interior,</span>
<span id="cb3-5">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(area_ha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'interior'</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)   <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> interior_ha,</span>
<span id="cb3-6">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(area_ha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FILTER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">local</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'entorno'</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> entorno_ha,</span>
<span id="cb3-7">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(p.fim) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> inicio, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(p.fim) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> fim</span>
<span id="cb3-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_interior_entorno, periodo p</span>
<span id="cb3-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> classname <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IN</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO_CR'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO_VEG'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MINERACAO'</span>)</span>
<span id="cb3-10">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> view_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> p.fim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span></span>
<span id="cb3-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> nome_uc</span>
<span id="cb3-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> interior_ha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NULLS</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">LAST</span>;</span>
<span id="cb3-13"></span>
<span id="cb3-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.boletim_90_dias;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<p>With <strong>fpdf2</strong> (<code>pip install fpdf2</code>), the same package as the original scripts:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> date</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb4-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib</span>
<span id="cb4-5">matplotlib.use(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Agg"</span>)</span>
<span id="cb4-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb4-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> fpdf <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> FPDF</span>
<span id="cb4-8"></span>
<span id="cb4-9">CLASSES <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_CR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_VEG"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MINERACAO"</span>]</span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> nome(texto):</span>
<span id="cb4-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""FLORESTA NACIONAL DO JAMANXIM -&gt; Floresta Nacional do Jamanxim"""</span></span>
<span id="cb4-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>.join(p <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"de"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"do"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"da"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dos"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"das"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e"</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> p.capitalize()</span>
<span id="cb4-14">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> texto.lower().split())</span>
<span id="cb4-15"></span>
<span id="cb4-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Data: deforestation alerts from the last 90 days, by protected area and zone</span></span>
<span id="cb4-17">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/deter_interior_entorno.gpkg"</span>)</span>
<span id="cb4-18">d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>])</span>
<span id="cb4-19">fim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb4-20">inicio <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pd.Timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)</span>
<span id="cb4-21">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>].isin(CLASSES) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> inicio)]</span>
<span id="cb4-22">tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (d.pivot_table(index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>, values<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>,</span>
<span id="cb4-23">                        aggfunc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>, fill_value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-24">          .reindex(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interior"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"entorno"</span>], fill_value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-25">          .sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interior"</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>))</span>
<span id="cb4-26">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interior"</span>].groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>).size()</span>
<span id="cb4-27"></span>
<span id="cb4-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Chart for the report</span></span>
<span id="cb4-29">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.2</span>))</span>
<span id="cb4-30">tabela[::<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].rename(index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nome).plot.barh(ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#009E73"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E69F00"</span>], width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>)</span>
<span id="cb4-31">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Area of alerts (ha)"</span>)</span>
<span id="cb4-32">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb4-33">ax.legend([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inside"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 km surroundings"</span>], frameon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-34">ax.spines[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>]].set_visible(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-35">fig.tight_layout()</span>
<span id="cb4-36">fig.savefig(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/boletim_grafico.png"</span>, dpi<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb4-37"></span>
<span id="cb4-38"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. The PDF: header and footer on every page</span></span>
<span id="cb4-39"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Boletim(FPDF):</span>
<span id="cb4-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> header(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb4-41">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_fill_color(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">114</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">178</span>)</span>
<span id="cb4-42">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_text_color(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span>)</span>
<span id="cb4-43">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_font(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Helvetica"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>)</span>
<span id="cb4-44">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cell(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"  Deforestation bulletin for protected areas"</span>,</span>
<span id="cb4-45">                  fill<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, new_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMARGIN"</span>, new_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NEXT"</span>)</span>
<span id="cb4-46">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_text_color(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb4-47">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.ln(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb4-48"></span>
<span id="cb4-49">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> footer(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb4-50">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_y(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb4-51">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.set_font(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Helvetica"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb4-52">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cell(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Sources: INPE (DETER), MMA (CNUC). Generated on "</span></span>
<span id="cb4-53">                        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>date<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>today()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>Y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">."</span>, align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L"</span>)</span>
<span id="cb4-54">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.cell(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Page </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>page_no()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>)</span>
<span id="cb4-55"></span>
<span id="cb4-56">pdf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Boletim(orientation<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P"</span>, unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A4"</span>)</span>
<span id="cb4-57">pdf.set_auto_page_break(auto<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, margin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span>
<span id="cb4-58">pdf.add_page()</span>
<span id="cb4-59"></span>
<span id="cb4-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Summary, table and chart</span></span>
<span id="cb4-61">pdf.set_font(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Helvetica"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb4-62">pdf.multi_cell(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"DETER deforestation alerts (clear-cut, with vegetation and mining) "</span></span>
<span id="cb4-63">                     <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"between </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>inicio<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>Y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> and </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>fim<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>Y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, inside the protected areas "</span></span>
<span id="cb4-64">                     <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"and in the 3 km band around them."</span>)</span>
<span id="cb4-65">pdf.ln(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb4-66">pdf.set_font(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Helvetica"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb4-67"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> pdf.table(col_widths<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>), text_align<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LEFT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RIGHT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RIGHT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RIGHT"</span>)) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> t:</span>
<span id="cb4-68">    t.row([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Protected area"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alerts"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inside (ha)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Surroundings (ha)"</span>])</span>
<span id="cb4-69">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> uc, linha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> tabela.iterrows():</span>
<span id="cb4-70">        t.row([nome(uc), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(alertas.get(uc, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)),</span>
<span id="cb4-71">               <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>linha[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'interior'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>linha[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'entorno'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>])</span>
<span id="cb4-72">pdf.ln(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb4-73">pdf.image(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/boletim_grafico.png"</span>, w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>)</span>
<span id="cb4-74"></span>
<span id="cb4-75"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. One page per protected area, with the map from the previous article</span></span>
<span id="cb4-76">ucs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span>, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>).set_index(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nome_uc"</span>)</span>
<span id="cb4-77"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> uc <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> tabela.index:</span>
<span id="cb4-78">    pdf.add_page()</span>
<span id="cb4-79">    pdf.set_font(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Helvetica"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb4-80">    pdf.cell(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, nome(uc), new_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMARGIN"</span>, new_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NEXT"</span>)</span>
<span id="cb4-81">    pdf.set_font(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Helvetica"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb4-82">    pdf.cell(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ucs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>loc[uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'categoria'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> · </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ucs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>loc[uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'grupo'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> · "</span></span>
<span id="cb4-83">                   <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"CNUC </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ucs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>loc[uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cnuc'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, new_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMARGIN"</span>, new_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NEXT"</span>)</span>
<span id="cb4-84">    pdf.cell(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Inside: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tabela<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>loc[uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'interior'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> ha · "</span></span>
<span id="cb4-85">                   <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Surroundings: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tabela<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>loc[uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'entorno'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> ha"</span>, new_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LMARGIN"</span>, new_y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NEXT"</span>)</span>
<span id="cb4-86">    pdf.ln(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-87">    pdf.image(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"dados/mapas_py/mapa_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ucs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>loc[uc, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cnuc'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.png"</span>, w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">185</span>)</span>
<span id="cb4-88"></span>
<span id="cb4-89">pdf.output(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/boletim.pdf"</span>)</span>
<span id="cb4-90"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"boletim.pdf with"</span>, pdf.page_no(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pages"</span>)</span>
<span id="cb4-91"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(tabela.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<p>A <code>boletim.qmd</code> file is the <strong>template</strong>; the <code>cnuc</code> parameter picks the protected area. The PDF is produced by <strong>Typst</strong>, which ships with Quarto (no LaTeX needed).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb5-2"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">title:</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> "Deforestation bulletin"</span></span>
<span id="cb5-3"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">format:</span></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  typst:</span></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    papersize: a4</span></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    margin: {x: 2cm, y: 2cm}</span></span>
<span id="cb5-7"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">lang:</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> en</span></span>
<span id="cb5-8"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">execute:</span></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  echo: false</span></span>
<span id="cb5-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  warning: false</span></span>
<span id="cb5-11"><span class="an" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">jupyter:</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> python3</span></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb5-13"></span>
<span id="cb5-14"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{python}</span></span>
<span id="cb5-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| tags: [parameters]</span></span>
<span id="cb5-16">cnuc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0000.00.0266"</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># replaced when rendering: -P cnuc:...</span></span>
<span id="cb5-17"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb5-18"></span>
<span id="cb5-19"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{python}</span></span>
<span id="cb5-20"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb5-21"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb5-22"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> IPython.display <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Markdown</span>
<span id="cb5-23"></span>
<span id="cb5-24"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> nome(texto):</span>
<span id="cb5-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>.join(p <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"de"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"do"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"da"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dos"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"das"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"e"</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> p.capitalize()</span>
<span id="cb5-26">                    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> texto.lower().split())</span>
<span id="cb5-27"></span>
<span id="cb5-28">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../dados/artigos.gpkg"</span></span>
<span id="cb5-29">uc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(G, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ucs"</span>).query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc == @cnuc"</span>).iloc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb5-30">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../dados/deter_interior_entorno.gpkg"</span>).query(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cnuc == @cnuc"</span>)</span>
<span id="cb5-31">d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>])</span>
<span id="cb5-32">fim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb5-33">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>].isin([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_CR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_VEG"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MINERACAO"</span>])</span>
<span id="cb5-34">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> (d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> fim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pd.Timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>))]</span>
<span id="cb5-35">area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>().reindex([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interior"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"entorno"</span>], fill_value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb5-36"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb5-37"></span>
<span id="cb5-38"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{python}</span></span>
<span id="cb5-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| output: asis</span></span>
<span id="cb5-40"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"## </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>nome(uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'nome_uc'</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb5-41"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'categoria'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> · </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>uc[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'grupo'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> · CNUC </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cnuc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb5-42"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"In the 90 days up to **</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>fim<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>Y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>m<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-%</span>d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">**, DETER recorded **</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>area[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'interior'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> ha** "</span></span>
<span id="cb5-43">      <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"of deforestation inside the protected area and **</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>area[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'entorno'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> ha** in the "</span></span>
<span id="cb5-44">      <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"3 km surroundings.</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb5-45"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb5-46"></span>
<span id="cb5-47"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{python}</span></span>
<span id="cb5-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#| fig-cap: "Map of the protected area with alerts since August 2026."</span></span>
<span id="cb5-49"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> IPython.display <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Image</span>
<span id="cb5-50">Image(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"../dados/mapas_py/mapa_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cnuc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.png"</span>, width<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">600</span>)</span>
<span id="cb5-51"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span>
<span id="cb5-52"></span>
<span id="cb5-53"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```{python}</span></span>
<span id="cb5-54">tabela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (d.groupby([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>])[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>].agg([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-55">          .reset_index()</span>
<span id="cb5-56">          .rename(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>{<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"local"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zone"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class"</span>,</span>
<span id="cb5-57">                           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alerts"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Area (ha)"</span>}))</span>
<span id="cb5-58">Markdown(tabela.to_markdown(index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>))</span>
<span id="cb5-59"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
<p>To produce one PDF per protected area, call Quarto in a loop (on Windows, use a PowerShell <code>for</code>):</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> cnuc <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> 0000.00.0266 0000.00.0267 0000.00.0268<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">do</span></span>
<span id="cb6-2">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">quarto</span> render boletim.qmd <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-P</span> cnuc:<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$cnuc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--output</span> boletim_<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$cnuc</span>.pdf</span>
<span id="cb6-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">done</span></span></code></pre></div></div>
<p>Quarto needs <code>jupyter</code>, <code>papermill</code> and <code>tabulate</code> installed in the same Python as the data (<code>pip install jupyter papermill tabulate</code>).</p>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The Python (fpdf2), Quarto and QGIS versions produced PDFs with the same numbers. In the 90 days up to 2026-10-16:</p>
<table class="table-striped caption-top table">
<caption>DETER deforestation alerts (clear-cut, with vegetation and mining) in the 90 days up to 2026-10-16.</caption>
<thead>
<tr class="header">
<th>Protected area</th>
<th style="text-align: right;">Alerts inside</th>
<th style="text-align: right;">Inside (ha)</th>
<th style="text-align: right;">Surroundings (ha)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Jamanxim National Forest</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">628.5</td>
<td style="text-align: right;">9.1</td>
</tr>
<tr class="even">
<td>Altamira National Forest</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">436.1</td>
<td style="text-align: right;">24.2</td>
</tr>
<tr class="odd">
<td>Tapajós Environmental Protection Area</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">433.0</td>
<td style="text-align: right;">274.0</td>
</tr>
<tr class="even">
<td>Jamanxim National Park</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">73.5</td>
<td style="text-align: right;">100.9</td>
</tr>
<tr class="odd">
<td>Rio Novo National Park</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">48.2</td>
<td style="text-align: right;">0</td>
</tr>
</tbody>
</table>
<p>In Jamanxim National Park, a strict-protection area, the surroundings had <strong>more</strong> deforestation than the inside during the period. That is the kind of finding a weekly bulletin shows early.</p>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Dates typed into the text
</div>
</div>
<div class="callout-body-container callout-body">
<p>“Bulletin for September 1 to 30” typed by hand is wrong the first time the script runs in another month. Compute the period from the data (<code>max(view_date)</code>), as in the code above.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Special characters in the PDF
</div>
</div>
<div class="callout-body-container callout-body">
<p>fpdf2’s standard fonts (Helvetica, Times) only have Latin-1 characters: accented letters work, but em dashes (—), curly quotes and emoji do not. For any character, load a TTF font with <code>pdf.add_font("DejaVu", "", "DejaVuSans.ttf")</code>.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Attribute table in a QGIS layout, from code
</div>
</div>
<div class="callout-body-container callout-body">
<p>Create the table with <code>QgsLayoutItemAttributeTable.create(layout)</code> and register it with <code>layout.addMultiFrame(tabela)</code> <strong>before</strong> configuring it. Without that, Python deletes the table from memory and QGIS crashes silently when exporting.</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Logos and visual identity
</div>
</div>
<div class="callout-body-container callout-body">
<p>Official bulletins carry the agency’s logo and header. In a personal or study bulletin, <strong>do not use institutional logos</strong>: make it clear that it is your own product, made with public data.</p>
</div>
</div>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/10-automated-maps.html">One map per protected area, automatically</a></li>
<li><a href="https://hendesson.github.io/guia-espacial/curso/07-power-bi.html">Course module 7: Power BI</a> (in Portuguese), for a dashboard instead of a PDF</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/10-automated-maps.html" class="anterior"><span class="direcao">← Previous</span> One map per protected area, automatically: from ArcMap to QGIS and Python</a> <a href="../articles/12-prioritization-arcgis-qgis.html" class="proximo"><span class="direcao">Next →</span> Prioritizing alerts in ArcGIS Pro, QGIS and Python</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/11-relatorio-pdf.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Automated {PDF} Report: The Bulletin That Writes Itself},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/11-pdf-report.html},
  doi = {10.5281/zenodo.23780266},
  langid = {en},
  abstract = {A deforestation bulletin in PDF, with a summary, a table,
    a chart and one page with a map for each protected area, generated
    by a script. Four paths: fpdf2, Quarto with Typst, the QGIS Atlas
    and ArcGIS Pro reports.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Automated PDF Report: The Bulletin That
Writes Itself.”</span> In <em>Geoprocessing Articles for Environmental
Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780266">https://doi.org/10.5281/zenodo.23780266</a>.
</div></div></section></div> ]]></description>
  <category>Maps</category>
  <category>Reports</category>
  <category>Quarto</category>
  <guid>https://hendesson.github.io/en/articles/11-pdf-report.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/boletim.png" medium="image" type="image/png" height="204" width="144"/>
</item>
<item>
  <title>Prioritizing alerts in ArcGIS Pro, QGIS and Python</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/12-prioritization-arcgis-qgis.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Advanced <span class="rotulo">Reading</span> 14 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>With hundreds of alerts a month, enforcement teams need to decide <strong>where to start</strong>. The script I used did this in four steps: put alerts that are close to each other into a <strong>group</strong>, measure each group, turn the measurements into <strong>indices from 0 to 1</strong> and add them up with <strong>weights</strong>.</p>
<p><a href="https://hendesson.github.io/guia-espacial/curso/05-priorizacao.html">Course module 5</a> (in Portuguese) does a version of this in SQL, step by step. Here is the original script’s logic, with the same parameters, in each program.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/priorizacao_grupos.png" class="img-fluid figure-img" alt="Map of the Novo Progresso region with protected areas in light green and groups of alerts colored: very low in light yellow, low in yellow, medium in orange, high in red and very high in dark red. The 11 very-high-priority groups are scattered across the study area, several of them on the edges of protected areas."></p>
<figcaption>Groups of alerts from the last year, colored by priority.</figcaption>
</figure>
</div>
<section id="the-logic" class="level2">
<h2 class="anchored" data-anchor-id="the-logic">The logic</h2>
<div class="cell" data-eval="true" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">flowchart LR
    A["Alerts from&lt;br/&gt;the last year"] --&gt; B["Group neighbors&lt;br/&gt;60 m / 800 m"]
    B --&gt; C["Group measures:&lt;br/&gt;area, count,&lt;br/&gt;recency, speed"]
    C --&gt; D["Indices 0 to 1&lt;br/&gt;(with limits)"]
    D --&gt; E["Weighted score&lt;br/&gt;0.2 / 0.2 / 0.2 / 0.4"]
    E --&gt; F["Class:&lt;br/&gt;Very Low to&lt;br/&gt;Very High"]
</pre>
</div>
<p></p><figcaption> From alert to group to score</figcaption> </figure><p></p>
</div>
</div>
</div>
<table class="caption-top table">
<colgroup>
<col style="width: 18%">
<col style="width: 18%">
<col style="width: 18%">
<col style="width: 18%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Group measure</th>
<th>How it is computed</th>
<th>Limits: clear-cut</th>
<th>Limits: mining</th>
<th style="text-align: right;">Weight</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Area</td>
<td>sum of the alerts’ area (ha)</td>
<td>3 to 30 ha</td>
<td>0.1 to 5 ha</td>
<td style="text-align: right;">0.2</td>
</tr>
<tr class="even">
<td>Count</td>
<td>number of alerts in the group</td>
<td>1 to 4</td>
<td>1 to 10</td>
<td style="text-align: right;">0.2</td>
</tr>
<tr class="odd">
<td>Recency</td>
<td>days between the group’s newest alert and the reference date</td>
<td>−365 to −14 days</td>
<td>−365 to −14 days</td>
<td style="text-align: right;">0.2</td>
</tr>
<tr class="even">
<td>Speed</td>
<td>area ÷ days since the group’s first alert (ha/day)</td>
<td>0.1 to 0.5</td>
<td>0.00057 to 0.1</td>
<td style="text-align: right;">0.4</td>
</tr>
</tbody>
</table>
<ul>
<li><strong>Grouping:</strong> alerts within <strong>60 m</strong> of each other (clear-cut) or <strong>800 m</strong> (mining, which spreads along rivers) become a single group.</li>
<li><strong>Index with limits:</strong> below the minimum it is 0, above the maximum it is 1, and in between it grows linearly. A group of 30 ha or more already has an area index of 1.</li>
<li><strong>Classes:</strong> ≥ 0.95 Very High; ≥ 0.8 High; ≥ 0.6 Medium; ≥ 0.4 Low; below that, Very Low.</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Reference date
</div>
</div>
<div class="callout-body-container callout-body">
<p>The original script measured recency up to <strong>today</strong>. Here, the reference is the <strong>date of the newest alert</strong> (2026-10-16), so the result does not change just because the script ran on another day. For daily use, switch to today’s date.</p>
</div>
</div>
</section>
<section id="the-code" class="level2">
<h2 class="anchored" data-anchor-id="the-code">The code</h2>
<p>Data: the <code>deter</code> layer of the articles’ GeoPackage (<a href="../articles/about.html#data">how to download</a>). The original script used alerts from the Brasil MAIS program, which are restricted to government agencies; DETER works the same way. Names in Portuguese: <code>grupos</code> = groups, <code>tipo</code> = type (<code>corte_raso</code> = clear-cut, <code>mineracao</code> = mining), <code>qtde</code> = count, <code>tempo</code> = recency, <code>velo</code> = speed, <code>primeiro</code>/<code>ultimo</code> = first/last, <code>indice</code> = score, <code>prioridade</code> = priority.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> timedelta</span>
<span id="cb1-3"></span>
<span id="cb1-4">arcpy.env.overwriteOutput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-5">arcpy.env.outputCoordinateSystem <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.SpatialReference(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># meters</span></span>
<span id="cb1-6">gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rtigos</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gpkg"</span></span>
<span id="cb1-7">gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb"</span></span>
<span id="cb1-8"></span>
<span id="cb1-9">PARAM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"corte_raso"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, area<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>), qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), velo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)),</span>
<span id="cb1-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mineracao"</span>:  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, area<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), velo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00057</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)),</span>
<span id="cb1-12">}</span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> norm(valor, limites):</span>
<span id="cb1-15">    a, b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> limites</span>
<span id="cb1-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, (valor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> a) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> a)))</span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> classe(x):</span>
<span id="cb1-19">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> limite, nome <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> [(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Very High"</span>), (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>), (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medium"</span>), (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</span>)]:</span>
<span id="cb1-20">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> limite:</span>
<span id="cb1-21">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> nome</span>
<span id="cb1-22">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Very Low"</span></span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1 and 2. Alerts from the last year, repaired, with area and type</span></span>
<span id="cb1-25">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.CopyFeatures(gpkg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\m</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ain</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">deter"</span>, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter"</span>)</span>
<span id="cb1-26">ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(r[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> r <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(deter, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>]))</span>
<span id="cb1-27">corte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>)).strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-28">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(</span>
<span id="cb1-29">    deter, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">lertas"</span>,</span>
<span id="cb1-30">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname IN ('DESMATAMENTO_CR', 'DESMATAMENTO_VEG', 'MINERACAO') "</span></span>
<span id="cb1-31">    <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"AND view_date &gt; timestamp '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>corte<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> 00:00:00'"</span>)</span>
<span id="cb1-32">arcpy.management.RepairGeometry(alertas)</span>
<span id="cb1-33">arcpy.management.CalculateGeometryAttributes(</span>
<span id="cb1-34">    alertas, [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AREA_GEODESIC"</span>]], area_unit<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HECTARES"</span>)</span>
<span id="cb1-35">arcpy.management.CalculateField(</span>
<span id="cb1-36">    alertas, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'mineracao' if !classname! == 'MINERACAO' else 'corte_raso'"</span>,</span>
<span id="cb1-37">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>, field_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>)</span>
<span id="cb1-38"></span>
<span id="cb1-39">tabelas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb1-40"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> tipo, p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> PARAM.items():</span>
<span id="cb1-41">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(alertas, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"tipo = '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb1-42"></span>
<span id="cb1-43">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Group: buffer of half the distance, dissolve everything and split the parts</span></span>
<span id="cb1-44">    grupos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.PairwiseBuffer(a, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\b</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">uf_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb1-45">                                           <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dist'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> Meters"</span>, dissolve_option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ALL"</span>)</span>
<span id="cb1-46">    grupos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.management.MultipartToSinglepart(grupos, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">grupos_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-47">    arcpy.management.CalculateField(grupos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_' + str(!OBJECTID!)"</span>,</span>
<span id="cb1-48">                                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>, field_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>)</span>
<span id="cb1-49">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.SpatialJoin(a, grupos, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\a</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">j_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"JOIN_ONE_TO_ONE"</span>,</span>
<span id="cb1-50">                                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"KEEP_COMMON"</span>, match_option<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INTERSECT"</span>)</span>
<span id="cb1-51"></span>
<span id="cb1-52">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Measures of each group</span></span>
<span id="cb1-53">    t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Statistics(a, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">rf"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\t</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb1-54">                                  [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUM"</span>], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COUNT"</span>],</span>
<span id="cb1-55">                                   [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIN"</span>], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MAX"</span>]], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>)</span>
<span id="cb1-56"></span>
<span id="cb1-57">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Indices, score and class</span></span>
<span id="cb1-58">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> campo, tipo_campo <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> [(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tempo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LONG"</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DOUBLE"</span>),</span>
<span id="cb1-59">                              (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DOUBLE"</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TEXT"</span>)]:</span>
<span id="cb1-60">        arcpy.management.AddField(t, campo, tipo_campo)</span>
<span id="cb1-61">    campos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SUM_area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"COUNT_gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MIN_view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MAX_view_date"</span>,</span>
<span id="cb1-62">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tempo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>]</span>
<span id="cb1-63">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> arcpy.da.UpdateCursor(t, campos) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> cursor:</span>
<span id="cb1-64">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> area, qtde, primeiro, ultimo, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>_ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> cursor:</span>
<span id="cb1-65">            tempo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (ultimo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ref).days</span>
<span id="cb1-66">            velo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, (ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> primeiro).days)</span>
<span id="cb1-67">            indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm(area, p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area"</span>]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm(qtde, p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qtde"</span>])</span>
<span id="cb1-68">                      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm(tempo, (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> norm(velo, p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>]))</span>
<span id="cb1-69">            cursor.updateRow([area, qtde, primeiro, ultimo, tempo, velo, indice,</span>
<span id="cb1-70">                              classe(indice), tipo])</span>
<span id="cb1-71">    arcpy.management.JoinField(grupos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>, t, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>)</span>
<span id="cb1-72">    tabelas.append(grupos)</span>
<span id="cb1-73"></span>
<span id="cb1-74">arcpy.management.Merge(tabelas, gdb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">priorizacao_grupos"</span>)</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The original script grouped with <strong>Aggregate Polygons</strong> (<code>cartography.AggregatePolygons</code>), which is only available with an <strong>Advanced</strong> license. A dissolved buffer of half the distance gives the same groups and works with a Basic license.</p>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-2">G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/artigos.gpkg"</span></span>
<span id="cb2-3"></span>
<span id="cb2-4">PARAM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb2-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"corte_raso"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, area<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>), qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), velo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)),</span>
<span id="cb2-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mineracao"</span>:  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, area<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), velo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00057</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)),</span>
<span id="cb2-7">}</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> rodar(alg, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>p):</span>
<span id="cb2-10">    p.setdefault(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>)</span>
<span id="cb2-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> processing.run(alg, p)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> norm(campo, limites):</span>
<span id="cb2-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""QGIS expression: normalizes between the limits and clamps at 0 and 1."""</span></span>
<span id="cb2-15">    a, b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> limites</span>
<span id="cb2-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'clamp(0, ("</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>campo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">" - </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">) / (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> - </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">), 1)'</span></span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Reference date (the newest alert) and alerts from the last year</span></span>
<span id="cb2-19">fonte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>G<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=deter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb2-20">ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fonte.maximumValue(fonte.fields().indexOf(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>))</span>
<span id="cb2-21">corte <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ref.addDays(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>).toString(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yyyy-MM-dd"</span>)</span>
<span id="cb2-22">ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ref.toString(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yyyy-MM-dd"</span>)</span>
<span id="cb2-23">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyexpression"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>fonte, EXPRESSION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span></span>
<span id="cb2-24">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname IN ('DESMATAMENTO_CR', 'DESMATAMENTO_VEG', 'MINERACAO') "</span></span>
<span id="cb2-25">                <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"AND view_date &gt; '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>corte<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Repair, convert to meters, area in hectares and type</span></span>
<span id="cb2-28">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fixgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alertas)</span>
<span id="cb2-29">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:removenullgeometries"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alertas, REMOVE_EMPTY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb2-30">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:reprojectlayer"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alertas, TARGET_CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:5880"</span>)</span>
<span id="cb2-31">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alertas, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-32">                FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-33">                FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area(transform($geometry, 'EPSG:5880', 'ESRI:102033')) / 10000"</span>)</span>
<span id="cb2-34">alertas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alertas, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-35">                FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"if(classname = 'MINERACAO', 'mineracao', 'corte_raso')"</span>)</span>
<span id="cb2-36"></span>
<span id="cb2-37">partes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-38"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> tipo, p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> PARAM.items():</span>
<span id="cb2-39">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:extractbyattribute"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alertas, FIELD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>, OPERATOR<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, VALUE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tipo)</span>
<span id="cb2-40"></span>
<span id="cb2-41">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Group: buffer of half the distance, dissolve and split the parts</span></span>
<span id="cb2-42">    grupos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:buffer"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>a, DISTANCE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dist"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, DISSOLVE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb2-43">    grupos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:multiparttosingleparts"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>grupos)</span>
<span id="cb2-44">    grupos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>grupos, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-45">                   FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_' || to_string(@row_number)"</span>)</span>
<span id="cb2-46">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:joinattributesbylocation"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>a, JOIN<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>grupos, PREDICATE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>],</span>
<span id="cb2-47">              JOIN_FIELDS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>], METHOD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-48"></span>
<span id="cb2-49">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Measures of each group</span></span>
<span id="cb2-50">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:aggregate"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>a, GROUP_BY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"cluster"'</span>, AGGREGATES<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[</span>
<span id="cb2-51">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first_value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"cluster"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb2-52">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>},</span>
<span id="cb2-53">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first_value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"tipo"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb2-54">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>},</span>
<span id="cb2-55">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"area_ha"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span id="cb2-56">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>},</span>
<span id="cb2-57">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"gid"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qtde"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-58">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>},</span>
<span id="cb2-59">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"minimum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"view_date"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primeiro"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>,</span>
<span id="cb2-60">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>},</span>
<span id="cb2-61">        {<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aggregate"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"maximum"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"input"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"view_date"'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ultimo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>,</span>
<span id="cb2-62">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"length"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"precision"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>}])</span>
<span id="cb2-63">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tempo"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-64">              FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"-day(to_date('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ref<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">') - </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">ultimo</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>)</span>
<span id="cb2-65">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-66">              FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb2-67">              FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">area_ha</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> / max(1, day(to_date('</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ref<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">') - </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">primeiro</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\"</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">))"</span>)</span>
<span id="cb2-68"></span>
<span id="cb2-69">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Weighted score and class</span></span>
<span id="cb2-70">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indice"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-71">              FIELD_PRECISION<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(</span>
<span id="cb2-72">                  <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"0.2 * </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>norm(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'area_ha'</span>, p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'area'</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> + 0.2 * </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>norm(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'qtde'</span>, p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'qtde'</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> + "</span></span>
<span id="cb2-73">                  <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"0.2 * </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>norm(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tempo'</span>, (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> + 0.4 * </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>norm(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'velo'</span>, p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'velo'</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>))</span>
<span id="cb2-74">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:fieldcalculator"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>c, FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>, FIELD_TYPE<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-75">              FORMULA<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CASE WHEN indice &gt;= 0.95 THEN 'Very High' WHEN indice &gt;= 0.8 THEN "</span></span>
<span id="cb2-76">                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'High' WHEN indice &gt;= 0.6 THEN 'Medium' WHEN indice &gt;= 0.4 THEN 'Low' "</span></span>
<span id="cb2-77">                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ELSE 'Very Low' END"</span>)</span>
<span id="cb2-78">    partes.append(c)</span>
<span id="cb2-79"></span>
<span id="cb2-80">clusters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:mergevectorlayers"</span>, LAYERS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>partes, CRS<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"EPSG:5880"</span>,</span>
<span id="cb2-81">                 OUTPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/priorizacao_qgis.gpkg"</span>)</span>
<span id="cb2-82">resumo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rodar(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qgis:statisticsbycategories"</span>, INPUT<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>clusters,</span>
<span id="cb2-83">               CATEGORIES_FIELD_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>])</span>
<span id="cb2-84"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> resumo.getFeatures():</span>
<span id="cb2-85">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>], f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>])</span></code></pre></div></div>
<p>Grouping uses the same idea as in ArcGIS: two alerts within 60 m of each other have 30 m buffers that touch, and <strong>Dissolve</strong> merges them. The <code>clamp</code>, <code>day</code> and <code>max</code> functions in the expressions do the rest.</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p><code>ST_ClusterDBSCAN</code> does the grouping directly: with <code>minpoints = 1</code>, it joins every geometry within <code>eps</code> meters of another one in the group, just like <strong>Aggregate Polygons</strong>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Parameters of the original script</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> artigos.prior_param;</span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.prior_param (tipo text <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">PRIMARY</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">KEY</span>, dist <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>,</span>
<span id="cb3-4">    area_min <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, area_max <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, qtde_min <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, qtde_max <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>,</span>
<span id="cb3-5">    velo_min <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, velo_max <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>);</span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INTO</span> artigos.prior_param <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">VALUES</span></span>
<span id="cb3-7">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'corte_raso'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb3-8">    (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mineracao'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00057</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>);</span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- Normalization with limits: below the minimum = 0, above the maximum = 1</span></span>
<span id="cb3-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OR</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">REPLACE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FUNCTION</span> artigos.normalizar(valor <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, minimo <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>, maximo <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span>)</span>
<span id="cb3-12">RETURNS <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">numeric</span> LANGUAGE sql IMMUTABLE <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-13">$$ <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">least</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">greatest</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, (valor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> minimo) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (maximo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> minimo))) $$;</span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DROP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IF</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">EXISTS</span> artigos.prior_clusters;</span>
<span id="cb3-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.prior_clusters <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WITH</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(view_date) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter),</span>
<span id="cb3-18">alertas <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (                                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 1 and 2. last year, repaired, in meters</span></span>
<span id="cb3-19">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> a.gid, a.view_date,</span>
<span id="cb3-20">           <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CASE</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHEN</span> a.classname <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MINERACAO'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">THEN</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mineracao'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ELSE</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'corte_raso'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">END</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> tipo,</span>
<span id="cb3-21">           ST_Transform(ST_CollectionExtract(ST_MakeValid(a.geom), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-22">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter a, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref</span></span>
<span id="cb3-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> a.classname <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">IN</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO_CR'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'DESMATAMENTO_VEG'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'MINERACAO'</span>)</span>
<span id="cb3-24">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AND</span> a.view_date <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref</span>.d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span></span>
<span id="cb3-25">),</span>
<span id="cb3-26">agrupados <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (                                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 3. alerts within 60 m (or 800 m) = same group</span></span>
<span id="cb3-27">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>, (ST_Area(ST_Transform(geom, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:geography</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>):<span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">:numeric</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha,</span>
<span id="cb3-28">           <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CASE</span> tipo</span>
<span id="cb3-29">               <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHEN</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'mineracao'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">THEN</span> ST_ClusterDBSCAN(geom, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OVER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">PARTITION</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> tipo)</span>
<span id="cb3-30">               <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ELSE</span> ST_ClusterDBSCAN(geom, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">OVER</span> (<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">PARTITION</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> tipo)</span>
<span id="cb3-31">           <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">END</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">cluster</span></span>
<span id="cb3-32">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> alertas</span>
<span id="cb3-33">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHERE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">NOT</span> ST_IsEmpty(geom)</span>
<span id="cb3-34">),</span>
<span id="cb3-35">medidas <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> (                                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 4. measures of each group</span></span>
<span id="cb3-36">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> g.tipo, g.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">cluster</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(g.area_ha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> qtde,</span>
<span id="cb3-37">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(g.view_date) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref</span>.d <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> tempo,</span>
<span id="cb3-38">           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(g.area_ha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">greatest</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref</span>.d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(g.view_date)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> velo,</span>
<span id="cb3-39">           ST_Union(g.geom) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> geom</span>
<span id="cb3-40">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> agrupados g, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref</span></span>
<span id="cb3-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> g.tipo, g.<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">cluster</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ref</span>.d</span>
<span id="cb3-42">)</span>
<span id="cb3-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> m.<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>,                                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- 5. weighted score</span></span>
<span id="cb3-44">       <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> artigos.normalizar(m.area_ha, p.area_min, p.area_max)</span>
<span id="cb3-45">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> artigos.normalizar(m.qtde, p.qtde_min, p.qtde_max)</span>
<span id="cb3-46">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> artigos.normalizar(m.tempo, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)</span>
<span id="cb3-47">     <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> artigos.normalizar(m.velo, p.velo_min, p.velo_max) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> indice</span>
<span id="cb3-48"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> medidas m</span>
<span id="cb3-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">JOIN</span> artigos.prior_param p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> (tipo);</span>
<span id="cb3-50"></span>
<span id="cb3-51"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.prior_clusters <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">COLUMN</span> prioridade text;</span>
<span id="cb3-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span> artigos.prior_clusters <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> prioridade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CASE</span></span>
<span id="cb3-53">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHEN</span> indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">THEN</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Very High'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHEN</span> indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">THEN</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'High'</span></span>
<span id="cb3-54">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHEN</span> indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">THEN</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Medium'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">WHEN</span> indice <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">THEN</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Low'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ELSE</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Very Low'</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">END</span>;</span>
<span id="cb3-55"></span>
<span id="cb3-56"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> tipo, prioridade, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> grupos, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(area_ha)) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> area_ha</span>
<span id="cb3-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.prior_clusters</span>
<span id="cb3-58"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">GROUP</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> tipo, prioridade</span>
<span id="cb3-59"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ORDER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">BY</span> tipo, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(indice) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">DESC</span>;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb4-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parameters of the original script: grouping distance, limits of each index and weights</span></span>
<span id="cb4-5">PARAM <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb4-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"corte_raso"</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, area<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>), qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), velo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)),</span>
<span id="cb4-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mineracao"</span>:  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(dist<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, area<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), velo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.00057</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)),</span>
<span id="cb4-8">}</span>
<span id="cb4-9">TEMPO <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># days to the reference date (negative = past)</span></span>
<span id="cb4-10">PESOS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(area<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, tempo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, velo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>)</span>
<span id="cb4-11">CLASSES <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Very High"</span>), (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"High"</span>), (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Medium"</span>), (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low"</span>), (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Very Low"</span>)]</span>
<span id="cb4-12"></span>
<span id="cb4-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Clear-cut and mining alerts from the last 365 days</span></span>
<span id="cb4-14">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/artigos.gpkg"</span>, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>)</span>
<span id="cb4-15">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>])</span>
<span id="cb4-16">ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reference date: the newest alert</span></span>
<span id="cb4-17">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[(deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pd.Timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">365</span>))</span>
<span id="cb4-18">              <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>].isin([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_CR"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DESMATAMENTO_VEG"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MINERACAO"</span>])]</span>
<span id="cb4-19">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> c: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mineracao"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MINERACAO"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"corte_raso"</span>)</span>
<span id="cb4-20"></span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Repaired geometries, in meters, and area in hectares</span></span>
<span id="cb4-22">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter.make_valid(method<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"structure"</span>, keep_collapsed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb4-23">deter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>deter.is_empty].to_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb4-24">deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter.to_crs(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ESRI:102033"</span>).area <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10_000</span></span>
<span id="cb4-25"></span>
<span id="cb4-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> normalizar(valor, limites):</span>
<span id="cb4-27">    minimo, maximo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> limites</span>
<span id="cb4-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ((valor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> minimo) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (maximo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> minimo)).clip(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-29"></span>
<span id="cb4-30">partes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb4-31"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> tipo, p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> PARAM.items():</span>
<span id="cb4-32">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> deter[deter[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> tipo]</span>
<span id="cb4-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Group: buffer of half the distance, union and split the parts</span></span>
<span id="cb4-34">    grupos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.GeoDataFrame(geometry<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[a.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">buffer</span>(p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dist"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>).union_all()], crs<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5880</span>)</span>
<span id="cb4-35">    grupos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> grupos.explode(index_parts<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>).reset_index(drop<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb4-36">    grupos[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>tipo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(grupos))]</span>
<span id="cb4-37">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.sjoin(a, grupos, predicate<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"intersects"</span>).drop(columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"index_right"</span>)</span>
<span id="cb4-38"></span>
<span id="cb4-39">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Measures of each group</span></span>
<span id="cb4-40">    c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a.groupby(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cluster"</span>).agg(area_ha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sum"</span>), qtde<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>),</span>
<span id="cb4-41">                                 primeiro<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min"</span>), ultimo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>))</span>
<span id="cb4-42">    c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tempo"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ultimo"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ref).dt.days                         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0 = most recent</span></span>
<span id="cb4-43">    c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (ref <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primeiro"</span>]).dt.days.clip(lower<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-44"></span>
<span id="cb4-45">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5. Indices from 0 to 1, weighted score and class</span></span>
<span id="cb4-46">    c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indice"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (PESOS[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> normalizar(c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>], p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area"</span>])</span>
<span id="cb4-47">                   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> PESOS[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qtde"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> normalizar(c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qtde"</span>], p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qtde"</span>])</span>
<span id="cb4-48">                   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> PESOS[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tempo"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> normalizar(c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tempo"</span>], TEMPO)</span>
<span id="cb4-49">                   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> PESOS[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> normalizar(c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>], p[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>]))</span>
<span id="cb4-50">    c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indice"</span>].<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> lim, n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> CLASSES <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> lim))</span>
<span id="cb4-51">    partes.append(c.assign(tipo<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tipo))</span>
<span id="cb4-52"></span>
<span id="cb4-53">clusters <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat(partes)</span>
<span id="cb4-54"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"reference date:"</span>, ref.date())</span>
<span id="cb4-55"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(pd.crosstab(clusters[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>], clusters[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>]))</span>
<span id="cb4-56">colunas <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tipo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"area_ha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qtde"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tempo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"velo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prioridade"</span>]</span>
<span id="cb4-57"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(clusters.sort_values(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indice"</span>, ascending<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)[colunas].head(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb4-58">clusters.to_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/priorizacao_python.csv"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The <strong>QGIS</strong>, <strong>PostGIS</strong> and <strong>Python</strong> versions formed the same groups and gave exactly the same count in each class:</p>
<table class="table-striped caption-top table">
<caption>Groups of DETER alerts from 2025-10-17 to 2026-10-16 in the example area.</caption>
<thead>
<tr class="header">
<th>Priority</th>
<th style="text-align: right;">Clear-cut groups</th>
<th style="text-align: right;">Area (ha)</th>
<th style="text-align: right;">Mining groups</th>
<th style="text-align: right;">Area (ha)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Very High</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">6,974</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td>High</td>
<td style="text-align: right;">29</td>
<td style="text-align: right;">4,433</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">173</td>
</tr>
<tr class="odd">
<td>Medium</td>
<td style="text-align: right;">68</td>
<td style="text-align: right;">2,150</td>
<td style="text-align: right;">63</td>
<td style="text-align: right;">747</td>
</tr>
<tr class="even">
<td>Low</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">539</td>
<td style="text-align: right;">56</td>
<td style="text-align: right;">489</td>
</tr>
<tr class="odd">
<td>Very Low</td>
<td style="text-align: right;">129</td>
<td style="text-align: right;">1,929</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">110</td>
</tr>
</tbody>
</table>
<p>The 11 <strong>very high</strong> priority groups add up to almost 7,000 hectares, more than the 129 very low priority groups combined. That is the gain from prioritizing: almost 40% of the year’s alert area is in less than 3% of the groups (11 of 403).</p>
</section>
<section id="pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="pitfalls">Pitfalls</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Distance in degrees
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>AggregatePolygons</code> and <code>ST_ClusterDBSCAN</code> use the <strong>layer’s units</strong>. With data in SIRGAS 2000 (degrees), “60” means 60 degrees. Reproject to meters before grouping, as in all the code above.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Speed of an isolated alert
</div>
</div>
<div class="callout-body-container callout-body">
<p>A group with a single alert, detected today, would have a speed of “area ÷ 0 days”. The original divided by the difference up to today; here the divisor has a minimum of 1 day. Choose a rule and write it in the report.</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Limits and weights are choices
</div>
</div>
<div class="callout-body-container callout-body">
<p>The limits (30 ha, 4 alerts, 0.5 ha/day…) and the weights came from one team’s experience with one type of alert. They are not official criteria. Test other values and see how the list changes: if the top places do not change, the prioritization is robust.</p>
</div>
</div>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="https://hendesson.github.io/guia-espacial/curso/05-priorizacao.html">Course module 5: Prioritization</a> (in Portuguese), the SQL version with <code>WITH RECURSIVE</code></li>
<li><a href="../articles/09-kernel-density.html">Kernel density of deforestation</a>, another way to look at concentration</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/11-pdf-report.html" class="anterior"><span class="direcao">← Previous</span> Automated PDF report: the bulletin that writes itself</a> <a href="../articles/13-updating-layers.html" class="proximo"><span class="direcao">Next →</span> Updating layers without duplicates: replace, append and incremental load</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/12-priorizacao-arcgis-qgis.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Prioritizing Alerts in {ArcGIS} {Pro,} {QGIS} and {Python}},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/12-prioritization-arcgis-qgis.html},
  doi = {10.5281/zenodo.23780268},
  langid = {en},
  abstract = {Grouping neighboring alerts, measuring the area, count,
    recency and speed of each group and turning it all into a priority
    score. With the original script’s parameters, the 11 top-priority
    groups hold almost 40\% of the year’s alert area.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Prioritizing Alerts in ArcGIS Pro, QGIS
and Python.”</span> In <em>Geoprocessing Articles for Environmental
Monitoring</em>. Preprint, Hendesson Alves, October 29. <a href="https://doi.org/10.5281/zenodo.23780268">https://doi.org/10.5281/zenodo.23780268</a>.
</div></div></section></div> ]]></description>
  <category>DETER</category>
  <category>Prioritization</category>
  <guid>https://hendesson.github.io/en/articles/12-prioritization-arcgis-qgis.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/priorizacao_grupos.png" medium="image" type="image/png" height="192" width="144"/>
</item>
<item>
  <title>Updating layers without duplicates: replace, append and incremental load</title>
  <dc:creator>Hendesson Alves</dc:creator>
  <link>https://hendesson.github.io/en/articles/13-updating-layers.html</link>
  <description><![CDATA[ 





<div class="ficha">
<p><span class="rotulo">Level</span> Advanced <span class="rotulo">Reading</span> 10 min <span class="rotulo">Data</span> October 2026 <span class="rotulo">Programs</span> <span class="selo-texto">ArcGIS</span> <img src="https://hendesson.github.io/en/imagens/ferramentas/qgis.svg" class="img-fluid" alt="QGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/postgis.svg" class="img-fluid" alt="PostGIS"> <img src="https://hendesson.github.io/en/imagens/ferramentas/python.svg" class="img-fluid" alt="Python"></p>
</div>
<p>Almost every script in my folder ended the same way: <strong>delete</strong> the features of a layer and <strong>append</strong> the new ones (<code>DeleteFeatures</code> + <code>Append</code>). It works, but each script reinvented the rule, and an error halfway through left the layer empty or duplicated.</p>
<p>This article brings together the four patterns that appeared in the scripts and shows the most useful one for DETER, the <strong>incremental load</strong>, with three pitfalls that only showed up when testing with the real data.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://hendesson.github.io/en/imagens/artigos/atualizacao.png" class="img-fluid figure-img" alt="Four panels. Replace everything: the whole table is swapped. Replace a part: only the rows of one year are swapped. Append only the new: new rows go at the end. Insert or update: new rows go in and existing ones are updated by key."></p>
<figcaption>The four update patterns.</figcaption>
</figure>
</div>
<section id="the-four-patterns" class="level2">
<h2 class="anchored" data-anchor-id="the-four-patterns">The four patterns</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Pattern</th>
<th>When to use it</th>
<th>In the old script</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>1. Replace everything</strong></td>
<td>small layer that the source publishes in full (embargoes, protected areas)</td>
<td><code>DeleteFeatures</code> + <code>Append</code> of IBAMA embargoes</td>
</tr>
<tr class="even">
<td><strong>2. Replace a part</strong></td>
<td>data by period that may be republished (one PRODES year)</td>
<td>delete the year with <code>UpdateCursor</code> and append</td>
</tr>
<tr class="odd">
<td><strong>3. Append only the new</strong></td>
<td>data that only grows (daily alerts)</td>
<td>take the maximum date and append what is newer</td>
</tr>
<tr class="even">
<td><strong>4. Insert or update</strong> (“upsert”)</td>
<td>when a record can change after it is published</td>
<td>did not exist: it duplicated</td>
</tr>
</tbody>
</table>
<p>Pattern 2 is in the article <a href="../articles/03-prodes-protected-areas.html#updating-when-a-new-year-is-released">PRODES in protected areas</a>. Patterns 1, 3 and 4 are below.</p>
</section>
<section id="replace-everything-without-leaving-the-layer-empty" class="level2">
<h2 class="anchored" data-anchor-id="replace-everything-without-leaving-the-layer-empty">1. Replace everything, without leaving the layer empty</h2>
<p>The risk of “delete and append” is the script failing <strong>between</strong> the two steps: the layer stays empty until someone notices. Make the swap in one go:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Program</th>
<th>How</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>ArcGIS Pro</td>
<td><code>arcpy.management.TruncateTable(target)</code> and <code>Append(new, target, "NO_TEST")</code>, inside a <code>with arcpy.da.Editor(gdb):</code> to roll back on error</td>
</tr>
<tr class="even">
<td>QGIS</td>
<td><code>native:savefeatures</code> with <code>ACTION_ON_EXISTING_FILE = 1</code> (overwrites only the layer, in one operation)</td>
</tr>
<tr class="odd">
<td>PostGIS</td>
<td><code>BEGIN; TRUNCATE target; INSERT INTO target SELECT … FROM new; COMMIT;</code></td>
</tr>
<tr class="even">
<td>Python</td>
<td><code>gdf.to_file("base.gpkg", layer="embargos")</code> writes the whole layer again</td>
</tr>
</tbody>
</table>
</section>
<section id="and-4.-incremental-load-of-deter" class="level2">
<h2 class="anchored" data-anchor-id="and-4.-incremental-load-of-deter">3 and 4. Incremental load of DETER</h2>
<p>DETER publishes alerts <strong>every day</strong>. Downloading the whole service each time is slow; the right way is to request <strong>only what changed</strong> and merge it into the database. The recipe that worked, after testing:</p>
<ol type="1">
<li>Find the <strong>latest date</strong> in the database.</li>
<li>Ask the server for alerts from <strong>30 days before</strong> that date (the “safety window”).</li>
<li>Compare by the <strong>key</strong> <code>gid</code> + <code>classname</code> + <code>view_date</code> and keep only what is missing.</li>
<li><strong>Append</strong> (or, in PostGIS, insert and update what changed).</li>
</ol>
<p>To test it, I created an “old” database with only the alerts up to 2026-09-30 and ran each version <strong>twice in a row</strong>. Names in Portuguese: <code>chave</code> = key, <code>existentes</code> = existing, <code>ultima</code> = latest, <code>desde</code> = since, <code>janela</code> = window, <code>faltam</code> = missing, <code>novos</code> = new, <code>historico</code> = history.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset" data-group="programa">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">ArcGIS Pro</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">QGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">PostGIS</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="programa">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>ArcGIS Pro’s <code>WFSToFeatureClass</code> tool does not filter WFS by date. The way around it is to request the filtered GeoJSON from the server, as in the Python tab, and convert it with <code>JSONToFeatures</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arcpy</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> urllib.parse</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> urllib.request</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> timedelta</span>
<span id="cb1-5"></span>
<span id="cb1-6">arcpy.env.overwriteOutput <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb1-7">base <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\r</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">esultados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">gdb</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter_historico"</span></span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> chave(gid, classe, data):</span>
<span id="cb1-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""The gid alone repeats in DETER: the key is code + class + date."""</span></span>
<span id="cb1-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>gid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>classe<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(data)[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. How far does the database go? Fetch the previous 30 days again</span></span>
<span id="cb1-14">campos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>]</span>
<span id="cb1-15">existentes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {chave(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>linha) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> linha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(base, campos)}</span>
<span id="cb1-16">ultima <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>(linha[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> linha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(base, campos))</span>
<span id="cb1-17">desde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (ultima <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)).strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Ask the server only for the window, only in the study area</span></span>
<span id="cb1-20">filtro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"view_date &gt;= '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>desde<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">' AND BBOX(geom,-56.5,-8.5,-54.5,-5.5,'EPSG:4674')"</span></span>
<span id="cb1-21">url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://terrabrasilis.dpi.inpe.br/geoserver/deter-amz/ows?service=WFS&amp;version=2.0.0"</span></span>
<span id="cb1-22">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&amp;request=GetFeature&amp;typeNames=deter-amz:deter_amz&amp;outputFormat=application/json"</span></span>
<span id="cb1-23">       <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"&amp;srsName=EPSG:4674&amp;CQL_FILTER=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>urllib<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>parse<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>quote(filtro)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb1-24">urllib.request.urlretrieve(url, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter_janela</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">geojson"</span>)</span>
<span id="cb1-25">janela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.conversion.JSONToFeatures(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"C:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ados</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\d</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">eter_janela</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">geojson"</span>,</span>
<span id="cb1-26">                                         <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">janela"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"POLYGON"</span>)</span>
<span id="cb1-27"></span>
<span id="cb1-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Only what is missing from the database (the date comes as text in GeoJSON)</span></span>
<span id="cb1-29">faltam <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [oid <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> oid, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>linha <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> arcpy.da.SearchCursor(janela, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OID@"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> campos)</span>
<span id="cb1-30">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> chave(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>linha) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> existentes]</span>
<span id="cb1-31"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"new alerts:"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(faltam))</span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Convert the date and append</span></span>
<span id="cb1-34"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> faltam:</span>
<span id="cb1-35">    novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> arcpy.analysis.Select(janela, <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r"memory</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ovos"</span>,</span>
<span id="cb1-36">                                  <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"OBJECTID IN (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">','</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>join(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">map</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>, faltam))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>)</span>
<span id="cb1-37">    arcpy.management.CalculateField(</span>
<span id="cb1-38">        novos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"datetime.datetime.strptime(!view_date![:10], '%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">')"</span>,</span>
<span id="cb1-39">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PYTHON3"</span>, field_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DATE"</span>)</span>
<span id="cb1-40">    arcpy.management.DeleteField(novos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>)</span>
<span id="cb1-41">    arcpy.management.AlterField(novos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>)</span>
<span id="cb1-42">    arcpy.management.Append(novos, base, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NO_TEST"</span>)</span></code></pre></div></div>
<p>The same works for an <strong>ArcGIS Online</strong> layer: use the service URL (<code>.../FeatureServer/0</code>) as <code>base</code>, as the original scripts did.</p>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> processing</span>
<span id="cb2-2"></span>
<span id="cb2-3">BASE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/dados/deter_historico.gpkg"</span></span>
<span id="cb2-4">WFS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WFS:https://terrabrasilis.dpi.inpe.br/geoserver/deter-amz/ows"</span></span>
<span id="cb2-5">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|layername=deter-amz:deter_amz"</span>)</span>
<span id="cb2-6">CAMPOS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sensor"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"satellite"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipality"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"uf"</span>]</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> chave(f):</span>
<span id="cb2-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""The gid alone repeats in DETER: the key is code + class + date."""</span></span>
<span id="cb2-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'gid'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'classname'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>f[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'view_date'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>toString(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'yyyy-MM-dd'</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. How far does the database go? Fetch the previous 30 days again</span></span>
<span id="cb2-13">historico <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>BASE<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=deter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"historico"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb2-14">ultima <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> historico.maximumValue(historico.fields().indexOf(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>))</span>
<span id="cb2-15">desde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ultima.addDays(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>).toString(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yyyy-MM-dd"</span>)</span>
<span id="cb2-16">existentes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {chave(f) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> historico.getFeatures()}</span>
<span id="cb2-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"current database:"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(existentes), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alerts; fetching since"</span>, desde)</span>
<span id="cb2-18"></span>
<span id="cb2-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. WFS layer filtered on the server (date) and by the area-of-interest rectangle</span></span>
<span id="cb2-20">servidor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(WFS, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter_wfs"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb2-21">servidor.setSubsetString(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"view_date &gt;= '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>desde<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb2-22">retangulo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsFeatureRequest().setFilterRect(QgsRectangle(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">56.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">54.5</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>))</span>
<span id="cb2-23">janela <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> servidor.materialize(retangulo)           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># copies into memory</span></span>
<span id="cb2-24"></span>
<span id="cb2-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Keep only what is not yet in the database, and only the database's fields</span></span>
<span id="cb2-26">ids <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [f.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> janela.getFeatures() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> chave(f) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> existentes]</span>
<span id="cb2-27">novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> janela.materialize(QgsFeatureRequest().setFilterFids(ids))</span>
<span id="cb2-28">novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> processing.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:retainfields"</span>, {</span>
<span id="cb2-29">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT"</span>: novos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FIELDS"</span>: CAMPOS, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"memory:"</span>})[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>]</span>
<span id="cb2-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"new alerts:"</span>, novos.featureCount())</span>
<span id="cb2-31"></span>
<span id="cb2-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Append to the database layer (action 2 = append, without creating fields)</span></span>
<span id="cb2-33"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> novos.featureCount():</span>
<span id="cb2-34">    processing.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"native:savefeatures"</span>, {</span>
<span id="cb2-35">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"INPUT"</span>: novos, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OUTPUT"</span>: BASE, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LAYER_NAME"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>,</span>
<span id="cb2-36">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ACTION_ON_EXISTING_FILE"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>})</span>
<span id="cb2-37">depois <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> QgsVectorLayer(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>BASE<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">|layername=deter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"depois"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ogr"</span>)</span>
<span id="cb2-38"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"database after:"</span>, depois.featureCount(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alerts"</span>)</span></code></pre></div></div>
<p><code>materialize</code> copies the features into memory without going through Processing, which would stop at DETER’s invalid geometries (see the article <a href="../articles/02-inside-and-around.html">Inside and around</a>).</p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>In the database, the key becomes a <strong>primary key</strong>, and <code>INSERT … ON CONFLICT</code> inserts what is new and updates what already exists, in a single operation:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-- First time only: the base table, from the GeoPackage's deter layer</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.deter_hist <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> gid, classname, view_date, sensor, satellite, municipality, uf, geom</span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter;</span>
<span id="cb3-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ALTER</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">TABLE</span> artigos.deter_hist <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ADD</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">PRIMARY</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">KEY</span> (gid, classname, view_date);</span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">CREATE</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INDEX</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> artigos.deter_hist <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">USING</span> gist (geom);</span></code></pre></div></div>
<p>The update script uses <code>ogr2ogr</code> to bring the window from the WFS:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#!/usr/bin/env bash</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Incremental DETER load in PostGIS: only the alerts newer than those in the database.</span></span>
<span id="cb4-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-euo</span> pipefail</span>
<span id="cb4-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">BANCO</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>monitoramento_ambiental</span>
<span id="cb4-5"></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. How far does the database go? Fetch the previous 30 days again (safety window)</span></span>
<span id="cb4-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">DESDE</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">psql</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$BANCO</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-Atc</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SELECT max(view_date) - 30 FROM artigos.deter_hist"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb4-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fetching alerts since </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$DESDE</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-9"></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Bring only the window from the WFS into a staging table</span></span>
<span id="cb4-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">ogr2ogr</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-f</span> PostgreSQL <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PG:dbname=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$BANCO</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-12">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WFS:https://terrabrasilis.dpi.inpe.br/geoserver/deter-amz/ows"</span> deter-amz:deter_amz <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-nln</span> artigos.deter_novos <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-overwrite</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-nlt</span> MULTIPOLYGON <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-lco</span> GEOMETRY_NAME=geom <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-spat</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-56.5</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-8.5</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-54.5</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-5.5</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-where</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date &gt;= '</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$DESDE</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb4-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-select</span> gid,classname,view_date,sensor,satellite,municipality,uf</span>
<span id="cb4-16"></span>
<span id="cb4-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Insert the new ones; if the key already exists, update it</span></span>
<span id="cb4-18"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">psql</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$BANCO</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-v</span> ON_ERROR_STOP=1 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-f</span> upsert.sql</span></code></pre></div></div>
<p>And <code>upsert.sql</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode sql code-with-copy"><code class="sourceCode sql"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INSERT</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">INTO</span> artigos.deter_hist (gid, classname, view_date, sensor, satellite,</span>
<span id="cb5-2">                                municipality, uf, geom)</span>
<span id="cb5-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> gid, classname, view_date, sensor, satellite, municipality, uf, geom</span>
<span id="cb5-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_novos</span>
<span id="cb5-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">ON</span> CONFLICT (gid, classname, view_date) DO <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">UPDATE</span></span>
<span id="cb5-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SET</span> geom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EXCLUDED.geom,</span>
<span id="cb5-7">        municipality <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EXCLUDED.municipality;</span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">SELECT</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> alertas, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(view_date) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">AS</span> ate <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">FROM</span> artigos.deter_hist;</span></code></pre></div></div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> geopandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gpd</span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb6-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> urllib.parse <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> quote</span>
<span id="cb6-4"></span>
<span id="cb6-5">BASE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dados/deter_historico.gpkg"</span></span>
<span id="cb6-6">CAMPOS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sensor"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"satellite"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"municipality"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"uf"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"geometry"</span>]</span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> chave(gdf):</span>
<span id="cb6-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""The gid alone repeats in DETER: the key is code + class + date."""</span></span>
<span id="cb6-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> gdf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gid"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gdf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"classname"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"|"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gdf[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>].astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>)</span>
<span id="cb6-11"></span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. How far does the database go? Fetch the previous 30 days again (safety window)</span></span>
<span id="cb6-13">historico <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(BASE, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>)</span>
<span id="cb6-14">ultima <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.to_datetime(historico[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"view_date"</span>]).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb6-15">desde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (ultima <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pd.Timedelta(days<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)).date()</span>
<span id="cb6-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"current database:"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(historico), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alerts, up to"</span>, ultima.date(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"- fetching since"</span>, desde)</span>
<span id="cb6-17"></span>
<span id="cb6-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Ask the server only for the window, only in the study area (the filter runs on the server)</span></span>
<span id="cb6-19">filtro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"view_date &gt;= '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>desde<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">' AND BBOX(geom,-56.5,-8.5,-54.5,-5.5,'EPSG:4674')"</span></span>
<span id="cb6-20">url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://terrabrasilis.dpi.inpe.br/geoserver/deter-amz/ows?service=WFS&amp;version=2.0.0"</span></span>
<span id="cb6-21">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&amp;request=GetFeature&amp;typeNames=deter-amz:deter_amz&amp;outputFormat=application/json"</span></span>
<span id="cb6-22">       <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"&amp;srsName=EPSG:4674&amp;CQL_FILTER=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>quote(filtro)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb6-23">novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(url)</span>
<span id="cb6-24"></span>
<span id="cb6-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> novos.empty:</span>
<span id="cb6-26">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nothing new on the server"</span>)</span>
<span id="cb6-27"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb6-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3. Keep only what is not yet in the database</span></span>
<span id="cb6-29">    novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> novos.set_crs(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4674</span>, allow_override<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)[CAMPOS]</span>
<span id="cb6-30">    novos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> novos[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>chave(novos).isin(chave(historico))]</span>
<span id="cb6-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 4. Append to the same GeoPackage layer</span></span>
<span id="cb6-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(novos):</span>
<span id="cb6-33">        novos.to_file(BASE, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>, mode<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a"</span>)</span>
<span id="cb6-34">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alerts appended:"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(novos))</span>
<span id="cb6-35"></span>
<span id="cb6-36">depois <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> gpd.read_file(BASE, layer<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"deter"</span>)</span>
<span id="cb6-37"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"database after:"</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(depois), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alerts; repeated keys:"</span>, chave(depois).duplicated().<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>())</span></code></pre></div></div>
</div>
</div>
</div>
</section>
<section id="checking-the-result" class="level2">
<h2 class="anchored" data-anchor-id="checking-the-result">Checking the result</h2>
<p>The three tested versions (QGIS, PostGIS and Python) started from the same database of 7,483 alerts:</p>
<table class="table-striped caption-top table">
<caption>Incremental DETER load in the example area (data up to 2026-10-16).</caption>
<thead>
<tr class="header">
<th></th>
<th style="text-align: right;">1st run</th>
<th style="text-align: right;">2nd run</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Database before</td>
<td style="text-align: right;">7,483</td>
<td style="text-align: right;">7,823</td>
</tr>
<tr class="even">
<td>Alerts appended</td>
<td style="text-align: right;">340</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td>Database after</td>
<td style="text-align: right;">7,823</td>
<td style="text-align: right;">7,823</td>
</tr>
<tr class="even">
<td>Repeated keys</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
</tr>
</tbody>
</table>
<p>The second run appended nothing, which is the most important test: an update script must be able to run twice without damaging the database.</p>
</section>
<section id="the-three-pitfalls-the-data-revealed" class="level2">
<h2 class="anchored" data-anchor-id="the-three-pitfalls-the-data-revealed">The three pitfalls the data revealed</h2>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>1. The alert code repeats
</div>
</div>
<div class="callout-body-container callout-body">
<p>The <code>gid</code> field looks like an identifier, but <strong>28 codes</strong> in the study area appear in two different alerts (different class, date and place). E.g., <code>36608_hist</code> is a clearing from March 2024 <strong>and</strong> a burn scar from October 2024. With <code>gid</code> as the key, PostGIS rejects the table; using it in an <code>isin</code>, new alerts would be discarded. The combination <code>gid</code> + <code>classname</code> + <code>view_date</code> is unique across the 7,823 alerts.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>2. The date filter returns the last day again
</div>
</div>
<div class="callout-body-container callout-body">
<p>Through <code>ogr2ogr</code>, the filter <code>view_date &gt; '2026-10-16'</code> returned <strong>148 alerts from the 16th itself</strong>. The server compares the date as a date and time, with a time zone. <code>ON CONFLICT</code> prevented duplication, but a script that only appends would have duplicated those 148. Lesson: <strong>never rely on the date filter alone</strong>; compare by key.</p>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>3. Alerts published late
</div>
</div>
<div class="callout-body-container callout-body">
<p>An alert can enter the service days after the image date (<code>view_date</code>). If the script only fetches “what is newer than the maximum date”, that alert never gets in. The <strong>30-day window</strong> catches the late ones, and the key discards what was already in the database.</p>
</div>
</div>
</section>
<section id="scheduling-and-taking-it-to-power-bi" class="level2">
<h2 class="anchored" data-anchor-id="scheduling-and-taking-it-to-power-bi">Scheduling and taking it to Power BI</h2>
<ul>
<li><strong>Scheduling:</strong> Windows Task Scheduler or <code>cron</code> (see the article <a href="../articles/04-fire-hotspots.html#running-it-on-its-own-every-day">Fire hotspots</a>). Run it once a day, at night.</li>
<li><strong>Power BI:</strong> with the database updated in PostGIS, the dashboard from <a href="https://hendesson.github.io/guia-espacial/curso/07-power-bi.html">Module 7</a> (in Portuguese) only needs a <strong>Refresh</strong>. In the Power BI service, scheduled refresh of a database on your own computer requires the <strong>on-premises data gateway</strong> installed and running.</li>
</ul>
</section>
<section id="see-also" class="level2">
<h2 class="anchored" data-anchor-id="see-also">See also</h2>
<ul>
<li><a href="../articles/03-prodes-protected-areas.html">Annual PRODES deforestation in protected areas</a>, with pattern 2</li>
<li><a href="https://hendesson.github.io/guia-espacial/curso/06-python.html">Course module 6: Python + PostGIS</a> (in Portuguese), to write to the database without passwords in the code</li>
</ul>
<div class="navegacao-livro">
<p><a href="../articles/12-prioritization-arcgis-qgis.html" class="anterior"><span class="direcao">← Previous</span> Prioritizing alerts in ArcGIS Pro, QGIS and Python</a></p>
</div>
</section>
<section id="updates" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="updates">Updates</h2>
<ul>
<li><strong>2026-10-29:</strong> first published, with data from INPE, MMA, IBAMA and SICAR from October 2026.</li>
</ul>
<p><a href="https://hendesson.github.io/artigos/13-atualizar-camadas.html">Versão em português</a></p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@misc{alves2026,
  author = {Alves, Hendesson},
  publisher = {Hendesson Alves},
  title = {Updating Layers Without Duplicates: Replace, Append and
    Incremental Load},
  date = {2026-10-29},
  url = {https://hendesson.github.io/en/articles/13-updating-layers.html},
  doi = {10.5281/zenodo.23780270},
  langid = {en},
  abstract = {The four ways to update a database that receives new data,
    and an incremental-load recipe for DETER that survives three real
    pitfalls: repeated codes, a date filter with time zones and alerts
    published late.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-alves2026" class="csl-entry quarto-appendix-citeas">
Alves, Hendesson. 2026. <span>“Updating Layers Without Duplicates:
Replace, Append and Incremental Load.”</span> In <em>Geoprocessing
Articles for Environmental Monitoring</em>. Preprint, Hendesson Alves,
October 29. <a href="https://doi.org/10.5281/zenodo.23780270">https://doi.org/10.5281/zenodo.23780270</a>.
</div></div></section></div> ]]></description>
  <category>DETER</category>
  <category>Automation</category>
  <guid>https://hendesson.github.io/en/articles/13-updating-layers.html</guid>
  <pubDate>Thu, 29 Oct 2026 00:00:00 GMT</pubDate>
  <media:content url="https://hendesson.github.io/en/imagens/artigos/atualizacao.png" medium="image" type="image/png" height="57" width="144"/>
</item>
</channel>
</rss>
