Search code examples
rrastershapefile

R: Changing values from raster at certain coordinates


I run species distribution models in R and want to create variable rasters for the mainland of Africa, without the islands. I can only find shapefiles of Africa with its islands, not from the mainland only.

1) Where can I possibly download a shapefile of the mainland only?

2) If there is no shapefile, I would like to manually delete the islands from my raster. Is there a way to do this, f.e. setting parts of the rasters between certain coordinates to NA?


Solution

  • Yes. Here is a minimal, self-contained, reproducible example. The easiest approach might be to use Africa polygons africa and do

    library(raster)
    afr <- aggregate(africa)
    v <- disaggregate(afr)
    a <- area(v)
    afnois <- v[which.max(a), ]
    

    And then used that in mask to remove the islands from the rasters

    You can also create polygons with raster::drawPoly and use these for masking.