Skip to main content
1 of 6
dmh126
  • 6.8k
  • 2
  • 22
  • 36

With Shapely: http://toblerity.org/shapely/manual.html#polygons

The polygon in code below must be Polygon object.

from shapely import Point
def generate_random(number, polygon):
    list_of_points = []
    minx, miny, maxx, maxy = polygon.bounds
    counter = 0
    while counter < number:
        pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
        if polygon.contains(pnt):
            list_of_points.append(pnt)
            counter += 1
dmh126
  • 6.8k
  • 2
  • 22
  • 36