Select to view content in your preferred language

Define the output name of a raster using SA and Python

3123
2
10-26-2011 01:47 AM
XanderBakker
Esri Esteemed Contributor
There is a thing that I simply don???t understand or what I am not willing to accept. When using ArcPy.sa to create raster output in the Python window, the name the raster is given in the workspace (in my case a fgdb) is based on the function applied (e.g. ifthe_ras* when using a Con statement, or FocalSt_* when using focal statistics, etc).
  
  What I would really like (and I know I don???t stand alone in this) is that the name I have assigned when executing the command is used for the name of the raster in my output workspace.  Use the env.overwriteOutput setting and when the output raster exists and should not be overwritten throw an error. But please don???t make my script twice as long since after every command:

myRaster = Some ArcPy.sa command 

I have to say

myRaster.save("myRaster").:mad:
  
  I???m glad that ArcGIS 10(.1) is a lot more stable than the 9.4 beta. When I was using 9.4, I regularly ended up with a crash and a fgdb filled with a lot of rasters for which it was impossible to derive what they were based on???
  
  Should I submit this in the ideas section, or can we just consider this a bug, that can be resolved in the next release?
0 Kudos
2 Replies
RyanDeBruyn
Esri Contributor
Xander, thank you for you post.

One of the key features of the scripting with the arcpy SA module in 10.x  is the use of the mapalgebra syntax and the ability to more efficiently execute tools in a workflow.  Scripting gives you the oportunity to combine many tools or workflows together and when you have the final result you persist it and share it.  All the intermediate data may or may not be needed and can be managed accordingly.  In my opinion, this is a similar concept to stringing together multiple tools in a geoprocessing model builder.

Running Spatial Analyst tools in python, you may have noticed that you no longer need to specify an output name for Spatial analyst tools (there is no out parameter in scripting). This is one of the major differences in 10.x from 9.x

In its most basic form, an output raster is specified to the left of an equal sign (=), and the tools, operators, and their parameters are on the right.

For example:

from arcpy.sa import *
outShade = Hillshade("inelevation", 99, 33)

The primary raster output  (outShade) of the Spatial Analyst Map Algebra expression is a Raster object.  One of the most important behaviors of a Raster object is that when it is created from a Map Algebra expression, it references a temporary raster (with a default name). Temporary data, unless it is explicitly saved, may be removed when the ArcGIS session or script ends. There are also a number or raster properties you can access directly from the raster object without describing the data.

The data you are refering to that contains the function_ name is the temporary raster. You will need to persist the data using the .save() method otherwise it may be removed when the ArcGIS Desktop application ends (if document is not saved) or when the script is completed.

Working with the python window in ArcMap it is integrated so your result are added to the display with the name of the "raster object" you specified.  The save is still required if your want to persist your data with a defined name. 

If you are running single expression in the python window and simple want the data to be named and persisted one suggestion is to execute the GP tool directly rather than the two step process required in the python window.  

I hope this helps you understand the concepts a bit better and will allow you work with this in mind.  There are many tools for the job and they all have their own way to help you more quickly and effiently complete your work.  For example we can give you a hammer and screw, but it may be better to use a screwdriver. 🙂   We (at esri) need to work toward helping and teaching these concepts, so your input is well appreciated.

Please have a look at some of the documention to help better explain this. And feel free to dive in.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_using_Map_Algebra/00p6...

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Managing_output_from_Spatial_Analyst_t...

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v000000wt000000

Regards,
-Ryan
0 Kudos
XanderBakker
Esri Esteemed Contributor
Hi Ryan,



Thanks for the explanation, but I still don't subscribe to this point of view...



In my stubborn intent to combine the Hillshade command with the save command, something unexpected  happened. For the record; I didn�??t have my hopes up high, but wanted to know how ArcMap would respond.
  
I loaded a DEM called myDEM to ArcMap, created a new file geodatabase and set it as my default geodatabase. After activating the Spatial Analyst extension, I opened the Python window and started with the line:
  
from arcpy.sa import *
  
My Hillshade command would be as simple as:
  
myHS = Hillshade("myDEM",315,45)
  
To  force the temporary raster to be saved as �??myHS�?�, I started to type �??save(�?� at the end of the command. The moment the left bracket is typed, the command is executed. Every time you type another character the command is invoked again and again. In some cases an error about the background geoprocessing was thrown, and in another case ArcMap threw an error and closed�?�
  
Now, the nice thing is, that if you type the command in (say Notepad) and copy and paste it in the python window, the command is executed correctly and stores the raster with the given name:

myHS = Hillshade("myDEM",315,45).save("myHS")
  
The only strange behavior is that the resulting raster is not added to the TOC (although this is indicated in the Geoprocessing options)
  
OK, so I shouldn't do this, but is this the way ArcMap should react to such a situation?
  
  
In the old days when I used to work with ILWIS, they implemented two types of command lines.
  
Output = command
and
Output := command
  
The first resulting in a virtual object which would be (re)calculated in the moment it was requested and would change if the input data would change and the latter creating the physical output raster with the name provided.
  
I wonder if it would be possible to implement something like that (meaning := creates the raster with the name indicated, using only the = sign results in a temporary dataset) .
  
Why is there a difference between arcpy commands and arcpy.sa commands? If I use the arcpy.Clip_management command I don�??t use an output = command  construction, but I am able to define the output name:

arcpy.Clip_management("myDEM","1952602 294196 1953546 296176","myClip", "#", "#", "NONE")

In the command above I am able to define the output name of the raster (it will be saved as "myClip")�?�


Kind regards,
0 Kudos