# Enlarging polygon slightly using R?

Consider the following scenario:

3. The point data include points in small islands that I want to discard. So I use `over()` in the R-package `sp` to filter out such points.
4. However, hundreds of points at the coastline is slightly off my polygon(s), so they would also be discarded.

In the image below, only the points that are off are plotted. I think that if I could only enlarge the two polygons (mainland Africa + Madagascar) slightly, then I could filter out points using `over()`. Perhaps there is another solution that gives the same result, and if so that is great.

EDIT: using rgeos::gBuffer() as suggested by @mdsumner, the problem is now solved, which can be seen in this graph, where the points the are truly off mainland Africa and Madagascar are colored red and the points that where "false positives" are colored black.

The code used to enlarge the polygon(s) was:

``````Africa.2 <- spTransform(x=Africa, CRSobj=CRS("+init=epsg:32662"))
library(rgeos)
Africa.larger <- gBuffer(Africa.2, width = 50000)
`````` • rgeos::gBuffer will enlarge it, you may need byid=TRUE – mdsumner Mar 30 '17 at 14:40
• @mdsumner If you post the same text as an answer I'll accept it, because it solved my problem. – Hans Ekbrand Mar 31 '17 at 11:55

Use the `gBuffer` function from the `rgeos` package. It has a `width` argument to expand the polygons by a margin.
Use `byid = TRUE` to maintain the identity of the input polygons, else they get all merged together with the buffer.
Consider using `spTransform` to reproject to planar coordinate system for the units, but you can use "degrees" for longlat as well. Negative width values reduce the shape, and zero as width can fix some topology problems, so often it's used as a precursor before building an actual buffer.
• Please don't use `gBuffer` with Lon/lat coordinates . The results will be wrong. – Roman Jul 16 at 11:37