@turf/invariant vs @turf/helpers vs @turf/meta vs @turf/turf
Geospatial Processing in JavaScript
@turf/invariant@turf/helpers@turf/meta@turf/turf

Geospatial Processing in JavaScript

The Turf.js ecosystem provides a suite of geospatial libraries for JavaScript, enabling developers to perform advanced geographic calculations, manipulate spatial data, and analyze geographic information. These libraries are designed to work with GeoJSON data, making them highly compatible with web mapping applications, geographic information systems (GIS), and any project that requires spatial analysis. The Turf.js libraries are modular, allowing developers to import only the functions they need, which helps keep applications lightweight and efficient. Whether you need to calculate distances, perform geometric operations, or analyze spatial relationships, Turf.js offers a comprehensive set of tools to handle a wide range of geospatial tasks.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
@turf/invariant6,038,92810,30045.7 kB293a month agoMIT
@turf/helpers010,300151 kB293a month agoMIT
@turf/meta010,300292 kB293a month agoMIT
@turf/turf010,300601 kB293a month agoMIT

Feature Comparison: @turf/invariant vs @turf/helpers vs @turf/meta vs @turf/turf

Purpose and Functionality

  • @turf/invariant:

    The @turf/invariant package focuses on ensuring the integrity and validity of GeoJSON data. It provides functions for validating GeoJSON geometries, checking their types, and ensuring they conform to the GeoJSON specification. This package is crucial for developers who need to validate spatial data, enforce data quality, and prevent errors when working with geographic information.

  • @turf/helpers:

    The @turf/helpers package provides a collection of utility functions for creating and manipulating GeoJSON data. It includes functions for generating various GeoJSON geometries (points, lines, polygons), as well as helper methods for working with features and feature collections. This package is essential for developers who need lightweight, reusable functions to handle GeoJSON data without the overhead of a full geospatial library.

  • @turf/meta:

    The @turf/meta package offers tools for working with collections of GeoJSON features. It provides functions for iterating over features, applying transformations, and extracting data from feature collections. This package is useful for developers who need to perform operations on multiple features at once, such as aggregating values, applying styles, or modifying geometries in bulk.

  • @turf/turf:

    The @turf/turf package is a comprehensive geospatial library that includes a wide array of functions for spatial analysis, geometry manipulation, and geographic calculations. It provides tools for measuring distances, calculating areas, performing geometric operations (such as intersection and union), and much more. This package is ideal for developers who need a full-featured solution for complex geospatial tasks, all within a single, well-documented library.

GeoJSON Validation

  • @turf/invariant:

    The @turf/invariant package is dedicated to GeoJSON validation. It includes functions that check whether a given geometry or feature is valid according to the GeoJSON specification. This package helps developers ensure data integrity by providing tools to validate and normalize GeoJSON data before processing it.

  • @turf/helpers:

    The @turf/helpers package does not provide validation for GeoJSON data. Its primary focus is on creating and manipulating GeoJSON geometries and features. Developers are responsible for ensuring that the data they work with is valid GeoJSON.

  • @turf/meta:

    The @turf/meta package does not focus on validation but rather on iterating and manipulating collections of GeoJSON features. It assumes that the input data is valid GeoJSON and provides tools for working with features and feature collections efficiently.

  • @turf/turf:

    The @turf/turf package includes some validation features as part of its comprehensive geospatial toolkit. While it is not primarily a validation library, it provides functions that check the validity of geometries and features as part of its broader functionality.

Feature Creation

  • @turf/invariant:

    The @turf/invariant package does not focus on feature creation. Its primary purpose is to validate and ensure the integrity of GeoJSON data. It does not provide functions for creating features but rather for checking and enforcing their validity.

  • @turf/helpers:

    The @turf/helpers package excels in feature creation, providing a variety of functions to generate GeoJSON geometries, features, and feature collections. It includes utilities for creating points, lines, polygons, and more, making it easy for developers to generate valid GeoJSON data programmatically.

  • @turf/meta:

    The @turf/meta package is not designed for feature creation. It focuses on iterating over and manipulating existing GeoJSON features and collections. It provides tools for working with features but does not include utilities for generating new ones.

  • @turf/turf:

    The @turf/turf package provides some feature creation capabilities as part of its extensive geospatial functionality. However, it is not its primary focus. The package is more comprehensive, offering a wide range of tools for analysis, manipulation, and geometric operations.

Iterating Over Features

  • @turf/invariant:

    The @turf/invariant package does not include iteration functions. Its focus is on validating GeoJSON data rather than processing or iterating over features.

  • @turf/helpers:

    The @turf/helpers package does not provide specific functions for iterating over features. It focuses on feature creation and manipulation but does not include utilities for traversing or processing collections of features.

  • @turf/meta:

    The @turf/meta package specializes in iterating over features. It provides functions for traversing GeoJSON feature collections, applying transformations, and performing operations on multiple features efficiently. This package is ideal for developers who need to work with collections of features and perform batch processing or aggregation.

  • @turf/turf:

    The @turf/turf package includes some iteration capabilities as part of its geospatial toolkit. However, it is not specialized for iteration. The package provides functions that can operate on features and collections, but it does not focus exclusively on iterating over them.

