Buffers & Convex Hulls

An analysis common to modern population genetics is that of finding ecological distances between objects on a landscape.  The estimation of pairwise distance derived from spatial data is a computationally intensive thing, one that if you are not careful will bring your laptop to its knees!  One way to mitigate this data problem is to use a minimal amount raster area so that the estimation of the underlying distance graph can be done on a smaller set of points.  This example provides a simple solution using convex hulls.  Jump below for the complete example.

A Bounding Box Approach

The first option is to create a bounding box around your points.  For this, I’ll use the enigmatic arapat data set included in the gstudio package.

This is the minimal box around the points, but we probably want to have some kind of buffer as we don’t want our populations on the very edge of the raster.

Now we can crop the raster to this size.  I’ll use the altitude tile 22 from worldclim for this.  Here is the original raster.

which looks like.Unknown-1

We can crop it to the bounding box for the points as:


A Convex Hull Approach

As you look at the image above, you see that there is a lot of area that is not in ‘the midst’ of the points.  Moreover, there are those points on the mainland who are probably not going to be connected to the peninsular points due to the Sea of Cortez.  I’m going to first limit the points to the ones on the peninsula and then we will estimate a convex hull. A convex hull is the small convex polygon that contains all the points.  You can think of it like snapping a rubber band around the points.  This will give us the minimal raster area on which can estimate raster-based distances (though we probably want to put a buffer around it so that we do not have points at the very edge).  Here is how we do that.

First, I’ll extract just the peninsular populations.

Then we will load in the rgeos library and determine the hull


This is great!  So now, we probably want to put a buffer around it so that the points are on the very edge.  The units we will use depend upon the units of the raster.  So in this case, it will be in degrees of lat/lon (which is a bit dodgy since they are not quite equal distance metrics but for our purposes it is just fine).

Unknown-4Perfect!  Now, we need to only keep those components of our raster that are within the hull_plus_buffer polygon, and for this we use a mask.



Exact!  Now, when you run this raster and these points through gdistance , you will be using a more minimal set of raster data for the calculations and it will be more efficient—and reduce the thermal load you are imposing on your laptop!