R Trim non-raster data

I would like to crop the part without some raster data (example image in 1 , where there is no data in black) without manually specifying the size.

Any idea?

image with no-data

+6
source share
4 answers

You can use trim for this:

 library(raster) r <- raster(ncol=18,nrow=18) r[39:49] <- 1 r[205] <- 6 s <- trim(r) 
+16
source

[ subset methods and [<- replaced for raster objects, so you can just do r[ r[] == 1 ] <- NA to get rid of the values, where 1 is your value for the node (use NAvalue(r) to find out what R thinks your nodate value should be if you're not sure).

Note that you must use r[] inside the subset command [ to access the values. Here is a processed example ...

Example

 # Make a raster from system file logo1 <- raster(system.file("external/rlogo.grd", package="raster")) # Copy to see difference logo2 <- logo1 # Set all values in logo2 that are > 230 to be NA logo2[ logo2[] > 230 ] <- NA # Observe difference par( mfrow = c( 1,2 ) ) plot(logo1) plot(logo2) 

enter image description here

+3
source

I have two slightly different solutions. The first requires you to manually determine the degree, but uses predefined functions. The second is more automatic, but a little manual.

Create a playable raster for which the first 2 lines are: NA

 library(raster) # Create a reproducible example r1 <- raster(ncol=10, nrow=10) # The first 2 rows are filled with NAs (no value) r1[] <- c(rep(NA,20),21:100) 

Decision No. 1

Manually get size from drawing using drawExtent ()

 plot(r1) r1CropExtent <- drawExtent() 

Crop the raster using the scale selected from the picture.

 r2 <- crop(r1, r1CropExtent) 

Land for comparison

 layout(matrix(1:2, nrow=1)) plot(r1) plot(r2) 

Decision number 2

It identifies the rows and columns of the raster that have only NA values ​​and delete those that are on the border of the raster. Then it calculates the degree using extent() .

Convert the raster to a matrix that identifies whether the values ​​are NA or not.

 r1NaM <- is.na(as.matrix(r1)) 

Find columns and rows that are not completely filled with nAs

 colNotNA <- which(colSums(r1NaM) != nrow(r1)) rowNotNA <- which(rowSums(r1NaM) != ncol(r1)) 

Find the size of the new raster using the first and last columns and rows that are not completely filled with NA. Use crop() to crop the new raster.

 r3Extent <- extent(r1, rowNotNA[1], rowNotNA[length(rowNotNA)], colNotNA[1], colNotNA[length(colNotNA)]) r3 <- crop(r1, r3Extent) 

Separate rasters for comparison.

 layout(matrix(1:2, nrow=1)) plot(r1) plot(r3) 
+2
source

I wrote a small function based on Marie's answer to quickly draw cropped rasters. However, a memory problem may occur if the raster is extremely large, because the computer may not have enough RAM to load the raster in the form of a matrix.

Therefore, I wrote a memory security function that will use the Marie method if there is enough RAM on the computer (because it is the fastest way) or a method based on raster functions if there is not enough RAM on the computer (it is slower, but the memory is safe).

Here is the function:

 plotCroppedRaster <- function(x, na.value = NA) { if(!is.na(na.value)) { x[x == na.value] <- NA } if(canProcessInMemory(x, n = 2)) { x.matrix <- is.na(as.matrix(x)) colNotNA <- which(colSums(x.matrix) != nrow(x)) rowNotNA <- which(rowSums(x.matrix) != ncol(x)) croppedExtent <- extent(x, r1 = rowNotNA[1], r2 = rowNotNA[length(rowNotNA)], c1 = colNotNA[1], c2 = colNotNA[length(colNotNA)]) plot(crop(x, croppedExtent)) } else { xNA <- is.na(x) colNotNA <- which(colSums(xNA) != nrow(x)) rowNotNA <- which(rowSums(xNA) != ncol(x)) croppedExtent <- extent(x, r1 = rowNotNA[1], r2 = rowNotNA[length(rowNotNA)], c1 = colNotNA[1], c2 = colNotNA[length(colNotNA)]) plot(crop(x, croppedExtent)) } } 

Examples:

 library(raster) r1 <- raster(ncol=10, nrow=10) r1[] <- c(rep(NA,20),21:100) # Uncropped plot(r1) # Cropped plotCroppedRaster(r1) # If the no-data value is different, for example 0 r2 <- raster(ncol=10, nrow=10) r2[] <- c(rep(0,20),21:100) # Uncropped plot(r2) # Cropped plotCroppedRaster(r2, na.value = 0) 
0
source

Source: https://habr.com/ru/post/957022/


All Articles