I took this code from this source Select maximum number of points more than x meters apart
It seems to work for other users, but for me it’s an endless loop. I do not understand why, though the code makes sense. The code itself gives me the result I want to get.
Can you help me out of this endless loop:
#
import arcpy, sys
feature = arcpy.GetParameterAsText(0)
distance = 500
arcpy.gp.overwriteOutput = True
#running NEAR analysis - every point gets attribute of a distance to the nearest point
#in same feature class
arcpy.AddMessage("running first near analysis")
arcpy.Near_analysis(feature, feature)
arcpy.AddMessage("inserting cursor")
cur = arcpy.UpdateCursor(feature)
row = cur.next()
arcpy.AddMessage("starting loop")
i=0
while row:
i+=1
#fids list will store list of deleted points so if any other point will have
#deleted one as the nearest and distance < 150 will not get deleted as this
#distance is no longer true
fids = []
while row:
if row.NEAR_DIST < distance:
try:
#it seems I didn't know if .. in .. at the time
such a fun to dig
#this script up! index throws an exception if element is not in the
#list
fids.index(row.NEAR_FID)
arcpy.AddMessage("OBJECTID = " + str(row.OBJECTID) + " is listed!")
except:
arcpy.AddMessage("deleting OBJECTID = " + str(row.OBJECTID))
fids.append(row.FID)
cur.deleteRow(row)
d = 1
row = cur.next()
del cur, row, fids
try:
#this idiotic test is to break the loop when no points will have
#NEAR_DIST < 150, shameful - I know!
if d == 1:
pass
except:
sys.exit()
d = 0
arcpy.AddMessage("loop iteration " + str(i))
#and again we go..
arcpy.Near_analysis(feature, feature)
cur = arcpy.UpdateCursor(feature)
row = cur.next()