Help Saving .Tif File after Mask


Simon Tarr
 

I have just used rasterio.mask.mask() to crop a larger raster using a Django PolygonField() polygon:

# Open large raster file
file = rasterio.open('/data/corine2018_100m/test.tif') print(file) <open DatasetReader name='/data/corine2018_100m/test.tif' mode='r'>

# Dummy Django model object
obj = newJob.objects.create(job_loc="SRID=4326;POLYGON ((0.9063720703125029 52.32023207609735, 0.8239746093749998 52.10819209746323, 1.170043945312496 52.14191683166823, 1.170043945312496 52.31351619974807, 0.9063720703125029 52.32023207609735))")poly = ast.literal_eval(obj.job_loc.geojson)

# Grab PolygonField(), serialise to GeoJSON, put into a list
poly = ast.literal_eval(obj.job_loc.geojson)
poly = [poly]
print(poly)
[{'type': 'Polygon', 'coordinates': [[[0.906372070312503, 52.32023207609735], [0.823974609375, 52.10819209746323], [1.170043945312496, 52.14191683166823], [1.170043945312496, 52.31351619974807], [0.906372070312503, 52.32023207609735]]]}]

# Mask Raster
masked_raster = rasterio.mask.mask(file, poly, crop=True, filled=True, invert=False)
print(masked_raster)
(array([[[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[ 0, 12, 12, ..., 0, 0, 0],
[ 0, 12, 12, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0]]], dtype=uint8), Affine(0.0012806021160224688, 0.0, 0.8235730268502728,
0.0, -0.0012806021160261063, 52.32090505556345))

I now need to save the object masked_raster to disk. I have tried with the following code:

with rasterio.open('/data/my_output.tif', 'w', 
driver='GTiff',
height=masked_raster[0].shape[1],
width=masked_raster[0].shape[2],
count=1,
dtype=rasterio.ubyte,
crs=file.crs,
transform= # Not sure what this should map to!
) as outfile:
outfile.write(x, indexes=1)

But I get the error:

 File "rasterio/_io.pyx", line 1338, in rasterio._io.DatasetWriterBase.write
rasterio.errors.InvalidArrayError: Positional argument arr must be an array-like object

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!

 


Luke
 
Edited

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) 
 


Simon Tarr
 
Edited

Hi Luke, thanks for the reply. Your code has helped me move past my previous error.
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:
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)