I'm working on some niche modelling and I'm using the maps package to generate polygons for states my species is endemic to.
endemic <- maps::map(database = "state", regions = c("alabama","florida","georgia","kentucky","mississippi","north carolina","pennsylvania","south carolina","tennessee","virginia","west virginia"),
plot = TRUE, fill = TRUE)
The names of the states match the ones given by unique(states$region)
These are the spatial polygons.
endemic_sp <- map2SpatialPolygons(endemic, IDs = endemic$names, proj4string = CRS("+proj=longlat +datum=WGS84"))
sf_endemic <- st_as_sf(endemic_sp, coords = c("long","lat"), crs = 4326)
These are the spatial points.
occ_points <- st_as_sf(point_data, coords = c("long","lat"), crs = 4326)
It gets an error on this step:
endemic_polygon <- st_filter(occ_points, sf_endemic)
Error in `stopifnot()`:
In argument: `lengths(.predicate(x, y, ...)) > 0`.
Caused by error in `wk_handle.wk_wkb()`:
! Loop 0 is not valid: Edge 0 has duplicate vertex with edge 394.
The edge is overlapping for some reason, how do I fix this?
I tried checking for a syntax error in the States' names, checking the long and lat fields, and if they were using the same crs. All were correct, but the error is still there.