Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import modules
- import arcpy, os
- #set env
- arcpy.env.overwriteOutput = 1
- ws = r"C:TestDataFarFarTest.gdb"
- #make point and poly into feature layer
- arcpy.MakeFeatureLayer_management(r"C:TestDataFarFarTest.gdbpoints", "points")
- arcpy.MakeFeatureLayer_management(r"C:TestDataFarFarTest.gdbPolygs", "polys")
- #create cursor
- polyRows = arcpy.SearchCursor("polys")
- #create log to hold ObjectIDs of selected points
- tab = r"C:TestDataFarlog.txt"
- log = open(tab, "w")
- log.write("IDn")
- #loop through cursor
- for row in polyRows:
- #clear all selections
- arcpy.SelectLayerByAttribute_management("polys", "CLEAR_SELECTION")
- arcpy.SelectLayerByAttribute_management("points", "CLEAR_SELECTION")
- #get current ID
- fid = row.getValue("ObjectID_1")
- #create SQL expression to select the current row
- exp = '"ObjectID_1" = ' + str(fid)
- #select the current polygon
- arcpy.SelectLayerByAttribute_management("polys", "NEW_SELECTION", exp)
- #select the points in the current polygon
- arcpy.SelectLayerByLocation_management("points", "INTERSECT","polys")
- #point distance tool
- arcpy.PointDistance_analysis("points", "points", "in_memory/distance")
- #sort the point distances
- arcpy.Sort_management("in_memory/distance","in_memory/sort","DISTANCE DESCENDING","UR")
- #create empty list
- fidList = []
- #send cursor through table
- sortRows = arcpy.SearchCursor("in_memory/sort")
- for sortrow in sortRows:
- #grab the 5 longest distances (this number can be changed depending on the amount of points in each poly)
- if sortrow.getValue("OBJECTID") < 6:
- #find the FIDs for the points that have the longest distance between the two
- a = sortrow.getValue("INPUT_FID")
- b = sortrow.getValue("NEAR_FID")
- #append those values to a list
- fidList.append(a)
- fidList.append(b)
- #find the ID that is in the list the most times. This is likely the furthest point from other points
- farPoint = max(set(fidList), key=fidList.count)
- log.write(str(farPoint)+"n")
- #delete in memory table
- arcpy.Delete_management("in_memory/distance")
- arcpy.Delete_management("in_memory/sort")
- #delete cursor objects
- del sortrow, sortRows
- #delete cursor objects
- del row, polyRows
- #clear selections
- arcpy.SelectLayerByAttribute_management("points", "CLEAR_SELECTION")
- #close txt
- log.close()
- print("far IDs found")
- #make table view and join with point layer
- arcpy.MakeTableView_management(tab, "txt")
- arcpy.AddJoin_management("points", "OBJECTID", "txt", "ID", "KEEP_COMMON")
- print("far IDs selected")
- #export final output
- arcpy.FeatureClassToFeatureClass_conversion("points", ws, "farPoints")
- print("far IDs exported")
- #Delete Layers
- arcpy.Delete_management("polys")
- arcpy.Delete_management("points")
- arcpy.Delete_management("txt")
- arcpy.Delete_management(tab)
- print("Script complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement