I have a script that calculates the average distance between points. What I want is to rewrite it so I can input the point layer, input a distance, and the output field will tell me a count of the number of points at that distance. Can anyone help? so basically I just want a script that will tell me a count of the number of points from a point at a certain distance.
#Import standard library modules import sys, string, arcpy inputFC = arcpy.GetParameterAsText(0)# Input Point Layer inputdistance = arcpy.GetParameterAsText(1) #input distance outputfield = arcpy.GetParameterAsText(1) #output field # Read the point data into list. # xs =  ys =  rowCursor = arcpy.SearchCursor(inputFC) for row in rowCursor: geom = row.getValue (properties.shapeFieldName) cen = geom.centroid xs.append(cen.X) ys.append(cen.Y) del rowCursor # compute distance sumdist = 0.0 count = 0.0 for i in range(0,len(xs)): for j in range(i,len(xs)): if (i <> j): dist = ((xs[i] -xs[j])**2 + (ys[i] - ys[j])**2)**0.5 sumdist = sumdist + dist count = count + 1 print "The average inter-point distance is " + str(sumdist/count) arcpy.AddMessage("The average inter-point distance is " + str(sumdist/count))
With the following script. Ive tried to run it as Distance.add_nearby_points_count_field('gauges.shp', 100)
import sys import arcpy import math
def add_nearby_points_count_field(inputFC, inputdistance):
# Read the point data into list. coord_pairs =  desc = arcpy.Describe(inputFC) rowCursor = arcpy.SearchCursor(inputFC) for row in rowCursor: geom = row.getValue(desc.shapeFieldName) coord_pairs.append(geom.centroid) # Add a field called 'cnt' and calculate nearby points arcpy.AddField_management(inputFC, 'cnt', 'LONG') #i also tried cutting out this part and creating the "cnt" field manually rowCursor = arcpy.UpdateCursor(inputFC) for row in rowCursor: geom = row.getValue(desc.shapeFieldName) from_point = geom.centroid near_pts = 0 for to_point in coord_pairs: distance = math.sqrt(pow((to_point.X - from_point.X), 2) + pow((to_point.Y - from_point.Y), 2)) if distance <= inputdistance: near_pts += 1 row.cnt = near_pts - 1 # Subtract 1 to remove the measurement to itself rowCursor.updateRow(row) del rowCursor if __name__ == '__main__': inputFC ='guages.shp' #originally its a featureclass but I converted it to a shapefile to try both methods inputdistance = 100 add_nearby_points_count_field(inputFC, inputdistance)