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

coordinate-operations.mddocs/

Coordinate and Projection Operations

Functions for transforming coordinates, changing projections, and manipulating geometric positioning. These operations modify the spatial position and orientation of geographic features.

Capabilities

Geometric Transformations

Apply spatial transformations to features including translation, rotation, and scaling.

/**
 * Moves any GeoJSON Feature or Geometry of a specified distance along a Rhumb Line
 * on the provided direction angle.
 */
function transformTranslate<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  distance: number,
  direction: number,
  options?: { units?: Units; zTranslation?: number; mutate?: boolean }
): FeatureCollection<G> | Feature<G>;

/**
 * Rotates any GeoJSON Feature or Geometry of a specified angle, around its centroid or a given pivot point.
 */
function transformRotate<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  angle: number,
  options?: { pivot?: Coord; mutate?: boolean }
): FeatureCollection<G> | Feature<G>;

/**
 * Scale a GeoJSON from a given point by a factor of scaling (ex: factor=2 would make the GeoJSON 200% larger).
 */
function transformScale<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  factor: number,
  options?: { origin?: string | Coord; mutate?: boolean }
): FeatureCollection<G> | Feature<G>;

Usage Examples:

import { transformTranslate, transformRotate, transformScale, polygon, point } from "@turf/turf";

const poly = polygon([[[0, 29], [3.5, 29], [2.5, 32], [0, 29]]]);

// Translate (move) polygon 100km northeast
const translated = transformTranslate(poly, 100, 45, { 
  units: 'kilometers' 
});

// Rotate polygon 90 degrees around its centroid
const rotated = transformRotate(poly, 90);

// Rotate around specific pivot point
const pivotPoint = point([0, 30]);
const rotatedAroundPivot = transformRotate(poly, 45, { 
  pivot: pivotPoint 
});

// Scale polygon to 150% of original size
const scaled = transformScale(poly, 1.5);

// Scale from specific origin point
const scaledFromOrigin = transformScale(poly, 2, { 
  origin: [0, 29] 
});

console.log('Translated polygon:', translated);
console.log('Rotated polygon:', rotated);
console.log('Scaled polygon:', scaled);

Coordinate System Projections

Convert between different coordinate reference systems.

/**
 * Converts a WGS84 GeoJSON object into Mercator projection.
 */
function toMercator<G>(geojson: G, options?: { mutate?: boolean }): G;

/**
 * Converts a Mercator GeoJSON object to WGS84 projection.
 */
function toWgs84<G>(geojson: G, options?: { mutate?: boolean }): G;

Usage Examples:

import { toMercator, toWgs84, point, polygon } from "@turf/turf";

// WGS84 coordinates (longitude, latitude)
const wgs84Point = point([-7.936523, 37.344592]);
const wgs84Polygon = polygon([[[2, 2], [8, 2], [8, 8], [2, 8], [2, 2]]]);

// Convert to Mercator projection
const mercatorPoint = toMercator(wgs84Point);
const mercatorPolygon = toMercator(wgs84Polygon);

// Convert back to WGS84
const backToWgs84Point = toWgs84(mercatorPoint);

console.log('WGS84 point:', wgs84Point.geometry.coordinates);
console.log('Mercator point:', mercatorPoint.geometry.coordinates);
console.log('Back to WGS84:', backToWgs84Point.geometry.coordinates);

// Mutate original (modify in place)
const originalPoly = polygon([[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]);
toMercator(originalPoly, { mutate: true });
console.log('Mutated polygon:', originalPoly.geometry.coordinates);

Coordinate Manipulation

Modify coordinate arrays and geometric properties.

/**
 * Takes a Feature or FeatureCollection and truncates the precision of the geometry.
 */
function truncate<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  options?: { precision?: number; coordinates?: number; mutate?: boolean }
): FeatureCollection<G> | Feature<G>;

/**
 * Flips the coordinates of any GeoJSON Feature or Geometry.
 */
function flip<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  options?: { mutate?: boolean }
): FeatureCollection<G> | Feature<G>;

/**
 * Rewinds the coordinates of any GeoJSON Polygon, MultiPolygon, or LineString 
 * to be counter-clockwise for exterior rings and clockwise for interior rings (holes).
 */
function rewind<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  options?: { reverse?: boolean; mutate?: boolean }
): FeatureCollection<G> | Feature<G];

/**
 * Removes redundant coordinates from any GeoJSON Geometry.
 */
function cleanCoords<G>(
  geojson: FeatureCollection<G> | Feature<G> | G,
  options?: { mutate?: boolean }
): FeatureCollection<G> | Feature<G>;

Usage Examples:

import { truncate, flip, rewind, cleanCoords, polygon, lineString } from "@turf/turf";

// High precision coordinates
const precisePoly = polygon([[[
  1.123456789, 2.987654321
], [
  3.555555555, 4.444444444
], [
  5.111111111, 6.222222222
], [
  1.123456789, 2.987654321
]]]);

