Skip to content Skip to sidebar Skip to footer

Stacking Small Polygons Inside Another Bigger One

I have the following shapely pixel polygon big_poly = Polygon([(94.5, 77.0), (92.5, 53.0), (87.5, 45.0), (66.0, 20.5),

Solution 1:

Here is what we could do:

  1. Construct two arrays of X and Y coordinates based on the boundaries of the given polygon
  2. Construct a grid of rectangles covering completely the given polygon by iterating over the consecutive pairs of X and Y coordinates
  3. Filter out those rectangles that don't lie completely inside the polygon

This is, most probably, not the most efficient solution, but it performs well for your case:

dx = 4
dy = 6
xmin, ymin, xmax, ymax = big_poly.bounds
xs = np.arange(np.floor(xmin), np.ceil(xmax) + 1, dx) 
ys = np.arange(np.floor(ymin), np.ceil(ymax) + 1, dy)
rectangles = (Polygon([(x_start, y_start), (x_end, y_start), 
                       (x_end, y_end), (x_start, y_end)])
              for x_start, x_end in zip(xs, xs[1:])
              for y_start, y_end in zip(ys, ys[1:]))
rectangles = [rectangle for rectangle in rectangles 
              if big_poly.contains(rectangle)]

enter image description here

Post a Comment for "Stacking Small Polygons Inside Another Bigger One"