Date
1 - 3 of 3
Help Saving .Tif File after Mask
Simon Tarr <si.tarr@...>
I have just used rasterio.mask.mask() to crop a larger raster using a Django PolygonField() polygon:
I now need to save the object masked_raster to disk. I have tried with the following code:
I'm clearly passing the wrong data from masked_raster and I don't understand what I'm supposed to input for the transform argument. Any help would be gratefully received!
|
|
rasterio.mask.mask returns a two element tuple - element 0 is the array, element 1 is the transform. Try something like (untested):
# Mask Raster masked_raster, transform = rasterio.mask.mask(file, poly, crop=True, filled=True, invert=False)
with rasterio.open('/data/my_output.tif', 'w',
driver='GTiff',
height=masked_raster.shape[1],
width=masked_raster.shape[2],
count=1,
dtype=rasterio.ubyte,
crs=file.crs,
transform=transform
) as outfile:
outfile.write(masked_raster, indexes=1)
|
|
Hi Luke, thanks for the reply. Your code has helped me move past my previous error.
toggle quoted message
Show quoted text
I had to delete the indexes argument when calling `write' but apart from that, it works like a charm. Thank you for your help! On Wed, Apr 7, 2021 at 02:41 AM, Luke wrote:
|
|