Solved! Go to Solution.
import arcpy def get_id_of_higest(fc, attr): """Return object ID of a feature with the highest value of attribute attr. Returns None if fc is an empty feature class. fc -- feature class (MUST BE STORED IN A GEODATABASE!!!) attr -- name of the attribute to examine """ ret = None with arcpy.da.SearchCursor(fc, ['OID@', attr], sql_clause=(None, 'ORDER BY "' + str(attr) + '" DESC')) as sc: for row in sc: ret = row[0] break return ret myfc = r'c:\temp\cities.shp' myattr = 'pop_max' maxido = get_id_of_higest(myfc, myattr) # now you can use maxido to select the feature
import arcpy def get_id_of_higest(fc, attr): """Return object ID of a feature with the highest value of attribute attr. Returns None if fc is an empty feature class. fc -- feature class (MUST BE STORED IN A GEODATABASE!!!) attr -- name of the attribute to examine """ ret = None with arcpy.da.SearchCursor(fc, ['OID@', attr], sql_clause=(None, 'ORDER BY "' + str(attr) + '" DESC')) as sc: for row in sc: ret = row[0] break return ret myfc = r'c:\temp\cities.shp' myattr = 'pop_max' maxido = get_id_of_higest(myfc, myattr) # now you can use maxido to select the feature
copiedFeatureNumber = 1
featureLayer = arcpy.mapping.Layer(featureClass)
while True:
# Break out of loop when feature class is empty
if int(arcpy.GetCount_management(geodatabaseLayer).getOutput(0)) == 0:
break
# Get List from Search Cursor
sortedList = [row for row in arcpy.da.SearchCursor(featureLayer,["OID@","pointValue"])]
# Sort List by the Point Value
sortedList = sorted(sortedList, key=lambda x: x[1], reverse=True)
# Get the FID of the largest Point Value
FID = sortedList[0][0]
# Select that Feature
arcpy.SelectLayerByAttribute_management(featureLayer,"NEW_SELECTION","FID = {0}".format(FID))
# Add to Selection all features within 5 Miles of that Feature
arcpy.SelectLayerByLocation_management(featureLayer,"WITHIN_A_DISTANCE",featureLayer,"5 Miles","ADD_TO_SELECTION")
# Make a copy of those features for whatever use desired
arcpy.CopyFeatures_management(featureLayer,"featureLayer_Num_{0}".format(copiedFeatureNumber))
# Make a list of the selected FIDs
FIDList = [row[0] for row in arcpy.da.SearchCursor(featureLayer,["OID@"])]
# Clear Selection
arcpy.SelectLayerByAttribute_management("address_final_lm","CLEAR_SELECTION")
# Iterate through that Feature Class and remove all features in the selected FID List
with arcpy.da.UpdateCursor(featureLayer, ["OID@"]) as updatecursor:
for row in updatecursor:
if row[0] in FIDList:
updatecursor.deleteRow()
copiedFeatureNumber += 1