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:
- packages/turf-clusters-kmeans/package.json
- packages/turf-clusters-dbscan/package.json
- packages/turf-clusters/package.json
- packages/turf-standard-deviational-ellipse/package.json
- packages/turf-center-median/package.json
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:
- Clustering - Point clustering algorithms including K-means and DBSCAN
- Grid Generation - Creating structured spatial grids for analysis
- Statistical Analysis - Distribution analysis, center calculations, and spatial statistics
Integration with Core Modules
Spatial analysis functions integrate extensively with Turf.js core infrastructure:
Spatial Analysis Data Flow
- packages/turf-clusters-kmeans/package.json79-87
- packages/turf-clusters-dbscan/package.json79-87
- packages/turf-standard-deviational-ellipse/package.json71-80
- packages/turf-center-median/package.json70-78
Core Module Dependencies
| Module | Dependencies | Purpose |
|---|---|---|
@turf/clusters-kmeans | @turf/clone, @turf/helpers, @turf/invariant, @turf/meta, skmeans | K-means clustering with point cloning and iteration |
@turf/clusters-dbscan | @turf/clone, @turf/distance, @turf/helpers, @turf/meta, rbush | DBSCAN clustering with spatial indexing |
@turf/standard-deviational-ellipse | @turf/center-mean, @turf/ellipse, @turf/helpers, @turf/invariant, @turf/meta, @turf/points-within-polygon | Statistical ellipse generation |
@turf/center-median | @turf/center-mean, @turf/centroid, @turf/distance, @turf/helpers, @turf/meta | Iterative median center calculation |
- packages/turf-clusters-kmeans/package.json79-87
- packages/turf-clusters-dbscan/package.json79-87
- packages/turf-standard-deviational-ellipse/package.json71-80
- packages/turf-center-median/package.json70-78
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.
- packages/turf-concave/package.json
- packages/turf-concave/index.ts42-76
- packages/turf-concave/README.md
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.
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.
- packages/turf-boolean-contains/package.json
- packages/turf-boolean-within/package.json
- packages/turf-boolean-overlap/package.json
- packages/turf-boolean-disjoint/package.json
Key Boolean Operations:
| Function | Description | Inverse Of |
|---|---|---|
booleanContains | Tests if the second geometry is completely within the first | N/A |
booleanWithin | Tests if the first geometry is completely within the second | booleanContains |
booleanOverlap | Tests if geometries have partial overlap (without complete containment) | N/A |
booleanDisjoint | Tests if geometries have no overlap | All 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:
- packages/turf-nearest-point-to-line/package.json
- packages/turf-point-to-line-distance/package.json
- packages/turf-clone/package.json
- packages/turf-projection/package.json
For information about related spatial analysis techniques involving grids and interpolation, see Grids and Interpolation.