Thursday 15 June 2017

r - Saving levelplot to a TIFF raster without changing pixel size?


I am working on change detection and have been able to visualize the results that I have created through levelplot. Previously I used to use Writeraster for storing my plots as tif files which did not manipulated my data in terms of pixel width or height. With levelplot, I am unable to use this function,as it throws error missing trellis, character function.


How can I save a levelpot as a TIFF imagery without legends to be opened in QGIS so that I can overlay it over basemaps?


Here is how I am creating levelplot:



changeDet1 <- calc(stack(lc1,lc2), fun = change)
codes_ <- data.frame(ID = grid_$code,value = paste0('from ',grid_[,1],' to ',grid_[,2]))
logical_test <- which(grid_$change == T) # remove no change classes
codes_ <- codes_[logical_test,]
codes_
a<-subset(codes_, ID %in% c(2,10,12,13,14,15,16,17,18,20,29,38,47,56,65,74))
rat <- levels(changeDet1)[[1]]
rat[["Changes"]] <- codes_
levels(changeDet1) <- rat
levelplot(changeDet1, par.settings=PuOrTheme(), xlab="", ylab="")


Answer



From documentation:




Write Raster Data To A File


Write an entire Raster* object to a file, using one of the many supported formats.


When writing a file to disk, the file format is determined by the 'format=' argument if supplied, or else by the file extension (if the extension is known). If other cases the default format is used.



This function is used over raster objects, not plot results. So if you wanna save the closest result seeing in plot function, use the same approach to mask non-desired values:


changeDet1 <- calc(stack(lc1,lc2), fun = change)

finalRaster <- mask(changeDet1, changeDet1 %in% c(2,10,12,13,14,15,16,17,18,20,29,38,47,56,65,74), maskvalue = 0)
class(finalRaster)
## [1] "RasterLayer"
## attr(,"package")
## [1] "raster"
writeRaster(finalRaster, 'output.tif')

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...