Using arcpy to create slope surfaces

2086
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

the other thread isn't closed

*.tif is the preferred raster format, so the destination of the raster should be a compliant folder path

0 Kudos
JoeBorgione
MVP Emeritus

As mentioned, when I run the tool manually, all is good:

Note the values used by default in the symbology:  I'm not sure what was going on when I created the slope surfaces mentioned in the other thread getting the values that were nonsensical (to me).  These values are what I expect:

Looks like DansSlope is a good ol' FGDBR type, right?

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

*tif is all I use for rasters, I don't even use esri grids anymore and the last thing I would want is for my work to be tied up in a fgdb if I had to use it somewhere else that can't read fgdb .  With *.tif, I can look at in image software, process it in python, numpy etc.  even esri recommends tif (buried in the help,). 

symbology is after the fact.

0 Kudos
JoeBorgione
MVP Emeritus

Let me give that a try: slope.save(C:\path\to\DansSlope.tif), right?

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

the reason the "grid" format failed... you can't have esri grids begin with a number

SteveLynch
Esri Regular Contributor

...and a "grid" name cannot exceed 13 characters.

JoeBorgione
MVP Emeritus

Okay: got it wired using both of your suggestions.  Here's the final 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


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


for i in enumerate(imageList): 
    inRaster = i[1]
    outName = f'Slope{i[0]}'  # f'Slope{i[0]}.tif'
    outRaster = f'{outWS}\{outName}'
    outMeasurement = 'PERCENT_RISE'
    zFactor = 1
    method = 'PLANAR'
    zUnit = 'METER'
    
    outSlope = Slope(inRaster, outMeasurement, zFactor, method, zUnit)
    outSlope.save(outRaster)

I read an article earlier today suggesting enumerate() instead of range() in a for loop and went that route. Line 33

can go to an FGDB grid or TIF image depending on which line (6 or 7) is executed.  

So in essence you both answered my question so I'll let you duke it out for bragging rights and just assign it 'Assumed Answered'...  

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

In anticipation of your next question....

Then I suppose I shouldn't tell you what happens if you do the spyder 4.1 update and let you figure it out on your own

0 Kudos
JoeBorgione
MVP Emeritus

Actually the laptop I'm using has spyder 3.3 on it since I just went with what ships with Pro 2.5; remember I'm home  practicing good social distancing.  My desktop back at the office has a spyder 3.3 env as well as spyder 4.0 env; I have some packages I use that would have downgraded the 4.0 env so now I have two.  Sounds like I may have three once I'm back there..... 

(you're not even gonna give me a hint?)

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