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
    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

The print statements seem correct each time; .isTemporary comes out False and .name returns the outName. Despite this, the output rasters keep getting overwritten.

  • Try with os.pah.join: outName = os.path.join(arcpy.env.scratchWorkspace, raster.name.replace('Int_Raster','Resc')). – BERA Aug 7 '18 at 6:51
  • @BERA: Unfortunately the output remained the same. – Joel Salzman Aug 7 '18 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.