[R] Generating a polygon around points

MacQueen, Don macqueen1 at llnl.gov
Fri Aug 15 20:04:46 CEST 2014

I have been using a process like the following to create polygons that
(closely) surround a non-convex set of points.

  buf1 <- gBuffer(tmpb.ne, width=bstart, byid=TRUE)
  buf2 <- gUnaryUnion(buf1)
  buf <-  gBuffer(buf2, width=bshrink)

These functions are from the rgeos package. In my case, tmpb.ne is a
SpatialPointsDataFrame (sp package) whose coordinates slot has the
coordinates of interest. You would have to tinker with the values for the
two width arguments.

The first gBuffer() puts a buffer (a circle) around each individual point.

The gUnaryUnion() joins those circles into one or more polygons, depending
on how they overlap.

The second gBuffer() shrinks the polygons to make the polygon(s) surround
the points more closely.

It will even respect holes, for example if all your points are on land,
but there¹s a lake in the middle somewhere.

I use it with projected coordinates, and the width parameters are in the
same units as the projection. I have never tried it with lat/long.

R-sig-geo would be a good place to follow-up. I think that was where I
found the above solution (I didn¹t figure out by myself!)


Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

On 8/15/14, 7:51 AM, "Bob O'Hara" <rni.boh at gmail.com> wrote:

>I've been struggling for half a day on what should be a simple problem...
>I have a data frame of lat/long coordinates that describe a region, and I
>want to draw a polygon around them so I can use that as a boundary (to be
>thrown at INLA, but those details aren't important). The coordinates are
>almost on a regular grid: there is some variation in latitude (because
>we're on a globe).
>If the coordinates were on a regular grid, I could use as.owin() to create
>a mask, and go on from there (I have code that will work). But as.owin()
>doesn't like unevenly spaced points.
>Can anyone suggest a way to sort this out? Preferable without having to
>mess around transforming the coordinates.
>Bob O'Hara
>Biodiversity and Climate Research Centre
>Senckenberganlage 25
>D-60325 Frankfurt am Main,
>Tel: +49 69 798 40226
>Mobile: +49 1515 888 5440
>WWW:   http://www.bik-f.de/root/index.php?page_id=219
>Blog: http://occamstypewriter.org/boboh/
>Journal of Negative Results - EEB: www.jnr-eeb.org
>	[[alternative HTML version deleted]]
>R-help at r-project.org mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list