mardi 14 juillet 2020

Python Shapely - Generating random Points in irregular shaped Polygon

Currently I have the function: (iterated in for loop to generate 100 points)

from shapely.geometry import Point

def new_position(poly1):
    min_x, min_y, max_x, max_y = poly1.bounds
    p = 0
    while p == 0:
        random_point = Point([random.uniform(min_x, max_x), random.uniform(min_y, max_y)])
        if (random_point.within((poly1)):
            return random_point

This returns a random point within the polygon (previously determined). It works for most shapes I have tested, though when I use an L-shape sometimes the points return a TypeError: 'Polygon' object is not iterable. I have pinpointed the issue to random_point.within((poly1), more specifically to inclusion of poly1 within the while loop. Does anyone know why I get this error? (Are there other methods for generating random points within the polygon?)




Aucun commentaire:

Enregistrer un commentaire