Skip to content

Spatial Analysis

Spatial analysis in Turf.js provides advanced geospatial operations for clustering, statistical analysis, and pattern detection. This section covers specialized analytical functions that extract insights from geographical data distributions and relationships. For basic geometric operations, see Geometric Operations.

Overview of Spatial Analysis Functions

Turf.js provides three main categories of spatial analysis capabilities:

SVG
100%

The spatial analysis functions build upon Turf.js core infrastructure to provide advanced analytical capabilities. Each category addresses specific spatial analysis needs through specialized algorithms and data structures.

For detailed information about specific analysis types, see:

Integration with Core Modules

Spatial analysis functions integrate extensively with Turf.js core infrastructure:

Spatial Analysis Data Flow

SVG
100%

Core Module Dependencies

ModuleDependenciesPurpose
@turf/clusters-kmeans@turf/clone, @turf/helpers, @turf/invariant, @turf/meta, skmeansK-means clustering with point cloning and iteration
@turf/clusters-dbscan@turf/clone, @turf/distance, @turf/helpers, @turf/meta, rbushDBSCAN clustering with spatial indexing
@turf/standard-deviational-ellipse@turf/center-mean, @turf/ellipse, @turf/helpers, @turf/invariant, @turf/meta, @turf/points-within-polygonStatistical ellipse generation
@turf/center-median@turf/center-mean, @turf/centroid, @turf/distance, @turf/helpers, @turf/metaIterative median center calculation

Shape Analysis

Shape analysis generates geometries that represent patterns or boundaries of point sets.

Concave Hull

A concave hull creates a tighter-fitting boundary around points than a convex hull by allowing "indentations" in the boundary.

SVG
100%

The concave hull implementation:

  • Uses a triangulated irregular network (TIN) as the basis
  • Filters triangles based on edge length
  • Merges remaining triangles to form the hull
  • Returns null if no valid hull can be computed

Example Usage:

const hull = turf.concave(points, {
  maxEdge: 2, // maximum edge length
  units: 'kilometers' // units for maxEdge
});

Ellipse Generation

Creates an elliptical polygon from a center point, given two semi-axes and optional parameters.

SVG
100%

The ellipse function:

  • Creates a polygon in the shape of an ellipse
  • Supports rotation angle specification
  • Customizable resolution via steps parameter
  • Supports different units for axes lengths

Example Usage:

const ellipse = turf.ellipse(center, 5, 3, {
  units: 'kilometers', // units for axes lengths
  steps: 64, // number of vertices
  angle: 45 // rotation angle in degrees
});

Boolean Spatial Analysis

Boolean spatial operations test topological relationships between geometries, returning true or false.

SVG
100%

Key Boolean Operations:

FunctionDescriptionInverse Of
booleanContainsTests if the second geometry is completely within the firstN/A
booleanWithinTests if the first geometry is completely within the secondbooleanContains
booleanOverlapTests if geometries have partial overlap (without complete containment)N/A
booleanDisjointTests if geometries have no overlapAll other boolean functions

Example Usage:

// A contains B if B is completely inside A
const contains = turf.booleanContains(polygonA, polygonB);

// A within B is the same as B contains A
const within = turf.booleanWithin(polygonA, polygonB);

// Overlap means partial intersection without complete containment
const overlaps = turf.booleanOverlap(polygonA, polygonB);

// Disjoint means no overlap at all
const isDisjoint = turf.booleanDisjoint(polygonA, polygonB);

Integration with Other Turf Components

Spatial analysis functions typically build upon the core modules and integrate with other Turf.js components:

SVG
100%

For information about related spatial analysis techniques involving grids and interpolation, see Grids and Interpolation.