Saving rasters in Arcpy loop

1920
3
08-09-2018 11:52 AM
RaeFuhrman
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)                                    
                                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')) already and that didn't work either. 
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

Rae... could you format your code using python syntax highlighting to make it readable

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
DanPatterson_Retired
MVP Emeritus

a duplicate of https://community.esri.com/thread/219450-arcpy-loop-not-saving-rasters 

just the OP name is different

0 Kudos
SharolynAnderson1
New Contributor II

Sorry no solution but I have the same problem:

i = 0
#loop through rasters in list
for ir in rrings:
    #sum rasters together
    if i == 0:
      ringSUM = arcpy.Raster(ir)
      i += 1
   else:
      ringSUM = ringSUM + arcpy.Raster(ir)
      i += 1

#save final output to the current dirctory
ringSUM.save('C:/SALR_0801/wksp/ringSUMall.tif')

ERROR:

Traceback (most recent call last):
File "C:\SALR_0801\fixCrashSave.py", line 87, in <module>
ringSUM.save('C:/SALR_0801/wksp/ringSUMall.tif')
RuntimeError: ERROR 999998: Unexpected Error.

Sorry that the code is not in  python syntax did not copy optimally.   The thing for me and wondering if for others is that it seems the raster.save() doesn't work no matter what I have tried.   (using variable with path&file, changing to GRID even though I want a TIF, etc).

 

I want to understand this as I have several other programs to modify and can't seem to get a generic solution.  I have lots of disk space and memory running ArcGIS 10.6

Thanks.

0 Kudos