Home > Enterprise >  Converting raster to a dataframe, and then back to a raster again? [R]
Converting raster to a dataframe, and then back to a raster again? [R]

Time:09-29

I'm using raster to read in a raster (a GeoTIFF) and then turn it in to a table so I can do some analysis and re-classify some values. Like so:

r_ndvi <- raster(paste0(path, "ndvi.tif")) #read in as raster
rdf_ndvi <- as.data.frame(r_ndvi) #turn tif into df of DN

after I do these calculations, I want to convert the table back to raster format so it can be opened as an image in QGIS. How can I accomplish this?

CodePudding user response:

You can use the rasterFromXYZ function to convert the data.frame back to raster like the following code

library(raster)

logo <- stack(system.file("external/rlogo.grd", package="raster"))
plot(logo)

rdf_ndvi <- as.data.frame(logo, xy = T) #turn tif into df of DN

raster <- rasterFromXYZ(rdf_ndvi)
plot(raster)
  • Related