{ region_geotable : Region of Interest ? Generate random polygons inside this region }
{ random_polygon_count : Random Polygon Count ? Generate this number of random polygons }
{ buffer_radius_in_meters : Buffer Radius in Meters ? Set approximate radius of circle circumscribing each random polygon }
{ maximum_vertex_count : Maximum Vertex Count ? Set maximum number of vertices for each random polygon }
# CrossCompute
region_geotable_path = 'selected-region.csv'
random_polygon_count = 10
buffer_radius_in_meters = 5000
maximum_vertex_count = 5
target_folder = '/tmp'
import geotable
from shapely.ops import unary_union
proj4 = geotable.load_utm_proj4(region_geotable_path)
region_geotable = geotable.load(region_geotable_path, target_proj4=proj4)
region_geometry = unary_union(region_geotable.geometries)
region_bounds = region_geometry.bounds
import random
def get_random_xy(min_x, min_y, max_x, max_y):
x = random.uniform(min_x, max_x)
y = random.uniform(min_y, max_y)
return x, y
get_random_xy(*region_bounds)
from shapely.geometry import MultiPoint, Point
def get_random_square():
xy = get_random_xy(*region_bounds)
buffer = Point(xy).buffer(buffer_radius_in_meters)
return buffer.envelope
def get_random_polygon():
square = get_random_square()
vertex_xys = [get_random_xy(*square.bounds) for i in range(maximum_vertex_count)]
return MultiPoint(vertex_xys).convex_hull
get_random_polygon()
random_polygons = []
while len(random_polygons) < random_polygon_count:
polygons = [get_random_polygon() for i in range(random_polygon_count)]
random_polygons.extend(filter(region_geometry.contains, polygons))
random_polygons = random_polygons[:random_polygon_count]
from os.path import join
t = geotable.GeoTable()
t['geometry_object'] = random_polygons
t['geometry_proj4'] = proj4
target_path = join(target_folder, 'random-polygons.csv')
t.save_csv(target_path, target_proj4=geotable.LONGITUDE_LATITUDE_PROJ4)
print('random_polygon_geotable_path = %s' % target_path)
{ random_polygon_geotable : Generated Polygons ? Random polygons generated in region of interest }