Exemple #1
0
def get_tiles(ds, width, height):
    """
    Takes as input the a tile and returns chips.
    ==========================================
    :width: Desired width of each chip.
    :height: Desired height of each chip.
    :out_path: Desired output file storage folder.
    :in_path: Folder where the input tile is stored.
    :input_filename: Name of the input tile
    :output_filename: Desired output file pattern
    ===========================================
    """
    ncols = ds.meta["width"]
    nrows = ds.meta["height"]

    offsets = product(range(0, ncols, width), range(0, nrows, height))

    big_window = windows.Window(col_off=0,
                                row_off=0,
                                width=ncols,
                                height=nrows)

    for col_off, row_off in offsets:

        window = windows.Window(
            col_off=col_off,
            row_off=row_off,
            width=width,
            height=height,
        ).intersection(big_window)

        transform = windows.transform(window, ds.transform)
        yield window, transform
def get_tiles(src, nr_tiles):

    if nr_tiles < 36:

        scale = int(np.ceil(np.sqrt(nr_tiles)))
        size = int(src.meta['width'] / scale)

        #get original image dimension
        ncols, nrows = src.meta['width'], src.meta['height']

        #define offsets (iterator) based on specified tile size
        offsets = product(range(0, ncols, size), range(0, nrows, size))

        #create a large tile from original image dimension
        big_window = windows.Window(col_off=0,
                                    row_off=0,
                                    width=ncols,
                                    height=nrows)

        #find intersection with the large tile and get the tile dimension and affine
        tiles = []
        for col_off, row_off in offsets:
            tile = windows.Window(col_off=col_off,
                                  row_off=row_off,
                                  width=size,
                                  height=size).intersection(big_window)
            tile_affine = windows.transform(tile, src.transform)
            tiles.append((tile, tile_affine))
        return tiles
    else:
        print('Try number of tiles < 36')
Exemple #3
0
def get_tiles(dataset, tile_a, tile_b):
    """Creates rasterio.windows for a given Band with the size of
    tile_a x tile_b
    :parameter:
    Open Source,
    tile-width in m,
    tile-height in m,
    :returns:
    rasterio window """

    # calculate Width and Height as Distance from Origin
    tile_x, tile_y = (dataset.bounds.left + tile_a,
                      dataset.bounds.top - tile_b)
    height, width = dataset.index(tile_x, tile_y)

    # get max rows and cols of dataset
    nols, nrows = dataset.meta['width'], dataset.meta['height']
    # create offset for the window processing
    offsets = product(range(0, nols, width), range(0, nrows, height))
    # create big_window around the whole dataset
    big_window = windows.Window(col_off=0, row_off=0, width=nols, height=nrows)

    # create custom set blocks
    for col_off, row_off in offsets:
        # get windows with the custom parameters
        # until it intersects with
        # the boundaries of the source dataset
        window = windows.Window(col_off=col_off,
                                row_off=row_off,
                                width=width,
                                height=height).intersection(big_window)
        transform = windows.transform(window, dataset.transform)
        yield window, transform
