Skip to content

Instantly share code, notes, and snippets.

@Chronum94
Last active August 4, 2017 06:40
Show Gist options
  • Save Chronum94/0a25e798abbe98f3bb508154c0dc1f49 to your computer and use it in GitHub Desktop.
Save Chronum94/0a25e798abbe98f3bb508154c0dc1f49 to your computer and use it in GitHub Desktop.
ShapeTest
class Rect2D():
"""TODO"""
def __init__(self, center, size):
self.xcen, self.ycen = center
self.xspan, self.yspan = size
self. verts = [(self.xcen - self.xspan/2, self.ycen - self.yspan/2),
(self.xcen + self.xspan/2, self.ycen - self.yspan/2),
(self.xcen + self.xspan/2, self.ycen + self.yspan/2),
(self.xcen - self.xspan/2, self.ycen + self.yspan/2)]
def mask(self, image_x_size=0, image_y_size=0):
"""TODO"""
# If a domain of the raster is not specified, create default domain.
if image_x_size == 0:
image_x_size = 1.1*self.xspan
if image_y_size == 0:
image_y_size = 1.1*self.yspan
# Maybe I'll let people change these later...
nx, ny = 100, 100
# Create path from vertices
path = Path(self.verts)
# Create and flatten meshgrid. Use meshgrid to get mask. Reshape back.
x_line = np.linspace(self.xcen - image_x_size/2,
self.xcen + image_x_size/2, nx)
y_line = np.linspace(self.ycen - image_y_size/2,
self.ycen + image_y_size/2, ny)
x_grid, y_grid = np.meshgrid(x_line, y_line)
x_flat, y_flat = x_grid.flatten(), y_grid.flatten()
points = np.vstack([x_flat, y_flat]).T
# Precise point where mask is created.
mask = path.contains_points(points)
mask = mask.reshape([nx, ny])
return mask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment