Skip to main content

With Shapely: https://shapely.readthedocs.io/en/latest/manual.html#polygons

The polygon in code below must be Polygon object.

import random
from shapely.geometry 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
    return list_of_points
import random
from shapely.geometry import Point

def generate_random(number, polygon):
    points = []
    minx, miny, maxx, maxy = polygon.bounds
    while len(points) < number:
        pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
        if polygon.contains(pnt):
            points.append(pnt)
    return points

With Shapely: https://shapely.readthedocs.io/en/latest/manual.html#polygons

The polygon in code below must be Polygon object.

import random
from shapely.geometry 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
    return list_of_points

With Shapely: https://shapely.readthedocs.io/en/latest/manual.html#polygons

The polygon in code below must be Polygon object.

import random
from shapely.geometry import Point

def generate_random(number, polygon):
    points = []
    minx, miny, maxx, maxy = polygon.bounds
    while len(points) < number:
        pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
        if polygon.contains(pnt):
            points.append(pnt)
    return points

With Shapely: http://toblerity.org/shapely/manual.html#polygonshttps://shapely.readthedocs.io/en/latest/manual.html#polygons

The polygon in code below must be Polygon object.

import random
from shapely.geometry 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
    return list_of_points

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

The polygon in code below must be Polygon object.

import random
from shapely.geometry 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
    return list_of_points

With Shapely: https://shapely.readthedocs.io/en/latest/manual.html#polygons

The polygon in code below must be Polygon object.

import random
from shapely.geometry 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
    return list_of_points
missing import
Source Link
bugmenot123
  • 11.3k
  • 3
  • 38
  • 75

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

The polygon in code below must be Polygon object.

import random
from shapely.geometry 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
    return list_of_points

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

The polygon in code below must be Polygon object.

from shapely.geometry 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
    return list_of_points

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

The polygon in code below must be Polygon object.

import random
from shapely.geometry 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
    return list_of_points
edited "from shapely" to "from shapely.geometry"
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404
Loading
added 31 characters in body
Source Link
dmh126
  • 6.8k
  • 2
  • 22
  • 36
Loading
Source Link
dmh126
  • 6.8k
  • 2
  • 22
  • 36
Loading