def test_window_class_toslices(): """Test Window.toslices""" window = Window(row_off=0, col_off=1, num_rows=100, num_cols=200) yslice, xslice = window.toslices() assert yslice.start == 0 assert yslice.stop == 100 assert xslice.start == 1 assert xslice.stop == 201
def make_windows_iterator(image_size, window_size, valid_pixels, validity_threshold): """Iterates of patch windows corresponding to valid pixel positions Args: image_size (tuple[int]): (height, width) in pixels window_size (tuple[int]): (window_height, window_width) in pixels valid_pixels (np.ndarray): (height, width) boolean array validity_threshold (float): percentage of valid pixels in a window to be considered valid Yields: type: rasterio.windows.Window """ # Make full raster window object height, width = image_size full_image_window = Window(col_off=0, row_off=0, width=width, height=height) # Create offsets range to iterate on window_height, window_width = window_size col_row_offsets = product(range(0, width, window_width), range(0, height, window_height)) for window_idx, (col_offset, row_offset) in enumerate(col_row_offsets): # Create window instance window = Window(col_off=col_offset, row_off=row_offset, width=window_width, height=window_height) # Verify the window is valid window_valid_pixels = valid_pixels[window.toslices()] is_valid = window_valid_pixels.sum( ) / window_valid_pixels.size > validity_threshold if not is_valid: continue # Intersect and return window window = window.intersection(full_image_window) yield window_idx, window