2

Consider the following scenario:

  1. I have made polygon of mainland Africa + Madagascar.
  2. From a different source I have got some point data about this area.
  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.

The problem points

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)

Solved

  • 4
    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
5

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
  • It depends what you are doing it for. Don't presume you know what folks are doing, you aren't the authority on wrong or right. It depends. The post even says "consider using spTransform to reproject ...". – mdsumner Jul 16 at 12:02
1

The best way I could think of is using "buffer" tool package. Something to remember is to check your coordinates first and use the right buffer units. To start, you could look into GDAL library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.