SCAR / EGABIcourse19

10 stars 8 forks source link

How can I to create buffer masks? #12

Closed lyrodus closed 5 years ago

lyrodus commented 5 years ago

As the geographical dimensions are crucial for ENM (see my previous issue), I consider that I should generate a 30-km (current intertidal zone) and 50-km (LGM intertidal zone) off- shore buffer masks to delimitate the appropriate habitat. In this sense, I would like to know how can I to create this buffer masks?

Cheers

raymondben commented 5 years ago

@mdsumner any quick pointers please?

mdsumner commented 5 years ago

I guess they are separate tasks? You want to buffer a polygon and then use that to mask a raster? In short:

buf <- sf::st_buffer(poly, 30000) ## 30 km

masked <- raster::mask(rast, buf)

(If buf is in longlat you will need to transform it to a projection to use straightforward 30000 value).

But, that leaves the data in rast, so another way to think about it is

mask_layer <- !is.na(masked) ## now we have a TRUE in the buffer, FALSE outside

If speed is an issue fasterize can be used

buf$is_poly <- 1  ## set a constant for any polygon
mask <- !is.na(fasterize::fasterize(buf, rast, field = "is_poly")

If the buffer is of a line it will be slightly different