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

meta.mddocs/

Geometry Iteration and Manipulation

Utilities for iterating over and manipulating GeoJSON geometries, coordinates, and properties using functional programming patterns. These functions provide efficient ways to traverse and transform geographic data structures.

Capabilities

Coordinate Iteration

Functions for iterating over coordinate arrays in GeoJSON objects.

/**
 * Iterate over coordinates in any GeoJSON object, similar to Array.forEach()
 */
function coordEach(
  geojson: AllGeoJSON,
  callback: (
    currentCoord: number[],
    coordIndex: number,
    featureIndex: number,
    multiFeatureIndex: number,
    geometryIndex: number
  ) => void,
  excludeWrapCoord?: boolean
): void;

/**
 * Reduce coordinates in any GeoJSON object, similar to Array.reduce()
 */
function coordReduce<Reducer>(
  geojson: AllGeoJSON,
  callback: (
    previousValue: Reducer,
    currentCoord: number[],
    coordIndex: number,
    featureIndex: number,
    multiFeatureIndex: number,
    geometryIndex: number
  ) => Reducer,
  initialValue?: Reducer
): Reducer;

/**
 * Get all coordinates from any GeoJSON object.
 */
function coordAll(geojson: AllGeoJSON): number[][];

Usage Examples:

import { coordEach, coordReduce, coordAll, polygon } from "@turf/turf";

const poly = polygon([[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]]);

// Iterate over each coordinate
coordEach(poly, (coord, coordIndex) => {
  console.log(`Coordinate ${coordIndex}: [${coord[0]}, ${coord[1]}]`);
});

// Calculate the sum of x coordinates
const sumX = coordReduce(poly, (sum, coord) => sum + coord[0], 0);

// Get all coordinates as an array
const allCoords = coordAll(poly);
// Returns: [[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]

Feature Iteration

Functions for iterating over features in FeatureCollections.

/**
 * Iterate over features in any GeoJSON object, similar to Array.forEach.
 */
function featureEach<G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | Feature<GeometryCollection, P>,
  callback: (currentFeature: Feature<G, P>, featureIndex: number) => void
): void;

/**
 * Reduce features in any GeoJSON object, similar to Array.reduce().
 */
function featureReduce<Reducer, G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | Feature<GeometryCollection, P>,
  callback: (
    previousValue: Reducer,
    currentFeature: Feature<G, P>,
    featureIndex: number
  ) => Reducer,
  initialValue?: Reducer
): Reducer;

Usage Examples:

import { featureEach, featureReduce, featureCollection, point } from "@turf/turf";

const points = featureCollection([
  point([0, 0], { name: "Point A" }),
  point([1, 1], { name: "Point B" }),
  point([2, 2], { name: "Point C" })
]);

// Iterate over each feature
featureEach(points, (feature, index) => {
  console.log(`Feature ${index}: ${feature.properties.name}`);
});

// Count features with specific properties
const namedFeatures = featureReduce(points, (count, feature) => {
  return feature.properties.name ? count + 1 : count;
}, 0);

Property Iteration

Functions for iterating over feature properties.

/**
 * Iterate over properties in any GeoJSON object, similar to Array.forEach()
 */
function propEach<Props>(
  geojson: Feature<any> | FeatureCollection<any> | Feature<GeometryCollection>,
  callback: (currentProperties: Props, featureIndex: number) => void
): void;

/**
 * Reduce properties in any GeoJSON object into a single value, similar to how Array.reduce works.
 */
function propReduce<Reducer, P>(
  geojson: Feature<any, P> | FeatureCollection<any, P> | Geometry,
  callback: (
    previousValue: Reducer,
    currentProperties: P,
    featureIndex: number
  ) => Reducer,
  initialValue?: Reducer
): Reducer;

Usage Examples:

import { propEach, propReduce, featureCollection, point } from "@turf/turf";

const points = featureCollection([
  point([0, 0], { population: 1000, name: "City A" }),
  point([1, 1], { population: 2000, name: "City B" }),
  point([2, 2], { population: 1500, name: "City C" })
]);

// Iterate over properties
propEach(points, (properties, index) => {
  console.log(`${properties.name}: ${properties.population}`);
});

// Calculate total population
const totalPopulation = propReduce(points, (total, props) => {
  return total + (props.population || 0);
}, 0);

Geometry Iteration

Functions for iterating over geometries within GeoJSON objects.

/**
 * Iterate over each geometry in any GeoJSON object, similar to Array.forEach()
 */
function geomEach<G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,
  callback: (
    currentGeometry: G,
    featureIndex: number,
    featureProperties: P,
    featureBBox: BBox,
    featureId: Id
  ) => void
): void;

/**
 * Reduce geometry in any GeoJSON object, similar to Array.reduce().
 */
function geomReduce<Reducer, G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,
  callback: (
    previousValue: Reducer,
    currentGeometry: G,
    featureIndex: number,
    featureProperties: P,
    featureBBox: BBox,
    featureId: Id
  ) => Reducer,
  initialValue?: Reducer
): Reducer;

Flattened Feature Iteration

Functions for iterating over flattened features (Multi* geometries split into individual features).

