1

Why does the slope tool run fine until I use a user input to set the workspace?

Background: Below you will see the code that works fine at the top preceded by ##. When I change the workspace to be defined by a user input as shown beneath that, it no longer works. This is part of a larger script. In the larger script the workspace is set with the user input and it works fine with mosaic to new raster, project raster, and clip raster, but then it errors on the slope tool. I am now testing in the shorter script shown below so I don't have to wait for it to run each time.

I have tried changing drives, shortening the path, I switched everything from Esri Grid to tif because of the naming limitations, I tried a shorter name for the output raster.

##import arcpy
##from arcpy.sa import *
##arcpy.env.workspace = r'E:\My_Workspace\Processed_Products'
##
##
##
##if arcpy.CheckExtension('spatial') == 'Available':
##    arcpy.CheckOutExtension ('spatial')
##    outraster = Slope('DEM_Clip.tif',"PERCENT_RISE",1)
##    outraster.save('DEM_Slope1.tif')
##    print "The slope dataset has been generated"
##else:
##    print 'spatial analyst not available'

import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.overwriteOutput = True

ws = raw_input('Please paste the workspace path here. ')
arcpy.env.workspace = "r" + "'" + ws + "'"

print " ws is " + ws

DEM_Clip = ws + "\\Processed_Products\\DEM_Clip.tif"

print "The file below has been created and it is in the Processed_Products folder"
print DEM_Clip

if arcpy.CheckExtension('spatial') == 'Available':
    arcpy.CheckOutExtension ('spatial')
    outraster = Slope(DEM_Clip,"PERCENT_RISE",1)
    outraster.save('DEM_Slope1.tif')
    print "The slope dataset has been generated"
else:
    print 'spatial analyst not available'

arcpy.CheckInExtension("spatial")

Output:

Please paste the workspace path here. E:\My_Workspace
 ws is E:\My_Workspace
The file below has been created and it is in the Processed_Products folder
E:\My_Workspace\Processed_Products\DEM_Clip.tif

Traceback (most recent call last):
  File "G:\GIS_Programming\488_Data\Lecture_10_Scripts\Lecture_10_4.py", line 33, in <module>
    outraster = Slope(DEM_Clip,"PERCENT_RISE",1)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Functions.py", line 6054, in Slope
    z_factor)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
    result = wrapper(*args, **kwargs)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Functions.py", line 6049, in Wrapper
    z_factor)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 000875: Output raster: r'E:\My_Workspace'\Slope_DEM_Cl1's workspace is an invalid output workspace.
ERROR 000581: Invalid parameters.
Failed to execute (Slope).
2

Your problem is in setting your arcpy.env.workspace. Your code says

arcpy.env.workspace = "r" + "'" + ws + "'"

which is producing a path of "r'E:\My_Workspace'" which is a string that contains quoted text. You want it to instead look like r'E:\My_Workspace' (note no extra quote marks)

Your workspace should just be

arcpy.env.workspace = ws
  • Thank you, that fixed the problem. I don't understand why it worked as it was written with all the other tools though. – GeographyNerd Feb 17 '17 at 20:09
  • @GeographyNerd I'd be surprised if it did work as is, but rather it probably fixed it elsewhere in the scripts – Midavalo Feb 17 '17 at 20:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.