Skip to content

Instantly share code, notes, and snippets.

@maptastik
Last active December 1, 2018 15:28
Show Gist options
  • Save maptastik/4e0470efe97e3f51b537d5f09530f6b8 to your computer and use it in GitHub Desktop.
Save maptastik/4e0470efe97e3f51b537d5f09530f6b8 to your computer and use it in GitHub Desktop.
This function takes an input GeoJSON dataset and creates a generates a hex grid from the bounding box of that layer.

This function takes an input GeoJSON dataset and creates a generates a hex grid from the bounding box of that layer. Because of the way hex grids are created using turfjs, using the bounding box doesn't necessarily result in every area of the input GeoJSON being covered by a hex grid. As such you can also set buffer parameters, radius and bufferUnits, to expand the area for which the bounding box is created. You can define the size of the hexagons with the cellSide and hexUnits parameters.

function hexGridFromLayerBBox(
bboxJson,
cellSide = 1,
hexUnits = "miles",
radius = 0,
bufferUnits = "miles"
) {
// requires turfjs
let bbox = turf.bbox(
turf.buffer(bboxJson, radius, {
units: bufferUnits
})
);
return turf.hexGrid(bbox, cellSide, {
unit: hexUnits
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment