A JavaScript library for performing geospatial operations with GeoJSON data
—
Utilities for validating GeoJSON inputs and extracting coordinates and geometries safely with type checking. These functions ensure data integrity and provide safe access to GeoJSON components.
Safely extract coordinates from various GeoJSON input types.
/**
* Extracts coordinate from Point Feature, Geometry or array.
* Unwraps a coordinate from a Point Feature, Geometry or a single coordinate.
*/
function getCoord(coord: Feature<Point> | Point | number[]): number[];
/**
* Extracts coordinates from Feature, Geometry or array.
* Unwraps coordinates from a Feature, Geometry Object or an Array.
*/
function getCoords<G>(coords: any[] | Feature<G> | G): any[];Usage Examples:
import { getCoord, getCoords, point, polygon } from "@turf/turf";
// Extract coordinates from various formats
const pt = point([102, 0.5]);
const coord1 = getCoord(pt); // [102, 0.5]
const coord2 = getCoord([102, 0.5]); // [102, 0.5]
const coord3 = getCoord(pt.geometry); // [102, 0.5]
// Extract coordinates from polygon
const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);
const coords = getCoords(poly); // [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]
console.log('Point coordinates:', coord1);
console.log('Polygon coordinates:', coords);Extract geometries from Features safely.
/**
* Gets Geometry from Feature or Geometry Object.
* Get Geometry Object from Feature.
*/
function getGeom<G>(geojson: Feature<G> | G): G;
/**
* Gets GeoJSON object's type.
* Get GeoJSON object's type, prioritizing Geometry type over Feature type.
*/
function getType(geojson: Feature<any> | FeatureCollection<any> | Geometry, name?: string): string;Usage Examples:
import { getGeom, getType, point, polygon, featureCollection } from "@turf/turf";
const pt = point([102, 0.5]);
const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);
const collection = featureCollection([pt, poly]);
// Extract geometries
const pointGeom = getGeom(pt); // Point geometry
const polyGeom = getGeom(poly); // Polygon geometry
// Get types
const pointType = getType(pt); // "Point"
const polyType = getType(poly); // "Polygon"
const collectionType = getType(collection); // "FeatureCollection"
console.log('Point geometry:', pointGeom);
console.log('Types:', pointType, polyType, collectionType);Validate GeoJSON data and enforce type expectations.
/**
* Checks if coordinates contains numbers.
* Checks if coordinates contains a number.
*/
function containsNumber(coordinates: any[]): boolean;
/**
* Enforces GeoJSON type expectations.
* Enforce expectations about types of GeoJSON objects for Turf.
*/
function geojsonType(value: any, type: string, name: string): void;
/**
* Enforces Feature geometry type expectations.
* Enforce expectations about types of Feature inputs for Turf.
* Internally this uses geojsonType to judge geometry types
*/
function featureOf(feature: Feature<any>, type: string, name: string): void;
/**
* Enforces FeatureCollection geometry type expectations.
* Enforce expectations about types of FeatureCollection inputs for Turf.
* Internally this uses geojsonType to judge geometry types.
*/
function collectionOf(featureCollection: FeatureCollection<any>, type: string, name: string): void;Usage Examples:
import {
containsNumber,
geojsonType,
featureOf,
collectionOf,
point,
polygon,
featureCollection
} from "@turf/turf";
// Validate coordinate arrays
const validCoords = [102, 0.5];
const invalidCoords = ["102", "0.5"];
console.log('Valid coords contain numbers:', containsNumber(validCoords)); // true
console.log('Invalid coords contain numbers:', containsNumber(invalidCoords)); // false
// Type enforcement (these will throw errors if types don't match)
const pt = point([102, 0.5]);
const poly = polygon([[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]]);
const collection = featureCollection([pt]);
try {
// These will pass
geojsonType(pt, 'Feature', 'myFunction');
featureOf(pt, 'Point', 'myFunction');
collectionOf(collection, 'Point', 'myFunction');
// This would throw an error
// featureOf(pt, 'Polygon', 'myFunction'); // Error: Invalid input to myFunction: must be a Polygon, given Point
} catch (error) {
console.error(error.message);
}The invariant functions will throw descriptive errors when validation fails:
geojsonType() throws if the GeoJSON object is not of the expected typefeatureOf() throws if the Feature doesn't contain the expected geometry typecollectionOf() throws if the FeatureCollection doesn't contain features of the expected geometry typegetCoord() throws if the input cannot be converted to a coordinate arraygetCoords() throws if the input cannot be converted to coordinate arraysError Example:
import { featureOf, point, polygon } from "@turf/turf";
const pt = point([102, 0.5]);
try {
featureOf(pt, 'Polygon', 'myFunction');
} catch (error) {
console.error(error.message);
// "Invalid input to myFunction: must be a Polygon, given Point"
}These validation functions are designed to be used internally by other Turf functions to ensure type safety and provide clear error messages when incorrect data types are passed.
Install with Tessl CLI
npx tessl i tessl/npm-turf--turf