Geometric Operations
This page covers the primary geospatial operations for manipulating and analyzing geometric shapes in Turf.js. These operations transform input geometries into new geometric forms through mathematical algorithms, including buffering, clipping, hull generation, and grid creation.
For spatial relationship functions (intersects, contains, within), see Boolean Operations. For line-specific operations, see Line Operations. For measurements and coordinate transformations, see Measurements and Transformations.
Operation Categories
Turf's geometric operations are organized into several functional categories, each serving specific spatial analysis needs:
Core Dependencies and Architecture
The geometric operations rely on both internal Turf modules and external computational geometry libraries:
Buffer Operations
The @turf/buffer module creates buffer zones around input geometries using geodesic projections and the Java Topology Suite (JSTS) algorithms.
Buffer Algorithm Implementation
The buffer operation uses a sophisticated coordinate projection approach to handle Earth's curvature:
| Component | Purpose | Implementation |
|---|---|---|
defineProjection() | Creates azimuthal equidistant projection | Uses d3-geo.geoAzimuthalEquidistant() centered on feature |
projectCoords() | Converts to planar coordinates | Recursively projects coordinate arrays |
BufferOp.bufferOp() | Performs geometric buffer | JSTS library operation in projected space |
unprojectCoords() | Converts back to geographic | Inverse projection to WGS84 coordinates |
The main buffer() function handles multiple geometry types through recursive processing:
- FeatureCollection: Processes each feature individually via
featureEach() - GeometryCollection: Recursively processes geometries via
geomEach() - Individual Features: Direct processing through
bufferFeature()
Negative buffer values are supported for "erosion" operations, though invalid geometries may result from excessive negative values.
Polygon Clipping Operations
Union, intersection, and difference operations use the polyclip-ts library for robust polygon clipping algorithms.
Clipping Operation Matrix
| Operation | Module | Input | Output | Algorithm |
|---|---|---|---|---|
| Union | @turf/union | Two or more polygons | Single merged polygon | Greiner-Hormann clipping |
| Intersect | @turf/intersect | Two polygons | Shared area polygon | Sutherland-Hodgman clipping |
| Difference | @turf/difference | Base and clip polygons | Remaining area polygon | Boolean difference operation |
All three operations follow similar patterns:
- Input validation through
@turf/invariant - Coordinate extraction via
@turf/meta - Algorithm execution through
polyclip-ts - Result packaging with
@turf/helpers
Hull Generation
Hull operations create boundary polygons around point sets using different algorithms for convex and concave hulls.
Hull Algorithm Comparison
The @turf/convex module uses the concaveman library for both convex and concave hull generation, while @turf/concave provides additional concave-specific functionality with topological analysis.
Grid Generation Systems
Grid generation creates regular tessellations within bounding boxes for spatial analysis and visualization.
Grid Type Specifications
| Grid Type | Module | Shape | Tessellation Pattern |
|---|---|---|---|
| Hexagonal | @turf/hex-grid | Regular hexagons | Honeycomb pattern |
| Square | @turf/square-grid | Rectangles/squares | Cartesian grid |
| Triangular | @turf/triangle-grid | Equilateral triangles | Triangular lattice |
| Point | @turf/point-grid | Points | Regular point array |
All grid generators share common parameters:
- bbox:
[minX, minY, maxX, maxY]bounding box - cellSide: Cell size in specified units
- units: Distance units from
@turf/helpers - mask: Optional polygon for clipping results
The @turf/square-grid delegates to @turf/rectangle-grid for implementation, while hexagonal and triangular grids implement custom tessellation algorithms.
Contour Generation
Contour operations generate isolines and isobands from gridded data using marching squares algorithms.
Contour Processing Pipeline
Both @turf/isolines and @turf/isobands use the marchingsquares library for contour extraction, with different output geometries:
- Isolines: Generate
LineStringfeatures representing contour lines - Isobands: Generate
Polygonfeatures representing filled contour bands
Shape Analysis Operations
Shape analysis functions compute geometric properties and derive simplified representations of input features.
Analysis Function Categories
| Category | Functions | Purpose |
|---|---|---|
| Center Calculation | center(), centroid(), centerOfMass() | Find representative points |
| Shape Simplification | simplify(), envelope() | Reduce complexity |
| Coordinate Manipulation | flip(), explode(), combine() | Transform coordinate structure |
| Spatial Aggregation | collect(), tag() | Combine features based on spatial relationships |
The @turf/simplify module implements the Ramer-Douglas-Peucker algorithm for line generalization, while @turf/envelope creates minimum bounding rectangles using @turf/bbox-polygon.
Performance Considerations
Geometric operations have varying computational complexity and memory requirements:
Algorithm Complexity
| Operation Type | Typical Complexity | Memory Usage | Optimization Strategy |
|---|---|---|---|
| Buffer | O(n × steps) | High (projection) | Reduce steps parameter |
| Polygon Clipping | O(n × m) | Medium | Pre-filter by bounds |
| Hull Generation | O(n log n) | Low | Spatial indexing |
| Grid Generation | O(cells) | Medium | Stream processing |
| Contour Generation | O(grid²) | High | Tile-based processing |
Large datasets should be processed in chunks using @turf/meta iteration functions (featureEach, geomEach) to manage memory usage effectively.