Selecting Max Value from Current Selection

01-05-2021 08:28 AM
Labels (3)
New Contributor III

Hello all,

I have a Feature Class data set that contains many GPS points that where collected over the course of a couple years.  The latest ones have better accuracy compared to the older ones.  For example, a valve could have 2-3 GPS points scattered around it.  I have made a model that will iterate through each GPS point, select all the ones around it and select the oldest ones and either copy and move or delete them.  I have chosen the model route because it seemed the simplest solution, but....

I am having trouble getting a SQL expression to work for the last part, Selecting the oldest points.  So far I have been using the expression, GPS_Time = (SELECT MIN( GPS_Time) FROM %Near_GPS_Point%) , but for some reason this doesn't seem to work for SUBSET_SELECTION.  I have tried many variations of this expression but invalid expression are my only results.

Not having any outputs created would be ideal as there are about 300k GPS points to iterate though.

Tags (4)
0 Kudos
1 Reply
New Contributor III

So the model I was creating to find these points wasn't working out as well as I hoped.  I decided to switch gears and change my process to a python code.  The code works very well by adding the points I want to a keep_list and the bad ones to a discard_list.  Code is a little messy but it works. 

The only issue I am having is at some point during a large dataset arcmap will crash.  To avoid this issue, I would like to populate the 'Status' field with "Good" if its on the keep_list.  I was thinking of doing an UpdateCursor but I am unsure how it can be done using a dictionary.  Any suggestions will be appreciated.

fc = 'wSystemValves_dupsTest'
unifield = 'UNIQUEID'
oidField = 'OBJECTID'
near_distn = 'NEAR_DISTN'
northCoord = 'Northing' # sort ascending
eastCoord = 'Easting' # sort decending
status = 'Status'
fields_sorted = 'Northing D'; 'Easting A'

keep_list = list()
discard_list = list()

sCursor = arcpy.SearchCursor(fc)
sRow =
while sRow:

if sRow.getValue(oidField) not in discard_list:
arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION", "OBJECTID = {}".format(sRow.getValue(oidField)))
arcpy.SelectLayerByLocation_management(fc, "INTERSECT", fc, 3, "NEW_SELECTION")
# get number of points selected
count = int(arcpy.GetCount_management(fc).getOutput(0))

# List all values:
sel_values = [list(i) for i in arcpy.da.SearchCursor(fc, [oidField, northCoord, eastCoord, near_distn, unifield])]
# Sort by Northing and Easting:
sel_values_sorted = sorted(sel_values, key=lambda x: (x[1], -x[2]), reverse=False)

if count == 2 and sel_values_sorted[0][-2] < 3:

print (sel_values_sorted)
# keep the one with the correct coordinates
keepOID = sel_values_sorted[0][0]
# discard the one with bad coordinates
discardOID = sel_values_sorted[1][0]

if keepOID not in keep_list:
if discardOID not in discard_list:
print (keep_list)
print (discard_list)
# Use OIDS from dictionary values to select:

if count == 1 and sel_values_sorted[0][-2] == -1:
# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "wSystemValves_dupsTest"
arcpy.CalculateField_management(fc, "Status", '"good"', "PYTHON_9.3")
keepOID = sel_values_sorted[0][0]
if keepOID not in keep_list:
print (keep_list)
print (discard_list)

sRow =


0 Kudos