I have a numpy array (called out_window
) with the following values:
out_window:
[[[499 498 481]
[494 490 464]
[483 480 -1]
[482 -1 -1]]]
This numpy array was obtained by clipping a raster file using a polygon feature and altering only those elements with value different to -1 (-1 is NoData)
I'm trying to update that same raster file with those new values.
import rasterio
import geopandas
raster_in = r'C:\.......\raster_in_file.tif'
raster_out = r'C:\.......\raster_out_file.tif'
copy(raster_in, raster_out)
polyg = gpd.read_file(gdb_vml_polygs, driver='FileGDB', layer=fc_vml_polygs)
out_window, out_transform = mask(raster_in, polyg['geometry'], all_touched=True, crop=True)
out_meta = raster_in.meta.copy()
#this is not relevant
width = ...
height = ...
x = ...
y = ...
with rasterio.open(raster_out, "r+", **out_meta) as dest:
dest.write(out_window, window=Window(x, y, width, height))
dest.close()
The problem is that the instruction dest.write(.....)
is overwriting the raster with all 12 values of the out_window
array. I do not want to overwrite the raster with the -1 values. Those 3 elements of the raster should be left unaltered.
Is there any way to do this?
np.where
). Of course, it could be a parameter in the.write()
method but its probably not implemented yet. Maybe it could be a good feature to contribute torasterio
if you have the time. Furthermore, feel free to post your answer so people with the same problem in the future have this as a reference.