rgisr-rasterrasterize

Problems with rasterize function in R


I have three SpatialPointsDataFrame objects that are actually just one point each. My intention is to make a raster for each of them with an extent that includes the point, in such a way that all cells but the point are "NA", so then I can use the distance() function in the package raster to generate a raster layer where the z value is the distance to the only cell in which z is not "NA".

My code works without problem with the first of the three objects, but the following error appears for the other two:

    error in seq.default(zrng[1], zrng[2], length.out = cuts + 2) : 
    'from' cannot be NA, NaN or infinite
    In addition: Warning messages:
    1: In asMethod(object) :
      complete map seems to be NA's -- no selection was made
    2: In min(x) : no non-missing arguments to min; returning Inf
    3: In max(x) : no non-missing arguments to max; returning -Inf

I have double and triple checked that my points are contained in the extent of the raster, and I really can't pinpoint the problem

Here's my code:

library(raster)

TIM <- data.frame()
TIM[1,1] <- -13.8309
TIM[1,2] <- 28.9942

VEN <- data.frame()
VEN[1,1] <- -15.7886
VEN[1,2] <- 27.8444

MCL <- data.frame()
MCL[1,1] <- -13.5325
MCL[1,2] <- 29.2914


coordinates(TIM) <- ~V1+V2
coordinates(VEN) <- ~V1+V2
coordinates(MCL) <- ~V1+V2

bb2 <- matrix(c(-20, -9.5, 20.5, 31.5), nrow = 2, ncol = 2, byrow = T)
bb2 <- extent(bb2)

r <- raster(nrows = 1217, ncols = 1047)
r <- setExtent(r, bb2, keepres=F)

rMCL <- rasterize(MCL, r)
spplot(rMCL)

#so far so good, but from now on it doesn't work

rVEN <- rasterize(VEN, r)
spplot(rVEN)


rTIM <- rasterize(TIM, r)
spplot(rTIM)

Edit: I have tried turning it to a SpatialGridDataFrame and I get to plot it but my point is not in the extent of the raster, i.e. the plot is empty. Code:

rr <- as(rTIM, "SpatialGridDataFrame")
spplot(rr)
#this produces an empty plot

I have also tried plotting it in a raster without a predetermined number of columns and rows, and it works:

r <- raster()
r <- setExtent(r, bb2, keepres=F)
rTIM <- rasterize(TIM, r)
spplot(rTIM)
# this produces a raster containing my point

The problem is, I really would need to set the resolution of the plot so each cell of the raster represents approx 1 squared km, which is what I get with the number of rows and columns I had previously used. Any ideas?


Solution

  • I can get it to work by adding all three sets of coordinates to the same dataframe and then using the count function when creating the raster:

    library(raster)
    
    # Add all 3 sets of coordinates to the same dataframe
    
    df <- data.frame()
    df[1,1] <- -13.8309
    df[1,2] <- 28.9942
    df[2,1] <- -15.7886
    df[2,2] <- 27.8444
    df[3,1] <- -13.5325
    df[3,2] <- 29.2914
    
    # Create new column in dataframe (we will use this for the count function)
    
    df$x <- c(1,1,1)
    
    # Convert to spatial points dataframe
    
    df.sp <- df
    coordinates(df.sp) <- ~ V1+V2
    
    # Make raster
    
    bb2 <- matrix(c(-20, -9.5, 20.5, 31.5), nrow = 2, ncol = 2, byrow = T)
    bb2 <- extent(bb2)
    
    r <- raster(nrows = 1217, ncols = 1047)
    r <- setExtent(r, bb2, keepres=F)
    
    # Rasterise using the count function
    
    raster <- rasterize(df.sp, r, field= "x", fun="count")
    
    # The table shows there are 3 cells with a value of 1 so it seems to have worked
    
    table(values(raster))
    
    1 
    3 
    
    spplot(raster)
    
    raster
    
    class       : RasterLayer 
    dimensions  : 1217, 1047, 1274199  (nrow, ncol, ncell)
    resolution  : 0.01002865, 0.00903862  (x, y)
    extent      : -20, -9.5, 20.5, 31.5  (xmin, xmax, ymin, ymax)
    coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
    data source : in memory
    names       : layer 
    values      : 1, 1  (min, max)
    

    The plot doesn't make a whole lot of sense to me but I think that's because you have a lot of cells in your raster so you can't really see anything. The values in the raster are definitely 3 cells with a value of 1 and all the rest are NA so I think this what you want.