// Truncate to 3 decimal places
const truncated = truncate(precisePoly, { precision: 3 });

// Flip coordinates (longitude, latitude) to (latitude, longitude)
const flipped = flip(precisePoly);

// Create polygon with wrong winding order
const wrongWinding = polygon([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]);
const rewound = rewind(wrongWinding, { reverse: false });

// Line with redundant coordinates
const redundantLine = lineString([
  [0, 0], [1, 1], [1, 1], [2, 2], [2, 2], [3, 3]
]);
const cleaned = cleanCoords(redundantLine);

console.log('Original precision:', precisePoly.geometry.coordinates[0][0]);
console.log('Truncated:', truncated.geometry.coordinates[0][0]);
console.log('Flipped coordinates:', flipped.geometry.coordinates[0][0]);
console.log('Original line length:', redundantLine.geometry.coordinates.length);
console.log('Cleaned line length:', cleaned.geometry.coordinates.length);

Point Positioning

Calculate positions relative to features and find specific points.

/**
 * Takes a Point and calculates the location of a destination point given a distance
 * in degrees, radians, miles, or kilometers; and bearing in degrees.
 */
function destination(
  origin: Coord,
  distance: number,
  bearing: number,
  options?: { units?: Units; properties?: GeoJsonProperties }
): Feature<Point>;

/**
 * Takes any Feature or a FeatureCollection and returns a Point guaranteed to be on the surface of the feature.
 */
function pointOnFeature<P>(
  geojson: FeatureCollection<any, P> | Feature<any, P>
): Feature<Point, P>;

/**
 * Takes a Feature or FeatureCollection and calculates the centroid using the mean of all vertices.
 */
function centroid<P>(
  geojson: AllGeoJSON,
  options?: { properties?: P }
): Feature<Point, P>;

/**
 * Takes one or more features and calculates the absolute center point of all features.
 */
function center<P>(
  geojson: AllGeoJSON,
  options?: { properties?: P }
): Feature<Point, P>;

Usage Examples:

import { destination, pointOnFeature, centroid, center, point, polygon } from "@turf/turf";

const origin = point([-75.343, 39.984]);
const complexPoly = polygon([[[
  [0, 0], [10, 0], [10, 10], [5, 15], [0, 10], [0, 0]
]]]);

// Calculate destination point
const dest = destination(origin, 50, 90, { 
  units: 'kilometers',
  properties: { type: 'destination' }
});

// Find point guaranteed to be on feature surface
const surfacePoint = pointOnFeature(complexPoly);

// Calculate geometric centroid (mean of vertices)
const geometricCenter = centroid(complexPoly);

// Calculate absolute center (center of bounding box)
const absoluteCenter = center(complexPoly);

console.log('Destination point:', dest.geometry.coordinates);
console.log('Point on feature:', surfacePoint.geometry.coordinates);
console.log('Centroid:', geometricCenter.geometry.coordinates);
console.log('Absolute center:', absoluteCenter.geometry.coordinates);

Coordinate Validation and Utilities

Validate and manipulate coordinate precision and format.

/**
 * Takes a Point and a Polygon or MultiPolygon and determines if the point
 * resides inside the polygon.
 */
function pointsWithinPolygon<G, P>(
  points: FeatureCollection<Point, P> | Feature<Point, P>,
  polygon: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon
): FeatureCollection<Point, P>;

/**
 * Calculates the distance from a point to a line segment.
 */
function pointToLineDistance(
  pt: Coord,
  line: Feature<LineString> | LineString,
  options?: { units?: Units; method?: 'geodesic' | 'planar' }
): number;

/**
 * Calculates the shortest distance from a Point to a Polygon outlines.
 */
function pointToPolygonDistance(
  point: Coord,
  polygon: Feature<Polygon | MultiPolygon> | Polygon | MultiPolygon,
  options?: { units?: Units }
): number;

Usage Examples:

import { pointsWithinPolygon, pointToLineDistance, pointToPolygonDistance, point, polygon, lineString, randomPoint } from "@turf/turf";

// Generate random points
const randomPoints = randomPoint(100, { bbox: [-5, -5, 5, 5] });

// Define test polygon
const testPoly = polygon([[[0, 0], [0, 3], [3, 3], [3, 0], [0, 0]]]);

// Find points within polygon
const pointsInside = pointsWithinPolygon(randomPoints, testPoly);

// Calculate distances
const testPoint = point([1, 1]);
const testLine = lineString([[0, 0], [2, 2], [4, 0]]);

const distanceToLine = pointToLineDistance(testPoint, testLine, { 
  units: 'kilometers' 
});

const distanceToPolygon = pointToPolygonDistance(testPoint, testPoly, { 
  units: 'meters' 
});

console.log(`Found ${pointsInside.features.length} points inside polygon`);
console.log(`Distance to line: ${distanceToLine} km`);
console.log(`Distance to polygon: ${distanceToPolygon} meters`);

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