Hello everyone,
I found what I think might be a bug, and I am hoping perhaps someone can help me understand what's happening. I've been working with Python and arcpy for about a year now, so I like to think I have a fairly good handle on how the process is supposed to work, but maybe I'm missing something obvious.
I am trying to use the ExtractByMask tool programmatically to "clip" down a raster to only the cells inside a polygon. When I use the tool manually, I get the expected result. That is, the raster that is output from the process is named (in Contents, and in the .gdb) with the name that I have selected for it in the tool's 'Output_raster' field.
When I use Python, the output raster is named with the name of the variable I used to define the name, not the name (defined as a string) itself.
Here's a few screenshots which will hopefully clarify what I mean:
In this first example, you can see on the right how the defined name for the output is 'Clark_County_Rainfall_Jan_2020', and after the tool is run the name in the Contents panel shows it correctly.
When I use History > right-click (successful) Extract by Mask entry > Copy Python Command, paste into my Notebook, and edit the entry to use variables, it looks like this:
When I run the cell I get an entry in the Contents pane which has the layer named as 'rasterMask', but the name is correct in the .gdb:
Thanks for any help anyone can provide.
ArcGIS Pro 3.0.1
Python 3.9.11
The code examples in the help show saving to a specified path and filename
Extract by Mask (Spatial Analyst)—ArcGIS Pro | Documentation
although I would recommend throwing a *.tif to the filename so you don't have issues with truncated names when an esri grid is returned
Hi @DanPatterson , thanks for your reply!
I definitely reviewed the documentation you linked to before posting. The program doesn't seem to work the way the documentation indicates it should. Going by the example 2 in the docs, one would expect the final result to be named 'extractmask', yes? Instead, I get the equivalent of 'outExtractByMask', which definitely isn't expected behavior. I've run several other similar cells of code, all named with variables, and this is the only function that produces a different result than expected.
I did, at one point during my troubleshooting as you suggested, try specifying the full path and filename, and got the same result (incorrect name in Contents). I should probably add that I use env.workspace = my.gdb, which is where the output from the tool went when running it manually, and where all the other output from other tools goes, too.
Trying with a .tif extension still results in the same incorrect name in Contents, and now produces an error. It's still ignoring my specified name, and it's attempting to use a name 'Extract_PRIS3', which I believe is the default output name for this tool:
I can output directly to a non-.gdb location on disk with a .tif extension, but it's still named incorrectly in Contents ('rasterMask'). In any case, shouldn't the name in Contents be the name I specify?
I looked around for information about the error message, but couldn't quite understand what would account for a difference between the Python method and running the tool manually.
Note: You can't save .tif format to output GDB workspace. Use full path when saving or verify your scratchWorkspace is set to file directory if using .tif or other file based extensions.
@rytz Thank you for your post and great investigation.
With respect to your OP...
"When I use Python, the output raster is named with the name of the variable I used to define the name, not the name (defined as a string) itself."
Please understand that this is "by design" in that specifically when working with Spatial Analyst tools and operations in Python you are performing Map Algebra.
Any tool or operator that produces an output raster to the left of the equal sign creates a raster object.
For example, in the following expression, out_raster is a raster object.
out_raster = Slope("inelevation")
When a Raster object is returned from a Map Algebra expression, by default, the object (the variable and associated dataset) is temporary.
The temporary dataset associated with a raster object can become permanent by calling the raster object's save method.
If the referenced raster is not made permanent, the variable and the referenced raster dataset will be deleted when the variable goes out of scope, such as when a stand-alone script completes or ArcGIS is closed.
In your case, working within the Interactive Python window inside Pro automatically adds the temporary raster to the display (there is an option to turn this off) and we honour the variable name (raster object). When you save, the data thus becomes permanent and the output name is updated internally.
Hope this helps! Good luck!
Here are some additional resources for further review.
Raster Object
Working-with-raster-objects