Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@turf/meta

Turfjs16.4mMIT7.3.2TypeScript support: included

Provides tools for iterating over and manipulating GeoJSON objects.

functional, programming, turfjs, geojson, meta, flattenEach, flattenReduce, segmentEach, segmentReduce, coordEach, coordReduce, propEach, propReduce, featureEach, featureReduce, coordAll, geomEach, geomReduce, lineEeach, lineReduce

readme

@turf/meta

coordEachCallback

Callback for coordEach

Type: Function

Parameters

  • currentCoord Array<number> The current coordinate being processed.
  • coordIndex number The current index of the coordinate being processed.
  • featureIndex number The current index of the Feature being processed.
  • multiFeatureIndex number The current index of the Multi-Feature being processed.
  • geometryIndex number The current index of the Geometry being processed.

Returns void

coordEach

Iterate over coordinates in any GeoJSON object, similar to Array.forEach()

Parameters

  • geojson AllGeoJSON any GeoJSON object
  • callback coordEachCallback a method that takes (currentCoord, coordIndex, featureIndex, multiFeatureIndex)
  • excludeWrapCoord boolean whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. (optional, default false)

Examples

var features = turf.featureCollection([
  turf.point([26, 37], {"foo": "bar"}),
  turf.point([36, 53], {"hello": "world"})
]);

turf.coordEach(features, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
  //=currentCoord
  //=coordIndex
  //=featureIndex
  //=multiFeatureIndex
  //=geometryIndex
});

Returns void

coordReduceCallback

Callback for coordReduce

The first time the callback function is called, the values provided as arguments depend on whether the reduce method has an initialValue argument.

If an initialValue is provided to the reduce method:

  • The previousValue argument is initialValue.
  • The currentValue argument is the value of the first element present in the array.

If an initialValue is not provided:

  • The previousValue argument is the value of the first element present in the array.
  • The currentValue argument is the value of the second element present in the array.

Type: Function

Parameters

  • previousValue Reducer The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
  • currentCoord Array<number> The current coordinate being processed.
  • coordIndex number The current index of the coordinate being processed. Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
  • featureIndex number The current index of the Feature being processed.
  • multiFeatureIndex number The current index of the Multi-Feature being processed.
  • geometryIndex number The current index of the Geometry being processed.

Returns Reducer

coordReduce

Reduce coordinates in any GeoJSON object, similar to Array.reduce()

Parameters

  • geojson AllGeoJSON any GeoJSON object
  • callback coordReduceCallback a method that takes (previousValue, currentCoord, coordIndex)
  • initialValue Reducer? Value to use as the first argument to the first call of the callback.
  • excludeWrapCoord boolean whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. (optional, default false)

Examples

var features = turf.featureCollection([
  turf.point([26, 37], {"foo": "bar"}),
  turf.point([36, 53], {"hello": "world"})
]);

turf.coordReduce(features, function (previousValue, currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
  //=previousValue
  //=currentCoord
  //=coordIndex
  //=featureIndex
  //=multiFeatureIndex
  //=geometryIndex
  return currentCoord;
});

Returns Reducer The value that results from the reduction.

propEachCallback

Callback for propEach

Type: Function

Parameters

  • currentProperties GeoJsonProperties The current Properties being processed.
  • featureIndex number The current index of the Feature being processed.

Returns void

propEach

Iterate over properties in any GeoJSON object, similar to Array.forEach()

Parameters

Examples

var features = turf.featureCollection([
    turf.point([26, 37], {foo: 'bar'}),
    turf.point([36, 53], {hello: 'world'})
]);

turf.propEach(features, function (currentProperties, featureIndex) {
  //=currentProperties
  //=featureIndex
});

Returns void

propReduceCallback

Callback for propReduce

The first time the callback function is called, the values provided as arguments depend on whether the reduce method has an initialValue argument.

If an initialValue is provided to the reduce method:

  • The previousValue argument is initialValue.
  • The currentValue argument is the value of the first element present in the array.

