import random import arcpy arcpy.env.workspace = ???D:\???\database.gdb??? in_points = ???D:\???\???\geodatabase.gdb\Geocoding_Results.shp??? polygon = ???D:\???\geodatabase.gdb\zipcode.shp??? def point_in_poly(poly, x, y): ??????"Returns if the point is inside the polygon. Parameters: poly: arcpy.Polygon() geometry x: x coordinate (float) y: y coordinate (float) ??????" pg = arcpy.PointGeometry(arcpy.Point(x, y), poly.spatialReference) return poly.contains(pg) def disperse_points(in_points, polygon): ??????"Randomly disperse points inside a polygon. Parameters: in_points: Point feature class/layer (with or without selection) polygon: arcpy.Polygon() geometry ??????" lenx = polygon.extent.width leny = polygon.extent.height with arcpy.da.UpdateCursor(in_points, "shape@xy") as points: for p in points: x = (random.random() * lenx) + polygon.extent.XMin y = (random.random() * leny) + polygon.extent.YMin inside = point_in_poly(polygon, x, y) while not inside: x = (random.random() * lenx) + polygon.extent.XMin y = (random.random() * leny) + polygon.extent.YMin inside = point_in_poly(polygon, x, y) points.updateRow([(x, y)])
Solved! Go to Solution.
Xander
Thank you for your reply.
I still get an error message for now.
Thank you.
John
Error below:
Traceback (most recent call last):
File "H:\DispersePoints.py", line 49, in <module>
main()
File "H:\DispersePoints.py", line 22, in main
disperse_points(fcPoints,polygon)
File "H:\DispersePoints.py", line 40, in disperse_points
while not inside:
UnboundLocalError: local variable 'inside' referenced before assignment
My code:
#-------------------------------------------------------------------------------
# Name: Disperse3.py
# Purpose: Disperse points in multiple polygons
# Author: arcpy Team xander_bakker https://community.esri.com/people/xander_bakker
# http://arcpy.wordpress.com/2013/06/07/disperse-overlapping-points/
# Created: 02-dec-2013
#-------------------------------------------------------------------------------
import arcpy, random
def main():
fcPoints = r"H:\\CondoAddressPoints.gdb\\new_points2"
fcPolygon = r"H:\\CondoAddressPoints.gdb\\new_polys"
arcpy.env.overwriteOutput = True
with arcpy.da.SearchCursor(fcPolygon, ("SHAPE@")) as cursor:
for row in cursor:
polygon = row[0]
disperse_points(fcPoints,polygon)
del row
print "ready..."
def point_in_poly(poly, x, y):
pg = arcpy.PointGeometry(arcpy.Point(x, y), poly.spatialReference)
return poly.contains(pg)
def disperse_points(in_points, polygon):
lenx = polygon.extent.width
leny = polygon.extent.height
with arcpy.da.UpdateCursor(in_points, "SHAPE@XY") as points:
for p in points:
if point_in_poly(polygon, p[0][0], p[0][1]):
x = (random.random() * lenx) + polygon.extent.XMin
y = (random.random() * leny) + polygon.extent.YMin
inside = point_in_poly(polygon, x, y)
while not inside:
x = (random.random() * lenx) + polygon.extent.XMin
y = (random.random() * leny) + polygon.extent.YMin
inside = point_in_poly(polygon, x, y)
points.updateRow([(x, y)])
else:
pass # don't update location if point doesn't originally falls inside current polygon
if __name__ == '__main__':
main()
Try initializing 'inside' to None just inside the p loop:
for p in points:
inside = None
edit: compare your indentation to Xander's at 'while not inside'. It's different and is responsible for your problem.
After making the change in indentation the script runs perfectly.
Thank you everyone
John