Home > Mobile >  Plotting points extracted from a dataframe in a Raster layer
Plotting points extracted from a dataframe in a Raster layer

Time:12-08

I would like to plot points from a data frame into a raster layer that I have. For every point, I would like the value of the cell to be 1 (all the other cell on the initial raster layer have a value of zero).

My dataframe (data) looks like this (first three rows only)

Year<-c(2020, 2019, 2018)
Lat<-c(48.3,48.79,48.4)
Long<-c(-123.62, -123.36, -123.29)

I managed to plot those points with the following code

points = st_as_sf(data, coords = c("Long", "Lat"), crs = 4326)
plot(st_geometry(points), pch=16, col="navy")

And got this graph: Graph points plotted

I now want to plot those points into a raster layer that I have for the area. The parameters of my raster layer are as follow:

class      : RasterLayer 
dimensions : 44, 41, 1804  (nrow, ncol, ncell)
resolution : 0.2916667, 0.2916667  (x, y)
extent     : -133.2625, -121.3042, 41.3875, 54.22083  (xmin, xmax, ymin, ymax)
crs        : NA 
names      : Blank_Map 

Every cell of this raster as a value of 0, which is what I need. Now, I would like to add the points from my dataframe to this raster layer, using of value 1 for every cell where those data points are. I would also like to save the whole thing as a new raster layer (which would then have 0 and 1 values).

Could anybody help me achieve this? I have been trying for days, but nothing seems to work any help is appreciated!

CodePudding user response:

1. Please find below a reprex providing a possible solution using raster and sf libraries.

library(raster)
library(sf)


# Create from scratch the raster with 0 values
raster0 <- raster(nrows = 44, ncols = 41, 
                  xmn = -133.2625, xmx = -121.3042, 
                  ymn = 41.3875, ymx = 54.22083, 
                  vals=0)


# Convert points 'sf' object into 'sp' object
points_sp <- as(points, "Spatial")

# Extract the cells of raster0 matching the points geometry of the 'sp' object
cells_ID <- extract(raster0, points_sp, cellnumbers=TRUE)[,"cells"]

# Copy raster0 (as you want the final result in another raster)
raster01 <- raster0

# Replace 0 values to 1 for cells matching points geometry in the 'raster01'
raster01[cells_ID] <- 1

# Visualization of the final result
plot(raster01)

Created on 2021-12-07 by the reprex package (v2.0.1)


2. Please find below a reprex providing another solution using terra and sf libraries

library(terra)
library(sf)

# Create from scratch the 'SpatRaster' with 0 values
raster0 <- rast(nrows=44, ncols=41, 
                nlyrs=1, 
                xmin=-133.2625, xmax=-121.3042, 
                ymin=41.3875, ymax=54.22083, 
                vals = 0)


# Convert points 'sf' object into 'SpatVector' object
points <- vect(points)

# Extract the cells of raster0 matching the points geometry of the 'sp' object
cells_ID <- terra::extract(raster0, points, cells = TRUE)

# Copy raster0 (as you want the final result in another raster)
raster01 <- raster0

# Replace 0 values to 1 for cells matching points geometry in the 'raster01'
raster01[cells_ID[,"cell"]] <- 1

# Visualization of the final result
plot(raster01)

Created on 2021-12-07 by the reprex package (v2.0.1)

  • Related