# 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? – Michael Stimson Feb 20 '19 at 5:25
• I've found someone using standard distance and convert it to distance using numpy, nout sure is it the same thing? – Palm Chonlathit Feb 20 '19 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 