If an initialValue is not provided:

  • The previousValue argument is the value of the first element present in the array.
  • The currentValue argument is the value of the second element present in the array.

Type: Function

Parameters

  • previousValue Reducer The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
  • currentProperties GeoJsonProperties The current Properties being processed.
  • featureIndex number The current index of the Feature being processed.

Returns Reducer

propReduce

Reduce properties in any GeoJSON object into a single value, similar to how Array.reduce works. However, in this case we lazily run the reduction, so an array of all properties is unnecessary.

Parameters

  • geojson (FeatureCollection | Feature | Geometry) any GeoJSON object
  • callback propReduceCallback a method that takes (previousValue, currentProperties, featureIndex)
  • initialValue Reducer? Value to use as the first argument to the first call of the callback.

Examples

var features = turf.featureCollection([
    turf.point([26, 37], {foo: 'bar'}),
    turf.point([36, 53], {hello: 'world'})
]);

turf.propReduce(features, function (previousValue, currentProperties, featureIndex) {
  //=previousValue
  //=currentProperties
  //=featureIndex
  return currentProperties
});

Returns Reducer The value that results from the reduction.

featureEachCallback

Callback for featureEach

Type: Function

Parameters

  • currentFeature Feature<any> The current Feature being processed.
  • featureIndex number The current index of the Feature being processed.

Returns void

featureEach

Iterate over features in any GeoJSON object, similar to Array.forEach.

Parameters

Examples

var features = turf.featureCollection([
  turf.point([26, 37], {foo: 'bar'}),
  turf.point([36, 53], {hello: 'world'})
]);

turf.featureEach(features, function (currentFeature, featureIndex) {
  //=currentFeature
  //=featureIndex
});

Returns void

featureReduceCallback

Callback for featureReduce

The first time the callback function is called, the values provided as arguments depend on whether the reduce method has an initialValue argument.

If an initialValue is provided to the reduce method:

  • The previousValue argument is initialValue.
  • The currentValue argument is the value of the first element present in the array.

If an initialValue is not provided:

  • The previousValue argument is the value of the first element present in the array.
  • The currentValue argument is the value of the second element present in the array.

Type: Function

Parameters

  • previousValue Reducer The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
  • currentFeature Feature The current Feature being processed.
  • featureIndex number The current index of the Feature being processed.

Returns Reducer

featureReduce

Reduce features in any GeoJSON object, similar to Array.reduce().

Parameters

Examples

var features = turf.featureCollection([
  turf.point([26, 37], {"foo": "bar"}),
  turf.point([36, 53], {"hello": "world"})
]);

turf.featureReduce(features, function (previousValue, currentFeature, featureIndex) {
  //=previousValue
  //=currentFeature
  //=featureIndex
  return currentFeature
});

Returns Reducer The value that results from the reduction.

coordAll

Get all coordinates from any GeoJSON object.

Parameters

  • geojson AllGeoJSON any GeoJSON object

Examples

var features = turf.featureCollection([
  turf.point([26, 37], {foo: 'bar'}),
  turf.point([36, 53], {hello: 'world'})
]);

var coords = turf.coordAll(features);
//= [[26, 37], [36, 53]]

Returns Array<Array<number>> coordinate position array

geomEachCallback

Callback for geomEach

Type: Function

Parameters

  • currentGeometry GeometryObject The current Geometry being processed.
  • featureIndex number The current index of the Feature being processed.
  • featureProperties GeoJsonProperties The current Feature Properties being processed.
  • featureBBox BBox The current Feature BBox being processed.
  • featureId Id The current Feature Id being processed.

Returns void

geomEach

Iterate over each geometry in any GeoJSON object, similar to Array.forEach()

Parameters

Examples

var features = turf.featureCollection([
    turf.point([26, 37], {foo: 'bar'}),
    turf.point([36, 53], {hello: 'world'})
]);

turf.geomEach(features, function (currentGeometry, featureIndex, featureProperties, featureBBox, featureId) {
  //=currentGeometry
  //=featureIndex
  //=featureProperties
  //=featureBBox
  //=featureId
});

