2

I am trying to multiply each raster by coefficient using Map Algebra in Python. I receive the following error for the code below:

calcRaster.save(calcRasterName) AttributeError: 'unicode' object has no attribute 'save'

import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = r"C:\SomeFolder"
outFolder = r"C:\SomeFolder"

arcpy.CheckOutExtension("Spatial")

rasters = arcpy.ListRasters()
for inRaster in rasters:
    calcRaster = inRaster * 720
    calcRasterName = outFolder + "/" + calcRaster
    calcRaster.save(calcRasterName)

Based on some searching, I redefined the calcRaster variable to:

  calcRaster = (Raster(inRaster) * 720)

However, I receive some comments and a new error:

File "C:\Users\rrotz\Desktop\Pycharm\RasterCalculator.py", line 13, in calcRasterName = outFolder + "/" + calcRaster

File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 4329, in Plus in_raster_or_constant2)

File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper result = wrapper(*args, **kwargs)

File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 4326, in Wrapper ["Plus", in_raster_or_constant1, in_raster_or_constant2])

RuntimeError: ERROR 000732: Input Raster: Dataset S:\Group Projects\HDF5\2 Tiff Data\8 Monthly Scaled/ does not exist or is not supported

The last RunTimeError is regarding the outfolder path that I designated for the final calculated rasters.

2 Answers 2

2

calcRaster is a temporary raster, not a text string. You will need to find another way of naming your result, like outRasterName = '{}720'.format(inRaster).

Python has tools for creating complete path names from path fragments (folder & file names) use os.path.join like this: CalcRasterName = os.path.join(outFolder, outRasterName).

2
  • Thanks Bjorn, I edited the code am still getting an error: outRasterName = '{}720'.format(inRaster) calcRasterName = os.path.join(outFolder, outRasterName) outRasterName.save(calcRasterName) AttributeError: 'str' object has no attribute 'save' What am I doing wrong? Commented Jul 9, 2018 at 22:02
  • You still want to save calcRaster which has the value (arcpy.Raster(InRaster) * 720), so it should be calcRaster.save(CalcRasterName). Beware the file extension! if your input raster is myRaster.tif your CalcRasterName = myRaster.tif720 which isn't a recognized extension. Use FileName, FileExt = os.path.splitext(inRaster) then CalcRasterName ='{}_720{}'.format(FileName,FileExt) to make the new name MyRaster_720.tif in this example. Commented Jul 9, 2018 at 23:53
1

I was able to get the code working using the following approach.

import arcpy,os,sys,string
from arcpy import env
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

input_folder = r'SomeFolder'
arcpy.env.workspace = input_folder
output_folder = r'SomeFolder'

#Set reflectance constant
const = 672

# Get raster list, loop through and multiply be constant
raster_list = arcpy.ListRasters()
for inRaster in raster_list:
      print inRaster
      rast_mult = Raster(inRaster) * const 
      raster_mult_out = output_folder + os.sep + inRaster + ".tif"
      rast_mult.save(raster_mult_out)

I'll post another version as advised above shortly. Thanks all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.