Select to view content in your preferred language

Can I see the Python Script involved in Map Algebra / Raster Calculator

831
2
10-26-2013 04:09 AM
MichaelHammond
Emerging Contributor
I am a beginner to Python in ArcGIS and would like to perform a calculation on a raster that's a bit complicated than what Raster Calculator can do. I know the python code for my expression, so I thought I could do a simple function in Raster Calculator (e.g Multiply by 2.0) and then see the python code for that, and modify as required.

Can I do this, and if so, how can I get to see the python script involved in this?

Thanks in advance
0 Kudos
2 Replies
XanderBakker
Esri Esteemed Contributor
I am a beginner to Python in ArcGIS and would like to perform a calculation on a raster that's a bit complicated than what Raster Calculator can do. I know the python code for my expression, so I thought I could do a simple function in Raster Calculator (e.g Multiply by 2.0) and then see the python code for that, and modify as required.

Can I do this, and if so, how can I get to see the python script involved in this?

Thanks in advance


Hi Michael,

Yes you can. Below you see a visual explanation of how it's done:

[ATTACH=CONFIG]28638[/ATTACH]

You execute your simple Raster Calculator expression. After execution you can open the Results window (Geoprocessing menu, Results). When you right click on the Raster Calculator a menu appears where you can select the option "Copy As Python Snippet". This gives you the Python code. You can change this and for instance execute the new statement from the Python window.

Now here's why you shouldn't do that:
If you look at the syntax there are a lot of quotes (three at the start, three at the end of the expression and around each raster). This makes the statement more complex than actually needed. The same thing goes for using the field calculator, which makes an expression a lot more complex that simply using a cursor to update a field (just have a look at all the threads discussing the proper syntax for field calculator)...

You can also do this in a more simplistic way, all from Python. In this case I assume all the rasters you need are in the TOC, you have Spatial Analyst extension switched on, your environment setting are all set and you will work with the Python window.

Enter the following code:

import arcpy
myInputRaster = arcpy.Raster("theNameOfMyRasterInTheTOC")
myResult = myInputRaster * 2
myResult.save("theNameOfMyOutputRaster")


With this code you:

  • import the arcpy library

  • you define a raster object based on the name or your raster in the TOC "theNameOfMyRasterInTheTOC"

  • You calculate the result (change the expression)

  • you save the resulting raster with the name "theNameOfMyOutputRaster" to you current workspace


You could actually use the tool "Times" to multiply by 2:
arcpy.sa.Times("theNameOfMyRasterInTheTOC",2)


... but that will make complex expressions more complex to write. That's why I recommend you to use "arcpy.Raster()" to create a raster object, which enables you to use "+,-,/,*" etc

If you want to share the expression you wish to execute I can give it a look and provide you with a small Python snippet.

Kind regards,

Xander
0 Kudos
curtvprice
MVP Alum
The help has many good examples of how to do raster calculations in ArcPy map algebra.

I agree with Xander that the syntax is much easier to work directly than using the Raster Calculator tool.

Desktop 10.2 help: A quick tour of using Map Algebra
http://resources.arcgis.com/en/help/main/10.2/index.html#//00p600000003000000
0 Kudos