/**
 * Iterate over flattened features in any GeoJSON object, similar to Array.forEach.
 */
function flattenEach<G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,
  callback: (
    currentFeature: Feature<G, P>,
    featureIndex: number,
    multiFeatureIndex: number
  ) => void
): void;

/**
 * Reduce flattened features in any GeoJSON object, similar to Array.reduce().
 */
function flattenReduce<Reducer, G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | G | GeometryCollection | Feature<GeometryCollection, P>,
  callback: (
    previousValue: Reducer,
    currentFeature: Feature<G, P>,
    featureIndex: number,
    multiFeatureIndex: number
  ) => Reducer,
  initialValue?: Reducer
): Reducer;

Line and Segment Iteration

Functions for iterating over line segments and linear features.

/**
 * Iterate over 2-vertex line segment in any GeoJSON object, similar to Array.forEach()
 * (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
 */
function segmentEach<P>(
  geojson: AllGeoJSON,
  callback: (
    currentSegment?: Feature<LineString, P>,
    featureIndex?: number,
    multiFeatureIndex?: number,
    segmentIndex?: number,
    geometryIndex?: number
  ) => void
): void;

/**
 * Reduce 2-vertex line segment in any GeoJSON object, similar to Array.reduce()
 * (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
 */
function segmentReduce<Reducer, P>(
  geojson: FeatureCollection<Lines, P> | Feature<Lines, P> | Lines | Feature<GeometryCollection, P> | GeometryCollection,
  callback: (
    previousValue?: Reducer,
    currentSegment?: Feature<LineString, P>,
    featureIndex?: number,
    multiFeatureIndex?: number,
    segmentIndex?: number,
    geometryIndex?: number
  ) => Reducer,
  initialValue?: Reducer
): Reducer;

/**
 * Iterate over line or ring coordinates in LineString, Polygon, MultiLineString, MultiPolygon Features or Geometries, similar to Array.forEach.
 */
function lineEach<P>(
  geojson: FeatureCollection<Lines, P> | Feature<Lines, P> | Lines | Feature<GeometryCollection, P> | GeometryCollection,
  callback: (
    currentLine: Feature<LineString, P>,
    featureIndex?: number,
    multiFeatureIndex?: number,
    geometryIndex?: number
  ) => void
): void;

/**
 * Reduce features in any GeoJSON object, similar to Array.reduce().
 */
function lineReduce<Reducer, P>(
  geojson: FeatureCollection<Lines, P> | Feature<Lines, P> | Lines | Feature<GeometryCollection, P> | GeometryCollection,
  callback: (
    previousValue?: Reducer,
    currentLine?: Feature<LineString, P>,
    featureIndex?: number,
    multiFeatureIndex?: number,
    geometryIndex?: number
  ) => Reducer,
  initialValue?: Reducer
): Reducer;

Usage Examples:

import { segmentEach, lineEach, polygon, lineString } from "@turf/turf";

const line = lineString([[0, 0], [1, 1], [2, 2], [3, 3]]);

// Iterate over each segment
segmentEach(line, (segment, featureIndex, multiFeatureIndex, segmentIndex) => {
  console.log(`Segment ${segmentIndex}:`, segment?.geometry.coordinates);
});

// Iterate over each line (useful for polygons with multiple rings)
const poly = polygon([
  [[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]], // exterior ring
  [[2, 2], [2, 8], [8, 8], [8, 2], [2, 2]]      // hole
]);

lineEach(poly, (line, featureIndex, multiFeatureIndex, geometryIndex) => {
  console.log(`Ring ${geometryIndex}:`, line.geometry.coordinates);
});

Geometry Finder Functions

Functions for finding specific geometric elements by index.

/**
 * Finds a particular 2-vertex LineString Segment from a GeoJSON using @turf/meta indexes.
 * Negative indexes are permitted. Point & MultiPoint will always return null.
 */
function findSegment<G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | G,
  options?: {
    featureIndex?: number;
    multiFeatureIndex?: number;
    geometryIndex?: number;
    segmentIndex?: number;
    properties?: P;
    bbox?: BBox;
    id?: Id;
  }
): Feature<LineString, P>;

/**
 * Finds a particular Point from a GeoJSON using @turf/meta indexes.
 * Negative indexes are permitted.
 */
function findPoint<G, P>(
  geojson: Feature<G, P> | FeatureCollection<G, P> | G,
  options?: {
    featureIndex?: number;
    multiFeatureIndex?: number;
    geometryIndex?: number;
    coordIndex?: number;
    properties?: P;
    bbox?: BBox;
    id?: Id;
  }
): Feature<Point, P>;

Usage Examples:

import { findSegment, findPoint, polygon } from "@turf/turf";

const poly = polygon([[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]]]);

// Find specific segment
const secondSegment = findSegment(poly, { segmentIndex: 1 });
// Returns: LineString from [0, 10] to [10, 10]

// Find specific point
const thirdPoint = findPoint(poly, { coordIndex: 2 });
// Returns: Point at [10, 10]

// Use negative indices
const lastPoint = findPoint(poly, { coordIndex: -1 });
// Returns: Point at [0, 0] (last coordinate)

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