Reputation: 145
I am trying to plot out n evenly spaced points inside a polygon which is the site shown below on a google map. Right now, I've managed to do this using gmplot.
My aim is to create a grid of points inside the polygon.
My recent approach is using np.meshgrid and then selecting random points from the grid. Below is the code which I use to get the points. But,the result is not what I expected.
def random_points_within(poly, num_points):
min_x, min_y, max_x, max_y = poly.bounds
x = (np.linspace(min_x,max_x,1000))
y= (np.linspace(min_y,max_y,1000))
xx,yy = np.meshgrid(x,y,sparse=True)
xx=xx[0]
points = []
while len(points) < num_points:
random_point = Point([random.choice(xx), random.choice(yy)])
if (random_point.within(poly)):
points.append(list(random_point.coords))
print(min_x,max_x,max_y,min_y)
return points
#poly is a polygon created by the edges for the site
Update:
Tried iterating through all the points on the meshgrid and ended up getting the below image.
def random_points_within(poly, num_points):
min_x, min_y, max_x, max_y = poly.bounds
x = (np.linspace(min_x,max_x,num_points))
y= (np.linspace(min_y,max_y,num_points))
xx,yy = np.meshgrid(x,y,sparse=True)
xx = xx.reshape((np.prod(xx.shape),))
yy = yy.reshape((np.prod(yy.shape),))
points = []
for (x,y) in zip(xx,yy):
random_point = Point([x, y])
if (random_point.within(poly)):
points.append(list(random_point.coords))
print(min_x,max_x,max_y,min_y)
return points,xx,yy
Output:
Is there a way to get evenly spaced points ?
Upvotes: 0
Views: 2457
Reputation: 3648
Since I don't have access to the poly object best I can do is make a guess:
def random_points_within(poly, num_points):
min_x, min_y, max_x, max_y = poly.bounds
x = (np.linspace(min_x,max_x,num_points))
y= (np.linspace(min_y,max_y,num_points))
xx,yy = np.meshgrid(x,y,sparse=True)
xx = xx.reshape((np.prod(xx.shape),))
yy = yy.reshape((np.prod(yy.shape),))
points = []
for x in xx:
for y in yy:
random_point = Point([x, y])
if (random_point.within(poly)):
points.append(list(random_point.coords))
print(min_x,max_x,max_y,min_y)
return points,xx,yy
The only change I made compared to your program is that it loops both over xx and yy so you'll cover an area rather than a line.
Upvotes: 1