3

I have a feature class in a file geodatabase using ArcGIS 10.1. I want to create a model that selects the three features with the highest values in one field (Average Price). Basically I want my selection to include the three features with the highest average prices. I can't quite figure out how to accomplish this. Any suggestions would be greatly appreciated!

  • possible duplicate of Select top 5 most items in a geodatabase – radouxju Mar 11 '14 at 19:54
  • I am using a file geodatabase so this will not work. – ge0m3try Mar 11 '14 at 20:01
  • I agree that the different data format involved means this is not a duplicate. – PolyGeo Mar 11 '14 at 20:55
  • I can definitely provide a ModelBuilder only Answer to this but what license level are you using because that will dictate the tools involved? – PolyGeo Mar 12 '14 at 22:07
1

One way to solve this would be to use a sql clause in a arcpy.da.SearchCursor to sort your values in descending order. Then you would store the third value of the sorted column. The trick is that you only want to get the third highest so you can utilize a counter variable. Without testing the code something like this could get the job done:

searchFields = ‘AveragePrice’
query = ‘AveragePrice’
sqlClause = (None, ‘ORDER BY ‘ + query +  ‘ DESC’)
ct = 0
with arcpy.da.SearchCursor(fc, field_names = searchFields, sql_clause = sqlClause) as searchCursor:
    for row in searchCursor:
        if ct == 3:
            break
        elif ct == 2:
            thirdHighestPrice = row[0]
        else:
            ct += 1
            continue

Then feed that value of thirdHighestPrice into a arcpy.SelectLayerByAttribute_management statement to select all values that are greater than or equal to the variable thirdHighestPrice.

  • 1
    I was hoping to be able to use model builder though... – ge0m3try Mar 11 '14 at 20:14
  • Python scripts in the form of custom tools can be used in Model Builder. See ERSI documentation here link. – GeoSharp Mar 11 '14 at 21:14
  • What does the None in the SQL do in this instance? – ianbroad Mar 25 '14 at 17:11
2

I would just do something like this:

field = "AveragePrice"

values = [row[0] for row in arcpy.da.SearchCursor(fc, field)]
top_three = sorted(values)[-3:]

query = "\"{0}\" in {1}".format(field, tuple(top_three))

arcpy.MakeFeatureLayer_management(fc, "top_3_layer", query)
0

Its not going to be a eloquent solution, but using "Summary Statistics" tool, you could get the MAX value of an attribute. Then do a join using that MAX value to select from your FC that record, remove that record from your original FC and put it aside (Switch Selection). Repeat three times and merge the outputs.

  • Hmm that sounds interesting, but is there a way to get have the selection occur within the existing FC? – ge0m3try Mar 11 '14 at 20:02
  • You would do the join, and retain only matching values, or have it return all values and then select where FC Value = Summary Stat Max Value. – BretW Mar 11 '14 at 20:30
  • Ok, this makes sense and all, but I am getting lost where you say do a join using the max value. How do I do this? – ge0m3try Mar 11 '14 at 20:36
  • If you join from Stat Summary to your FC, join based on MAX Value to Attribute value - the only match in your FC will be that record with the max value. – BretW Mar 12 '14 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.