Returns void

geomReduceCallback

Callback for geomReduce

The first time the callback function is called, the values provided as arguments depend on whether the reduce method has an initialValue argument.

If an initialValue is provided to the reduce method:

  • The previousValue argument is initialValue.
  • The currentValue argument is the value of the first element present in the array.

If an initialValue is not provided:

  • The previousValue argument is the value of the first element present in the array.
  • The currentValue argument is the value of the second element present in the array.

Type: Function

Parameters

  • previousValue Reducer The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
  • currentGeometry GeometryObject The current Geometry being processed.
  • featureIndex number The current index of the Feature being processed.
  • featureProperties GeoJsonProperties The current Feature Properties being processed.
  • featureBBox BBox The current Feature BBox being processed.
  • featureId Id The current Feature Id being processed.

Returns Reducer

geomReduce

Reduce geometry in any GeoJSON object, similar to Array.reduce().

Parameters

Examples

var features = turf.featureCollection([
    turf.point([26, 37], {foo: 'bar'}),
    turf.point([36, 53], {hello: 'world'})
]);

turf.geomReduce(features, function (previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId) {
  //=previousValue
  //=currentGeometry
  //=featureIndex
  //=featureProperties
  //=featureBBox
  //=featureId
  return currentGeometry
});

Returns Reducer The value that results from the reduction.

flattenEachCallback

Callback for flattenEach

Type: Function

Parameters

  • currentFeature Feature The current flattened feature being processed.
  • featureIndex number The current index of the Feature being processed.
  • multiFeatureIndex number The current index of the Multi-Feature being processed.

Returns void

flattenEach

Iterate over flattened features in any GeoJSON object, similar to Array.forEach.

Parameters

Examples

var features = turf.featureCollection([
    turf.point([26, 37], {foo: 'bar'}),
    turf.multiPoint([[40, 30], [36, 53]], {hello: 'world'})
]);

turf.flattenEach(features, function (currentFeature, featureIndex, multiFeatureIndex) {
  //=currentFeature
  //=featureIndex
  //=multiFeatureIndex
});

Returns void

flattenReduceCallback

Callback for flattenReduce

The first time the callback function is called, the values provided as arguments depend on whether the reduce method has an initialValue argument.

If an initialValue is provided to the reduce method:

  • The previousValue argument is initialValue.
  • The currentValue argument is the value of the first element present in the array.

If an initialValue is not provided:

  • The previousValue argument is the value of the first element present in the array.
  • The currentValue argument is the value of the second element present in the array.

Type: Function

Parameters

  • previousValue Reducer The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
  • currentFeature Feature The current Feature being processed.
  • featureIndex number The current index of the Feature being processed.
  • multiFeatureIndex number The current index of the Multi-Feature being processed.

Returns Reducer

flattenReduce

Reduce flattened features in any GeoJSON object, similar to Array.reduce().

Parameters

Examples

var features = turf.featureCollection([
    turf.point([26, 37], {foo: 'bar'}),
    turf.multiPoint([[40, 30], [36, 53]], {hello: 'world'})
]);

turf.flattenReduce(features, function (previousValue, currentFeature, featureIndex, multiFeatureIndex) {
  //=previousValue
  //=currentFeature
  //=featureIndex
  //=multiFeatureIndex
  return currentFeature
});

Returns Reducer The value that results from the reduction.

segmentEachCallback

Callback for segmentEach

Type: Function

Parameters

  • currentSegment Feature<LineString> The current Segment being processed.
  • featureIndex number The current index of the Feature being processed.
  • multiFeatureIndex number The current index of the Multi-Feature being processed.
  • geometryIndex number The current index of the Geometry being processed.
  • segmentIndex number The current index of the Segment being processed.

Returns void

segmentEach

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.

Parameters

  • geojson AllGeoJSON any GeoJSON
  • callback segmentEachCallback a method that takes (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex)

Examples

var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]);

