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)
or nonZero
will always result in True. @blabbath is correctarcpy.Describe(fc_clipped).shapeType == "Point" or "Polygon"
will always returnTrue
. If you had a line feature class, it would returnTrue
. You needarcpy.Describe(fc_clipped).shapeType == "Point" or
arcpy.Describe(fc_clipped).shapeType == "Polygon"` ORarcpy.Describe(fc_clipped).shapeType in ("Point" , "Polygon")