I have a previously reclassified raster file which I run a simple process on and then need to generate a very basic report about the percentages of each class.
I'm able to calculate the total count for each raster value/class using summary stats and was wondering if there's a way to skip summary stats and accomplish this with NumPy instead?
Here's a sample code:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> pandas <SPAN class="keyword token">as</SPAN> pd
InRaster <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SomeSingleBandRaster"</SPAN> <SPAN class="comment token">##This raster was reclassified to have 4 classes##</SPAN>
OutGDB <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>scratchGDB
SlopeReport <SPAN class="operator token">=</SPAN> OutGDB <SPAN class="operator token">+</SPAN> <SPAN class="string token">'/'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"SlopeReport"</SPAN>
StatsTable <SPAN class="operator token">=</SPAN> OutGDB <SPAN class="operator token">+</SPAN> <SPAN class="string token">'/'</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"StatsTable"</SPAN>
<SPAN class="comment token">#Generate Summary Statistics#</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>analysis<SPAN class="punctuation token">.</SPAN>Statistics<SPAN class="punctuation token">(</SPAN>InRaster<SPAN class="punctuation token">,</SPAN> StatsTable<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Value SUM"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Count"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Create array and calculate percentate of each class</SPAN>
array <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>da<SPAN class="punctuation token">.</SPAN>TableToNumPyArray<SPAN class="punctuation token">(</SPAN>StatsTable<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Count'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SUM_Value'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
df <SPAN class="operator token">=</SPAN> pd<SPAN class="punctuation token">.</SPAN>DataFrame<SPAN class="punctuation token">(</SPAN>array<SPAN class="punctuation token">)</SPAN>
df<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'perc'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">=</SPAN> df<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Count"</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">/</SPAN> df<SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Count"</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>sum<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="number token">100</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This will generate the following output:
Count SUM_Value perc
<SPAN class="number token">0</SPAN> <SPAN class="number token">5.0</SPAN> <SPAN class="number token">4.0</SPAN> <SPAN class="number token">0.198255</SPAN>
<SPAN class="number token">1</SPAN> <SPAN class="number token">274.0</SPAN> <SPAN class="number token">3.0</SPAN> <SPAN class="number token">10.864393</SPAN>
<SPAN class="number token">2</SPAN> <SPAN class="number token">1057.0</SPAN> <SPAN class="number token">1.0</SPAN> <SPAN class="number token">41.911182</SPAN>
<SPAN class="number token">3</SPAN> <SPAN class="number token">1186.0</SPAN> <SPAN class="number token">2.0</SPAN> <SPAN class="number token">47.026170</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This works but I'm always eager to cut out unnecessary steps (while hopefully learning something in the process!)
Thanks!