Wednesday 30 January 2019

python - A count of points within a set distance using updatecursor


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)

Answer




EDIT based on comment...


Kinn, you can use this in a number of ways. One way is to save the script as "count_nearby_points.py", and as long as it is in your python path, you can use it in any other script by importing it.


import count_nearby_points

Then you should be able to call the count_nearby_points.add_nearby_points_count_field(inputFC, inputdistance) function from within your script.


Or, if you run this script directly, you can modify the parameters under if __name__ == '__main__':


You can also copy the part(s) that you want into your own script.


Feel free to ask if you have further questions.


#-------------------------------------------------------------------------------
# Name: count_nearby_points.py

#
# Purpose: Return a count of nearby point features
#
# Created: 29/09/2011
#-------------------------------------------------------------------------------
#!/usr/bin/env python

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')
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__':
# Test data for when running as __main__, make sure that 'points.shp' exists.
inputFC ='points.shp'
inputdistance = 100

add_nearby_points_count_field(inputFC, inputdistance)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...