ArcPy loop not saving rasters

1215
4
08-10-2018 02:11 PM
JoelSalzman
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 arcpy.sa import *
arcpy.env.workspace = r"[abspath]"
arcpy.env.scratchWorkspace = r"[abspath]\rescale.gdb"
aprx = arcpy.mp.ArcGISProject(r"[abspath]\filename.aprx")

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

                                # 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 + "\\" + raster.name.replace('Int_Raster','Resc')

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

                                # Save the rescaled raster
                                rescaledRaster.save(outName)    #NOT WORKING!!!                                
                                print(rescaledRaster.isTemporary)                                    
                                print(rescaledRaster.name)
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, raster.name.replace('Int_Raster','Resc')) as well and that didn't work either. 
0 Kudos
4 Replies
DanPatterson_Retired
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
JoelSalzman
New Contributor

Am I not already saving 'rescaledRaster' with rescaledRaster.save(outName)? I've tried the following as well, which is what I think you're suggesting:

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

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

arcpy.env.overwriteOutput=True
if arcpy.Exists(outName):
        arcpy.Delete_management(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
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
SharolynAnderson1
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