raster calculator attribute error 'str' object has no attribute 'save'

5971
2
Jump to solution
08-02-2012 01:35 PM
JoannaWhittier
New Contributor III
I am multiplying several .tif files by set values and adding the results together using raster calculator in arcgis 10.1.  If I manually load the files into raster calculator, the calculation completes.  Unfortunately I need to do this several hundred times so I would like to automate the process.  I created a model in model builder (and it worked) and then exported the code to python to see the format. 

Here is that coding
# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")


# Local variables:
RT_v4_2027_0_albers_tif = "RT_v4_2027_0_albers.tif"
RT_v4_2027_1_albers_tif = "RT_v4_2027_1_albers.tif"
RT_v4_2027_2_albers_tif = "RT_v4_2027_2_albers.tif"

rt_sum_2027_100_tif = "Y:\\RT_annual\\rt_sum_2027_100.tif"

# Process: Raster Calculator
arcpy.gp.RasterCalculator_sa("((\"%RT_v4_2027_0_albers.tif%\" * 31) + (\"%RT_v4_2027_1_albers.tif%\" * 28) + (\"%RT_v4_2027_2_albers.tif%\" * 31)) * 100", rt_sum_2027_100_tif)

As a first step toward automating I then tried to calculate the above for the next set of files using this code:
# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")


# Local variables:
RT_v4_2028_0_albers_tif = "Y:\\RT_albers\\RT_v4_2028_0_albers.tif"
RT_v4_2028_1_albers_tif = "Y:\\RT_albers\\RT_v4_2028_1_albers.tif"
RT_v4_2028_2_albers_tif = "Y:\\RT_albers\\RT_v4_2028_2_albers.tif"
rt_sum_2028_100_tif = "Y:\\RT_annual\\rt_sum_2028_100.tif"

arcpy.gp.RasterCalculator_sa("((\"%RT_v4_2028_0_albers_tif%\" * 31) + (\"%RT_v4_2028_1_albers_tif%\" * 28) + (\"%RT_v4_2028_2_albers_tif%\" * 31)) * 100", rt_sum_2028_100_tif)

I get the following error whether I run the script in the ArcGIS python window or in IDLE:

Runtime error  Traceback (most recent call last):   File "<string>", line 24, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda>     return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: ERROR 000539: Error running expression: rcexec()  Traceback (most recent call last):   File "<expression>", line 1, in <module>   File "<string>", line 8, in rcexec AttributeError: 'str' object has no attribute 'save'  Failed to execute (RasterCalculator).

Why does this script seem to be looking for an attribute called 'save'?  Any suggestions would be greatly appreciated.
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
arcpy.gp.RasterCalculator_sa("((\"%RT_v4_2028_0_albers_tif%\" * 31) + (\"%RT_v4_2028_1_albers_tif%\" * 28) + (\"%RT_v4_2028_2_albers_tif%\" * 31)) * 100", rt_sum_2028_100_tif)


The Raster Calculator tool is designed for use in ModelBuilder only.

For best results, if you are going to do this in Python you should rewrite the expression using Python map algebra.


# Import arcpy module import arcpy from arcpy.sa import *  # Check out any necessary licenses arcpy.CheckOutExtension("spatial")  # Local variables: RT_v4_2027_0_albers_tif = "RT_v4_2027_0_albers.tif" RT_v4_2027_1_albers_tif = "RT_v4_2027_1_albers.tif" RT_v4_2027_2_albers_tif = "RT_v4_2027_2_albers.tif"  rt_sum_2027_100_tif = "Y:\\RT_annual\\rt_sum_2027_100.tif"  rast = (Raster(RT_v4_2027_0_albers_tif) * 31.0 +\   Raster(RT_v4_2027_1_albers_tif) * 28.0 +\   Raster(RT_v4_2027_2_albers_tif)) * 100 rast.save(RT_v4_2027_2_albers_tif)

View solution in original post

0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor
arcpy.gp.RasterCalculator_sa("((\"%RT_v4_2028_0_albers_tif%\" * 31) + (\"%RT_v4_2028_1_albers_tif%\" * 28) + (\"%RT_v4_2028_2_albers_tif%\" * 31)) * 100", rt_sum_2028_100_tif)


The Raster Calculator tool is designed for use in ModelBuilder only.

For best results, if you are going to do this in Python you should rewrite the expression using Python map algebra.


# Import arcpy module import arcpy from arcpy.sa import *  # Check out any necessary licenses arcpy.CheckOutExtension("spatial")  # Local variables: RT_v4_2027_0_albers_tif = "RT_v4_2027_0_albers.tif" RT_v4_2027_1_albers_tif = "RT_v4_2027_1_albers.tif" RT_v4_2027_2_albers_tif = "RT_v4_2027_2_albers.tif"  rt_sum_2027_100_tif = "Y:\\RT_annual\\rt_sum_2027_100.tif"  rast = (Raster(RT_v4_2027_0_albers_tif) * 31.0 +\   Raster(RT_v4_2027_1_albers_tif) * 28.0 +\   Raster(RT_v4_2027_2_albers_tif)) * 100 rast.save(RT_v4_2027_2_albers_tif)
0 Kudos
JoannaWhittier
New Contributor III
Thank you!  I suspected that it was something simple.
0 Kudos