CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-turf--turf

A JavaScript library for performing geospatial operations with GeoJSON data

Pending
Overview
Eval results
Files

geometric-operations.mddocs/

Geometric Operations

Advanced geometric operations for creating, modifying, and combining geographic features including buffers, intersections, unions, and spatial transformations. These operations create new geometries from existing ones.

Capabilities

Buffer Operations

Create buffer zones around geometric features.

/**
 * Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
 */
function buffer(
  geojson: FeatureCollection<any> | Feature<any> | Geometry,
  radius: number,
  options?: { units?: Units; steps?: number }
): FeatureCollection<Polygon> | Feature<Polygon>;

Usage Examples:

import { buffer, point, lineString, polygon } from "@turf/turf";

// Buffer around a point
const pt = point([-90.548630, 14.616599]);
const bufferedPoint = buffer(pt, 500, { units: 'meters' });

// Buffer around a line
const line = lineString([[-90.548630, 14.616599], [-90.560837, 14.613841]]);
const bufferedLine = buffer(line, 0.5, { units: 'kilometers' });

// Buffer around a polygon with custom steps (more steps = smoother curves)
const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);
const bufferedPoly = buffer(poly, 2, { units: 'kilometers', steps: 64 });

console.log('Buffered point:', bufferedPoint);
console.log('Buffered line:', bufferedLine);

Set Operations

Perform geometric set operations between polygons.

/**
 * Takes two polygons and finds their intersection.
 */
function intersect(
  feature1: Feature<Polygon | MultiPolygon>,
  feature2: Feature<Polygon | MultiPolygon>,
  options?: { properties?: GeoJsonProperties }
): Feature<Polygon | MultiPolygon> | null;

/**
 * Takes input features and returns a combined polygon. If the input features are not contiguous, this function returns a MultiPolygon feature.
 */
function union(...features: Feature<Polygon | MultiPolygon>[]): Feature<Polygon | MultiPolygon> | null;

/**
 * Finds the difference between two polygons by clipping the second polygon from the first.
 */
function difference(
  feature1: Feature<Polygon | MultiPolygon>,
  feature2: Feature<Polygon | MultiPolygon>,
  options?: { properties?: GeoJsonProperties }
): Feature<Polygon | MultiPolygon> | null;

Usage Examples:

import { intersect, union, difference, polygon } from "@turf/turf";

const poly1 = polygon([[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]]);
const poly2 = polygon([[[3, 3], [3, 8], [8, 8], [8, 3], [3, 3]]]);
const poly3 = polygon([[[6, 6], [6, 9], [9, 9], [9, 6], [6, 6]]]);

// Find intersection (overlapping area)
const intersection = intersect(poly1, poly2);

// Create union (combined area) 
const unionResult = union(poly1, poly2, poly3);

// Find difference (poly1 with poly2 removed)
const differenceResult = difference(poly1, poly2);

console.log('Intersection area:', intersection);
console.log('Union result:', unionResult);
console.log('Difference result:', differenceResult);

Hull Operations

Create convex and concave hulls around point sets.

/**
 * Takes a set of points and returns a concave hull Polygon or MultiPolygon.
 * A concave hull, by contrast to a convex hull, can describe the shape of a point set.
 */
function concave(
  geojson: FeatureCollection<Point>,
  maxEdge: number,
  options?: { units?: Units; properties?: GeoJsonProperties }
): Feature<Polygon> | null;

/**
 * Takes a Feature or a FeatureCollection and returns a convex hull Polygon.
 * A convex hull is the smallest convex polygon that contains all the given points.
 */
function convex(
  geojson: FeatureCollection<any> | Feature<any> | Geometry,
  options?: { concavity?: number; properties?: GeoJsonProperties }
): Feature<Polygon>;

Usage Examples:

import { concave, convex, featureCollection, point } from "@turf/turf";

// Create point collection
const points = featureCollection([
  point([10.195312, 43.755225]),
  point([10.404052, 43.8424511]),
  point([10.579833, 43.659924]),
  point([10.360107, 43.516688]),
  point([10.14038, 43.588348]),
  point([10.195312, 43.755225])
]);

// Create convex hull (always convex)
const convexHull = convex(points);

// Create concave hull (can follow point distribution shape)
const concaveHull = concave(points, 50, { units: 'kilometers' });

console.log('Convex hull:', convexHull);
console.log('Concave hull:', concaveHull);

Envelope and Bounding Operations

Create bounding shapes around features.

/**
 * Takes any number of features and returns a rectangular Polygon that encompasses all vertices.
 */
function envelope(geojson: AllGeoJSON): Feature<Polygon>;

/**
 * Takes a bounding box and uses it to clip a GeoJSON Feature or FeatureCollection.
 */
function bboxClip<G>(
  feature: Feature<G> | G,
  bbox: BBox
): Feature<G>;

/**
 * Takes a bbox and returns an equivalent polygon.
 */
function bboxPolygon(bbox: BBox, options?: { properties?: GeoJsonProperties; id?: Id }): Feature<Polygon>;

Usage Examples:

