<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: An arcpy solution for randomizing overlapping points within a shape? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/an-arcpy-solution-for-randomizing-overlapping/m-p/1136114#M63564</link>
    <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/dispersing-geocoded-points-within-a-polygon/m-p/526742#M41256" target="_blank"&gt;Solved: Re: Dispersing geocoded points within a Polygon - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Hope the following script works for you (Courtesy&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1108"&gt;@XanderBakker&lt;/a&gt;&amp;nbsp;and ArcPy Team)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#-------------------------------------------------------------------------------
# Name:        Disperse3.py
# Purpose:     Disperse points in multiple polygons
# Author:      arcpy Team
#              http://arcpy.wordpress.com/2013/06/07/disperse-overlapping-points/
# Created:     02-dec-2013
#-------------------------------------------------------------------------------

import arcpy
import random

def main():
    fcPoints = r"C:\Project\_Forums\Disperse\test.gdb\points3"
    fcPolygon = r"C:\Project\_Forums\Disperse\test.gdb\Polygons"
    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]):
                # I changed code here!
                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()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; *Change the path of the point and polygon feature classes in Line No. 13/14.&lt;/P&gt;</description>
    <pubDate>Sat, 22 Jan 2022 02:03:38 GMT</pubDate>
    <dc:creator>JayantaPoddar</dc:creator>
    <dc:date>2022-01-22T02:03:38Z</dc:date>
    <item>
      <title>An arcpy solution for randomizing overlapping points within a shape?</title>
      <link>https://community.esri.com/t5/python-questions/an-arcpy-solution-for-randomizing-overlapping/m-p/1136111#M63563</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I have a shapefile layer with polygons for the 50 U.S. states ("States"). I also have a point featureclass with potentially many overlapping points at the centroid of each state polygon ("Dots").&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I want to use the CreateRandomPoints_management() function to find new placements for all the points in each state, essentially creating a 1:1 dot density map with clickable point features.&lt;/P&gt;&lt;P&gt;Here's the workflow I have envisioned:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Loop through and select each state, one by one&lt;/LI&gt;&lt;LI&gt;Get the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;count&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;of points contained within each state polygon boundary&lt;/LI&gt;&lt;LI&gt;Use CreateRandomPoints_management to generate&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;count&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;number of randomized points within each state boundary&lt;/LI&gt;&lt;LI&gt;Move the original points to the new random locations and then delete the random points layer -or- Add fields and assign values to the newly created random points layer from the original points [Whichever's simpler and more reliable]&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I've tried several times to write this code, but my arcpy chops aren't that great yet. Any ideas or examples would be greatly appreciated!&lt;/P&gt;</description>
      <pubDate>Sat, 22 Jan 2022 01:18:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/an-arcpy-solution-for-randomizing-overlapping/m-p/1136111#M63563</guid>
      <dc:creator>BrianBowers1</dc:creator>
      <dc:date>2022-01-22T01:18:41Z</dc:date>
    </item>
    <item>
      <title>Re: An arcpy solution for randomizing overlapping points within a shape?</title>
      <link>https://community.esri.com/t5/python-questions/an-arcpy-solution-for-randomizing-overlapping/m-p/1136114#M63564</link>
      <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-questions/dispersing-geocoded-points-within-a-polygon/m-p/526742#M41256" target="_blank"&gt;Solved: Re: Dispersing geocoded points within a Polygon - Esri Community&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Hope the following script works for you (Courtesy&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1108"&gt;@XanderBakker&lt;/a&gt;&amp;nbsp;and ArcPy Team)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#-------------------------------------------------------------------------------
# Name:        Disperse3.py
# Purpose:     Disperse points in multiple polygons
# Author:      arcpy Team
#              http://arcpy.wordpress.com/2013/06/07/disperse-overlapping-points/
# Created:     02-dec-2013
#-------------------------------------------------------------------------------

import arcpy
import random

def main():
    fcPoints = r"C:\Project\_Forums\Disperse\test.gdb\points3"
    fcPolygon = r"C:\Project\_Forums\Disperse\test.gdb\Polygons"
    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]):
                # I changed code here!
                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()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; *Change the path of the point and polygon feature classes in Line No. 13/14.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Jan 2022 02:03:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/an-arcpy-solution-for-randomizing-overlapping/m-p/1136114#M63564</guid>
      <dc:creator>JayantaPoddar</dc:creator>
      <dc:date>2022-01-22T02:03:38Z</dc:date>
    </item>
    <item>
      <title>Re: An arcpy solution for randomizing overlapping points within a shape?</title>
      <link>https://community.esri.com/t5/python-questions/an-arcpy-solution-for-randomizing-overlapping/m-p/1136129#M63565</link>
      <description>&lt;P&gt;Thanks, Jayanta! That function does look like it might do what I need.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Jan 2022 11:10:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/an-arcpy-solution-for-randomizing-overlapping/m-p/1136129#M63565</guid>
      <dc:creator>BrianBowers1</dc:creator>
      <dc:date>2022-01-22T11:10:15Z</dc:date>
    </item>
  </channel>
</rss>