// Iterate over GeoJSON by 2-vertex segments
turf.segmentEach(polygon, function (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) {
  //=currentSegment
  //=featureIndex
  //=multiFeatureIndex
  //=geometryIndex
  //=segmentIndex
});

// Calculate the total number of segments
var total = 0;
turf.segmentEach(polygon, function () {
    total++;
});

Returns void

segmentReduceCallback

Callback for segmentReduce

The first time the callback function is called, the values provided as arguments depend on whether the reduce method has an initialValue argument.

If an initialValue is provided to the reduce method:

  • The previousValue argument is initialValue.
  • The currentValue argument is the value of the first element present in the array.

If an initialValue is not provided:

  • The previousValue argument is the value of the first element present in the array.
  • The currentValue argument is the value of the second element present in the array.

Type: Function

Parameters

  • previousValue Reducer The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
  • currentSegment Feature<LineString> The current Segment being processed.
  • featureIndex number The current index of the Feature being processed.
  • multiFeatureIndex number The current index of the Multi-Feature being processed.
  • geometryIndex number The current index of the Geometry being processed.
  • segmentIndex number The current index of the Segment being processed.

Returns Reducer

segmentReduce

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.

Parameters

Examples

var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]);

// Iterate over GeoJSON by 2-vertex segments
turf.segmentReduce(polygon, function (previousSegment, currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) {
  //= previousSegment
  //= currentSegment
  //= featureIndex
  //= multiFeatureIndex
  //= geometryIndex
  //= segmentIndex
  return currentSegment
});

// Calculate the total number of segments
var initialValue = 0
var total = turf.segmentReduce(polygon, function (previousValue) {
    previousValue++;
    return previousValue;
}, initialValue);

Returns Reducer

lineEachCallback

Callback for lineEach

Type: Function

Parameters

  • currentLine Feature<LineString> The current LineString|LinearRing being processed
  • featureIndex number The current index of the Feature being processed
  • multiFeatureIndex number The current index of the Multi-Feature being processed
  • geometryIndex number The current index of the Geometry being processed

Returns void

lineEach

Iterate over line or ring coordinates in LineString, Polygon, MultiLineString, MultiPolygon Features or Geometries, similar to Array.forEach.

Parameters

Examples

var multiLine = turf.multiLineString([
  [[26, 37], [35, 45]],
  [[36, 53], [38, 50], [41, 55]]
]);

turf.lineEach(multiLine, function (currentLine, featureIndex, multiFeatureIndex, geometryIndex) {
  //=currentLine
  //=featureIndex
  //=multiFeatureIndex
  //=geometryIndex
});

Returns void

lineReduceCallback

Callback for lineReduce

The first time the callback function is called, the values provided as arguments depend on whether the reduce method has an initialValue argument.

If an initialValue is provided to the reduce method:

  • The previousValue argument is initialValue.
  • The currentValue argument is the value of the first element present in the array.

If an initialValue is not provided:

  • The previousValue argument is the value of the first element present in the array.
  • The currentValue argument is the value of the second element present in the array.

Type: Function

Parameters

  • previousValue Reducer The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied.
  • currentLine Feature<LineString> The current LineString|LinearRing being processed.
  • featureIndex number The current index of the Feature being processed
  • multiFeatureIndex number The current index of the Multi-Feature being processed
  • geometryIndex number The current index of the Geometry being processed

Returns Reducer

lineReduce

Reduce features in any GeoJSON object, similar to Array.reduce().

Parameters

Examples

var multiPoly = turf.multiPolygon([
  turf.polygon([[[12,48],[2,41],[24,38],[12,48]], [[9,44],[13,41],[13,45],[9,44]]]),
  turf.polygon([[[5, 5], [0, 0], [2, 2], [4, 4], [5, 5]]])
]);

turf.lineReduce(multiPoly, function (previousValue, currentLine, featureIndex, multiFeatureIndex, geometryIndex) {
  //=previousValue
  //=currentLine
  //=featureIndex
  //=multiFeatureIndex
  //=geometryIndex
  return currentLine
});

