For GIS analysis in R
I recommend the sf
package. One of the great advantages of this package is that, once you loaded a file into your enviroment with read_sf()
, you can manipulate your data using the regular data.frame
methods commonly used in R.
# read your data into your environment, make sure your data is stored in your working directory
meow <- read_sf("meow_ecos.shp")
eez <- read_sf("eez_v11.shp")
Once loaded in, you will see that meow
has 232 observations and 10 variables, while eez
has 281 observations and 32 variables. st_intersection
, the tool that will allow you to get the overlapping areas, will add the variables of the two objects together. So, you will have 42 variables for every polygon in your intersection-object. It's handy to determine which variables you are interested in beforehand. (if you want to keep all of them, it's fine). Let's say we want to keep only the variable PROVINCE
from meow
, and the first 5 variables from eez
.
# subset meow and eez
meowprov <- subset(meow, select = PROVINCE)
eezfirst5 <- eez[, c(1-5)]
Before you can start your intersection analysis, you should check whether the coordinate reference systems are equal. When they are not equal, it will cause an error.
#make sure your coordinate systems projections are equal.
st_crs(meowprov) == st_crs(eezfirst5)
This returns True
, so we can continue. Now, since your EEZ file is large, you'll have to be patient doing the intersection.
# apply your intersection
meow_eez_intersection <- st_intersection (meowprov, eezfirst5)
After a lot of waiting, I got meow_eez_intersection
in my environment with 701 observations.
sf
(read_sf(your_file.gpkg)
andst_intersects(a_df, b_df)
for instance.