import { envelope, bboxClip, bboxPolygon, polygon, lineString } from "@turf/turf";

const line = lineString([[-2, -1], [4, 6], [8, 2], [3, -4]]);
const poly = polygon([[[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]]]);

// Create envelope around features
const env = envelope(line);

// Create polygon from bounding box
const bboxPoly = bboxPolygon([-4, -4, 4, 4]);

// Clip geometry to bounding box
const clipped = bboxClip(poly, [0, 0, 5, 5]);

console.log('Envelope:', env);
console.log('Clipped polygon:', clipped);

Circle and Ellipse Creation

Create circular and elliptical polygon approximations.

/**
 * Takes a Point and calculates the circle polygon given a radius in degrees, radians, miles, or kilometers; and steps for precision.
 */
function circle(
  center: Feature<Point> | Point | number[],
  radius: number,
  options?: { steps?: number; units?: Units; properties?: GeoJsonProperties }
): Feature<Polygon>;

/**
 * Takes a Point and calculates the ellipse polygon given two semi-axes expressed in miles, kilometers, or degrees.
 */
function ellipse(
  center: Feature<Point> | Point | number[],
  xSemiAxis: number,
  ySemiAxis: number,
  options?: { angle?: number; pivot?: Coord; steps?: number; units?: Units; properties?: GeoJsonProperties }
): Feature<Polygon>;

/**
 * Creates a circular sector of a circle of given radius and center Point,
 * between (clockwise) bearing1 and bearing2; 0 bearing is North of center point, positive clockwise.
 */
function sector(
  center: Coord,
  radius: number,
  bearing1: number,
  bearing2: number,
  options?: { units?: Units; steps?: number; properties?: GeoJsonProperties }
): Feature<Polygon>;

Usage Examples:

import { circle, ellipse, sector, point } from "@turf/turf";

const center = point([-75.343, 39.984]);

// Create circle
const circleShape = circle(center, 5, { 
  steps: 32, 
  units: 'kilometers' 
});

// Create ellipse
const ellipseShape = ellipse(center, 5, 3, {
  angle: 45,
  steps: 32,
  units: 'kilometers'
});

// Create sector (pie slice)
const sectorShape = sector(center, 5, 45, 135, {
  units: 'kilometers',
  steps: 32
});

console.log('Circle:', circleShape);
console.log('Ellipse:', ellipseShape);
console.log('Sector:', sectorShape);

Geometric Simplification

Simplify complex geometries by reducing coordinate density.

/**
 * Takes a GeoJSON object and returns a simplified version. Internally uses simplify-js to perform simplification using the Ramer-Douglas-Peucker algorithm.
 */
function simplify<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  options?: { tolerance?: number; highQuality?: boolean; mutate?: boolean }
): FeatureCollection<G> | Feature<G>;

Usage Examples:

import { simplify, lineString } from "@turf/turf";

// Complex line with many points
const complexLine = lineString([
  [0, 0], [0.1, 0.1], [0.2, 0.05], [0.3, 0.15], [0.4, 0.1],
  [0.5, 0.2], [0.6, 0.15], [0.7, 0.25], [0.8, 0.2], [0.9, 0.3], [1, 0.25]
]);

// Simplify with different tolerance levels
const simplified1 = simplify(complexLine, { tolerance: 0.01 });
const simplified2 = simplify(complexLine, { tolerance: 0.05, highQuality: true });

console.log('Original points:', complexLine.geometry.coordinates.length);
console.log('Simplified (0.01):', simplified1.geometry.coordinates.length);
console.log('Simplified (0.05):', simplified2.geometry.coordinates.length);

Dissolve Operations

Combine overlapping or adjacent polygons.

/**
 * Dissolves overlapping LineString or Polygon features into a single geometry.
 */
function dissolve<P>(
  geojson: FeatureCollection<Polygon | MultiPolygon, P>,
  options?: { propertyName?: keyof P }
): FeatureCollection<Polygon | MultiPolygon, P>;

Usage Examples:

import { dissolve, polygon, featureCollection } from "@turf/turf";

// Create overlapping polygons
const poly1 = polygon([[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]], { region: 'A' });
const poly2 = polygon([[[3, 3], [3, 8], [8, 8], [8, 3], [3, 3]]], { region: 'A' });
const poly3 = polygon([[[10, 0], [10, 5], [15, 5], [15, 0], [10, 0]]], { region: 'B' });

const collection = featureCollection([poly1, poly2, poly3]);

// Dissolve all polygons
const dissolved = dissolve(collection);

// Dissolve by property (group by region)
const dissolvedByRegion = dissolve(collection, { propertyName: 'region' });

console.log('Dissolved all:', dissolved);
console.log('Dissolved by region:', dissolvedByRegion);

Install with Tessl CLI

npx tessl i tessl/npm-turf--turf

docs

advanced-analysis.md

boolean-operations.md

coordinate-operations.md

data-generation.md

data-utilities.md

geometric-operations.md

helpers.md

index.md

invariant.md

line-processing.md

measurement.md

meta.md

spatial-analysis.md

tile.json