TheRealJimShady
TheRealJimShady

Reputation: 917

Extend from {terra} fills with 0 instead of 255?

I am looking to use the terra::extend function. Although my raster is datatype INT1U (where nodata is specified by the value 255), when I use extend the new empty cells are given the value 0. Which is problematic for my workflow (as I have other cells which have the value 0 but are not NA).

r <- rast(xmin=-150, xmax=-120, ymin=30, ymax=60, ncols=3, nrows=3)
values(r) <- c(0,1,2,3,4,5,6,7,255)

r <- writeRaster(r, filename = "/databricks/driver/test.tif", datatype = "INT1U", overwrite=T)

e <- ext(-180, -100, 40, 70)

re <- extend(r, e, file = "/databricks/driver/test2.tif", datatype = "INT1U", overwrite=T)

plot(re)

enter image description here

I feel like perhaps the extend function should have an option for the value to use for adding cells. For instance:

re <- extend(r, e, val = 255, file = "/databricks/driver/test2.tif", datatype = "INT1U", overwrite=T)

Interested in others thoughts. Thanks.

Upvotes: 1

Views: 311

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47491

I am not sure why, but this works if you use writeRaster in a separate step.

r <- rast(xmin=-150, xmax=-120, ymin=30, ymax=60, ncols=3, nrows=3)
values(r) <- c(0,1,2,3,4,5,6,7,255)
r <- writeRaster(r, filename = "test.tif", datatype = "INT1U", overwrite=T)
e <- ext(-180, -100, 40, 70)

re <- extend(r, e)
re <- writeRaster(re, file = "test2.tif", datatype = "INT1U", overwrite=T)
plot(re)

enter image description here

This only happens with "INT1U". Perhaps you can raise an issue here.

Upvotes: 3

Related Questions