turf meta module
npm install @spatial/metaCallback for coordEach
Type: [Function][1]
- currentCoord [Array][2]<[number][3]> The current coordinate being processed.
- coordIndex [number][3] The current index of the coordinate being processed.
- featureIndex [number][3] The current index of the Feature being processed.
- multiFeatureIndex [number][3] The current index of the Multi-Feature being processed.
- geometryIndex [number][3] The current index of the Geometry being processed.
Iterate over coordinates in any GeoJSON object, similar to Array.forEach()
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON object
- callback [Function][1] a method that takes (currentCoord, coordIndex, featureIndex, multiFeatureIndex)
- excludeWrapCoord [boolean][7] whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. (optional, default false)
``javascript
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
});
`
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][1]
- previousValue any The accumulated value previously returned in the last invocationcurrentCoord
of the callback, or initialValue, if supplied.
- [Array][2]<[number][3]> The current coordinate being processed.coordIndex
- [number][3] The current index of the coordinate being processed.featureIndex
Starts at index 0, if an initialValue is provided, and at index 1 otherwise.
- [number][3] The current index of the Feature being processed.multiFeatureIndex
- [number][3] The current index of the Multi-Feature being processed.geometryIndex
- [number][3] The current index of the Geometry being processed.
Reduce coordinates in any GeoJSON object, similar to Array.reduce()
- geojson ([FeatureCollection][4] \| [Geometry][6] \| [Feature][5]) any GeoJSON objectcallback
- [Function][1] a method that takes (previousValue, currentCoord, coordIndex)initialValue
- any? Value to use as the first argument to the first call of the callback.excludeWrapCoord
- [boolean][7] whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. (optional, default false)
`javascript
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 any The value that results from the reduction.
Callback for propEach
Type: [Function][1]
- currentProperties [Object][8] The current Properties being processed.featureIndex
- [number][3] The current index of the Feature being processed.
Iterate over properties in any GeoJSON object, similar to Array.forEach()
- geojson ([FeatureCollection][4] \| [Feature][5]) any GeoJSON objectcallback
- [Function][1] a method that takes (currentProperties, featureIndex)
`javascript
var features = turf.featureCollection([
turf.point([26, 37], {foo: 'bar'}),
turf.point([36, 53], {hello: 'world'})
]);
turf.propEach(features, function (currentProperties, featureIndex) {
//=currentProperties
//=featureIndex
});
`
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][1]
- previousValue any The accumulated value previously returned in the last invocationcurrentProperties
of the callback, or initialValue, if supplied.
- any The current Properties being processed.featureIndex
- [number][3] The current index of the Feature being processed.
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.
- geojson ([FeatureCollection][4] \| [Feature][5]) any GeoJSON objectcallback
- [Function][1] a method that takes (previousValue, currentProperties, featureIndex)initialValue
- any? Value to use as the first argument to the first call of the callback.
`javascript
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 any The value that results from the reduction.
Callback for featureEach
Type: [Function][1]
- currentFeature [Feature][5]<any> The current Feature being processed.featureIndex
- [number][3] The current index of the Feature being processed.
Iterate over features in any GeoJSON object, similar to
Array.forEach.
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON objectcallback
- [Function][1] a method that takes (currentFeature, featureIndex)
`javascript
var features = turf.featureCollection([
turf.point([26, 37], {foo: 'bar'}),
turf.point([36, 53], {hello: 'world'})
]);
turf.featureEach(features, function (currentFeature, featureIndex) {
//=currentFeature
//=featureIndex
});
`
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][1]
- previousValue any The accumulated value previously returned in the last invocationcurrentFeature
of the callback, or initialValue, if supplied.
- [Feature][5] The current Feature being processed.featureIndex
- [number][3] The current index of the Feature being processed.
Reduce features in any GeoJSON object, similar to Array.reduce().
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON objectcallback
- [Function][1] a method that takes (previousValue, currentFeature, featureIndex)initialValue
- any? Value to use as the first argument to the first call of the callback.
`javascript
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 any The value that results from the reduction.
Get all coordinates from any GeoJSON object.
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON object
`javascript
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][2]<[Array][2]<[number][3]>> coordinate position array
Callback for geomEach
Type: [Function][1]
- currentGeometry [Geometry][6] The current Geometry being processed.featureIndex
- [number][3] The current index of the Feature being processed.featureProperties
- [Object][8] The current Feature Properties being processed.featureBBox
- [Array][2]<[number][3]> The current Feature BBox being processed.featureId
- ([number][3] \| [string][9]) The current Feature Id being processed.
Iterate over each geometry in any GeoJSON object, similar to Array.forEach()
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON objectcallback
- [Function][1] a method that takes (currentGeometry, featureIndex, featureProperties, featureBBox, featureId)
`javascript
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
});
`
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][1]
- previousValue any The accumulated value previously returned in the last invocationcurrentGeometry
of the callback, or initialValue, if supplied.
- [Geometry][6] The current Geometry being processed.featureIndex
- [number][3] The current index of the Feature being processed.featureProperties
- [Object][8] The current Feature Properties being processed.featureBBox
- [Array][2]<[number][3]> The current Feature BBox being processed.featureId
- ([number][3] \| [string][9]) The current Feature Id being processed.
Reduce geometry in any GeoJSON object, similar to Array.reduce().
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON objectcallback
- [Function][1] a method that takes (previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId)initialValue
- any? Value to use as the first argument to the first call of the callback.
`javascript
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 any The value that results from the reduction.
Callback for flattenEach
Type: [Function][1]
- currentFeature [Feature][5] The current flattened feature being processed.featureIndex
- [number][3] The current index of the Feature being processed.multiFeatureIndex
- [number][3] The current index of the Multi-Feature being processed.
Iterate over flattened features in any GeoJSON object, similar to
Array.forEach.
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON objectcallback
- [Function][1] a method that takes (currentFeature, featureIndex, multiFeatureIndex)
`javascript
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
});
`
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][1]
- previousValue any The accumulated value previously returned in the last invocationcurrentFeature
of the callback, or initialValue, if supplied.
- [Feature][5] The current Feature being processed.featureIndex
- [number][3] The current index of the Feature being processed.multiFeatureIndex
- [number][3] The current index of the Multi-Feature being processed.
Reduce flattened features in any GeoJSON object, similar to Array.reduce().
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSON objectcallback
- [Function][1] a method that takes (previousValue, currentFeature, featureIndex, multiFeatureIndex)initialValue
- any? Value to use as the first argument to the first call of the callback.
`javascript
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 any The value that results from the reduction.
Callback for segmentEach
Type: [Function][1]
- currentSegment [Feature][5]<[LineString][10]> The current Segment being processed.featureIndex
- [number][3] The current index of the Feature being processed.multiFeatureIndex
- [number][3] The current index of the Multi-Feature being processed.geometryIndex
- [number][3] The current index of the Geometry being processed.segmentIndex
- [number][3] The current index of the Segment being processed.
Returns void
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.
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSONcallback
- [Function][1] a method that takes (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex)
`javascript
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
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][1]
- previousValue any The accumulated value previously returned in the last invocationcurrentSegment
of the callback, or initialValue, if supplied.
- [Feature][5]<[LineString][10]> The current Segment being processed.featureIndex
- [number][3] The current index of the Feature being processed.multiFeatureIndex
- [number][3] The current index of the Multi-Feature being processed.geometryIndex
- [number][3] The current index of the Geometry being processed.segmentIndex
- [number][3] The current index of the Segment being processed.
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.
- geojson ([FeatureCollection][4] \| [Feature][5] \| [Geometry][6]) any GeoJSONcallback
- [Function][1] a method that takes (previousValue, currentSegment, currentIndex)initialValue
- any? Value to use as the first argument to the first call of the callback.
`javascript
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
//= segmentInex
return currentSegment
});
// Calculate the total number of segments
var initialValue = 0
var total = turf.segmentReduce(polygon, function (previousValue) {
previousValue++;
return previousValue;
}, initialValue);
`
Returns void
Callback for lineEach
Type: [Function][1]
- currentLine [Feature][5]<[LineString][10]> The current LineString|LinearRing being processedfeatureIndex
- [number][3] The current index of the Feature being processedmultiFeatureIndex
- [number][3] The current index of the Multi-Feature being processedgeometryIndex
- [number][3] The current index of the Geometry being processed
Iterate over line or ring coordinates in LineString, Polygon, MultiLineString, MultiPolygon Features or Geometries,
similar to Array.forEach.
- geojson ([Geometry][6] \| [Feature][5]<([LineString][10] \| [Polygon][11] \| [MultiLineString][12] \| [MultiPolygon][13])>) objectcallback
- [Function][1] a method that takes (currentLine, featureIndex, multiFeatureIndex, geometryIndex)
`javascript
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
});
`
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][1]
- previousValue any The accumulated value previously returned in the last invocationcurrentLine
of the callback, or initialValue, if supplied.
- [Feature][5]<[LineString][10]> The current LineString|LinearRing being processed.featureIndex
- [number][3] The current index of the Feature being processedmultiFeatureIndex
- [number][3] The current index of the Multi-Feature being processedgeometryIndex
- [number][3] The current index of the Geometry being processed
Reduce features in any GeoJSON object, similar to Array.reduce().
- geojson ([Geometry][6] \| [Feature][5]<([LineString][10] \| [Polygon][11] \| [MultiLineString][12] \| [MultiPolygon][13])>) objectcallback
- [Function][1] a method that takes (previousValue, currentLine, featureIndex, multiFeatureIndex, geometryIndex)initialValue
- any? Value to use as the first argument to the first call of the callback.
`javascript
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 any The value that results from the reduction.
[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[4]: https://tools.ietf.org/html/rfc7946#section-3.3
[5]: https://tools.ietf.org/html/rfc7946#section-3.2
[6]: https://tools.ietf.org/html/rfc7946#section-3.1
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[10]: https://tools.ietf.org/html/rfc7946#section-3.1.4
[11]: https://tools.ietf.org/html/rfc7946#section-3.1.6
[12]: https://tools.ietf.org/html/rfc7946#section-3.1.5
[13]: https://tools.ietf.org/html/rfc7946#section-3.1.7
---
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.
Install this module individually:
`sh`
$ npm install @spatial/meta
Or install the Turf module that includes it as a function:
`sh``
$ npm install @turf/turf