-1

I am trying to extract data from a raster from a layer of random points.

The input data are the raster where I have to extract the values and a shapefile of polygons. With this shapefile, I get a random sampling of points that are inside the polygons. This is done with the SF package and I get a layer sfc_POINTS. Then, I try to extract the values of my raster with these points using the raster package.

I get this error: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"RasterLayer", "sfc_POINT"’

Here is the code:

# Clean environment 
rm(list = ls())

#Import packages
library(sf)
library(raster)


#Import data 
shp = st_read("PATH_TO_MY_SHP")
rst = raster("PATH_TO_MY_RASTER")

#Random sampling
Rdmsamp = st_sample(shp,  1000, "random")
Rdmsamp_values = raster::extract(rst, shp)

If someone can help me please. PS: Is it possible to integrate also a distance condition in the sample points setup (e.g. a distance to the edges of the polygons or a minimum distance between the points ?)

Thanks

camille
  • 16,432
  • 18
  • 38
  • 60
Carl Bethuel
  • 37
  • 10
  • 1
    Hi @Carl Bethuel, please provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that we can help you in the best way. Cheers. – lovalery Jan 22 '22 at 15:12
  • 1
    Hi @lovalery, thanks for your advices, I'll think about it next time. Here any vector and raster file can do the test, I think the problem comes from a conflict between package. Cheers. – Carl Bethuel Jan 23 '22 at 16:59

2 Answers2

2

First with terra (the replacement for raster):

library(terra)
fv <- system.file("ex/lux.shp", package="terra")
v <- vect(fv)
fr <- system.file("ex/elev.tif", package="terra")
r <- rast(fr)

pts <- spatSample(v, 100, "random")
e <- extract(r, pts)

Now with sampling in sf

library(sf)
shp <- st_as_sf(v)
rsamp = st_sample(shp, 100, "random")
rsp <- vect(rsamp)
vals <- extract(r, rsp)

Or use that sample with raster

library(raster)
rr <- raster(fr)
sfsamp <- st_as_sf(rsamp)
vv <- extract(rr, sfsamp)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thanks for your back ! I try to run the code with terre package and I get this error : "Error : [vect] Cannot open this file as a SpatVector" when I'm running the line : "v <- vect(fv)"... I thought it was coming from my shape file which is quite large so I tried with several other basic shape files and I get the same error every time. I used directly: "v = vect("MY_SHP_PATH", package="terra")" and it seems to work. However, when I use the "extract" function, it does not extract any values. When I look at the object that comes out, I have the 100 points but with NaN values. Any advice? Thanks – Carl Bethuel Jan 23 '22 at 17:44
0

I am here to answer my own question with the elements that I was given in the previous answer. The code used in the previous answer works well. A problem persisted on the extraction of the values for which I obtained only NaN values. This problem simply came from the CRS used for each layer, so you have to think about reprojecting your different layers to the same CRS. Hopefully this clarification will help some people in the future.

Carl Bethuel
  • 37
  • 10