Search code examples
rrasterterra

R terra rasterization create vertical white lines


I have a raster raster_coarse at 0.25 resolution

  class       : SpatRaster 
  dimensions  : 26, 31, 1  (nrow, ncol, nlyr)
  resolution  : 0.25, 0.25  (x, y)
  extent      : -87.75, -80, 24.5, 31  (xmin, xmax, ymin, ymax)
  coord. ref. : lon/lat WGS 84 (EPSG:4326) 
  source      : memory 
  name        : layer1 
  min value   :  0.08839285 
  max value   :  0.11517857 

enter image description here

I have another raster raster_fine at finer resolution

  class       : SpatRaster 
  dimensions  : 2377, 2758, 1  (nrow, ncol, nlyr)
  resolution  : 0.002777778, 0.002777778  (x, y)
  extent      : -87.6361, -79.97499, 24.39723, 31.00001  (xmin, xmax, ymin, ymax)
  coord. ref. : lon/lat WGS 84 (EPSG:4326) 
  source      : memory 
  name        : finer_res 
  min value   :     0 
  max value   :     1 
  

enter image description here

Here's what I am trying to do. I want to populate every pixel in raster_fine with the values from raster_coarse. The way I approached this is:

  # check the resolution difference 
  res_drop <- res(raster_coarse)[1]/res(raster_fine)[1] # 89.99999. 
  
  # disaggregate the raster_coarse
  raster_coarse <- terra::disagg(raster_coarse, fact = res_drop)
 
  # convert to points 
  raster_coarse_df <- as.data.frame(raster_coarse, xy = T, na.rm = TRUE)
  raster_coarse_pts <- terra::vect(raster_coarse_df, geom = c("x", "y"), crs(raster_fine))

 # create a blank canvas
 canvas <- terra::rast(xmin = ext(raster_fine)[1], 
                       xmax = ext(raster_fine)[2], 
                       ymin = ext(raster_fine)[3], 
                       ymax = ext(raster_fine)[4],
                       resolution = res(raster_fine),
                       crs = crs(raster_fine))

   # populate the blank canvas with raster_coarse_pts 
   raster_coarse <- terra::rasterize(x = raster_coarse_pts , y = canvas, field = "layer1", fun = max, touches = T, background = NA)  
   
   raster_coarse
   
   class       : SpatRaster 
   dimensions  : 2377, 2758, 1  (nrow, ncol, nlyr)
   resolution  : 0.002777778, 0.002777778  (x, y)
   extent      : -87.6361, -79.97499, 24.39723, 31.00001  (xmin, xmax, ymin, ymax)
   coord. ref. : lon/lat WGS 84 (EPSG:4326) 
   source      : memory 
   name        :      lyr.1 
   min value   : 0.08839285 
   max value   : 0.11517857

   plot(raster_coarse)

enter image description here

I am not able to understand why the vertical white lines are coming from?


Solution

  • Example data

    library(terra)
    r <- rast(nrow=26, ncol=31, ext=c(-87.75, -80, 24.5, 31))
    f <- geodata::gadm("USA", level=1, path=".")
    f <- f[f$NAME_1 == "Florida", ]
    values(r) <- runif(ncell(r))
    r <- mask(r, f)
    fine <- rast(nrow=2377, ncol=2758, ext=c(-87.6361, -79.97499, 24.39723, 31.00001))
    

    The solution is to use resample

    rr <- resample(r, x, "near")
    

    Instead of the nearest neighbor you could use, for example, bilinear interpolation:

    rr <- resample(r, x)
    

    The white lines occur because with your method there will be cells that are not covered. To better understand that you could crop out a small area and plot the points on top of the raster.