Select to view content in your preferred language

ArcGIS 3.0.2: What tool can select the records with the highest values in a particular field?

2588
15
Jump to solution
01-15-2024 03:18 AM
JamalNUMAN
Legendary Contributor

ArcGIS 3.0.2: What tool can select the records with the highest values in a particular field?

 

For example, in the screenshot below, I couldn’t figure out which tool can select the FOUR records with the highest population size in the “population” field

 

Clip_127.jpg

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
Tags (1)
2 Solutions

Accepted Solutions
DuncanHornby
MVP Notable Contributor

I have edited the model to work as intended, please review comments in model.

View solution in original post

Bud
by
Esteemed Contributor

I wonder if this would work in your model:

  1. Use the Sort geoprocessing tool to output a FC that is sorted by the population field (desc).
    Bud_2-1705560242179.png
  2. Use Select By Attributes or a definition query to isolate the first 4 ObjectIDs.
    Bud_4-1705560609719.png

Maybe that's the kind of thing @MErikReedAugusta had in mind in my ROW_NUMBER idea.

 

Somewhat related: Adding sequential numbers for the selected features

 

View solution in original post

Tags (1)
15 Replies
Eugene_Adkins
Frequent Contributor

Determine the threshold and use the select by attribute tool.

Bud
by
Esteemed Contributor

Can you elaborate on how "Determine the threshold" would be done in a model?

0 Kudos
DuncanHornby
MVP Notable Contributor

As long as your data is in a file geodatabase (so not a shape file) you can use a subquery to answer this.

An example is shown below, in this example ORN is the layer.

DuncanHornby_0-1705328469138.png

 

gargarcia
Occasional Contributor

If you use the data engineering pane and calculate the stats for an integer field it will list the min and max values and let you select them with a right click.

JamalNUMAN
Legendary Contributor

What sql code can select the FOUR records of the highest population size from the “population” field?

Clip_687.jpg

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
DuncanHornby
MVP Notable Contributor

My apologies I missed that you wanted the top four values. I'm not sure (but I could be wrong) that a file geodatabase can support the functions that would allow you to answer this question and you need to approach it in a two-step approach in modelbuilder, this how I would have done it.

I show this below, in my example I am processing a layer called ORN:

DuncanHornby_0-1705405510419.png

 

The Calculate Value tool is set up as-is, note it returns double:

DuncanHornby_2-1705405716022.png

 

The code block is:

def Fourth(layer):
    # You need to set field to the appropriate field
    field = "SourceID"
    s = set()
    with arcpy.da.SearchCursor(layer, field) as cursor:
        for row in cursor:
            if row[0] is not None:
                s.add(row[0])
    l = sorted(s, reverse=True)
    ml = l[0:4]  # first 4 values in sorted list
    mv = min(ml) # Get min value
    return mv

 

The select by attribute tool is:

DuncanHornby_3-1705405858346.png

This logic will select the top four values in the field sourceID, be aware that the selection may return a selection greater than 4 if you have duplicate values in your field. Also underlying assumption of the code is that you have a minimum of 4 unique values in your field.

JamalNUMAN
Legendary Contributor

Many thanks for the help

 

I couldn’t customize the code you have already provide to my local data. In addition to that, the content of the “calculate value” tool in my Pro appears to be different than yours as per the screenshot below.

 

I’m attaching my data along with the model I tried to build with no success

 

Clip_688.jpg

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
DuncanHornby
MVP Notable Contributor

I have edited the model to work as intended, please review comments in model.

Bud
by
Esteemed Contributor