More Buffered Samples with Hex Cells

Earth Engine by Example

I’ve never had much use for hex maps — until now.

A 50km hex grid based on an Albers equal-area projection with a random color scheme. https://goo.gle/3tTUssJ

In a previous article, I demonstrated how to generate a random sample with buffering to guarantee that no two samples are closer than a given distance. Since then, a (now obvious) idea occurred to me that using a hex grid instead of a rectangular one would pack more samples into the same region while still observing the minimum distance requirement.

Here’s a ‘relatively simple’ way to generate a hex grid like the image above, starting from a rectangular pixelCoordinates image in a given projection. Each input pixel is converted to a ‘hex coordinate’ and a unique ID is computed from those. This code is based on the affine transform math from a very nice article about hex adjacency by James McNeill.

Incorporating this function into the buffered samples example using the same 50km spacing, produces 775 samples for the rectangular grid and 898 samples for the hex grid. That’s 15% more samples, still with the same minimum spacing (experiments with smaller grids show that the +15% is pretty constant at all scales).

Buffered samples using a rectangular grid (left) and a hex grid (right) using a 50km buffer.

The full code can be found here: https://goo.gle/3bBnA1K

Caveats

  • Clipping grid-cell images can be tricky. You probably don’t want to split a cell in half by clipping out something like a river or a bay. One way to make sure you maintain whole cells is to generate a mask using reduceConnectedComponents() with a max reducer (or min if you want cells that are strictly inside the region) on the cells and a binary image of the clipping region. This will generate a result where the cells that touch the region will have a 1 and those that don’t will have a 0 (which can then be used as a mask). https://goo.gle/3f1OKRn.
    (Note that if you zoom in, you might exceed the 128 pixel neighborhood used in the reduceConnectedComponents call and end up with a blank image.)
Grid cells that touch the ROI are kept intact, while those that don’t are removed.
  • You can of course use hex cells for lots of other things. Justin Braaten says the research lab he worked at routinely used them for spatial aggregation:

Hexagons have a lower max distance between members and considering spatial autocorrelation, they may be more alike and better representatives of the group. Here is an example of summarizing MODIS NDVI by hexagon using mean reduction: https://code.earthengine.google.com/50accf356bbce369ebfa705c741c4ed2

MODIS NDVI aggregated into hex cells.

See Also

I’m a software engineer at Google and one of the founders of Google Earth Engine.