2

I have a program which should count the number of a feature class or the length of a feature class depending on the geometry of the input feature class. This part is working. There are two parameter-fields. One dropdown-field (optional) where you can choose a fieldname of the feature class and the other is a parameter field (optional) where you can choose a value depending on the fieldname which was chosen. If I put in values in both optional parameter fields, the number or the length of the feature class should be count by using the inputvalues in a SQL-expression (whereClause). If I am trying to do that, I get following error: IndexError: tuple index out of range in line:

whereClause = whereClause.format(arcpy.AddFieldDelimiters(fc_clipped, fieldname))

Second problem: Why are both fields optional? Because if I don't put in any value to both optional parameters, the number of the feature class should be count (without a whereClause) with GetCountManagement. But the code is still running through the else-statement and I get an error because of leaving the fieldname-parameter empty even though the parameter is optional.

So what I need is that the SQL-Expressions works with the input-parameters and if the input-parameters are empty it should just use the GetCountManagement.

That is the full code:

    fc_clipped = os.path.join(out_gebietsscharfe_Objekte, objektname)+"_"+gebietsname #input feature
    fn = parameters[2].valueAsText #fieldname

    if arcpy.Describe(fc_clipped).shapeType == "Point" or "Polygon":
        if parameters[2].valueAsText == "#":
            COUNTER = arcpy.GetCount_management(fc_clipped)
        else:
            COUNTER = 0
            whereClause = """{} = '{}'"""
            whereClause = whereClause.format(arcpy.AddFieldDelimiters(fc_clipped, fn))
            for row in arcpy.da.SearchCursor(fc_clipped, fn, whereClause):
                COUNTER+=1
    else:
        fn = "Shape_Length"
        COUNTER = 0
        whereClause = """{} = '{}'"""
        whereClause = whereClause.format(arcpy.AddFieldDelimiters(fc_clipped, fn))
        for row in arcpy.da.SearchCursor(fc_clipped, fn, whereClause):
            COUNTER+=row[0]

    arcpy.AddMessage(COUNTER)
4
  • 2
    Normally SQL 'OR' requieres you to write: if arcpy.Describe(fc_clipped).shapeType == "Point" OR arcpy.Describe(fc_clipped).shapeType == "Polygon"
    – blabbath
    Feb 27, 2019 at 10:49
  • @blabbath Thanks for your response but that is not the issue in the program. It works with 'or'
    – Andreas
    Feb 27, 2019 at 10:52
  • 2
    No, it doesn't. You need to be careful with Boolean expressions in Python, because or nonZero will always result in True. @blabbath is correct
    – Vince
    Feb 27, 2019 at 11:51
  • arcpy.Describe(fc_clipped).shapeType == "Point" or "Polygon" will always return True. If you had a line feature class, it would return True. You need arcpy.Describe(fc_clipped).shapeType == "Point" or arcpy.Describe(fc_clipped).shapeType == "Polygon"` OR arcpy.Describe(fc_clipped).shapeType in ("Point" , "Polygon")
    – user2856
    Mar 1, 2019 at 6:29

1 Answer 1

4

As @blabbath has mentioned this line of code isn't doing what you'd like it to do:

if arcpy.Describe(fc_clipped).shapeType == "Point" or "Polygon":

This statement will always return True regardless of what arcpy.Describe(fc_clipped).shapeType returns:

>>> if 1 + 7 == 50 or 12:
    print "hello world"


hello world

Think of it like this:

if (1 + 7 == 50) OR (12)

1 + 7 does not equal 50, but 12 returns True.

Alternatives:

>>> if 1 + 7 == 50 or 1 + 7 == 12:
    print "hello world"


>>> if 1 + 7 in (50, 12):
    print "hello world"


>>> 

Your fix:

if arcpy.Describe(fc_clipped).shapeType in ("Point", "Polygon"):

2
  • Yes, you are all right with it. Thank you!
    – Andreas
    Feb 27, 2019 at 18:15
  • This is because non-empty strings are "Truthy".
    – Paul
    Feb 27, 2019 at 20:15

Your Answer

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

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