A JavaScript library for performing geospatial operations with GeoJSON data
—
Core GeoJSON creation functions, coordinate utilities, and foundational type definitions that support all other Turf operations. This module provides the building blocks for creating and manipulating GeoJSON data structures.
Functions for creating GeoJSON Features and FeatureCollections from coordinate data.
/**
* Wraps a GeoJSON Geometry in a GeoJSON Feature.
*/
function feature<G, P>(
geom: G | null,
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<G, P>;
/**
* Creates a GeoJSON Geometry from a Geometry string type & coordinates.
*/
function geometry(
type: "Point" | "LineString" | "Polygon" | "MultiPoint" | "MultiLineString" | "MultiPolygon",
coordinates: any[],
options?: Record<string, never>
): Geometry;
/**
* Creates a Point Feature from a Position.
*/
function point<P>(
coordinates: Position,
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<Point, P>;
/**
* Creates a Point FeatureCollection from an Array of Point coordinates.
*/
function points<P>(
coordinates: Position[],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): FeatureCollection<Point, P>;
/**
* Creates a Polygon Feature from an Array of LinearRings.
*/
function polygon<P>(
coordinates: Position[][],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<Polygon, P>;
/**
* Creates a Polygon FeatureCollection from an Array of Polygon coordinates.
*/
function polygons<P>(
coordinates: Position[][][],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): FeatureCollection<Polygon, P>;
/**
* Creates a LineString Feature from an Array of Positions.
*/
function lineString<P>(
coordinates: Position[],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<LineString, P>;
/**
* Creates a LineString FeatureCollection from an Array of LineString coordinates.
*/
function lineStrings<P>(
coordinates: Position[][],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): FeatureCollection<LineString, P>;
/**
* Creates a MultiLineString Feature based on a coordinate array.
*/
function multiLineString<P>(
coordinates: Position[][],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<MultiLineString, P>;
/**
* Creates a MultiPoint Feature based on a coordinate array.
*/
function multiPoint<P>(
coordinates: Position[],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<MultiPoint, P>;
/**
* Creates a MultiPolygon Feature based on a coordinate array.
*/
function multiPolygon<P>(
coordinates: Position[][][],
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<MultiPolygon, P>;
/**
* Creates a Feature<GeometryCollection> based on a coordinate array.
*/
function geometryCollection<P>(
geometries: Array<Point | LineString | Polygon | MultiPoint | MultiLineString | MultiPolygon>,
properties?: P,
options?: { bbox?: BBox; id?: Id }
): Feature<GeometryCollection, P>;
/**
* Takes one or more Features and creates a FeatureCollection.
*/
function featureCollection<G, P>(
features: Array<Feature<G, P>>,
options?: { bbox?: BBox; id?: Id }
): FeatureCollection<G, P>;Usage Examples:
import { point, polygon, lineString, featureCollection } from "@turf/turf";
// Create basic geometries
const pt = point([102, 0.5], { name: "Sample Point" });
const line = lineString([[102, -10], [103, 1], [104, 0], [130, 4]]);
const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);
// Create collections
const collection = featureCollection([pt, line, poly]);
// Add metadata
const pointWithBbox = point([102, 0.5], { name: "Tagged Point" }, {
bbox: [101, 0, 103, 1],
id: "point-001"
});Functions for converting between different measurement units and coordinate systems.
/**
* Convert a distance measurement from radians to a more friendly unit.
*/
function radiansToLength(radians: number, units?: Units): number;
/**
* Convert a distance measurement from a real-world unit into radians.
*/
function lengthToRadians(distance: number, units?: Units): number;
/**
* Convert a distance measurement from a real-world unit into degrees.
*/
function lengthToDegrees(distance: number, units?: Units): number;
/**
* Converts a length from one unit to another.
*/
function convertLength(
length: number,
originalUnit?: Units,
finalUnit?: Units
): number;
/**
* Converts an area from one unit to another.
*/
function convertArea(
area: number,
originalUnit?: AreaUnits,
finalUnit?: AreaUnits
): number;Usage Examples:
import { convertLength, convertArea, radiansToLength } from "@turf/turf";
// Convert between length units
const kilometers = convertLength(1, "miles", "kilometers"); // ~1.609
const meters = convertLength(100, "feet", "meters"); // ~30.48
// Convert between area units
const squareKm = convertArea(1, "acres", "kilometers"); // ~0.004
const hectares = convertArea(100, "acres", "hectares"); // ~40.47
// Convert from radians
const miles = radiansToLength(0.1, "miles"); // Distance in milesFunctions for working with bearings, angles, and coordinate transformations.
/**
* Converts any bearing angle from the north line direction (positive clockwise)
* and returns an angle between 0-360 degrees (positive clockwise).
*/
function bearingToAzimuth(bearing: number): number;
/**
* Converts any azimuth angle from the north line direction (positive clockwise)
* and returns an angle between -180 and +180 degrees (positive clockwise).
*/
function azimuthToBearing(angle: number): number;
/**
* Converts an angle in radians to degrees.
*/
function radiansToDegrees(radians: number): number;
/**
* Converts an angle in degrees to radians.
*/
function degreesToRadians(degrees: number): number;
/**
* Round number to precision.
*/
function round(num: number, precision?: number): number;Usage Examples:
import { bearingToAzimuth, azimuthToBearing, degreesToRadians, round } from "@turf/turf";
// Convert between bearing systems
const azimuth = bearingToAzimuth(-45); // 315 degrees
const bearing = azimuthToBearing(315); // -45 degrees
// Convert angle units
const radians = degreesToRadians(90); // π/2
const rounded = round(3.14159265, 2); // 3.14Functions for validating and testing data types.
/**
* Validates if input is a number.
*/
function isNumber(num: any): boolean;
/**
* Validates if input is an object (not array/function).
*/
function isObject(input: any): boolean;
/**
* Validates BBox format.
* @throws {Error} if BBox is not valid
*/
function validateBBox(bbox: any): void;
/**
* Validates GeoJSON ID format.
* @throws {Error} if Id is not valid
*/
function validateId(id: any): void;/**
* The Earth radius in meters. Used by Turf modules that model the Earth as a sphere.
* The mean radius was selected because it is recommended by the Haversine formula.
*/
const earthRadius: 6371008.8;
/**
* Unit of measurement factors based on earthRadius.
* Keys are the name of the unit, values are the number of that unit in a single radian.
*/
const factors: Record<Units, number>;
/**
* Area of measurement factors based on 1 square meter.
*/
const areaFactors: Record<AreaUnits, number>;// Core coordinate and unit types
type Coord = Feature<Point> | Point | Position;
type Units = "meters" | "metres" | "millimeters" | "millimetres" | "centimeters" | "centimetres" | "kilometers" | "kilometres" | "miles" | "nauticalmiles" | "inches" | "yards" | "feet" | "radians" | "degrees";
type AreaUnits = Exclude<Units, "radians" | "degrees"> | "acres" | "hectares";
type Grid = "point" | "square" | "hex" | "triangle";
type Corners = "sw" | "se" | "nw" | "ne" | "center" | "centroid";
type Lines = LineString | MultiLineString | Polygon | MultiPolygon;
type AllGeoJSON = Feature | FeatureCollection | Geometry | GeometryCollection;
type Id = string | number;Install with Tessl CLI
npx tessl i tessl/npm-turf--turf