Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #import modules
  2. import arcpy, os
  3.  
  4. #set env
  5. arcpy.env.overwriteOutput = 1
  6. ws = r"C:TestDataFarFarTest.gdb"
  7.  
  8. #make point and poly into feature layer
  9. arcpy.MakeFeatureLayer_management(r"C:TestDataFarFarTest.gdbpoints", "points")
  10. arcpy.MakeFeatureLayer_management(r"C:TestDataFarFarTest.gdbPolygs", "polys")
  11.  
  12. #create cursor
  13. polyRows = arcpy.SearchCursor("polys")
  14.  
  15. #create log to hold ObjectIDs of selected points
  16. tab = r"C:TestDataFarlog.txt"
  17. log = open(tab, "w")
  18. log.write("IDn")
  19.  
  20. #loop through cursor
  21. for row in polyRows:
  22. #clear all selections
  23. arcpy.SelectLayerByAttribute_management("polys", "CLEAR_SELECTION")
  24. arcpy.SelectLayerByAttribute_management("points", "CLEAR_SELECTION")
  25.  
  26. #get current ID
  27. fid = row.getValue("ObjectID_1")
  28.  
  29. #create SQL expression to select the current row
  30. exp = '"ObjectID_1" = ' + str(fid)
  31.  
  32. #select the current polygon
  33. arcpy.SelectLayerByAttribute_management("polys", "NEW_SELECTION", exp)
  34.  
  35. #select the points in the current polygon
  36. arcpy.SelectLayerByLocation_management("points", "INTERSECT","polys")
  37.  
  38. #point distance tool
  39. arcpy.PointDistance_analysis("points", "points", "in_memory/distance")
  40.  
  41. #sort the point distances
  42. arcpy.Sort_management("in_memory/distance","in_memory/sort","DISTANCE DESCENDING","UR")
  43.  
  44. #create empty list
  45. fidList = []
  46.  
  47. #send cursor through table
  48. sortRows = arcpy.SearchCursor("in_memory/sort")
  49. for sortrow in sortRows:
  50. #grab the 5 longest distances (this number can be changed depending on the amount of points in each poly)
  51. if sortrow.getValue("OBJECTID") < 6:
  52. #find the FIDs for the points that have the longest distance between the two
  53. a = sortrow.getValue("INPUT_FID")
  54. b = sortrow.getValue("NEAR_FID")
  55. #append those values to a list
  56. fidList.append(a)
  57. fidList.append(b)
  58.  
  59. #find the ID that is in the list the most times. This is likely the furthest point from other points
  60. farPoint = max(set(fidList), key=fidList.count)
  61.  
  62. log.write(str(farPoint)+"n")
  63.  
  64. #delete in memory table
  65. arcpy.Delete_management("in_memory/distance")
  66. arcpy.Delete_management("in_memory/sort")
  67.  
  68. #delete cursor objects
  69. del sortrow, sortRows
  70.  
  71. #delete cursor objects
  72. del row, polyRows
  73.  
  74. #clear selections
  75. arcpy.SelectLayerByAttribute_management("points", "CLEAR_SELECTION")
  76.  
  77. #close txt
  78. log.close()
  79. print("far IDs found")
  80.  
  81. #make table view and join with point layer
  82. arcpy.MakeTableView_management(tab, "txt")
  83. arcpy.AddJoin_management("points", "OBJECTID", "txt", "ID", "KEEP_COMMON")
  84. print("far IDs selected")
  85.  
  86. #export final output
  87. arcpy.FeatureClassToFeatureClass_conversion("points", ws, "farPoints")
  88. print("far IDs exported")
  89.  
  90. #Delete Layers
  91. arcpy.Delete_management("polys")
  92. arcpy.Delete_management("points")
  93. arcpy.Delete_management("txt")
  94. arcpy.Delete_management(tab)
  95.  
  96. print("Script complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement