davisam1

problem with nested while loops

Discussion created by davisam1 on Oct 20, 2011
Latest reply on Oct 21, 2011 by davisam1
Hi!
I'm trying to write a python script that will look for points that are within a certain distance of each other and delete one of the pair at random. 
I've taken a stab at this and the program runs but it doesn't get all the points.  I think it may be a problem with the nested while loops.  I put the script below and tried to comment it as much as possible. 
Can someone please give me advice on how to fix this. 
Any help is greatly appreciated.
Amelie

# ---------------------------------------------------------------------------
# IterativeNearAnalysis.py
# Created on: thu oct 19 2011  by Amelie Davis
# for a shapefile in a folder
# if points are identical in terms of lat long, removes one point at random
# checks the distance between all points in that same shapefile is less than a specified distance
# deletes at random one of the two 'offending points'
# creates a new shapefile with only the points that are wanted
# checks the output
# final output is shapefile with only points beyond the specified distance
# ---------------------------------------------------------------------------

# Import system modules
import arcpy
from arcpy import env
import os

# input shapfile
inputfile = arcpy.GetParameterAsText(0)
# default inputfile = "d:/data/leap540/52spp_withid_1000m/BCN_Birds_P_top52spamecro.shp"

#distance to look for points
dist2 = arcpy.GetParameterAsText(1)
dist = dist2 + " Meters"
# default dist2 = 500

# outputfiles
# shapefile from which points that are located at the exact same spot are removed
out1 = arcpy.GetParameterAsText(2)
# default out1 = inputfile[:-4] + "_sort.shp"
# table listing points that are within the specified distance
out2 = out1[:-4] + dist2 + "_near.dbf"
# out2 table sorted by random2 field
out3 = out2[:-4] + "_dist.dbf"
#
out4 = inputfile[:-4] + "_f.dbf"
out5 = out4[:-4] + "_check.dbf"

arcpy.AddField_management(inputfile,"RANDOM","SHORT","5","#","#","#","NULLABLE","NON_REQUIRED","#")
codeblock1= "dim max, min\nmax=99999\nmin=0\nx=(Int((max-min+1)*Rnd+min))"
arcpy.CalculateField_management(inputfile,"RANDOM","x","VB",codeblock1)

# delete records that are identical in latitute and longitude
# make sure the point to delete is selected at random by sorting by random
arcpy.Sort_management(inputfile,out1,"RANDOM ASCENDING","UR")
fields = ["LATITUDE","LONGITUDE"]
arcpy.DeleteIdentical_management(out1,fields,"#","0")

# generate list of points that are within a certain distance (specified by 'dist') or each other
arcpy.GenerateNearTable_analysis(out1,out1,out2,dist,"NO_LOCATION","NO_ANGLE","ALL","0")
arcpy.AddField_management(out2,"RANDOM2","SHORT","5","#","#","#","NULLABLE","NON_REQUIRED","#")
arcpy.CalculateField_management(out2,"RANDOM2","x","VB",codeblock1)
arcpy.Sort_management(out2,out3,"RANDOM2 ASCENDING","UR")
arcpy.DeleteIdentical_management(out3,"NEAR_DIST","#","0")


rows = arcpy.SearchCursor(out3)
i_fid = "NEAR_FID"

row = rows.next()
while row:
    i_fid1 = row.getValue(i_fid)
    arcpy.AddMessage("FID of row to delete is "+ str(i_fid1))
    rows2 = arcpy.UpdateCursor(out1)
    row2 = rows2.next()
    while row2:
        i_fid2 = row2.getValue("FID")
        arcpy.AddMessage("FID of row we are on is "+str(i_fid2))
        if i_fid2 == i_fid1:
            rows2.deleteRow(row2)
            arcpy.AddMessage("deleting row")
            row2 = rows2.next()
        else:
            row2 = rows2.next()
            arcpy.AddMessage("not deleting anything")
    row = rows.next()
arcpy.AddMessage("out of loop")

del row
del row2

arcpy.GenerateNearTable_analysis(out1,out1,out5,dist,"NO_LOCATION","NO_ANGLE","ALL","0")
# this table should be empty (need to implement this as a code and give an error message if not #empty.

# delete out2 and out3

Outcomes