Skip to content

Grid Generation

Grid generation functions in Turf.js create regular geometric patterns of points or polygons within a specified area. These utilities are essential for spatial analysis tasks such as sampling, tessellation, and creating uniform spatial data structures. The grid generation system provides four distinct grid types: hexagonal, square, triangular, and point grids, each optimized for different analytical use cases.

For information about spatial clustering algorithms, see Clustering. For statistical analysis functions that work with grid-based data, see Statistical Analysis.

Grid Types and Module Structure

The grid generation system consists of five specialized modules, each implementing a specific grid pattern:

ModuleFunctionOutputPrimary Use Case
@turf/point-gridpointGrid()Point featuresSampling, analysis locations
@turf/square-gridsquareGrid()Square polygon featuresRegular tessellation, equal-area cells
@turf/rectangle-gridrectangleGrid()Rectangle polygon featuresBase grid implementation
@turf/hex-gridhexGrid()Hexagonal polygon featuresOptimal coverage, adjacency analysis
@turf/triangle-gridtriangleGrid()Triangular polygon featuresTriangulation-based analysis

Module Dependencies and Architecture

SVG
100%

Point Grid Implementation

The pointGrid function generates a regular array of point features within a bounding box. This is the most commonly used grid type for sampling and analysis operations.

Function Signature and Parameters

All grid generation functions follow a consistent API pattern:

gridFunction(bbox: BBox, cellSide: number, options?: GridOptions)

Standard Parameters:

ParameterTypeDescription
bboxBBoxBounding box array [minX, minY, maxX, maxY] defining grid area
cellSidenumberDistance between grid cells (spacing or side length)
optionsObjectOptional configuration object

Common Options:

OptionTypeDefaultModulesDescription
unitsstring"kilometers"AllUnit of measurement: "miles", "kilometers", "degrees", etc.
maskFeature<Polygon>undefinedpoint-gridPolygon to clip grid points
propertiesObject{}AllProperties to add to each feature
trianglesbooleanfalsehex-gridReturn triangular grid instead of hexagons

Grid-Specific Processing Flow

SVG
100%

Usage Examples and Configuration

Point Grid Example:

const bbox = [-95, 30, -85, 40];
const cellSide = 50;
const options = { 
  units: 'miles',
  properties: { gridType: 'analysis' }
};
const grid = pointGrid(bbox, cellSide, options);

Hexagonal Grid with Triangulation:

const bbox = [-96.6, 31.1, -84.9, 40.6];
const cellSide = 50;
const options = {
  units: 'miles',
  triangles: true,  // Returns triangular cells instead of hexagons
  properties: { foo: 'bar' }
};
const hexGrid = hexGrid(bbox, cellSide, options);
// Access properties: hexGrid.features[0].properties.foo

Square Grid via Rectangle Grid:

The squareGrid() function internally delegates to rectangleGrid() to create square cells, ensuring equal dimensions:

const bbox = [-95, 30, -85, 40];
const cellSide = 10;
const grid = squareGrid(bbox, cellSide, { units: 'kilometers' });

Configuration Options Matrix:

Grid TypeSupports MaskingSupports TriangulationCell Clipping Method
pointGridYes (via mask option)No@turf/boolean-within
squareGridNoNoN/A
rectangleGridNoNoN/A
hexGridYes (implicit)Yes (via triangles option)@turf/intersect
triangleGridYes (implicit)No@turf/intersect

Hexagonal Grid Special Features

The @turf/hex-grid module provides unique functionality compared to other grid types:

Hexagon vs Triangle Mode

The hexGrid() function can generate either hexagonal or triangular grids based on the triangles option:

SVG
100%

Hexagon Geometry Calculation

Hexagonal grids use specialized geometry based on regular hexagon properties:

Hexagon PropertyCalculationPurpose
Side lengthcellSide parameterDirect input
Width (flat-topped)√3 × sideLengthHorizontal spacing
Height (flat-topped)2 × sideLengthVertical spacing
Row offsetwidth / 2Alternating row alignment
Vertex anglesMultiples of 60°Regular hexagon vertices

Distance and Intersection Usage

The hex-grid module leverages other Turf modules for accurate calculations:

SVG
100%

Geographic Edge Cases and Projections

Grid generation handles several geographic edge cases, particularly around the International Date Line and polar regions:

Coordinate System Handling

The system processes coordinates across various geographic boundaries:

Test CaseCoordinatesSpecial Handling
Fiji[-180.34, -17.11, -179.57, -16.48]Date line crossing
Arctic[-95.87, 74.46, -94.03, 74.90]High latitude
Large bbox[-220.78, -80.64, -29.53, 78.34]Extended longitude range

Unit Conversion System

Grid spacing is specified in various units and converted to appropriate coordinate deltas:

SVG
100%

Masking and Filtering

The grid generation system supports polygon masking to limit grid points to specific geographic areas:

SVG
100%

The masking system processes complex polygons with multiple vertices, as demonstrated in the Piedmont region test case with 28 coordinate pairs defining the boundary.

Integration with Core Modules

Grid generation functions integrate closely with Turf's core helper and metadata modules:

SVG
100%

The integration ensures consistent GeoJSON output format and leverages shared coordinate processing utilities across all grid types.