Returns Reducer The value that results from the reduction.

findSegment

Finds a particular 2-vertex LineString Segment from a GeoJSON using @turf/meta indexes.

Negative indexes are permitted. Point & MultiPoint will always return null.

Parameters

  • geojson (FeatureCollection | Feature | Geometry) Any GeoJSON Feature or Geometry
  • options Object Optional parameters (optional, default {})

    • options.featureIndex number Feature Index (optional, default 0)
    • options.multiFeatureIndex number Multi-Feature Index (optional, default 0)
    • options.geometryIndex number Geometry Index (optional, default 0)
    • options.segmentIndex number Segment Index (optional, default 0)
    • options.properties Object Translate Properties to output LineString (optional, default {})
    • options.bbox BBox Translate BBox to output LineString (optional, default {})
    • options.id (number | string) Translate Id to output LineString (optional, default {})

Examples

var multiLine = turf.multiLineString([
    [[10, 10], [50, 30], [30, 40]],
    [[-10, -10], [-50, -30], [-30, -40]]
]);

// First Segment (defaults are 0)
turf.findSegment(multiLine);
// => Feature<LineString<[[10, 10], [50, 30]]>>

// First Segment of 2nd Multi Feature
turf.findSegment(multiLine, {multiFeatureIndex: 1});
// => Feature<LineString<[[-10, -10], [-50, -30]]>>

// Last Segment of Last Multi Feature
turf.findSegment(multiLine, {multiFeatureIndex: -1, segmentIndex: -1});
// => Feature<LineString<[[-50, -30], [-30, -40]]>>

Returns Feature<LineString> 2-vertex GeoJSON Feature LineString

findPoint

Finds a particular Point from a GeoJSON using @turf/meta indexes.

Negative indexes are permitted.

Parameters

  • geojson (FeatureCollection | Feature | Geometry) Any GeoJSON Feature or Geometry
  • options Object Optional parameters (optional, default {})

    • options.featureIndex number Feature Index (optional, default 0)
    • options.multiFeatureIndex number Multi-Feature Index (optional, default 0)
    • options.geometryIndex number Geometry Index (optional, default 0)
    • options.coordIndex number Coord Index (optional, default 0)
    • options.properties Object Translate Properties to output Point (optional, default {})
    • options.bbox BBox Translate BBox to output Point (optional, default {})
    • options.id (number | string) Translate Id to output Point (optional, default {})

Examples

var multiLine = turf.multiLineString([
    [[10, 10], [50, 30], [30, 40]],
    [[-10, -10], [-50, -30], [-30, -40]]
]);

// First Segment (defaults are 0)
turf.findPoint(multiLine);
// => Feature<Point<[10, 10]>>

// First Segment of the 2nd Multi-Feature
turf.findPoint(multiLine, {multiFeatureIndex: 1});
// => Feature<Point<[-10, -10]>>

// Last Segment of last Multi-Feature
turf.findPoint(multiLine, {multiFeatureIndex: -1, coordIndex: -1});
// => Feature<Point<[-30, -40]>>

Returns Feature<Point> 2-vertex GeoJSON Feature Point


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/meta

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

$ npm install @turf/turf

changelog

Changelog is no longer maintained. See Turf Github releases

7.0.0

