I have a use case where given a polygon on the US map, I need to generate n rectangles within the polygon defined on the US map. I have been trying stuff with shapely, but stuck.
So the code would work in this way.
- Define the upper level polygon
poly = Polygon([(-123.52380921651545,44.92842807389871), (-115.78943421651545,32.651610397456906),(-81.95154359151545,33.71815014381751),(-78.74353577901545,39.29454038362893),(-91.57556702901545,42.223006851069464),(-106.29724671651545,45.207760924293545),(-123.52380921651545,44.92842807389871)])
- Write function which takes polygon defined in step 1, and number of rectangles that need to be created within that polygon.
def random_polygon (poly, num_polygons, points_per_poly=4):
min_x, min_y, max_x, max_y = poly.bounds
random_poly = []
while len(random_poly) < num_polygons:
- What is the syntax in this piece, where I generate a random rectangle, where the length and breadth is fixed, but the co-ordinates are different for each rectangle. Once the rectangle is generated I check if it is within bounds of polygon created in Step 1. If yes I add it to random_poly and continue.