2

To start, I'm very new to scripting. On ArcGIS, there is a python script for a Sinuosity tool http://resources.arcgis.com/en/help/main/10.1/index.html#//001500000022000000

I've tried it and it didn't work and so I did some changes to it and while it runs without error, I get no output in my attribute table. What are my errors? What can I do/add to this script to get an output?

import arcpy
import math
import os

#overwrite existing output
arcpy.env.overwriteOutput = True

class Toolbox(object):
    def __init__(self):
        self.label =  "Sinuosity toolbox"
        self.alias  = "sinuosity"

        # List of tool classes associated with this toolbox
        self.tools = [CalculateSinuosity] 

class CalculateSinuosity(object):
    def __init__(self):
        self.label       = "Calculate Sinuosity"
        self.description = "Sinuosity measures the amount that a river " + \
                       "meanders within its valley, calculated by " + \
                       "dividing total stream length by valley length."

    def getParameterInfo(self):
        #Define parameter definitions

        # Input Features parameter
        in_features = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        in_features.filter.list = ["Polyline"]

        # Sinuosity Field parameter
        sinuosity_field = arcpy.Parameter(
            displayName="Sinuosity Field",
            name="sinuosity_field",
            datatype="Field",
            parameterType="Optional",
            direction="Input")

        sinuosity_field.value = "sinuosity"

        # Derived Output Features parameter
            out_features = arcpy.Parameter(
            displayName="Output Features",
            name="out_features",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")

        out_features.parameterDependencies = [in_features.name]
        out_features.schema.clone = True

        parameters = [in_features, sinuosity_field, out_features]        
        return parameters

    def isLicensed(self): #optional
        return True

    def updateParameters(self, parameters): #optional
        if parameters[0].altered:
            parameters[1].value = arcpy.ValidateFieldName(parameters[1].value,parameters[0].value)
        return

    def updateMessages(self, parameters): #optional
        return

    def execute(self, parameters, messages):
        in_features  = parameters[0].valueAsText
        fieldName   = parameters[1].valueAsText

        if fieldName in ["#", "", None]:
            fieldName = "sinuosity"

        arcpy.AddField_management(inFeatures, fieldName, 'DOUBLE')

        expression =
    def getSinuosity(shape):
        length = shape.length
        d = math.sqrt((shape.firstPoint.X - shape.lastPoint.X) ** 2 +
              (shape.firstPoint.Y - shape.lastPoint.Y) ** 2)
        return d/length

    arcpy.CalculateField_management(in_features,fieldName,'getSinuosity(!shape!)','PYTHON_9.3')

1 Answer 1

2

Look at the help page What is a Python toolbox? and then scroll to the bottom of the page and look at the code sample. Note how the the function getSinuosity which is the expression is enclosed in 3 single quotes. Your code is not and you do not include it as a parameter in the CalculateField line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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