import arcpy, traceback mxd = arcpy.mapping.MapDocument("CURRENT") lstLayers = arcpy.mapping.ListLayers(mxd) flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0] alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0] FRows = arcpy.SearchCursor(flayer) ARows = arcpy.SearchCursor(alayer) ffields = arcpy.ListFields(flayer, "FLAG", "String") afields = arcpy.ListFields(alayer, "TFLAG", "String") FList = [] AList = [] for row in FRows: Fvalue = row.getValue("FLAG") FList.append(str(Fvalue)) for row in ARows: Avalue = row.getValue("TFLAG") AList.append(str(Avalue)) matched = set(FList) & set(AList) for x in matched: exp = '"FLAG" = ' + "'" + x + "'" arcpy.SelectLayerByAttribute_management("AADT", "ADD_TO_SELECTION", exp) arcpy.SelectLayerByAttribute_management("AADT", "SWITCH_SELECTION")
Solved! Go to Solution.
My program is to find missing records when comparing to layers inside an mxd and then highlight the ones that are missing. For example, if one layer has 105H1 then the other layer should have it if not then it highlights that record to alert the user it is missing in the other layer. Right, now it is highlighting ones that are missing but also highlighting random ones that are in both layers??? I think my problem is the loop at the bottom of my program but a) I'm not sure and b) I don't know how to fix it.
import arcpy, traceback mxd = arcpy.mapping.MapDocument("CURRENT") lstLayers = arcpy.mapping.ListLayers(mxd) flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0] alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0] FRows = arcpy.SearchCursor(flayer) ARows = arcpy.SearchCursor(alayer) ffields = arcpy.ListFields(flayer, "FLAG", "String") afields = arcpy.ListFields(alayer, "TFLAG", "String") FList = [] AList = [] for row in FRows: Fvalue = row.getValue("FLAG") FList.append(str(Fvalue)) for row in ARows: Avalue = row.getValue("TFLAG") AList.append(str(Avalue)) matched = set(FList) & set(AList) for x in matched: exp = '"FLAG" = ' + "'" + x + "'" arcpy.SelectLayerByAttribute_management("AADT", "ADD_TO_SELECTION", exp) arcpy.SelectLayerByAttribute_management("AADT", "SWITCH_SELECTION","#")
When I changed the AADT in the selectLayer to flayer, it gave me an error of "flayer does not exist". I think that's because I don't think I made the AADT layer named flayer, flayer was assigned to listlayers. Maybe, it gave me that error because I am running it inside the mxd with the layers it needs. I did put in the MakeFeatureLayer_management above the SelectLayers. But my result was the same. I only have 524 missing records and it is finding 878.
import arcpy, traceback mxd = arcpy.mapping.MapDocument("CURRENT") flayer = arcpy.mapping.ListLayers(mxd, "AADT")[0] alayer = arcpy.mapping.ListLayers(mxd, "AADTAnnoLabel")[0] FRows = arcpy.SearchCursor(flayer) ARows = arcpy.SearchCursor(alayer) FList = [] AList = [] for row in FRows: Fvalue = row.getValue("FLAG") FList.append(str(Fvalue)) Fset = set(FList) ### I used sets here to eliminate possible duplicate values for row in ARows: Avalue = row.getValue("TFLAG") AList.append(str(Avalue)) Aset = set(AList) ### I used sets here to eliminate possible duplicate values for x in Fset: if x not in Aset: exp = '"FLAG" = ' + "'" + x + "'" #print exp arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)