Using arcpy to create slope surfaces

2036
12
03-19-2020 08:58 AM
JoeBorgione
MVP Emeritus

ArcGIS Pro 2.5

I have a number of .tif  and ,img images that I need to convert into slope surfaces (as discussed here Display A Slope Raster ) and have  been following the steps shown here:  Slope—Help | Documentation .

However, I get one of two errors depending on where I send the slope.save() to.  Here is my script:

import arcpy
from arcpy.sa import *
import os
 
inDir = r'C:\JoesStuff\SouthernImages'
#outWS = r'C:\JoesStuff\SouthernImages\SlopeRasters' # to a folder
outWS = r'C:\JoesStuff\SouthernImages\Rasters.gdb'  # to a gdb

for i in os.listdir(inDir): 
    if i.endswith('.tif') or i.endswith('.img'):
        inRaster = os.path.join(inDir,i)
        outFile = i.split('.')[0]
        outRaster = f'{outWS}\{outFile}Slope'
        outMeasurement = 'PERCENT_RISE'
        zFactor = 1
        method = 'PLANAR'
        zUnit = 'METER'
    
    outSlope = Slope(inRaster, outMeasurement, zFactor, method, zUnit)
    outSlope.save(outRaster)

with line 6 commented and line 7 active this error is returned:

RuntimeError: ERROR 010240: Could not save raster dataset to 
C:\JoesStuff\SouthernImages\Rasters.gdb\12TVK1276Slope with output format FGDBR.

if I try to save the slope to a directory by executing line 6 with line 7 commented  this error is returned

RuntimeError: ERROR 010240: Could not save raster dataset to 
C:\JoesStuff\SouthernImages\SlopeRasters\12TVK1276Slope with output format GRID.

I know the slope surfaces are being created, because when I comment line 20, and replace it with a print statement, the loop runs just fine.  I have run tool manually and it works just great on individual input .tif or .img.

That should just about do it....
0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

wait until you see a 4.1.1 version

0 Kudos
by Anonymous User
Not applicable

Hi Joe,

I checked out this thread and the script you created. I'm glad you were able to reach a solution.

I wanted to show you another way to script it, using arcpy.env.workspace, and a boolean variable.

Using arcpy.env.workspace, you don't need to code the full path to every image in your list.

Using a boolean variable (i.e. 'tifOut' and some 'if' logic), you only need to update the code in one place, if you wish to switch from TIF to fgdb.

Please reach out if you have questions.

Happy scripting!
Dana

# ----------------------------------------------------#
# Program: Loop through images and create Slope output
# Save outputs as either TIF or FGDB raster
# ----------------------------------------------------#

# ----------------------------------------------------#
# imports
import arcpy
from arcpy.sa import *
import os
# ----------------------------------------------------#

# ----------------------------------------------------#
# environments
arcpy.env.workspace = r'C:\JoesStuff\SouthernImages'
# ----------------------------------------------------#

# ----------------------------------------------------#
# variables: tifOut, fgdbOut, imageList
# ----------------------------------------------------#
# Set tifOut variable
# when True: output to workspace as tif
# when False: output to fgdbOut 
tifOut = False
fgdbOut = 'Rasters.gdb'

imageList = ['12TVK1276.tif',
 '12TVK1474.tif',
 '12TVK1476.tif',
 'C12TVK1674.tif',
 '12TVK1676.tif',
 '12TVK1874.tif',
 '12TVK1876.tif',
 '12TVK1877.tif',
 '12TVK1976.tif',
 '12TVK1977.tif',
 'hh12TVK1200076000.img',
 'hh12TVK1200078000.img',
 'hh12TVK1400076000.img',
 'hh12TVK1400078000.img',
 'hh12TVK1600076000.img',
 'hh12TVK1600078000.img',
 'hh12TVK1800074000.img',
 'hh12TVK1800076000.img',
 'hh12TVK1800078000.img']

# ----------------------------------------------------#
# Main:
# 1. Loop through images
# 2. Set up Slope parameters
#    a) Check if tifOut is True or False
# 3. Execute Slope tool
# 4. Save output
# ----------------------------------------------------#

# 1. Loop through images
for image in imageList:
    # 2. Set up Slope parameters
    inRaster = image
    outRaster = 'Slope' + os.path.splitext(image)[0]

    # a) Check if tifOut is True or False
    # If tifOut is True, add extension
    if tifOut == True:
        outRaster = outRaster + ".tif"
    # If tifOut is False, add fgdbOut
    else:  
        outRaster = os.path.join(fgdbOut,outRaster)
    
    outMeasurement = 'PERCENT_RISE'
    zFactor = 1
    method = 'PLANAR'
    zUnit = 'METER'
    # 3. Execute Slope tool
    outSlope = Slope(inRaster, outMeasurement, zFactor, method, zUnit)
    # 4. Save output
    outSlope.save(outRaster)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JoeBorgione
MVP Emeritus

Thanks for the tip Dana!  I vacillate between arcpy and straight up python through out my scripting: that list came from  an os.listdir() I ran on the directory where I had  stashed the originals....

That should just about do it....
0 Kudos