• API
• FAQ
• Tools
• Archive
daily pastebin goal
8%
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