snippet for minimum bounding snapped window?


Gregory, Matthew
 

Hi all,

Often I'm given unsnapped coordinates and want to find the minimum bounding snapped box based on a rasterio Dataset. I'm guessing someone here has a better way of doing it or I'm missing a method somewhere.

Here's a function with some comments:

def get_minimum_snapped_extent(src, bounds):
# Get window from bounds (assume bounds is sequence of
# left, bottom, right, top
w1 = src.window(*bounds)

# Round to include top-left corner (op="floor" is default)
w2 = w1.round_offsets()

# Union these to get a new window
w3 = rasterio.windows.union(w1, w2)

# Round shape to buffer to bottom-right corner
w4 = w3.round_shape(op="ceil")

# Return the coordinates associated with this window
return src.window_bounds(w4)

Obviously, I don't need all the temp variables, but is there a more succinct way of doing this?

thanks, matt

Join main@rasterio.groups.io to automatically receive all group messages.