Simple Spatial Routines




Pay Notebook Creator: Roy Hyunjin Han0
Set Container: Numerical CPU with TINY Memory for 10 Minutes 0
Total0

Create Random Polygons in Region of Interest

{ 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 }

In [59]:
# CrossCompute
region_geotable_path = 'selected-region.csv'
random_polygon_count = 10
buffer_radius_in_meters = 5000
maximum_vertex_count = 5
target_folder = '/tmp'
In [60]:
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
In [61]:
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)
Out[61]:
(444959.1859043865, 4061016.646204741)
In [62]:
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()
Out[62]:
<shapely.geometry.polygon.Polygon at 0x7f6cefb5a9b0>
In [64]:
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]
In [16]:
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_square_geotable_path = /tmp/random-squares.csv

Generated Polygons

{ random_polygon_geotable : Generated Polygons ? Random polygons generated in region of interest }