Select to view content in your preferred language

ArcPy loop not saving rasters

08-10-2018 02:11 PM
New Contributor
I'm trying to do Map Algebra on a bunch of rasters by iterating through a for loop and saving them in an empty geodatabase. However, they aren't saving properly. Only the last raster is created and it is named "rescaledRaster" rather than the outName. Here's the code:
import arcpy, os
from import *
arcpy.env.workspace = r"[abspath]"
arcpy.env.scratchWorkspace = r"[abspath]\rescale.gdb"
aprx ="[abspath]\filename.aprx")

# Select correct interpolated rasters
for lyr in aprx.listMaps()[0].listLayers():
        if lyr.isRasterLayer:
                if 'Temp' in
                        if 'Int_Raster' in

                                # Set local variables
                                raster = Raster(lyr.dataSource)
                                thresholds = [14, 23]
                                bounds = [14, 23]
                                scale = [1, 0.496714]
                                transformation = TfLinear( bounds[0], bounds[1], thresholds[0], 1, thresholds[1], 0 )

                                outName = arcpy.env.scratchWorkspace + "\\" +'Int_Raster','Resc')

                                # Rescale the raster
                                rescaledRaster = RescaleByFunction( raster, transformation, scale[0], scale[1] )

                                # Save the rescaled raster
                          #NOT WORKING!!!                                
The print statements seem correct each time; .isTemporary comes out False and .name returns the outName. Despite this, the output rasters keep getting overwritten.
I also tried outName = os.path.join(arcpy.env.scratchWorkspace,'Int_Raster','Resc')) as well and that didn't work either. 
0 Kudos
4 Replies
MVP Emeritus

save is not saving the 'raster', you should be saving 'rescaledRaster'

rather than trying to fiddle around with the name before you process it... rename it after it is saved

Rename—Data Management toolbox | ArcGIS Desktop 

0 Kudos
New Contributor

Am I not already saving 'rescaledRaster' with I've tried the following as well, which is what I think you're suggesting:

name ='Int_Raster','Resc')
arcpy.Rename_management(rescaledRaster, name)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've done it with and without the following addition beforehand, as per this thread.

if arcpy.Exists(outName):

I get the following error message regardless:

arcgisscripting.ExecuteError: ERROR 999999: Error executing function.
The table already exists. [name]
Failed to execute (Rename).‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

("name" is the actual name, not the variable). I cannot do

arcpy.Rename_management(rescaledRaster, outName)‍‍‍‍‍

 or else I get this:

ERROR 000670: output Output Data is same as input Input Data Element
0 Kudos
MVP Emeritus

Throw in some print statements to make sure that the name is correct. I can't tell from the code

0 Kudos
New Contributor II

Hello All -- I still have the same problem and wondering if anyone has solved the issue with .save throwing the RuntimeError: ERROR 999998: Unexpected Error..  

0 Kudos