Пример #1
0
    def pad(self, pad_width, mode="constant", constant_values=0):
        """Pad raster in all directions.

        :param pad_width: pad width in pixels, int.
        :param mode: str, how to pad, see rasterio.pad. Optional (default: 'constant')
        :param constant_values: nodata value, padding should be filled with, optional (default: 0)
        :return: closed, buffered dataset in memory
        """
        destination = np.zeros(
            (
                self.dataset.count,
                self.__arr.shape[1] + 2 * pad_width,
                self.__arr.shape[2] + 2 * pad_width,
            ),
            self.__arr.dtype,
        )

        for i in range(0, self.dataset.count):
            destination[i], transform = rasterio.pad(
                self.__arr[i],
                self.dataset.transform,
                pad_width,
                mode,
                constant_values=constant_values,
            )

        self.__arr = destination
        self.__update_dataset(self.dataset.crs,
                              transform,
                              nodata=self.dataset.nodata)
Пример #2
0
def pad_extent(src, src_transform, dst_transform, src_crs, dst_crs, **kwargs):
    """
    Pad the extent of `src` by an equivalent of one cell of the target raster.

    This ensures that the array is large enough to not be treated as nodata in
    all cells of the destination raster. If src.ndim > 2, the function expects
    the last two dimensions to be y,x.
    Additional keyword arguments are used in `np.pad()`.
    """
    if src.size == 0:
        return src, src_transform

    left, top, right, bottom = *(src_transform * (0, 0)), *(src_transform *
                                                            (1, 1))
    covered = transform_bounds(src_crs, dst_crs, left, bottom, right, top)
    covered_res = min(abs(covered[2] - covered[0]),
                      abs(covered[3] - covered[1]))
    pad = int(dst_transform[0] // covered_res * 1.1)

    kwargs.setdefault('mode', 'constant')

    if src.ndim == 2:
        return rio.pad(src, src_transform, pad, **kwargs)

    npad = ((0, 0), ) * (src.ndim - 2) + ((pad, pad), (pad, pad))
    padded = np.pad(src, npad, **kwargs)
    transform = list(src_transform)
    transform[2] -= pad * transform[0]
    transform[5] -= pad * transform[4]
    return padded, rio.Affine(*transform[:6])
Пример #3
0
def test_pad():
    arr = numpy.ones((10, 10))
    trans = affine.Affine(1.0, 0.0, 0.0, 0.0, -1.0, 10.0)
    arr2, trans2 = rasterio.pad(arr, trans, 2, 'edge')
    assert arr2.shape == (14, 14)
    assert trans2.xoff ==  -2.0
    assert trans2.yoff ==  12.0