I am masking a raster file using rasterio. I am doing this using the mask
function. The result of the masking changes the value of the pixels when loaded to GIS.
import fiona
import rasterio
import rasterio.mask
with fiona.open("test_polygon.json", "r") as shapefile:
shapes = [feature["geometry"] for feature in shapefile]
with rasterio.open("exampe.tif") as src:
out_image, out_transform = rasterio.mask.mask(src, shapes, crop=True)
out_meta = src.meta
out_meta.update({"driver": "GTiff",
"dtype": rasterio.float32,
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})
with rasterio.open("swr_masked.tif", "w", **out_meta) as dest:
dest.write(out_image)
The example files are located HERE.
When loaded to a GIS application like QGIS, the value of the masked data changes. It is like it was changed to another data type like int16 or something. I specified the dtype in writing to be the same with the input float32. But still have the same result.