Select to view content in your preferred language

Syntax error using ArcPy Spatial Analyst module in Python

3100
3
12-14-2014 01:07 PM
JessicaSorrell
Deactivated User

I'm very new to using python and figure I'm missing something obvious in the following script.  I keep getting a "syntax error" message no matter how much I fiddle around with the first few lines.  I think it has something to do with calling the spatial analyst module into python while checking out the spatial analyst extension. Any help would be greatly appreciated!

#Script takes an input DEM raster and delineates watershed boundaries based on

# flow direction and accumulation

# Import arcpy module

import arcpy

from arcpy.sa import *

arcpy.env.overwriteOutput = True

# Check out the Spatial Analyst extension

arcpy.CheckOutExtension("Spatial")

#Specify the input raster

inDEM = arcpy.GetParameterAsText(0)

#Specify the input coordinate system

spatialReference = arcpy.GetParameterAsText(1)

#Specify the minimum number of cells that are assumed to represent a stream

# (cell threshold)

inMin = arcpy.GetParameterAsText(2)

Try:

    # Process: Project Raster

    arcpy.ProjectRaster_management(inDEM, projectDEM, spatialReference)

    # Process: Fill

    fillDEM = Fill(projectDEM)

    # Process: Flow Direction

    flowDir = FlowDirection(fillDEM, "NORMAL")

    # Process: Flow Accumulation

    flowAccum = FlowAccumulation(flowDir)

    # Process: Raster Calculator

    outRaster = RasterCalculator((flowAccum > inMin))

    # Process: Raster to Polyline

    arcpy.RasterToPolyline_conversion(outRaster, polyLines, "ZERO", "0", "SIMPLIFY", "VALUE")

    # Process: Feature Vertices To Points

    arcpy.FeatureVerticesToPoints_management(polyLines, lineVertices, "END")

    # Process: Watershed

    outWatershed = Watershed(flowDir, lineVertices)

    # Process: Raster to Polygon

    arcpy.RasterToPolygon_conversion(outWatershed, watershedPolygons, "SIMPLIFY", "VALUE")

# Check in the Spatial Analyst extension

arcpy.CheckInExtension("Spatial")

#error handling

except:

    arcpy.AddError("Script failed to write successfully")

    arcpy.AddMessage(arcpy.GetMessages())

0 Kudos
3 Replies
JoshuaBixby
MVP Esteemed Contributor

Posting code with syntax highlighting and numbers is useful, as well as specific error messages.  For syntax highlighting, click the 'Use advanced editor' link in the upper right corner of the question/reply.

Do you know whether the Spatial Analyst extension is being correctly checked out?  What is the return value of checking out the license?

Luke_Pinner
MVP Regular Contributor

Your Try: statement is capitalised, it should be lower case try:

DanPatterson_Retired
MVP Emeritus

# Check in the Spatial Analyst extension

arcpy.CheckInExtension("Spatial")

these 2 lines are incorrectly dedented, they should be within the try block ... also 'try' is lower case

And do format the code using the advanced editor's syntax highlighting