3

I have an ArcPy script that applies the slope function to raster files in ArcGIS. I tried running the script with two raster files in the table of contents, and the script runs, but only saves one layer and loads it into the table of contents. I think that the script is overwriting, and only saving the last file. Here is my script:

ArcPy Slope Script

1
  • 1
    you didn't put anything inside the parentheses. I think it should be outSlope.save(outputLayer) after outputLayer = "C:\DEM Files\SOMENAME" Oct 29 '14 at 18:45
1

How about this ... EDIT: This actually works now. It adds both layers at the bottom.

import arcpy
from arcpy import env
from arcpy.sa import *
#comment
env.workspace = "C:\DEM Files"
outWorkspace = "C:\DEM files\OUTPUT"
MXD = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(MXD, "Layers")[0]
# Not sure what this line I skipped is doing for you
# rasters = arcpy.mapping.ListLayers(MXD, "", df) #don't do this
rasters = arcpy.ListRasters("*", "TIF")
n=0
for layer in rasters:
    n+=1
    # took out inRaster variable and used layer directly
    outMeasurement = "PERCENT_RISE"
    zFactor = 1
    outSlope = Slope(layer, outMeasurement, zFactor)
    outLayer = str(outWorkspace) + "\\SLP" + str(n)
    outSlope.save(outLayer)
    addLayer = arcpy.mapping.Layer(outLayer)
    arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
MXD.save()
del MXD
8
  • I received the message: "Parsing error SyntaxError: invalid syntax (line 7)" (referring to rasters = arcpy.mapping.ListLayers(MXD, "", df))
    – amoore9984
    Oct 29 '14 at 19:11
  • I was just re-typing the code from your screenshot. Try changing that line to this (from your previous question ;-) rasters = arcpy.ListRasters("*", "TIFF") Oct 29 '14 at 19:16
  • Unfortunately, I'm still getting the same message referring to that same line.
    – amoore9984
    Oct 29 '14 at 19:22
  • Ok well I needed to close parentheses on the line with df = arcpy.mapping.ListDataFrames(MXD, "Layers")[0] Perhaps that was the line with the problem. I'm not getting any error on the line with ListRasters in it! Try it now. Oct 29 '14 at 19:35
  • It didn't do anything at all this time. It didn't run and it didn't give me any error messages.
    – amoore9984
    Oct 29 '14 at 19:39

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.