# Calculating distance between points within grid polygon

I'm very new to ArcGIS, and I would like to calculate the average distance between points within a grid for every grid. Is there any idea on how to implement this?

My data look like this • Do you have an advanced license? It's a little tricky but it could be done in arcpy with an intersect of points and polygons then generate near table (advanced license required) or some math. Do you have any python ability? Feb 20, 2019 at 5:25
• I've found someone using standard distance and convert it to distance using numpy, nout sure is it the same thing? Feb 20, 2019 at 6:20

If this is your definition of average point distance, code below should work: (1+2+4+4+3+2)/6

A new field will be added to the grid fc and calculated as average Point distance.

``````import arcpy
from collections import defaultdict
from itertools import combinations

#Change the three lines below to match your data
arcpy.env.workspace = r'C:\Default.gdb' #Geodatabase of folder holding grid and point fc below
grid = 'fishnet_500m' #Name of your grid feature class
points = 'randompoints' #Name of your Point feature class

resultfield = 'meanpointdistance' #New field that will be added to the grid fc and populated with mean point distance

#Intersect grid with points
inter = 'grid_inter_points'
arcpy.Intersect_analysis(in_features=[grid,points], out_feature_class=inter)
fid_grid_field = 'FID_'+grid

#Group points together by fishnet id using defaultdict(list)
allpoints = defaultdict(list)
with arcpy.da.SearchCursor(inter,[fid_grid_field,'SHAPE@']) as cursor:
for row in cursor:
allpoints[row].append(row)

#Calculate average distances between all point combinations per fishnet id and store in a dictionary
d = {}
for k,v in allpoints.iteritems(): #d.items() in py3/ArcGIS Pro
if len(v)>1:
pointdistances = [c.distanceTo(c) for c in combinations(v,2)]
d[k] = sum(pointdistances)/len(pointdistances)

#Add new field and calculate as mean point distance
with arcpy.da.UpdateCursor(grid,['OID@',resultfield]) as cursor:
for row in cursor:
if row in d:
row = d[row]
cursor.updateRow(row)
`````` • @Palm_Chonlathit if your definition of "average distance between points" meant the Mean Center then search help for the tool "Mean Center". You would then adapt the excellent script above to measure distances from that point. Feb 20, 2019 at 15:28
• Thank you so much, but I've already tried running the code on python window, then it shows an error "Runtime error Traceback (most recent call last): File "<string>", line 3, in <module> File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\analysis.py", line 333, in Intersect raise e ExecuteError: ERROR 000732: Input Features: Dataset fishnet_500m #;randompoints # does not exist or is not supported ERROR 000800: The value is not a member of INPUT | LINE | POINT." Feb 20, 2019 at 16:08
• @PalmChonlathit could you try again? You need to change the three lines at the start as indicated. I also found an error in the intersect line which i corrected now
– BERA
Feb 21, 2019 at 6:29