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(wherevValue) 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)
أكثر...
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(wherevValue) 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)
أكثر...