Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Dispersing geocoded points within a Polygon

$
0
0
I have posted the following question within the geocoding forum:
http://forums.arcgis.com/threads/977...thin-a-Polygon

But perhaps it is more suitable to be listed here as it is more a Python question. Please use the link above for answers to prevent double posting. Please excuse the circumstances.

Original post:

Hi,

I have a problem with dispersing geocoded points (randomely/)equally within a polygon. More specific I have geocoded adresses using zip codes and would like to disperse them within a polygon. I know that it is possible to disperse markers but I would like to disperse the points. Dispersing them randomely would be not a problem as it is i.e. possible with Geowizard.

Therefore, I have searched the forums and have found some solutions for Avenue but in my ArcGIS 10 this obviously is not working. Based on the post here: http://arcpy.wordpress.com/2013/06/0...s/#comment-220 I have tried to develop a script. But it is not working.

To visualize what I would like to achieve:

The geocoding result (with only one ZIP/normally I have around 500):
Attachment 29401

And the result I would like to receive:

Attachment 29402

Can anyone advice what could be changed to make it work? Is it necessary to have the x, y already in the table defined as columns?

Thank you very much!

Code:

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)])


Viewing all articles
Browse latest Browse all 2485

Trending Articles