Skip to content
Prev 28448 / 29559 Next

Restricting geographical space for SDM analyses in R

I would approach this as follows:

Get a polygon of the country boundary where your study area is located, 
then buffer it inside, (using a negative buffer distance). Now you can 
"difference" the full country boundary and the inner buffer to get just 
the coastal strip.


Here's an example:


library(sf)
library(maptools)
library(dplyr)
library(ggplot2)
theme_set(theme_bw())

data("wrld_simpl") # From maptools package
# Example for Egypt
egypt = st_as_sf(wrld_simpl[wrld_simpl$NAME == "Egypt",])
# Transform to Spherical Mercator for correct buffering in meters
egypt = st_transform(egypt, 3857)

# Inside buffer size 50 kilometers
d = 50000

# Setup polygon to crop only coast region
coast_corners = matrix(c(26,32,34,32,34,30,26,30,26,32),
 ????????????????????? ncol=2, byrow=TRUE)
coast_region = st_sf(st_sfc(st_polygon(list(coast_corners))))
st_crs(coast_region) = st_crs(4326)
# But transform this region also to Spherical Mercator
coast_region = st_transform(coast_region, 3857)

# Use negative buffer distance to get inner buffer
egypt_buffer =? st_buffer(egypt, -d)

# Now Difference the full country and buffer polygons,
# then Crop out only coast region
coastal_strip = st_difference(egypt, egypt_buffer) %>%
 ? st_crop(., coast_region)

# Plot result
ggplot(data = egypt) +
 ? geom_sf() +
 ? geom_sf(data = coastal_strip, fill="red")



HTH,

Micha
On 11/16/20 1:44 PM, Francisco Borges wrote: