turf.squareGrid
What is turf.squareGrid?
turf.squareGrid tiles a bounding box with square polygons and returns them as a FeatureCollection<Polygon>. Cell dimensions are kept constant in degrees, not in projected metres.
turf.squareGrid(bbox, cellSide, options?) → FeatureCollection<Polygon>Options include:
units— units forcellSide, converted internally to degrees (default'kilometers')mask— aPolygonorMultiPolygon; cells outside the mask are omittedproperties— properties attached to every cell
When would you use turf.squareGrid?
Use turf.squareGrid for quick spatial binning — counting incidents per 500 m cell, computing mean sensor readings per tile, or producing a Web-Mercator-aligned grid for raster-style thematic maps.
Square grids are the simplest grid to reason about and the easiest to align with raster tiles, but they suffer from more pronounced directional bias than hexagons for analysis. If you are aggregating counts for visualisation, turf.hexGrid often reads better.
1const bbox = [-122.6, 37.6, -122.3, 37.9];
2const grid = turf.squareGrid(bbox, 1, { units: 'kilometers' });FAQs
How do I install Turf.js to use this function?
Install npm install @turf/square-grid and import import { squareGrid } from '@turf/square-grid', or use turf.squareGrid from @turf/turf.
Are the squares actually square on the map?
Only in degree units. squareGrid maintains constant degree width and height, so cells look roughly square at low latitudes and increasingly stretched north–south towards the poles when projected to Web Mercator. This is a consequence of lat/lng geometry, not a bug.
Can I limit the grid to an irregular region?
Yes, pass options.mask with a Polygon or MultiPolygon. Only cells whose centroid lies inside the mask are returned, which is much faster than post-filtering.
How should I choose between square, hex, and triangle grids?
Use squareGrid when the output needs to align with raster tiles or orthogonal axes. Use hexGrid for spatial-binning choropleths to avoid directional bias. Use triangleGrid for scientific viz or when you need a triangulation for downstream interpolation.