HOW TO
In ArcGIS Pro, dispersing points is a process to distribute a set of point features across a specific area. This is useful for a variety of reasons, such as to improve the visual representation of data from closely clustered points and for spatial modeling or simulation to create more realistic representations of features are distributed in the real world.
This article describes the workflow to disperse geocoded point features within a polygon using ArcPy in ArcGIS Pro.
Note: This workflow requires the full script to run in the ArcGIS Pro Python window.
import arcpy import random
def main(): fcPoints = r"<filePath1>" fcPolygon = r"<filePath2>" 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
if __name__ == '__main__': main()
The code block below demonstrates the full working script.
import arcpy import random def main(): fcPoints = r"C:\Users\Documents\ArcGIS\Projects\MyProject22\MyProject22.gdb\point" fcPolygon = r"C:\Users\Documents\ArcGIS\Projects\MyProject22\MyProject22.gdb\TestPolygons" 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 if __name__ == '__main__': main()
The points are dispersed in the polygon as demonstrated in the image below.
Get help from ArcGIS experts
Download the Esri Support App