Spatial - Map Algebra and Raster Calculator

1551
15
11-09-2009 05:54 PM
SteveKopp
Esri Contributor
Several threads have touched on the changes to the Spatial Analyst Map Algebra and disappearance of the Raster Calculator. For all you who love Map Algebra and the Raster Calculator, the team agrees with you! The changes we have made to the Map Algebra language to make it integrate well with Python do change the language, but we feel it still looks and behaves enough like Map Algebra that you???ll feel the benefit is worth it.

There are two Map Algebra experiences, the scripting experience and the raster calculator experience. Improvements to the language were made to improve the scripting experience. Having Map Algebra tightly integrated with our native scripting environment, Python, has not been possible except in Workstation when we wrote our own verison of both. Python provides a rich set of capabilities for you to take advantage of and integrate with our out of the box tools. We believe that Python will be the language of GIScience and this integration will play a key role. A feature you will see in beta2 that compliments this is  the ability to move blocks of pixels back and forth between ArcGIS rasters and Python arrays, allowing you to write your own raster analysis tools.

The other side of Map Algebra is the raster calculator experience. We know that the Python window is not a substitute for the point and click experience and direct output of the raster calculator. We plan to build a calculator-like tool that will provide the point and click experience but also be something that you can drop into a model. Envision a combination of the old raster calculator and the SOMA tool. Unfortunately it won???t make it into beta2.

And while I have your attention I???d like to suggest that if you have colleagues who seldom use Spatial Analyst, don???t make them learn Map Algebra. Show them where to find the tools and run it from the dialogs, or better yet build them a little ModelBuilder model with a couple parameters exposed and hide all the details.

Steve
0 Kudos
15 Replies
WentaoChe
Occasional Contributor II
I have used the following Algebra in 9.2, it worked perfectly.
   h_grd = color2hue(tmp_grd, rgb.txt)

But it did not work in 9.3.1. like this:
  gp.MultiOutputMapAlgebra_sa("h_grd = color2hue(tmp_grd, rgb.txt)")
I can still make it work in 9.3.1 ArcInfo GRID

How to make it work in 9.4? I appreciate your help.

I use GRID daily, but I want to shift my AML to Python, and this color2hue stops my work.
0 Kudos
WentaoChe
Occasional Contributor II
I wrote in the last post that gp.MultiOutputMapAlgebra_sa("h_grd = color2hue(tmp_grd, rgb.txt)") does not work. Now I know the reason.

I need to set absolute path for the output grid like this in Python:

inpath = "d:\\test"
h_grd =  inpath + os.sep + 'h_grd'
gp.MultiOutputMapAlgebra_sa(h_grd + " = color2hue(tmp_grd, rgb.txt)")

This is also true in 9.4.

I am glad to know that gp still works in 9.4.
0 Kudos
TedCronin
MVP Honored Contributor
Steve -

Whats Coming in Beta 2 for python and gp?
0 Kudos
DavidWynne
Esri Contributor
Hi Ted,
For Python, continued fixes and polishing the experience.  There are a couple of things that I'd like to specific note:

  • Exception classes for ArcPy: Dale wrote about these on the arcgisscripting side on the geoprocessing blog a year ago, but they've always been a little hidden.  They also were not exposed on arcpy with Beta 1.  At Beta 2, they'll be part of arcpy and more obvious.

  • Steve touched on this, but also new functions that will allow you to convert rasters to and from numpy arrays.  These functions should really open up a lot of modeling opportunities.


-Dave
0 Kudos
TedCronin
MVP Honored Contributor
Yep, I remember that post, just did not read it, saw try except (which I use), and moved on, but I see now about the result objects, so I will need to re read, whats happening on the blog side of gp.  Perhaps a blog post about all the new MB iterators would be nice.  C'mon Ash.
0 Kudos
CamPatterson
New Contributor II
import os,sys,string
import arcpy
from arcpy.sa import *

# this throws an error
>>> outraster1 = Raster('DEM2_Sept09') - Raster(1)
Runtime error <type 'exceptions.RuntimeError'>: HRESULT(E_FAIL): Unspecified error

# this works
>>> outraster1 = Raster('DEM2_Sept09') - 1

#this crashes ArcMap
>>> outraster1 = Raster(1)
0 Kudos
RyanDeBruyn
Esri Contributor
Thank you for your post. Please note that this is a known issue and currently a change request as been submitted. It will be fully supported for 9.4 final.

Casting a constant value in a raster object is similar to creating a constant raster with a defined cellsize and extent.

When used in conjuction with a spatial tool you do not need to cast the number as a raster because the properties of the raster are determined from the output from the tool and the value is simply added to the result.

out = Slope(input) + 1


To create a raster from a constant it will require the user to specify the environment cellsize and extent or an error will occur.

arcpy.env.cellsize = 1
arcpy.env.extent = "0 0 100 100"
out = Raster(1)


-Ryan DeBruyn
ESRI Spatial Analyst Team
0 Kudos
curtvprice
MVP Esteemed Contributor
Hi Ted,
For Python, continued fixes and polishing the experience.  There are a couple of things that I'd like to specific note:

  • Exception classes for ArcPy: Dale wrote about these on the arcgisscripting side on the geoprocessing blog a year ago, but they've always been a little hidden.  They also were not exposed on arcpy with Beta 1.  At Beta 2, they'll be part of arcpy and more obvious.


-Dave


That blog post was very helpful. I used it to build a little python module to help developing script tools, so now with a small template set up I can easily have my scripts tell me where they fail.
0 Kudos
ThomasHack1
New Contributor
Hi there,

since the Map Algebra Toolset is empty in 9.4 beta I wonder how I can use any Map Algebra in a model without having the SOMA Tool?

Regards

Thomas Hack
0 Kudos