I have a dataset where I'm trying to look determine determine which polygons my sampling locations occur in. I have been following the example here:
https://gis.stackexchange.com/questions/282750/identify-polygon-containing-point-with-r-sf-package
But I'm running into error messages I don't understand, nor do I follow all the code.
#polygon shapefile information
bcr
Simple feature collection with 378 features and 1 field
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -179.1413 ymin: 14.53287 xmax: -52.63629 ymax: 83.11063
geographic CRS: NAD83
First 10 features:
BCRNAME geometry
1 ALEUTIAN/BERING SEA ISLANDS MULTIPOLYGON (((-179.1036 5...
2 WESTERN ALASKA MULTIPOLYGON (((-162.4558 5...
3 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-162.6464 6...
4 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-94.83377 5...
5 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-61.63853 5...
6 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-101.9977 6...
7 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-102.0227 6...
8 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-101.9978 6...
9 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-79.70593 5...
10 ARCTIC PLAINS AND MOUNTAINS MULTIPOLYGON (((-69.77826 5...
#select distinct set of points
pnts <- df %>% select(y = Latitude, x = Long) %>% distinct()
#create a points collection
pnts_sf <- do.call("st_sfc",
c(lapply(1:nrow(pnts),
function(i) {st_point(as.numeric(pnts[i, ]))}), list("crs" = 4326)))
#My question: what is this doing?? what does 4326 come from?
pnts_trans <- st_transform(pnts_sf, 2136) # apply transformation to pnts sf
bcr_trans <- st_transform(bcr, 2163) #apply transformation to polygon sf
#intersect and extract BCR name
pnts$bcrName <- apply(st_intersects(bcr_trans, pnts_trans, sparse = FALSE), 2,
function(col) {
bcr_trans[which(col), ]$BCRNAME
})
#My question: So this is where we're looking at the interaction between the points and polygons, but what is 2 for and what is the function doing?
When I try to run everything, I get the following error message:
Error in st_geos_binop("intersects", x, y, sparse = sparse, prepared = prepared, :
st_crs(x) == st_crs(y) is not TRUE
Unclear if it's coming from a variable name I didn't change or something else entirely.