Ease of Use: Code Examples

  • @turf/invariant:

    Validating a GeoJSON Feature with @turf/invariant

    import { isValid } from '@turf/invariant';
    const myFeature = {
    type: 'Feature',
    geometry: {
    type: 'Point',
    coordinates: [102.0, 0.5]
    },
    properties: {}
    };
    console.log(isValid(myFeature)); // true
    
  • @turf/helpers:

    Creating a Point Feature with @turf/helpers

    import { point } from '@turf/helpers';
    const myPoint = point([102.0, 0.5]);
    console.log(myPoint);
    
  • @turf/meta:

    Iterating Over Features with @turf/meta

    import { featureEach } from '@turf/meta';
    const featureCollection = {
    type: 'FeatureCollection',
    features: [
    { type: 'Feature', geometry: { type: 'Point', coordinates: [102.0, 0.5] }, properties: {} },
    { type: 'Feature', geometry: { type: 'Point', coordinates: [103.0, 1.0] }, properties: {} }
    ]
    };
    featureEach(featureCollection, (feature) => {
    console.log(feature);
    });
    
  • @turf/turf:

    Calculating Distance with @turf/turf

    import { distance } from '@turf/turf';
    const point1 = [102.0, 0.5];
    const point2 = [103.0, 1.0];
    const dist = distance(point1, point2);
    console.log(dist);
    

How to Choose: @turf/invariant vs @turf/helpers vs @turf/meta vs @turf/turf

  • @turf/invariant:

    Select @turf/invariant if your project requires validation and type-checking for GeoJSON data. This package is essential for ensuring that the geographic data you work with conforms to the GeoJSON specification, making it a valuable tool for developers who need to validate, normalize, or enforce data integrity in their geospatial applications.

  • @turf/helpers:

    Choose @turf/helpers if you need utility functions for creating and manipulating GeoJSON data. This package is ideal for developers who require a set of helper functions to work with geographic features, such as points, lines, and polygons, without needing the full functionality of the entire Turf.js library.

  • @turf/meta:

    Use @turf/meta when you need to perform operations that involve iterating over multiple GeoJSON features or collections. This package provides functions for efficiently traversing and manipulating collections of geographic features, making it useful for tasks such as aggregating data, applying transformations, or extracting information from complex GeoJSON structures.

  • @turf/turf:

    Opt for @turf/turf if you need a comprehensive geospatial library that includes a wide range of functions for spatial analysis, geometry manipulation, and geographic calculations. This package is suitable for developers who require a full-featured solution with extensive capabilities, including distance calculations, area measurements, and advanced geometric operations, all within a single package.

README for @turf/invariant

@turf/invariant

getCoord

Unwrap a coordinate from a Point Feature, Geometry or a single coordinate.

Parameters

Examples

var pt = turf.point([10, 10]);

var coord = turf.getCoord(pt);
//= [10, 10]

Returns Array<number> coordinates

getCoords

Unwrap coordinates from a Feature, Geometry Object or an Array

Parameters

Examples

var poly = turf.polygon([[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]);

var coords = turf.getCoords(poly);
//= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]

Returns Array<any> coordinates

containsNumber

Checks if coordinates contains a number

Parameters

  • coordinates Array<any> GeoJSON Coordinates

Returns boolean true if Array contains a number

geojsonType

Enforce expectations about types of GeoJSON objects for Turf.

Parameters

  • value GeoJSON any GeoJSON object
  • type string expected GeoJSON type
  • name string name of calling function
  • Throws Error if value is not the expected type.

Returns void

featureOf

Enforce expectations about types of Feature inputs for Turf. Internally this uses geojsonType to judge geometry types.

Parameters

  • feature Feature a feature with an expected geometry type
  • type string expected GeoJSON type
  • name string name of calling function
  • Throws Error error if value is not the expected type.

Returns void

collectionOf

Enforce expectations about types of FeatureCollection inputs for Turf. Internally this uses geojsonType to judge geometry types.

Parameters

  • featureCollection FeatureCollection a FeatureCollection for which features will be judged
  • type string expected GeoJSON type
  • name string name of calling function
  • Throws Error if value is not the expected type.

getGeom

Get Geometry from Feature or Geometry Object

Parameters

Examples

var point = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [110, 40]
  }
}
var geom = turf.getGeom(point)
//={"type": "Point", "coordinates": [110, 40]}
  • Throws Error if geojson is not a Feature or Geometry Object

Returns (Geometry | null) GeoJSON Geometry Object

getType

Get GeoJSON object's type, Geometry type is prioritize.

Parameters

  • geojson GeoJSON GeoJSON object
  • _name string?
  • name string name of the variable to display in error message (unused) (optional, default "geojson")

Examples

var point = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [110, 40]
  }
}
var geom = turf.getType(point)
//="Point"

Returns string GeoJSON type


This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.

Installation

Install this single module individually:

$ npm install @turf/invariant

Or install the all-encompassing @turf/turf module that includes all modules as functions:

$ npm install @turf/turf