⚠️ Breaking

  • Move to @types/geojson package instead of declaring our own. Typescript consumers of Turf will need to import from @types/geojson. (#2158)
  • Move distribution JS to target ES2017 (#2237)
  • Please change to using named exports instead of default exports from the various packages
  • @turf/helpers Correct the conversion factor for degrees (#2177)
  • @turf/helpers polygon() will now throw if the first and last coordinates are not the same (#2173)
  • @turf/helpers Separate AreaUnits into its own type (#2393)
  • @turf/area Fix earth radius to use WGS84 median earth radius (#2166)
  • @turf/turf Remove re-exports for turf 4.x compatability (#2183)
  • @turf/rectangle-grid Fix correctness for large areas (#2106)
  • `@turf/square-grid Fix correctness for large areas (#2106)
  • @turf/union Accept FeatureCollection for multiple inputs (#2247)
  • @turf/difference Accept FeatureCollection for multiple inputs (#2247)
  • @turf/intersect Accept FeatureCollection for multiple inputs (#2247)
  • @turf/buffer Add undefined return for when the geometry is invalid (#2613)

🏅 New Features/Enhancements

🐛 Bug Fixes

📖 Documentation

🔔 Misc

  • `@turf/turf Add booleanIntersects typescript export (#2157)
  • `@turf/turf Add booleanTouches export (#2170)
  • `@turf/turf Add booleanConcave export (#2265)
  • @turf/simplify Clean up internals for less object churn (#2561)
  • `@turf/helpers Make isObject a little more accurate (#2176)
  • Migrate from geojsonhint to @placemark/check-geojson (#2571)
  • Add custom types entry point to exports, required by Typescript for node16, nodenext and bundler module resolution strategies (#2400, #2452)
  • types.ts tests are now run in strict mode (#2363)
  • Uses tslib now for smaller bundles (#2165)
  • Remove object-assign dependency from all packages (#2241)
  • Lots of dependencies have been upgraded

6.5.0

🏅 New Features/Enhancements

🐛 Bug Fixes

📖 Documentation

🔔 Misc

6.4.0

🏅 New Features/Enhancements

🐛 Bug Fixes

📖 Documentation

🔔 Misc

6.3.0

Fix issues importing Turf for react-native, webpack 5, and other bundlers

(PR https://github.com/Turfjs/turf/pull/2004 - Author r0b0t3d) (PR https://github.com/Turfjs/turf/pull/2011 - Author mfedderly)

[@turf/turf][turf] expose @turf/boolean-intersect

(PR https://github.com/Turfjs/turf/pull/2007 - Author rowanwins)

6.2.0

After a bit of hiatus, TurfJS is resuming releases.

⭐️ Major Updates

  • ES Modules available for all packages
  • Tree shaking should significantly reduce import size of @turf/turf
  • Better support for ESM modules (PR https://github.com/Turfjs/turf/pull/1942 - Author @diachedelic)
  • Clean-up of test and benchmark running to make publishing easier
  • Enforce styling using Prettier
  • Enable ESLint and get rid of unused variables
  • Upgrade rollup for more correct javascript module builds
  • Only include ES5 code

🚀 New Modules

[@turf/boolean-touches][boolean-touches]

Determines if two features touch but do not intersect

[@turf/boolean-valid][boolean-valid]

Checks if the geometry is a valid according to the OGC Simple Feature Specification

[@turf/quadrat-analysis][quadrat-analysis]

Performs a quadrat analysis on a set of points

[@turf/rectangle-grid][rectangle-grid]

Creates a grid of rectangles from a bounding box

@turf/voroni

Typescript types for the options parameter have been fixed (PR https://github.com/Turfjs/turf/pull/1424 - Author @stevage)

@turf/points-within-polygon

Typescript types around the Feature's Properties will now be preserved. (PR https://github.com/Turfjs/turf/pull/1761 - Author @rugheid)

@turf/rewind

Typescript types for the 'reverse' option are now correct. Previously it was misnamed as 'reversed'. (PR https://github.com/Turfjs/turf/pull/1786 - Author @jonnycornwell)

@turf/difference

No longer publishes an .mjs file.

@turf/meta

No longer publishes an .mjs file.

@turf/tag

Add MultiPolygon support. (PR https://github.com/Turfjs/turf/pull/1996 - Author bryceroney)

🐛 Bug Fixes

🏅 New Features/Enhancements

📖 Documentation

⚠️ Breaking Change

5.0.0 🎉

⭐️ Major Updates

  • TurfJS now supports ES Modules (Related PR's)
  • Optional parameters are now defined as an Object.

🚀 New Modules

@turf/voronoi

Takes a FeatureCollection of points, and a bounding box, and returns a FeatureCollection of Voronoi polygons. (PR https://github.com/Turfjs/turf/pull/1043 - Author @stevage)

@turf/shortest-path

Returns the shortest path from start to end without colliding with any feature in obstacles (PR https://github.com/Turfjs/turf/pull/956 - Author @stebogit)

@turf/boolean-parallel

Boolean-Parallel returns True if each segment of line1 is parallel to the correspondent segment of line2 (PR https://github.com/Turfjs/turf/pull/941 - Author @stebogit)

@turf/nearest-point-on-line

Takes a {@link Point} and a {@link LineString} and calculates the closest Point on the (Multi)LineString. (PR https://github.com/Turfjs/turf/pull/939 - Author @stebogit)

🏅 New Features/Enhancements

🐛 Bug Fixes

⚠️ Breaking Change

  • Optional parameters are now defined as an Object:

Before

var from = [-75.343, 39.984];
var to = [-75.534, 39.123];
var units = 'miles';
var distance = turf.distance(from, to, units);

After

var from = [-75.343, 39.984];
var to = [-75.534, 39.123];
var options = {units: 'miles'};
var distance = turf.distance(from, to, options);

4.7.0

🚀 New Modules

@turf/projection

  • toMercator: Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
  • toWgs84: Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection

(PR https://github.com/Turfjs/turf/pull/927 - Author @stebogit)

@turf/point-to-line-distance

Returns the minimum distance between a {@link Point} and a {@link LineString}, being the distance from a line the minimum distance between the point and any segment of the LineString.

(PR https://github.com/Turfjs/turf/pull/925 - Author @stebogit)

@turf/boolean-within

Boolean-within returns true if the first geometry is completely within the second geometry. The interiors of both geometries must intersect and, the interior and boundary of the primary (geometry a) must not intersect the exterior of the secondary (geometry b). Boolean-within returns the exact opposite result of the @turf/boolean-contains.

(PR https://github.com/Turfjs/turf/pull/924 - Author @rowanwins)

🏅 New Features/Enhancements

🐛 Bug Fixes

4.6.0

🚀 New Modules

🏅 New Features/Enhancements

🐛 Bug Fixes

4.5.0

🚀 New Modules

🏅 New Features/Enhancements

🐛 Bug Fixes

4.4.0

🚀 New Modules

🏅 New Features

🐛 Bug Fixes

4.3.0

🚀 New Modules

🏅 New Features

🐛 Bug Fixes

4.2.0

New Modules

Enhancements

Bug Fixes

Changes

Documentation

4.1.0

New Modules

Enhancements

Bug Fixes

3.6.4

Typescript definitions index.d.ts added to all the packages.

3.0.11

Fix turf-line-slice bug with vertical linestrings.

3.0.1

This is a big change in Turf! 3.0.0 is a release that targets the development cycle of Turf, letting us work on it more and release more often.

Monorepo

Turf 3.x and forward is a monorepo project. We publish lots of little modules as usual, but there's one repo - turfjs/turf - that contains all the code and the issues for the Turf source code. We use lerna to link these packages together and make sure they work.

Why? We already had internal turf modules, like turf-meta, and development was harder and harder - we had a bunch of custom scripts to do releases and tests, and these were just written for Turf. Lerna is from the very popular and very well-maintained babel project, and it works really well, and reduces maintainer sadness.

Simplicity

Turf grew a bunch of modules that weren't totally necessary, or were expressing only a line or two of JavaScript. We want to make things easier, but these modules didn't make code more expressive and they hid complexity where it didn't need to be hidden. Turf 3.x focuses on the core functionalities we need, making sure they're tested and performant.

turf-erase has been renamed turf-difference to make its name more similar to the equivalents in other libraries.

Removed modules: merge, sum, min, max, average, median, variance, deviation, filter, remove, jenks, quantile. See the upgrade guide below for replacements.

Upgrading from v2

If you were using turf-merge

turf-merge repeatedly called turf-union on an array of polygons. Here's how to implement the same thing without the special module

var clone = require('clone');
var union = require('turf-union');
function merge(polygons) {
  var merged = clone(polygons.features[0]), features = polygons.features;
  for (var i = 0, len = features.length; i < len; i++) {
    var poly = features[i];
    if (poly.geometry) merged = union(merged, poly);
  }
  return merged;
}

An alternative method that merges pairs of features recursively. With large numbers and similar complexity of input geometries this can speed up run time by factor 10. Choose depending on your use case.

var union = require('turf-union');
function mergeBin(polygons) {
  var features = polygons.features;

  do {
    var merged = [], len = features.length;
    for (var i = 0; i < len-1; i += 2) {
      merged.push(turf.union(features[i], features[i+1]));
    }
    if (len % 2 !== 0) {
      merged.push(features[len-1]);
    }
    features = merged;
  } while(features.length > 1);

  return features[0];
}

If you were using turf-sum, min, max, average, median, variance, deviation

The turf-collect method provides the core of these statistical methods and lets you bring your own statistical library, like simple-statistics, science.js, or others.

For example, here's how to find the median of matched values with simple-statistics. Finding other statistics, like variance, mean, and so on simply use other methods from the statistics library.

var ss = require('simple-statistics');
var turf = require('@turf/turf');

var poly1 = turf.polygon([[[0,0],[10,0],[10,10],[0,10],[0,0]]]);
var poly2 = turf.polygon([[[10,0],[20,10],[20,20],[20,0],[10,0]]]);
var polyFC = turf.featureCollection([poly1, poly2]);
var pt1 = turf.point([5,5], {population: 200});
var pt2 = turf.point([1,3], {population: 600});
var pt3 = turf.point([14,2], {population: 100});
var pt4 = turf.point([13,1], {population: 200});
var pt5 = turf.point([19,7], {population: 300});
var ptFC = turf.featureCollection([pt1, pt2, pt3, pt4, pt5]);

// collects values from matching points into an array called 'values'
var collected = turf.collect(polyFC, ptFC, 'population', 'values');

// finds the median of those values.
collected.features.forEach(function (feature) {
  feature.properties.median = ss.median(feature.properties.values);
});

console.log(JSON.stringify(collected, null, 2));

If you were using turf-filter, turf-remove

These modules were thin wrappers around native JavaScript methods: use Array.filter instead:

var filteredFeatures = features.filter(function(feature) {
  return feature.properties.value > 10;
});

If you were using turf-jenks, turf-quantile

Use Array.map to get values, and then bring your own statistical calculation, like simple-statistics or science.js.

var values = features.map(function(feature) {
  return feature.properties.value;
});

If you were using turf-extent

turf-extent's name was changed to turf-bbox. It is functionally the same.

turf.bbox(poly) // [minx, miny, maxx, maxy]

2.0.0

  • turf-grid renamed turf-point-grid (turf.grid => turf.pointGrid)
  • turf-hex renamed turf-hex-grid (turf.hex => turf.hexGrid)
  • turf-hex-grid now has a required unit parameter
  • remove turf-isobands; use turf-isolines instead
  • added turf-square-grid (turf.squareGrid)
  • added turf-triangle-grid (turf.triangleGrid)
  • constrain turf-point-grid to the bbox

1.4.0

  • update all module dependencies to master
  • add support for features in turf.intersection
  • fix issues with turf.polygon coordinate wrapping inconsistencies
  • add unit parameter to turf.concave

1.3.5

  • harmonize turf-tin dependency tree

1.3.4

  • fixes bug in turf-along

1.3.3

  • added turf-line-slice for segmenting LineStrings with Points
  • turf-point-on-line for calculating the closest Point from a Point to a LineString

1.3.2

  • tin ~7x faster
  • Fix mutability issues with flip, erase: data passed to Turf should never be changed in place.
  • added turf-line-distance for geodesic measuring of LineStrings
  • added turf-along for calculating a the location of a Point x distance along a LineString
  • added turf-area for calculating the area of a given feature