Took the Virtual Campus Creating Python Scripts for Raster Analysis, and the exercise shows me how to enter raster processing steps in the ArcGIS Pro python window, then at the end of the exercise, how to save the window contents to a python file so I could run it in a IDE or tool. I opened the file in IDLE, edited it, tried to run it, but the line "slopeRaster2 = sa.Slope(baseRaster,'DEGREE')" kept blowing up on me. I got it to work by changing the Slope line to arcpy.sa.Slope instead. It seemed to me that in IDLE, Slope might have been going to Slope_3d by default which has two required arguments, inraster and outraster, while sa.Slope only has one required argument. "DEGREE" wasn't working in Slope_3D which thought it was supposed to be the outraster.
The script exported from the exercise:
# coding: utf-8 import arcpy from arcpy import * baseRaster = Raster('DEM_elkhorn.tif') baseRaster.maximum # 2856.9016113281 baseRaster.minimum # 2174.0673828125 baseRaster.noDataValue # -3.4028230607370965e+38 slopeRaster = Slope('DEM_elkhorn.tif', 'DEGREE') # Traceback (most recent call last): # File "<string>", line 1, in <module> # NameError: name 'Slope' is not defined from arcpy.sa import * slopeRaster = Slope('DEM_elkhorn.tif', 'DEGREE') gThan40 = slopeRaster > 40 cliff = baseRaster - baseRaster.mean cliffPresent = arcpy.sa.GreaterThan(cliff,160) FalconHabitat = gThan40 & cliffPresent FalconHabitat.save("C:\\EsriTraining\\PythonScriptsRaster\\Data\\HabitatOutput.tif")
After editing a bit to clean up:
import arcpy from arcpy import * from arcpy.sa import * import arcpy.sa baseRaster = Raster("C:\\EsriTraining\\PythonScriptsRaster\\Data\\DEM_elkhorn.tif") #slopeRaster = Slope('C:\\EsriTraining\\PythonScriptsRaster\\Data\\DEM_elkhorn.tif', 'DEGREE') slopeRaster2 = sa.Slope(baseRaster,'DEGREE') gThan402 = slopeRaster > 40 cliff2 = baseRaster - baseRaster.mean cliffPresent2 = arcpy.sa.GreaterThan(cliff,160) FalconHabitat2 = gThan40 & cliffPresent #FalconHabitat2.save("C:\\EsriTraining\\PythonScriptsRaster\\Data\\HabitatOutput2.tif")
I would have thought that the lines 1-4 trying every which way to import sa would have been sufficient, but line 6 kept blowing up, and going to line 7 using sa.Slope still wasn't good enough.
But this worked:
# coding: utf-8 #HabitatScriptEd3.py #Created on 8/03/2018 by PH #From exercise in ESRI Virtual Campus Course Creating Python Scripts for Raster Analysis import arcpy from arcpy import * from arcpy.sa import * arcpy.CheckOutExtension("spatial") baseRaster = Raster("C:\\EsriTraining\\PythonScriptsRaster\\Data\\DEM_elkhorn.tif") #slopeRaster = Slope('C:\\EsriTraining\\PythonScriptsRaster\\Data\\DEM_elkhorn.tif', 'DEGREE') slopeRaster = Slope(baseRaster, 'DEGREE') #slopeRaster2 = arcpy.sa.Slope(baseRaster,'DEGREE') gThan402 = slopeRaster > 40 cliff2 = baseRaster - baseRaster.mean cliffPresent2 = arcpy.sa.GreaterThan(cliff2,160) FalconHabitat2 = gThan402 & cliffPresent2 #FalconHabitat2.save("C:\\EsriTraining\\PythonScriptsRaster\\Data\\HabitatOutput2.tif")
So apparently, the three imports aren't enough to make sa available in a script running outside the Python window. I also need to check out the spatial extension.
Along the way, I learned there is a different Python version installed for ArcMap 10.6.1 (Python 2.7.14) and for ArcPro 2.2 (Python 3.6.5). You can find the two different versions of IDLE and run the one that is appropriate by searching way down in Program files for idle.pyw. But an easy way to launch the correct version of IDLE is to examine the folder containing the script in File Explorer, right click, and there is a choice provided on which version to use. https://www.dropbox.com/s/z22101a2149z33x/pythonIDLE.png?dl=0
But it turns out with the full name to the Slope method, the script will run in python 2.7.14, the version from ArcMap 10.6.1. https://www.dropbox.com/s/t9utofy3fydra42/Python27.PNG?dl=0
I have SA licensed in Pro with a local license server, concurrent licensing. https://www.dropbox.com/s/kbvazf3vzrmm2mr/Screenshot%202018-08-01%2010.58.42.png?dl=0 But how does python in IDLE know this?
I was real hopeful that http://pro.arcgis.com/en/pro-app/arcpy/functions/checkoutextension.htm was the answer since that's exactly my situation. But I got the same error as before. It seems that sa must be getting loaded because print (baseRaster.maximum) returns an answer. The error message seems indicate that python thinks Slope is a variable name that python doesn't know about. But it is not that Slope shouldn't be capitalized.