def get_tiles(datasource, width=64, height=64, fold=1, extension=16):
    """Function to create the product matrix for the tiles from the big picture.
    Width & Height corresponds to the tile dimensions, whereas fold refers to the overlap of the tiles.
    1 = no overlap, 2 = overlapping by a half tile, and so on."""
    ncols, nrows = datasource.meta['width'], datasource.meta['height']
    offsets = product(range(0, ncols, width//fold), range(0, nrows, height//fold))  # control the level of overlap
    big_window = windows.Window(col_off=0, row_off=0, width=ncols, height=nrows)
   
    # defining window and transfor functions (original and extended)
    for col_off, row_off in offsets:
        # creating the original tile
        window_original = windows.Window(col_off=col_off, row_off=row_off, width=width, height=height).intersection(big_window)
        transform_original = windows.transform(window_original, datasource.transform)        
        
        # creating the extended boundary tile
        col_off_extended = col_off - extension
        row_off_extended = row_off - extension
        width_extended = width + extension*2
        height_extended = height + extension*2

        # windowing with the extended tile
        window_extended = windows.Window(col_off = col_off_extended, 
                                         row_off = row_off_extended, 
                                         width = width_extended, 
                                         height = height_extended).intersection(big_window)

        transform_extended = windows.transform(window_extended, datasource.transform)
                
        yield window_original, transform_original, window_extended, transform_extended
def generate_tiles(input_tif, width=500, height=500):
    n_cols, n_rows = input_tif.meta['width'], input_tif.meta['height']
    offsets = product(range(0, n_cols, width), range(0, n_rows, height))
    full_window = windows.Window(col_off=0, row_off=0, width=n_cols, height=n_rows)
    for col_off, row_off in offsets:
        window=windows.Window(col_off=col_off, row_off=row_off, width=width, height=height).intersection(full_window)
        transform = windows.transform(window, input_tif.transform)
        yield window, transform
Exemple #6
0
def get_tiles(ds, width, height):
	nols, nrows = ds.meta['width'], ds.meta['height']
	offsets = product(range(0, nols, width), range(0, nrows, height))
	big_window = windows.Window(col_off=0, row_off=0, width=nols, height=nrows)
	for col_off, row_off in  offsets:
		window =windows.Window(col_off=col_off, row_off=row_off, width=width, height=height).intersection(big_window)
		transform = windows.transform(window, ds.transform)
		yield window, transform
Exemple #7
0
def get_tiles(ds, width=256, height=256):
    nols, nrows = ds.meta['width'], ds.meta['height']
    offsets = product(range(0, nols, int(width / 2)),
                      range(0, nrows, int(height / 2)))
    big_window = windows.Window(col_off=0, row_off=0, width=nols, height=nrows)
    for col_off, row_off in offsets:
        window = windows.Window(col_off=col_off,
                                row_off=row_off,
                                width=width,
                                height=height).intersection(big_window)
        if window.width == width and window.height == height:
            transform = windows.transform(window, ds.transform)
            yield window, transform
Exemple #8
0
 def _get_window_transform(width, height):
     num_rows, num_cols = src.meta['height'], src.meta['width']
     offsets = itertools.product(range(0, num_cols, width),
                                 range(0, num_rows, height))
     big_window = windows.Window(col_off=0,
                                 row_off=0,
                                 width=num_cols,
                                 height=num_rows)
     for col_off, row_off in offsets:
         window = windows.Window(col_off=col_off,
                                 row_off=row_off,
                                 width=width,
                                 height=height).intersection(big_window)
         transform = windows.transform(window, src.transform)
         yield window, transform
Exemple #9
0
def _get_window_transform(width, height, transform, num_tile_subdivisions):
    dst_width = width // num_tile_subdivisions
    dst_height = height // num_tile_subdivisions
    offsets = itertools.product(range(0, width, dst_width),
                                range(0, height, dst_height))
    big_window = windows.Window(col_off=0,
                                row_off=0,
                                width=width,
                                height=height)
    for col_off, row_off in offsets:
        dst_window = windows.Window(col_off=col_off,
                                    row_off=row_off,
                                    width=dst_width,
                                    height=dst_height).intersection(big_window)
        dst_transform = windows.transform(dst_window, transform)
        yield dst_window, dst_transform
Exemple #10
0
def open_chosen_bands(dirpath, chosen_bands, size, position):

    # the directory should only contain 13 image files
    # representing bands sensed by sentinel-2
    # the files should be named according to the bands
    # they represent: B01.jp2, B02.jp2 ...

    bands = listdir(dirpath)
    bands.sort()
    bands = [Path(dirpath, band) for n, band in enumerate(bands)]

    # TODO: check image limits
    coef_h, coef_w, mh, mw = get_size_coefs(dirpath)

    tile_layers = []

    for band in chosen_bands:
        with rasterio.open(bands[band]) as src:
            # Size and position are set for the tile from the band with
            # the lowest resolution (among the chosen), thus the bands
            # with higher resolutoins have proportionally bigger tiles.
            img = src.read(1, window=windows.Window(\
                position[0] // coef_h[band],
                position[1] // coef_w[band],
                size[0] // coef_h[band], size[1] // coef_w[band]))
        # resize tiles to the size of the biggest tile
        img = cv2.resize(
            img, (img.shape[1] * coef_h[band], img.shape[0] * coef_w[band]))
        resid_y = size[0] - img.shape[0]
        resid_x = size[1] - img.shape[1]
        img = np.pad(img, ((0, resid_y), (0, resid_x)),
                     'constant',
                     constant_values=((0, 0), (0, 0)))
        tile_layers.append(img)
    return tile_layers
Exemple #11
0
def test_window_rt_north_down(dataset):
    """Get correct window for full dataset extent"""
    left, top = dataset.transform * (0, 0)
    right, bottom = dataset.transform * (dataset.width, dataset.height)
    assert_windows_almost_equal(
        dataset.window(left, bottom, right, top),
        windows.Window(0, 0, dataset.width, dataset.height))
Exemple #12
0
 def get_values(self, window=None, band=None, **kwargs):
     i = 1 if band is None else band
     window = windows.Window(0, 0, self._src.shape[1], self._src.shape[0]) \
         if window is None else window
     if window is not None:
         assert isinstance(window, windows.Window)
     return self._src.read(i, window=window, **kwargs)
Exemple #13
0
 def get_iter_windows(width,
                      height,
                      chunk_size=0,
                      overlap=0,
                      row_off=0,
                      col_off=0):
     h = chunk_size
     for i in range(int(row_off), int(row_off + height + chunk_size),
                    chunk_size):
         if i + h > row_off + height:
             h = height - i
             if h <= 0:
                 break
         w = chunk_size
         for j in range(int(col_off), int(col_off + width + chunk_size),
                        chunk_size):
             if j + w > col_off + width:
                 w = width - j
                 if w <= 0:
                     break
             o = overlap
             while j + w + o > width:
                 o -= 1
             w += o
             o = overlap
             while i + h + o > height:
                 o -= 1
             h += o
             yield windows.Window(j, i, w, h)
Exemple #14
0
 def values(self, *i, **kwargs):
     window = kwargs.pop('window', None)
     window = windows.Window(0, 0, self._src.shape[1], self._src.shape[0]) \
         if window is None else window
     if window is not None:
         assert isinstance(window, windows.Window)
     return self._src.read(*i, window=window, **kwargs)
Exemple #15
0
def get_window_tiles(ds: rasterio.io.DatasetReader,
                     height: int = 128,
                     width: int = 128,
                     **kwargs) -> List[rasterio.windows.Window]:
    """a generator for rasterio specific slices given a rasterio dataset

    Args:
        ds (rasterio.io.DatasetReader): a rasterio dataset object
        height (int): the height for the slice
        width (int): the width for the slice

    Yields:
        window (rasterio.windows.Window): slicing
    """
    # extract the row height from the dataset
    n_columns, n_rows = ds.meta["width"], ds.meta["height"]

    # create the offsets
    offsets = product(range(0, n_columns, width), range(0, n_rows, height))
    list_of_windows = []
    for col_offset, row_offset in offsets:
        iwindow = windows.Window(col_off=col_offset,
                                 row_off=row_offset,
                                 width=width,
                                 height=height,
                                 **kwargs)
        list_of_windows.append(iwindow)

    return list_of_windows
def test_window():
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        left, bottom, right, top = src.bounds
        dx, dy = src.res
        eps = 1.0e-8
        assert_window_almost_equals(
            src.window(left + eps, bottom + eps, right - eps, top - eps),
            windows.Window(0, 0, src.width, src.height))
        assert src.index(left + 400, top - 400) == (1, 1)
        assert src.index(left + dx + eps, top - dy - eps) == (1, 1)
        assert_window_almost_equals(
            src.window(left, top - 400, left + 400, top),
            windows.Window(0, 0, 400 / src.res[0], 400 / src.res[1]))
        assert_window_almost_equals(
            src.window(left, top - 2 * dy - eps, left + 2 * dx - eps, top),
            windows.Window(0, 0, 2, 2))
Exemple #17
0
    def test_get_tiles(self):
        for idx, each in enumerate(self.img.get_tiles(5, 5, 1)):
            self.assertIsInstance(each, windows.Window)
            if idx == 2578:
                self.assertEqual(each, windows.Window(col_off=79, row_off=649, width=7, height=7))

        self.assertEqual(idx, 20807)
def test_window_no_exception():
    with rasterio.open('tests/data/RGB.byte.tif') as src:
        left, bottom, right, top = src.bounds
        left -= 1000.0
        assert_window_almost_equals(
            src.window(left, bottom, right, top),
            windows.Window(-1000 / src.res[0], 0,
                           src.width + 1000 / src.res[0], src.height))
Exemple #19
0
def get_tiles(ds, width=256, height=256):
    out_window = []
    out_transform = []
    ncols, nrows = ds.meta['width'], ds.meta['height']
    offsets = product(range(0, ncols, width), range(0, nrows, height))
    big_window = windows.Window(col_off=0,
                                row_off=0,
                                width=ncols,
                                height=nrows)
    for col_off, row_off in offsets:
        window = windows.Window(col_off=col_off,
                                row_off=row_off,
                                width=width,
                                height=height).intersection(big_window)
        out_window.append(window)
        out_transform.append(windows.transform(window, ds.transform))
    return out_window, out_transform
    def get_tile(self, file, width=512, height=512, col_off=0, row_off=0):
        with rio.open(file, dtype='float32') as data:
            meta = data.meta.copy()
            return data.read().transpose(1, 2, 0).astype(rio.float32), meta

            ncols, nrows = meta['width'], meta['height']
            offsets = product(range(0, ncols, width), range(0, nrows, height))
            big_window = wnd.Window(col_off=0, row_off=0, width=ncols, height=nrows)

            window = wnd.Window(col_off=col_off * width, row_off=row_off * height,
                                width=width, height=height).intersection(big_window)

            transform = wnd.transform(window, data.transform)

            meta['transform'] = transform
            meta['width'], meta['height'] = window.width, window.height

            return data.read(window=window).transpose(1, 2, 0).astype(rio.float32), meta
Exemple #21
0
def get_window(file_path, width=512, height=512, col_off=0, row_off=0):
    with rio.open(file_path) as data:
        meta = data.meta.copy()

        ncols, nrows = meta['width'], meta['height']
        offsets = product(range(0, ncols, width), range(0, nrows, height))

        full_image = wnd.Window(col_off=0, row_off=0, width=ncols, height=nrows)

        window = wnd.Window(col_off=col_off * width, row_off=row_off * height,
                            width=width, height=height).intersection(full_image)

        transform = wnd.transform(window, meta['transform'])

        meta['transform'] = transform
        meta['width'], meta['height'] = window.width, window.height

        return data.read(window=window).transpose(1, 2, 0), meta
Exemple #22
0
 def get_x(self, window=None):
     window = windows.Window(0, 0, self._src.shape[1], self._src.shape[0]) \
         if window is None else window
     if window is not None:
         assert isinstance(window, windows.Window)
         width = window.width
     else:
         width = self.shape[0]
     x0, y0, x1, y1 = self.get_array_bounds(window)
     return np.linspace(x0, x1, width)
Exemple #23
0
 def get_y(self, window=None):
     window = windows.Window(0, 0, self._src.shape[1], self._src.shape[0]) \
         if window is None else window
     if window is not None:
         assert isinstance(window, windows.Window)
         height = window.height
     else:
         height = self.shape[1]
     x0, y0, x1, y1 = self.get_array_bounds(window)
     return np.linspace(y1, y0, height)
Exemple #24
0
def get_tiles(ds, tile_a, tile_b):
    """Tiles a band into blocks with the dimension of tile_a x tile_b
    :parameter: Band, tile-width and tile-height
    :returns: rasterio window """
    # calculate Width and Height as Distance from Origin
    x, y = (ds.bounds.left + tile_a, ds.bounds.top - tile_b)
    height, width = ds.index(x, y)

    nols, nrows = ds.meta['width'], ds.meta['height']
    offsets = product(range(0, nols, width), range(0, nrows, height))
    big_window = windows.Window(col_off=0, row_off=0, width=nols, height=nrows)

    # create custom set blocks
    for col_off, row_off in offsets:
        window = windows.Window(col_off=col_off,
                                row_off=row_off,
                                width=width,
                                height=height).intersection(big_window)
        transform = windows.transform(window, ds.transform)
        yield window, transform
Exemple #25
0
def get_patch_windows(img_size: ImageSize, patch_size: PatchSize, patch_residue: PatchResidue = None, img_transform=None) -> Iterator:
    img_w, img_h = img_size
    big_window = windows.Window(
        col_off=0, row_off=0, width=img_w, height=img_h)
    offsets = get_patch_offsets((img_w, img_h), patch_size, patch_residue)
    patch_w, patch_h = normalize_patch_size(patch_size)

    for col_off, row_off in offsets:
        window = windows.Window(
            col_off=col_off,
            row_off=row_off,
            width=patch_w,
            height=patch_h
        ).intersection(big_window)

        if img_transform:
            transform = windows.transform(window, img_transform)
            yield window, transform
        else:
            yield window
Exemple #26
0
def get_tiles(dataset,
              width=utils.constants.width,
              height=utils.constants.height):
    """
    Implementation from https://github.com/jesseniagonzalezv/App_segmentation_water_bodies
    :param dataset: data reader
    :param width: patch expected width
    :param height: patch expected height
    :return: crop windows and Affine transform
    """
    nols, nrows = dataset.meta['width'], dataset.meta['height']
    offsets = product(range(0, nols, width), range(0, nrows, height))
    big_window = windows.Window(col_off=0, row_off=0, width=nols, height=nrows)
    for col_off, row_off in offsets:
        window = windows.Window(col_off=col_off,
                                row_off=row_off,
                                width=width,
                                height=height).intersection(big_window)
        transform = windows.transform(window, dataset.transform)
        yield window, transform
Exemple #27
0
def overlapping_blocks(src, overlap=0, band=1, boundless=False):

    big_window = windows.Window(col_off=0,
                                row_off=0,
                                width=src.meta['width'],
                                height=src.meta['height'])
    for ji, window in src.block_windows(band):

        if overlap == 0:
            yield window

        else:
            window = windows.Window(col_off=window.col_off - overlap,
                                    row_off=window.row_off - overlap,
                                    width=window.width + overlap * 2,
                                    height=window.height + overlap * 2)
            if boundless:
                yield window
            else:
                yield window.intersection(big_window)
Exemple #28
0
def overlapping_tiles(src, overlap, width, height, boundless=False):
    """"width & height not including overlap i.e requesting a 256x256 window with 
        1px overlap will return a 258x258 window (for non edge windows)"""
    offsets = product(range(0, src.meta['width'], width),
                      range(0, src.meta['height'], height))
    big_window = windows.Window(col_off=0,
                                row_off=0,
                                width=src.meta['width'],
                                height=src.meta['height'])
    for col_off, row_off in offsets:

        window = windows.Window(col_off=col_off - overlap,
                                row_off=row_off - overlap,
                                width=width + overlap * 2,
                                height=height + overlap * 2)

        if boundless:
            yield window
        else:
            yield window.intersection(big_window)
Exemple #29
0
 def test_write_window(self):
     name = os.path.join(self.tempdir, "test_write_window.tif")
     a = np.ones((50, 50), dtype=rasterio.ubyte) * 127
     with rasterio.open(
             name, 'w',
             driver='GTiff', width=100, height=100, count=1,
             dtype=a.dtype) as s:
         s.write(a, indexes=1, window=windows.Window(10, 30, 50, 50))
     # subprocess.call(["open", name])
     info = subprocess.check_output(["gdalinfo", "-stats", name])
     self.assertTrue(
         "Minimum=0.000, Maximum=127.000, "
         "Mean=31.750, StdDev=54.993" in info.decode('utf-8'),
         info)
Exemple #30
0
def crop(pixel_collection, data_format, offsets):
    data, (bounds, data_crs), _ = pixel_collection
    left, bottom, right, top = offsets

    if _isimage(data_format):
        width, height, _ = data.shape
        t = transform.from_bounds(*bounds, width=width, height=height)

        data = data[top:height - bottom, left:width - right, :]

        cropped_window = windows.Window(left, top, width, height)
        cropped_bounds = windows.bounds(cropped_window, t)

        return PixelCollection(data, Bounds(cropped_bounds, data_crs))

    _, height, width = data.shape
    t = transform.from_bounds(*bounds, width=width, height=height)

    data = data[:, top:height - bottom, left:width - right]

    cropped_window = windows.Window(left, top, width, height)
    cropped_bounds = windows.bounds(cropped_window, t)

    return PixelCollection(data, Bounds(cropped_bounds, data_crs))