When you are working arcpy map algebra it is useful to have a more complete understanding of how temporary rasters are created and how they are managed. The help articles on arcpy map algebra and the Raster object are helpful but leave out some details that I believe are good to know if you are making heavy use of arcpy map algebra.
- The ArcGIS raster engine (whether you access it through the Raster Calculator tool, arcpy, or ArcObjects) will delay execution of local operators (examples: Plus, Multiply, Divide) until the output is needed for processing a non-local operator (for example to run Zonal Statistics) or to save a raster with the .save() method.
- Temporary rasters are written to the current env.scratchWorkspace location. If this workspace is a geodatabase, temporary rasters are saved in FGDB format. If it is a folder, either Esri GRID (pre-10.5) or TIFF (10.5 or later) is used. If it is not set, the user's Default.gdb (C:\Users\username\ArcGIS\Default.gdb) is used. I recommend setting env.scratchWorkspace to a folder workspace for best performance and stability out of the raster tools. This is especially true in versions before 10.5, as bugs in the raster tools for handling non-GRID file formats have been fixed over time.
- When you run the .save() method on a temporary raster object, and the format and location is the same as the temporary raster (GRID pre-10.5, TIFF 10.5+) the raster file will rename, not copy, the temporary raster to permanent. (Usually this is not important, but if you have a large (say, > 1GB) raster you may notice the difference.)
The following Python dialog (ArcGIS 10.5) may provide some insight:
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> os
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">from</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>sa <SPAN class="keyword token">import</SPAN> <SPAN class="operator token">*</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Spatial"</SPAN><SPAN class="punctuation token">)</SPAN>
u<SPAN class="string token">'CheckedOut'</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> env<SPAN class="punctuation token">.</SPAN>addOutputsToMap <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN> <SPAN class="comment token"># I did this because I was using the ArcMap Pyt</SPAN>
on window
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> testwk <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Users\cprice\Documents\ArcGIS\xxwk"</SPAN> <SPAN class="comment token"># empty folder</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> testwk
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> env<SPAN class="punctuation token">.</SPAN>scratchWorkspace <SPAN class="operator token">=</SPAN> testwk
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># set raster properties</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> env<SPAN class="punctuation token">.</SPAN>extent <SPAN class="operator token">=</SPAN> <SPAN class="string token">"0 0 10 10"</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> env<SPAN class="punctuation token">.</SPAN>cellSize <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">xdir</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">from</SPAN> textwrap <SPAN class="keyword token">import</SPAN> fill <SPAN class="keyword token">as</SPAN> wrap
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>wrap<SPAN class="punctuation token">(</SPAN><SPAN class="string token">" "</SPAN><SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>listdir<SPAN class="punctuation token">(</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> xdir<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># scratchWorkspace is empty</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> ras1 <SPAN class="operator token">=</SPAN> CreateConstantRaster<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> xdir<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># temp raster written to disk</SPAN>
CreateConstantR1<SPAN class="punctuation token">.</SPAN>tfw CreateConstantR1<SPAN class="punctuation token">.</SPAN>tif CreateConstantR1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml
CreateConstantR1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg CreateConstantR1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
CreateConstantR1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>xml
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> ras1 <SPAN class="operator token">=</SPAN> CreateConstantRaster<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> xdir<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># temp raster written to disk</SPAN>
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tfw CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>xml
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> ras2 <SPAN class="operator token">=</SPAN> ras1 <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> ras3 <SPAN class="operator token">=</SPAN> ras2 <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> xdir<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># folder unchanged ("delayed execution")</SPAN>
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tfw CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf<SPAN class="punctuation token">.</SPAN>IGSKMCCWVM110W7<SPAN class="number token">.4604</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">4388.</SPAN>sr<SPAN class="punctuation token">.</SPAN>lock
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>xml
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> ras4 <SPAN class="operator token">=</SPAN> FocalStatistics<SPAN class="punctuation token">(</SPAN>ras3<SPAN class="punctuation token">)</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> xdir<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># The tool above forced delayed execution of both Plus operations</SPAN>
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tfw CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf<SPAN class="punctuation token">.</SPAN>IGSKMCCWVM110W7<SPAN class="number token">.4604</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">4388.</SPAN>sr<SPAN class="punctuation token">.</SPAN>lock
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>xml FocalSt_plus_ra1<SPAN class="punctuation token">.</SPAN>tfw FocalSt_plus_ra1<SPAN class="punctuation token">.</SPAN>tif
FocalSt_plus_ra1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml FocalSt_plus_ra1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>xml plus_ras<SPAN class="punctuation token">.</SPAN>tfw
plus_ras<SPAN class="punctuation token">.</SPAN>tif plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg
plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf<SPAN class="punctuation token">.</SPAN>IGSKMCCWVM110W7<SPAN class="number token">.4604</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">4388.</SPAN>sr<SPAN class="punctuation token">.</SPAN>lock plus_ras1<SPAN class="punctuation token">.</SPAN>tfw
plus_ras1<SPAN class="punctuation token">.</SPAN>tif plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg
plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf<SPAN class="punctuation token">.</SPAN>IGSKMCCWVM110W7<SPAN class="number token">.4604</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">4388.</SPAN>sr<SPAN class="punctuation token">.</SPAN>lock
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> ras4<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ras4"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># save ras4 in grid format</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> xdir<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># The tool above forced delayed execution of both Plus operations</SPAN>
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tfw CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf<SPAN class="punctuation token">.</SPAN>IGSKMCCWVM110W7<SPAN class="number token">.4604</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">4388.</SPAN>sr<SPAN class="punctuation token">.</SPAN>lock
CreateConstantR2<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>xml info plus_ras<SPAN class="punctuation token">.</SPAN>tfw plus_ras<SPAN class="punctuation token">.</SPAN>tif
plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
plus_ras<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf<SPAN class="punctuation token">.</SPAN>IGSKMCCWVM110W7<SPAN class="number token">.4604</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">4388.</SPAN>sr<SPAN class="punctuation token">.</SPAN>lock plus_ras1<SPAN class="punctuation token">.</SPAN>tfw
plus_ras1<SPAN class="punctuation token">.</SPAN>tif plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>cpg
plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf
plus_ras1<SPAN class="punctuation token">.</SPAN>tif<SPAN class="punctuation token">.</SPAN>vat<SPAN class="punctuation token">.</SPAN>dbf<SPAN class="punctuation token">.</SPAN>IGSKMCCWVM110W7<SPAN class="number token">.4604</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">4388.</SPAN>sr<SPAN class="punctuation token">.</SPAN>lock ras4
ras4<SPAN class="punctuation token">.</SPAN>aux<SPAN class="punctuation token">.</SPAN>xml
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="keyword token">for</SPAN> kk <SPAN class="keyword token">in</SPAN> <SPAN class="punctuation token">[</SPAN>ras1<SPAN class="punctuation token">,</SPAN> ras2<SPAN class="punctuation token">,</SPAN> ras3<SPAN class="punctuation token">,</SPAN> ras4<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">print</SPAN> kk<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">,</SPAN> kk<SPAN class="punctuation token">.</SPAN>isTemporary
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
None <SPAN class="token boolean">True</SPAN>
None <SPAN class="token boolean">True</SPAN>
None <SPAN class="token boolean">True</SPAN>
ras4 <SPAN class="token boolean">False</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># At end of python session all temporary rasters are deleted</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># only "ras4" and "info" will remain</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></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><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></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><SPAN></SPAN><SPAN></SPAN></SPAN>Update, I have noticed at 10.5 that the old ArcGIS 9.3 MultiOutputMapAlgebra tool does not write TIFFs. This may be helpful if your tifs are so big that they pass the 2.1GB file size limit running arcpy x32 (say from ArcMap). MOMA is not supported and is tricky to use, as it only handles grids and the tool syntaxes are different (and some tools just aren't available), but it does still work at 10.5 (and Pro). This is not currently supported, use at your own risk!
cmd <SPAN class="operator token">=</SPAN> <SPAN class="string token">"ras5 = focalmin(ras4)"</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>gp<SPAN class="punctuation token">.</SPAN>MultiOutputMapAlgebra_sa<SPAN class="punctuation token">(</SPAN>cmd<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
ras5 <SPAN class="operator token">=</SPAN> Raster<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ras5"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># for later use of arcpy map algebra</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>