2

I'm trying to transform a bunch of rasters in arcpy according to specific functions to create suitability rasters. This portion of the code is supposed to find all the rasters with interpolated temperature data and transform each as follows:

[0-14] --> 1, [14-23] --> decrease linearly from 1 according to the function, [23-31] --> 0.

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 rasters
for raster in aprx.listMaps()[0].listLayers():
    if raster.isRasterLayer:
        if 'Temp' in raster.name:
            if 'Int_Raster' in raster.name:

                # Set local variables
                thresholds = [14, 23]
                bounds = [31.8824, 0]
                scale = [1, (((-0.7937*thresholds[1])+25.3051)/((-0.7937*thresholds[0])+25.3051))]
                transformation = TfLinear( bounds[0], bounds[1], thresholds[0], 1, thresholds[1], 0 )

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

                # Save the rescaled raster
                name = str(raster).replace('Int_Raster','Resc_Raster')
                rescaledRaster.save(arcpy.env.scratchWorkspace + "\\" + name)
                print(rescaledRaster.name)

However, this script only creates a single raster called "rescaledRaster" rather than a bunch called "Temp_[identifier]_Resc_Raster", which it's supposed to. It hits the print statement correctly each time so I'm not sure why it isn't creating the individual rasters. I'm using other transformations like TfLogarithm and TfGaussian with other criteria so unfortunately Fuzzy Linear Membership isn't a good option.

EDIT: I have discovered that the single raster the script creates is simply the final raster it's supposed to make, which implies that the previous rasters are getting overwritten rather than saved. I'm not sure why this is the case or why it's still named wrong. Why aren't the rasters saving correctly?

  • 2
    Shouldn't name = str(raster)... be name = str(raster.name)...? – Bjorn Aug 2 '18 at 21:31
  • Welcome to GIS SE! As a new user be sure to take the Tour where you will see that there should be only one question asked per question under our focussed Q&A format. – PolyGeo Aug 2 '18 at 22:06
  • It's hard to tell without seeing your raster names, @Bjorn is right though your object is a layer not a raster. Beware though python is case sensitive so all comparisons between strings should be in either .uppper() or .lower() case - including your replace. If your raster has 'Int_raster' in its name that will not be replaced because it's not the same as 'Int_Raster'; it's possible that only one of your rasters has the exact case required by your if/replace statements. – Michael Stimson Aug 2 '18 at 22:43
  • @MichaelStimson: The case has nothing to do with it. The code hits the print statement correctly each time, which means that it is saving the rescaledRaster with the correct name but then not displaying it in ArcGIS. Perhaps the fact that it's a layer rather than a raster object has something to do with it? I've tried explicitly casting the layer as a raster but it makes no difference. – Joel Salzman Aug 3 '18 at 21:21
  • @Bjorn: Yes it should be! Sadly, changing that didn't fix the problem. – Joel Salzman Aug 4 '18 at 0:02

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.