Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Reading feature class attributes with or without quotes in ArcPy?

$
0
0

I’m working on a script that clips a batch of feature classes by a selection made in another feature class. The way it works is by reading the clip feature fields, then allowing the user to select the field they want to clip by. Then list of unique attributes fro the field is generated and the user can check multiple values to clip by. The multi value list is then passed to a SQL where clause that then gets incorporated into the SelectLayerByAttribute function.

The question that I’ve got doesn’t really relate to the script (I’ve come up with some logic to get around the problem), but to how the attributes retrieved from the clip feature are formatted.

As I look at the values retrieved, some are formatted with single quotes around them ('value') and others are not (value). And both conditions can exist in the same feature class and field.

Can anyone tell me why this is the case and if there is something I should be doing with my data to eliminate this condition?

I’ve included the script that I’ve been using. Note the if statement that includes .startswith and .endswith. That’s the logic I’ve been using to get around the problem.

import arcpy, os
workingGDB = arcpy.GetParameterAsText(0)
clipLyr = arcpy.GetParameterAsText(1)
clipField = arcpy.GetParameterAsText(2)
clipValue = arcpy.GetParameterAsText(3)
walk = arcpy.da.Walk(workingGDB, datatype="FeatureClass")
valueList = clipValue.split(";")
whereList = []
for v in valueList:
    if v.startswith("'") and v.endswith("'"):
        whereValue = '{0}'.format(v)
        whereList.append(whereValue)
    if not v.startswith("'") and not v.endswith("'"):
        whereValue = "'{0}'".format(v)
        whereList.append(whereValue)
arcpy.MakeFeatureLayer_management(clipLyr,"lyr")
arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", whereClause)
records = arcpy.GetCount_management("lyr")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        filePath = os.path.join(dirpath, filename)        
        outFile = filePath + "_clip"
        arcpy.Clip_analysis(filePath, "lyr", outFile)
        arcpy.Delete_management(filePath)
        arcpy.Rename_management(outFile, filePath)

Here is the validation code:

import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""

    if self.params[1].value and self.params[2].value:
        self.params[3].filter.list = sorted({row[0] for row in arcpy.da.SearchCursor(self.params[1].value, self.params[2].value.value) if row[0]})

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

Viewing all articles
Browse latest Browse all 767

Latest Images

Trending Articles



Latest Images