2

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

Example_of_data

  • 1
    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 at 5:25
  • 1
    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 at 6:20
1

If this is your definition of average point distance, code below should work:

enter image description here

(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[0]].append(row[1])

#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[0].distanceTo(c[1]) for c in combinations(v,2)]
        d[k] = sum(pointdistances)/len(pointdistances)

#Add new field and calculate as mean point distance
arcpy.AddField_management(in_table=grid, field_name=resultfield, field_type='DOUBLE')
with arcpy.da.UpdateCursor(grid,['OID@',resultfield]) as cursor:
    for row in cursor:
        if row[0] in d:
            row[1] = d[row[0]]
            cursor.updateRow(row)

enter image description here

  • 1
    @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. – Hornbydd Feb 20 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." – Palm Chonlathit Feb 20 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 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.