0

I have made s SpatialPolygonDataFrame (bs_land) with a shape file of the land and cropped it for the Artic Ocean. I also have a dataset with species records (data) and I want to check if all of them are marine. When I plot the data, I can see that some records are on land. I wanted to extract now the records that are only in the ocean but I can't seem to find a way.

Can somebody help?

Many thanks in advance!

library(ggOceanMaps)
library(sf)
land <- read_sf("ne_10m_land.shp")
lims <- c(-8, 65, 68, 82)
projection <- "EPSG:3995"
bs_land <- clip_shapefile(land, lims)
bs_land <- sp::spTransform(bs_land, CRSobj = sp::CRS(projection))
rgeos::gIsValid(bs_land) # Has to return TRUE, if not use rgeos::gBuffer
bs_land <- rgeos::gBuffer(bs_land, byid = TRUE, width = 0)
sp::plot(bs_land)


basemap(limits = 60, glaciers = TRUE, bathymetry = TRUE) + 
  annotation_spatial(bs_land) +
  geom_spatial_point(data = data, aes(x = decimalLongitude, y = decimalLatitude), color =     "darkorange")

I can see on the map that some points are on land. How can I extract the points that are only in the ocean so not on the shape file?

Phil
  • 7,287
  • 3
  • 36
  • 66
  • You are looking for a "Point in Polygon" solution. You can do this with with the `over` function in `sp`. I found these two links to be especially helpful: https://rpubs.com/Earlien/Creating_and_Manipulating_Polygon_Objects and https://stackoverflow.com/questions/19002744/spover-for-point-in-polygon-analysis – David Jun 30 '23 at 19:25
  • Since you are using ggOceanMaps, you may also use `dist2land()` or `get_depth()` functions. This requires the newest ggOceanMaps installed from GitHub. See more in detail: https://mikkovihtakari.github.io/ggOceanMaps/articles/new-features.html – Mikko Aug 01 '23 at 14:31

0 Answers0