I have a raster that I have resample using rasterio
, where img.shape
is (5490, 5490)
if img.res[1] == 20:
img = img.read(
out_shape=(img.count, img.height * 2, img.width * 2),
resampling=Resampling.nearest)
The Resampling
function of rasterio
is giving me the result as an array
img
array([[[2014, 2014, 1999, ..., 1705, 2136, 2136],
[2014, 2014, 1999, ..., 1705, 2136, 2136],
[1997, 1997, 1947, ..., 1709, 1769, 1769],
...,
[1871, 1871, 1975, ..., 1868, 1966, 1966],
[1817, 1817, 1892, ..., 1824, 1870, 1870],
[1817, 1817, 1892, ..., 1824, 1870, 1870]]], dtype=uint16)
However, in my next step I want to use rasterio
again to mask my raster
with a shapefile
using the following code:
out_image, out_transform = rasterio.mask.mask(img, features, crop=True)
This is not working as the input for mask
(img
) is not a rasterio
object but an array
.
AttributeError: 'numpy.ndarray' object has no attribute 'nodata'
What should be the procedure in this case. From other posts I have seen that an option would be to rasterize my shapefile
and read it as an array
but I would like avoid work-around solutions.
Best Answer
It's a bit of a pain, but you need to write the resampled numpy array to a rasterio
Dataset
(either on file or in memory) and adjust the transform to match the resampling..Here's an example of both: