I have a feature class with a large amount of points. My purpose is to randomly move these points to see what happens as a result. Are there specific tools or Python programming methods to accomplish this?
-
1Please specify why you want such and more context please.– LearnerCommented May 2, 2015 at 12:18
-
1For instance - do you actually need to move the points or is it sufficient to seed an area with x-number/density of points randomly?– MappaGnosisCommented May 2, 2015 at 12:30
-
All programming languages have some kind of random number generation function.– Martin FCommented May 2, 2015 at 20:50
2 Answers
You can manipulate individual point geometry using an Update Cursor. Accessing the SHAPE@XY token is by far the most efficient cursor-based approach. However, if you have a polygon of your area of interest, using a random point generator will likely be more efficient. In this example, I use a minimum and maximum threshold to set the limits for a random number. This random number is used to change the X and Y coordinates.
import arcpy, random
# The input point feature class
fc = r'C:\temp\points.shp'
# Set the random number limits (units will be PCS units)
min = -100
max = 100
# Start an update cursor to update the point geometry
with arcpy.da.UpdateCursor(fc, ["SHAPE@XY"]) as cursor:
for row in cursor:
rand_x = random.randint(min, max)
rand_y = random.randint(min, max)
x = row[0][0] + rand_x
y = row[0][1] + rand_y
xy = (x,y) # The new coordinates
row[0] = xy
cursor.updateRow(row)
Another approach would be to use the Field Calculator. Here is the code that you will need for the Field Calculator:
from random import randint,choice
def RandMove(old_pnt,min_shift,max_shift):
neg = [-1,1]
#get the first point from the geometry object
old_x = old_pnt.firstPoint.x
old_y = old_pnt.firstPoint.y
#calculate new coordinates
new_x = old_x + (choice(neg) * randint(min_shift,max_shift))
new_y = old_y + (choice(neg) * randint(min_shift,max_shift))
#create a new point object and geometry to return
new_pnt = arcpy.Point(new_x,new_y)
pt = arcpy.PointGeometry(new_pnt)
return pt
To use the Field Calculator, open the attribute table of your feature class and right click on the Shape field. Select the Python parser and click on the Show Codeblock check box, then past the above code into the Pre-Logic Script Code text box. The screen capture below shows what the Field Calculator should look like.
I'm not sure if this is any better than the approach by @Aaron but I think it's an interesting trick.