SHARE
TWEET

Untitled

a guest Feb 20th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import arcpy
  2. from collections import defaultdict
  3. from itertools import combinations
  4.  
  5. #Change the four lines below
  6. arcpy.env.workspace = r'C:Default.gdb'
  7. grid = 'fishnet_500m'
  8. resultfield = 'meanpointdistance' #New field that will be added and populated with mean point distance
  9. points = 'randompoints'
  10.  
  11. #Intersect grid with points
  12. inter = 'grid_inter_points'
  13. arcpy.Intersect_analysis(in_features=[grid,points], out_feature_class=inter, output_type='POLYGON')
  14. fid_grid_field = 'FID_'+grid
  15.  
  16. #Group points together in a defaultdict(list) using fishnet id
  17. allpoints = defaultdict(list)
  18. with arcpy.da.SearchCursor(inter,[fid_grid_field,'SHAPE@']) as cursor:
  19.     for row in cursor:
  20.         allpoints[row[0]].append(row[1])
  21.  
  22. #Calculate distances between all point combinations and store in a dictionary
  23. d = {}
  24. for k,v in allpoints.items(): #d.iteritems in py2/ArcMap
  25.     if len(v)>1:
  26.         pointdistances = [c[0].distanceTo(c[1]) for c in combinations(v,2)]
  27.         d[k] = sum(pointdistances)/len(pointdistances)
  28.  
  29. #Add new field and calculate as mean point distance
  30. arcpy.AddField_management(in_table=grid, field_name=resultfield, field_type='DOUBLE')
  31. with arcpy.da.UpdateCursor(grid,['OID@',resultfield]) as cursor:
  32.     for row in cursor:
  33.         if row[0] in d:
  34.             row[1] = d[row[0]]
  35.             cursor.updateRow(row)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top