I am doing a point in polygon analysis
library(terra)
library(rnaturalearth)
crdref <- "+proj=longlat +datum=WGS84"
lonlat<- structure(c(-123.115684, -81.391114, -74.026122, -122.629252,
-159.34901, 7.76101, 48.080979, 31.159987, 40.621058, 47.50331,
21.978049, 36.90086), .Dim = c(6L, 2L),
.Dimnames = list(NULL,c("longitude", "latitude")))
pts <- vect(lonlat, crs = crdref)
world_shp <- rnaturalearth::ne_countries()
world_shp <- terra::vect(world_shp, crs = crdref)
world_shp <- terra::project(world_shp, crdref)
plot(world_shp)
points(pts, col = "red", pch = 20)
All these points lie on the edges of polygon and hence when I try to extract the the polygon under which each point lies, I get an NA
e <- terra::extract(world_shp, pts)
e$sovereignt
NA
Is there any way I can return the nearest polygon for each point using the terra
package