0
votes

I was trying to use overlay function and the code is the following:

usa <- map('state',boundary = FALSE, lty=2,plot=FALSE,fill=TRUE)
usa_SP <- map2SpatialPolygons(usa, IDs=usa$names, proj4string = CRS("+proj=longlat"))

comB.dat <- read.csv("agbregion.csv",header=TRUE)
names(comB.dat) <- c("Lon","Lat","harvDB")
names(AGByr)<- c("Lon","Lat","harvDB")
names(AGBdiff) <- c("Lon","Lat","harvDB")

pts <- SpatialPoints(cbind(comB.dat$Lon,comB.dat$Lat))
ptsyr <- SpatialPoints(cbind(AGByr$Lon,AGByr$Lat))
ptsdiff <- SpatialPoints(cbind(AGBdiff$Lon,AGBdiff$Lat))

o <- overlay(pts, usa_SP)

But I got the following error: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘overlay’ for signature ‘"SpatialPoints", "SpatialPolygons"’

I don't know what's going on. Could anybody explain it for me? Thanks a lot.

1

1 Answers

0
votes

See help of ?raster::overlay. Notice the first few lines.

## S4 method for signature 'Raster,Raster'
overlay(x, y, ..., fun, filename="", recycle=TRUE, forcefun=FALSE)

## S4 method for signature 'Raster,missing'
overlay(x, y, ..., fun, filename="", unstack=TRUE, forcefun=FALSE)

This tells you that this method only works on Raster objects, such as RasterLayer, RasterStack and so on.

Consider using sp::over, which is declared to

consistent spatial overlay for points, grids and polygons: at the spatial locations of object x retrieves the indexes or attributes from spatial object y