@turf/meta
Advanced tools
Comparing version 3.10.4 to 3.10.5
74
bench.js
@@ -1,38 +0,48 @@ | ||
var meta = require('./'); | ||
var meta2 = require('./'); | ||
var random = require('@turf/random'); | ||
const Benchmark = require('benchmark'); | ||
const random = require('@turf/random'); | ||
const meta = require('.'); | ||
var n = 100000; | ||
var pnts = random('points', n).features; | ||
var plys = random('polygons', n).features; | ||
var combined = []; | ||
const fixtures = { | ||
point: random('points'), | ||
points: random('points', 1000), | ||
polygon: random('polygon'), | ||
polygons: random('polygons', 1000) | ||
}; | ||
while (pnts.length && plys.length) { | ||
var pt = pnts.pop(); | ||
var pl = plys.pop(); | ||
combined.push(pt); | ||
combined.push({ type: 'GeometryCollection', geometries: [pt.geometry, pl.geometry] }); | ||
combined.push(pt.geometry); | ||
combined.push({ type: 'FeatureCollection', features: [pt] }); | ||
combined.push(pl); | ||
combined.push(pl.geometry); | ||
combined.push({ type: 'FeatureCollection', features: [pl] }); | ||
} | ||
const suite = new Benchmark.Suite('turf-meta'); | ||
console.time('coordEach#1'); | ||
var sum = 0; | ||
combined.forEach(function(c) { | ||
meta.coordEach(c, function(coord) { | ||
sum += coord[0]; | ||
Object.keys(fixtures).forEach(name => { | ||
const fixture = fixtures[name]; | ||
suite.add('coordEach - ' + name, () => { | ||
meta.coordEach(fixture, (currentCoords, currentIndex) => { }); | ||
}); | ||
suite.add('coordReduce - ' + name, () => { | ||
meta.coordReduce(fixture, (previousValue, currentCoords, currentIndex) => { }); | ||
}); | ||
suite.add('propEach - ' + name, () => { | ||
meta.propEach(fixture, (currentProperties, currentIndex) => { }); | ||
}); | ||
suite.add('propReduce - ' + name, () => { | ||
meta.propReduce(fixture, (previousValue, currentProperties, currentIndex) => { }); | ||
}); | ||
suite.add('geomEach - ' + name, () => { | ||
meta.geomEach(fixture, (currentGeometry, currentIndex) => { }); | ||
}); | ||
suite.add('geomReduce - ' + name, () => { | ||
meta.geomReduce(fixture, (previousValue, currentGeometry, currentIndex) => { }); | ||
}); | ||
suite.add('featureEach - ' + name, () => { | ||
meta.featureEach(fixture, (currentFeature, currentIndex) => { }); | ||
}); | ||
suite.add('featureReduce - ' + name, () => { | ||
meta.featureReduce(fixture, (previousValue, currentFeature, currentIndex) => { }); | ||
}); | ||
suite.add('coordAll - ' + name, () => { | ||
meta.coordAll(fixture); | ||
}); | ||
}); | ||
console.timeEnd('coordEach#1'); | ||
console.time('coordEach#2'); | ||
var sum = 0; | ||
combined.forEach(function(c) { | ||
meta2.coordEach(c, function(coord) { | ||
sum += coord[0]; | ||
}); | ||
}); | ||
console.timeEnd('coordEach#2'); | ||
suite | ||
.on('cycle', (event) => { console.log(String(event.target)); }) | ||
.on('complete', () => {}) | ||
.run(); |
@@ -24,14 +24,14 @@ /// <reference types="geojson" /> | ||
*/ | ||
coordEach(layer: Points | Point | MultiPoint | MultiPoints, callback: (coords: Array<number>) => void, excludeWrapCoord?: boolean): void; | ||
coordEach(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (coords: Array<Array<number>>) => void, excludeWrapCoord?: boolean): void; | ||
coordEach(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (coords: Array<Array<Array<number>>>) => void, excludeWrapCoord?: boolean): void; | ||
coordEach(layer: GeometryCollection | GeometryObject, callback: (coords: Array<any>) => void, excludeWrapCoord?: boolean): void; | ||
coordEach(layer: Points | Point | MultiPoint | MultiPoints, callback: (currentCoords: Array<number>, currentIndex: number) => void, excludeWrapCoord?: boolean): void; | ||
coordEach(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (currentCoords: Array<Array<number>>, currentIndex: number) => void, excludeWrapCoord?: boolean): void; | ||
coordEach(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (currentCoords: Array<Array<Array<number>>>, currentIndex: number) => void, excludeWrapCoord?: boolean): void; | ||
coordEach(layer: GeometryCollection | GeometryObject, callback: (currentCoords: Array<any>, currentIndex: number) => void, excludeWrapCoord?: boolean): void; | ||
/** | ||
* http://turfjs.org/docs/#coordeach | ||
* http://turfjs.org/docs/#coordreduce | ||
*/ | ||
coordReduce(layer: Points | Point | MultiPoint | MultiPoints, callback: (memo: any, coords: Array<number>) => void, memo: any, excludeWrapCoord?: boolean): any; | ||
coordReduce(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (memo: any, coords: Array<Array<number>>) => void, memo: any, excludeWrapCoord?: boolean): any; | ||
coordReduce(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (memo: any, coords: Array<Array<Array<number>>>) => void, memo: any, excludeWrapCoord?: boolean): any; | ||
coordReduce(layer: GeometryCollection | GeometryObject, callback: (memo: any, coords: Array<any>) => void, memo: any, excludeWrapCoord?: boolean): any; | ||
coordReduce(layer: Points | Point | MultiPoint | MultiPoints, callback: (previousValue: any, currentCoords: Array<number>, currentIndex: number) => void, initialValue: any, excludeWrapCoord?: boolean): any; | ||
coordReduce(layer: LineStrings | LineString | MultiLineString | MultiLineStrings, callback: (previousValue: any, currentCoords: Array<Array<number>>, currentIndex: number) => void, initialValue: any, excludeWrapCoord?: boolean): any; | ||
coordReduce(layer: Polygons | Polygon | MultiPolygons | MultiPolygon, callback: (previousValue: any, currentCoords: Array<Array<Array<number>>>, currentIndex: number) => void, initialValue: any, excludeWrapCoord?: boolean): any; | ||
coordReduce(layer: GeometryCollection | GeometryObject, callback: (previousValue: any, currentCoords: Array<any>, currentIndex: number) => void, initialValue: any, excludeWrapCoord?: boolean): any; | ||
@@ -41,3 +41,3 @@ /** | ||
*/ | ||
propEach(layer: Feature | Features, callback: (properties: any) => void): void; | ||
propEach(layer: Feature | Features, callback: (currentProperties: any, currentIndex: number) => void): void; | ||
@@ -47,14 +47,26 @@ /** | ||
*/ | ||
propReduce(layer: Feature | Features, callback: (prev: any, props: any) => any, memo: any): any; | ||
propReduce(layer: Feature | Features, callback: (previousValue: any, currentProperties: any, currentIndex: number) => void, initialValue: any): any; | ||
/** | ||
* http://turfjs.org/docs/#featurereduce | ||
*/ | ||
featureReduce(layer: Point | Points, callback: (previousValue: any, currentFeature: Point, currentIndex: number) => void, initialValue: any): any; | ||
featureReduce(layer: LineString | LineStrings, callback: (previousValue: any, currentFeature: LineString, currentIndex: number) => void, initialValue: any): any; | ||
featureReduce(layer: Polygon | Polygons, callback: (previousValue: any, currentFeature: Polygon, currentIndex: number) => void, initialValue: any): any; | ||
featureReduce(layer: MultiPoint | MultiPoints, callback: (previousValue: any, currentFeature: MultiPoint, currentIndex: number) => void, initialValue: any): any; | ||
featureReduce(layer: MultiLineString | MultiLineStrings, callback: (previousValue: any, currentFeature: MultiLineString, currentIndex: number) => void, initialValue: any): any; | ||
featureReduce(layer: MultiPolygon | MultiPolygons, callback: (previousValue: any, currentFeature: MultiPolygon, currentIndex: number) => void, initialValue: any): any; | ||
featureReduce(layer: Feature | Features, callback: (previousValue: any, currentFeature: Feature, currentIndex: number) => void, initialValue: any): any; | ||
/** | ||
* http://turfjs.org/docs/#featureeach | ||
*/ | ||
featureEach(layer: Point | Points, callback: (feature: Point) => void): void; | ||
featureEach(layer: LineString | LineStrings, callback: (feature: LineString) => void): void; | ||
featureEach(layer: Polygon | Polygons, callback: (feature: Polygon) => void): void; | ||
featureEach(layer: MultiPoint | MultiPoints, callback: (feature: MultiPoint) => void): void; | ||
featureEach(layer: MultiLineString | MultiLineStrings, callback: (feature: MultiLineString) => void): void; | ||
featureEach(layer: MultiPolygon | MultiPolygons, callback: (feature: MultiPolygon) => void): void; | ||
featureEach(layer: Feature | Features, callback: (feature: Feature) => void): void; | ||
featureEach(layer: Point | Points, callback: (currentFeature: Point, currentIndex: number) => void): void; | ||
featureEach(layer: LineString | LineStrings, callback: (currentFeature: LineString, currentIndex: number) => void): void; | ||
featureEach(layer: Polygon | Polygons, callback: (currentFeature: Polygon, currentIndex: number) => void): void; | ||
featureEach(layer: MultiPoint | MultiPoints, callback: (currentFeature: MultiPoint, currentIndex: number) => void): void; | ||
featureEach(layer: MultiLineString | MultiLineStrings, callback: (currentFeature: MultiLineString, currentIndex: number) => void): void; | ||
featureEach(layer: MultiPolygon | MultiPolygons, callback: (currentFeature: MultiPolygon, currentIndex: number) => void): void; | ||
featureEach(layer: Feature | Features, callback: (currentFeature: Feature, currentIndex: number) => void): void; | ||
@@ -67,12 +79,24 @@ /** | ||
/** | ||
* http://turfjs.org/docs/#geomreduce | ||
*/ | ||
geomReduce(layer: Point | Points, callback: (previousValue: any, currentGeometry: GeoJSON.Point, currentIndex: number) => void, initialValue: any): any; | ||
geomReduce(layer: LineString | LineStrings, callback: (previousValue: any, currentGeometry: GeoJSON.LineString, currentIndex: number) => void, initialValue: any): any; | ||
geomReduce(layer: Polygon | Polygons, callback: (previousValue: any, currentGeometry: GeoJSON.Polygon, currentIndex: number) => void, initialValue: any): any; | ||
geomReduce(layer: MultiPoint | MultiPoints, callback: (previousValue: any, currentGeometry: GeoJSON.MultiPoint, currentIndex: number) => void, initialValue: any): any; | ||
geomReduce(layer: MultiLineString | MultiLineStrings, callback: (previousValue: any, currentGeometry: GeoJSON.MultiLineString, currentIndex: number) => void, initialValue: any): any; | ||
geomReduce(layer: MultiPolygon | MultiPolygons, callback: (previousValue: any, currentGeometry: GeoJSON.MultiPolygon, currentIndex: number) => void, initialValue: any): any; | ||
geomReduce(layer: Feature | Features, callback: (previousValue: any, currentGeometry: GeometryObject, currentIndex: number) => void, initialValue: any): any; | ||
geomReduce(layer: GeometryCollection | GeometryObject, callback: (previousValue: any, currentGeometry: GeometryObject, currentIndex: number) => void, initialValue: any): any; | ||
/** | ||
* http://turfjs.org/docs/#geomeach | ||
*/ | ||
geomEach(layer: Point | Points, callback: (geom: GeoJSON.Point) => void): void; | ||
geomEach(layer: LineString | LineStrings, callback: (geom: GeoJSON.LineString) => void): void; | ||
geomEach(layer: Polygon | Polygons, callback: (geom: GeoJSON.Polygon) => void): void; | ||
geomEach(layer: MultiPoint | MultiPoints, callback: (geom: GeoJSON.MultiPoint) => void): void; | ||
geomEach(layer: MultiLineString | MultiLineStrings, callback: (geom: GeoJSON.MultiLineString) => void): void; | ||
geomEach(layer: MultiPolygon | MultiPolygons, callback: (geom: GeoJSON.MultiPolygon) => void): void; | ||
geomEach(layer: Feature | Features, callback: (geom: GeometryObject) => void): void; | ||
geomEach(layer: GeometryCollection | GeometryObject, callback: (geom: GeometryObject) => void): void; | ||
geomEach(layer: Point | Points, callback: (currentGeometry: GeoJSON.Point, currentIndex: number) => void): void; | ||
geomEach(layer: LineString | LineStrings, callback: (currentGeometry: GeoJSON.LineString, currentIndex: number) => void): void; | ||
geomEach(layer: Polygon | Polygons, callback: (currentGeometry: GeoJSON.Polygon, currentIndex: number) => void): void; | ||
geomEach(layer: MultiPoint | MultiPoints, callback: (currentGeometry: GeoJSON.MultiPoint, currentIndex: number) => void): void; | ||
geomEach(layer: MultiLineString | MultiLineStrings, callback: (currentGeometry: GeoJSON.MultiLineString, currentIndex: number) => void): void; | ||
geomEach(layer: MultiPolygon | MultiPolygons, callback: (currentGeometry: GeoJSON.MultiPolygon, currentIndex: number) => void): void; | ||
geomEach(layer: Feature | Features, callback: (currentGeometry: GeometryObject, currentIndex: number) => void): void; | ||
geomEach(layer: GeometryCollection | GeometryObject, callback: (currentGeometry: GeometryObject, currentIndex: number) => void): void; | ||
} | ||
@@ -79,0 +103,0 @@ |
520
index.js
/** | ||
* Iterate over coordinates in any GeoJSON object, similar to | ||
* Array.forEach. | ||
* Callback for coordEach | ||
* | ||
* @private | ||
* @callback coordEachCallback | ||
* @param {[number, number]} currentCoords The current coordinates being processed. | ||
* @param {number} currentIndex The index of the current element being processed in the | ||
* array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. | ||
*/ | ||
/** | ||
* Iterate over coordinates in any GeoJSON object, similar to Array.forEach() | ||
* | ||
* @name coordEach | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (value) | ||
* @param {boolean=} excludeWrapCoord whether or not to include | ||
* @param {Function} callback a method that takes (currentCoords, currentIndex) | ||
* @param {boolean} [excludeWrapCoord=false] whether or not to include | ||
* the final coordinate of LinearRings that wraps the ring in its iteration. | ||
* @example | ||
* var point = { type: 'Point', coordinates: [0, 0] }; | ||
* turfMeta.coordEach(point, function(coords) { | ||
* // coords is equal to [0, 0] | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* turf.coordEach(features, function (currentCoords, currentIndex) { | ||
* //=currentCoords | ||
* //=currentIndex | ||
* }); | ||
@@ -20,2 +50,3 @@ */ | ||
wrapShrink = 0, | ||
currentIndex = 0, | ||
isGeometryCollection, | ||
@@ -55,14 +86,22 @@ isFeatureCollection = layer.type === 'FeatureCollection', | ||
if (geometry.type === 'Point') { | ||
callback(coords); | ||
callback(coords, currentIndex); | ||
currentIndex++; | ||
} else if (geometry.type === 'LineString' || geometry.type === 'MultiPoint') { | ||
for (j = 0; j < coords.length; j++) callback(coords[j]); | ||
for (j = 0; j < coords.length; j++) { | ||
callback(coords[j], currentIndex); | ||
currentIndex++; | ||
} | ||
} else if (geometry.type === 'Polygon' || geometry.type === 'MultiLineString') { | ||
for (j = 0; j < coords.length; j++) | ||
for (k = 0; k < coords[j].length - wrapShrink; k++) | ||
callback(coords[j][k]); | ||
for (k = 0; k < coords[j].length - wrapShrink; k++) { | ||
callback(coords[j][k], currentIndex); | ||
currentIndex++; | ||
} | ||
} else if (geometry.type === 'MultiPolygon') { | ||
for (j = 0; j < coords.length; j++) | ||
for (k = 0; k < coords[j].length; k++) | ||
for (l = 0; l < coords[j][k].length - wrapShrink; l++) | ||
callback(coords[j][k][l]); | ||
for (l = 0; l < coords[j][k].length - wrapShrink; l++) { | ||
callback(coords[j][k][l], currentIndex); | ||
currentIndex++; | ||
} | ||
} else if (geometry.type === 'GeometryCollection') { | ||
@@ -80,20 +119,73 @@ for (j = 0; j < geometry.geometries.length; j++) | ||
/** | ||
* Reduce coordinates 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 coordinates is unnecessary. | ||
* 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. | ||
* | ||
* @private | ||
* @callback coordReduceCallback | ||
* @param {*} previousValue The accumulated value previously returned in the last invocation | ||
* of the callback, or initialValue, if supplied. | ||
* @param {[number, number]} currentCoords The current coordinate being processed. | ||
* @param {number} currentIndex The index of the current element being processed in the | ||
* array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. | ||
*/ | ||
/** | ||
* Reduce coordinates in any GeoJSON object, similar to Array.reduce() | ||
* | ||
* @name coordReduce | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (memo, value) and returns | ||
* a new memo | ||
* @param {*} memo the starting value of memo: can be any type. | ||
* @param {boolean=} excludeWrapCoord whether or not to include | ||
* @param {Function} callback a method that takes (previousValue, currentCoords, currentIndex) | ||
* @param {*} [initialValue] Value to use as the first argument to the first call of the callback. | ||
* @param {boolean} [excludeWrapCoord=false] whether or not to include | ||
* the final coordinate of LinearRings that wraps the ring in its iteration. | ||
* @returns {*} combined value | ||
* @returns {*} The value that results from the reduction. | ||
* @example | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* turf.coordReduce(features, function (previousValue, currentCoords, currentIndex) { | ||
* //=previousValue | ||
* //=currentCoords | ||
* //=currentIndex | ||
* return currentCoords; | ||
* }); | ||
*/ | ||
function coordReduce(layer, callback, memo, excludeWrapCoord) { | ||
coordEach(layer, function (coord) { | ||
memo = callback(memo, coord); | ||
function coordReduce(layer, callback, initialValue, excludeWrapCoord) { | ||
var previousValue = initialValue; | ||
coordEach(layer, function (currentCoords, currentIndex) { | ||
if (currentIndex === 0 && initialValue === undefined) { | ||
previousValue = currentCoords; | ||
} else { | ||
previousValue = callback(previousValue, currentCoords, currentIndex); | ||
} | ||
}, excludeWrapCoord); | ||
return memo; | ||
return previousValue; | ||
} | ||
@@ -103,12 +195,42 @@ module.exports.coordReduce = coordReduce; | ||
/** | ||
* Iterate over property objects in any GeoJSON object, similar to | ||
* Array.forEach. | ||
* Callback for propEach | ||
* | ||
* @private | ||
* @callback propEachCallback | ||
* @param {*} currentProperties The current properties being processed. | ||
* @param {number} currentIndex The index of the current element being processed in the | ||
* array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. | ||
*/ | ||
/** | ||
* Iterate over properties in any GeoJSON object, similar to Array.forEach() | ||
* | ||
* @name propEach | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (value) | ||
* @param {Function} callback a method that takes (currentProperties, currentIndex) | ||
* @example | ||
* var point = { type: 'Feature', geometry: null, properties: { foo: 1 } }; | ||
* turfMeta.propEach(point, function(props) { | ||
* // props is equal to { foo: 1} | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"foo": "bar"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"hello": "world"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* turf.propEach(features, function (currentProperties, currentIndex) { | ||
* //=currentProperties | ||
* //=currentIndex | ||
* }); | ||
@@ -131,3 +253,27 @@ */ | ||
/** | ||
* 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. | ||
* | ||
* @private | ||
* @callback propReduceCallback | ||
* @param {*} previousValue The accumulated value previously returned in the last invocation | ||
* of the callback, or initialValue, if supplied. | ||
* @param {*} currentProperties The current properties being processed. | ||
* @param {number} currentIndex The index of the current element being processed in the | ||
* array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. | ||
*/ | ||
/** | ||
* Reduce properties in any GeoJSON object into a single value, | ||
@@ -139,24 +285,44 @@ * similar to how Array.reduce works. However, in this case we lazily run | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (memo, coord) and returns | ||
* a new memo | ||
* @param {*} memo the starting value of memo: can be any type. | ||
* @returns {*} combined value | ||
* @param {Function} callback a method that takes (previousValue, currentProperties, currentIndex) | ||
* @param {*} [initialValue] Value to use as the first argument to the first call of the callback. | ||
* @returns {*} The value that results from the reduction. | ||
* @example | ||
* // an example of an even more advanced function that gives you the | ||
* // javascript type of each property of every feature | ||
* function propTypes (layer) { | ||
* opts = opts || {} | ||
* return turfMeta.propReduce(layer, function (prev, props) { | ||
* for (var prop in props) { | ||
* if (prev[prop]) continue | ||
* prev[prop] = typeof props[prop] | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"foo": "bar"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"hello": "world"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* }, {}) | ||
* } | ||
* ] | ||
* }; | ||
* turf.propReduce(features, function (previousValue, currentProperties, currentIndex) { | ||
* //=previousValue | ||
* //=currentProperties | ||
* //=currentIndex | ||
* return currentProperties | ||
* }); | ||
*/ | ||
function propReduce(layer, callback, memo) { | ||
propEach(layer, function (prop, i) { | ||
memo = callback(memo, prop, i); | ||
function propReduce(layer, callback, initialValue) { | ||
var previousValue = initialValue; | ||
propEach(layer, function (currentProperties, currentIndex) { | ||
if (currentIndex === 0 && initialValue === undefined) { | ||
previousValue = currentProperties; | ||
} else { | ||
previousValue = callback(previousValue, currentProperties, currentIndex); | ||
} | ||
}); | ||
return memo; | ||
return previousValue; | ||
} | ||
@@ -166,2 +332,12 @@ module.exports.propReduce = propReduce; | ||
/** | ||
* Callback for featureEach | ||
* | ||
* @private | ||
* @callback featureEachCallback | ||
* @param {Feature<any>} currentFeature The current feature being processed. | ||
* @param {number} currentIndex The index of the current element being processed in the | ||
* array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. | ||
*/ | ||
/** | ||
* Iterate over features in any GeoJSON object, similar to | ||
@@ -172,7 +348,28 @@ * Array.forEach. | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (value) | ||
* @param {Function} callback a method that takes (currentFeature, currentIndex) | ||
* @example | ||
* var feature = { type: 'Feature', geometry: null, properties: {} }; | ||
* turfMeta.featureEach(feature, function(feature) { | ||
* // feature == feature | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* turf.featureEach(features, function (currentFeature, currentIndex) { | ||
* //=currentFeature | ||
* //=currentIndex | ||
* }); | ||
@@ -192,8 +389,104 @@ */ | ||
/** | ||
* Get all coordinates from any GeoJSON object, returning an array of coordinate | ||
* arrays. | ||
* 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. | ||
* | ||
* @private | ||
* @callback featureReduceCallback | ||
* @param {*} previousValue The accumulated value previously returned in the last invocation | ||
* of the callback, or initialValue, if supplied. | ||
* @param {Feature<any>} currentFeature The current Feature being processed. | ||
* @param {number} currentIndex The index of the current element being processed in the | ||
* array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. | ||
*/ | ||
/** | ||
* Reduce features in any GeoJSON object, similar to Array.reduce(). | ||
* | ||
* @name featureReduce | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (previousValue, currentFeature, currentIndex) | ||
* @param {*} [initialValue] Value to use as the first argument to the first call of the callback. | ||
* @returns {*} The value that results from the reduction. | ||
* @example | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"foo": "bar"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"hello": "world"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* turf.featureReduce(features, function (previousValue, currentFeature, currentIndex) { | ||
* //=previousValue | ||
* //=currentFeature | ||
* //=currentIndex | ||
* return currentFeature | ||
* }); | ||
*/ | ||
function featureReduce(layer, callback, initialValue) { | ||
var previousValue = initialValue; | ||
featureEach(layer, function (currentFeature, currentIndex) { | ||
if (currentIndex === 0 && initialValue === undefined) { | ||
previousValue = currentFeature; | ||
} else { | ||
previousValue = callback(previousValue, currentFeature, currentIndex); | ||
} | ||
}); | ||
return previousValue; | ||
} | ||
module.exports.featureReduce = featureReduce; | ||
/** | ||
* Get all coordinates from any GeoJSON object. | ||
* | ||
* @name coordAll | ||
* @param {Object} layer any GeoJSON object | ||
* @returns {Array<Array<number>>} coordinate position array | ||
* @example | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* var coords = turf.coordAll(features); | ||
* //=coords | ||
*/ | ||
@@ -210,16 +503,32 @@ function coordAll(layer) { | ||
/** | ||
* Iterate over each geometry in any GeoJSON object, similar to | ||
* Array.forEach. | ||
* Iterate over each geometry in any GeoJSON object, similar to Array.forEach() | ||
* | ||
* @name geomEach | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (value) | ||
* @param {Function} callback a method that takes (currentGeometry, currentIndex) | ||
* @example | ||
* var point = { | ||
* type: 'Feature', | ||
* geometry: { type: 'Point', coordinates: [0, 0] }, | ||
* properties: {} | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* turfMeta.geomEach(point, function(geom) { | ||
* // geom is the point geometry | ||
* turf.geomEach(features, function (currentGeometry, currentIndex) { | ||
* //=currentGeometry | ||
* //=currentIndex | ||
* }); | ||
@@ -231,2 +540,3 @@ */ | ||
isGeometryCollection, | ||
currentIndex = 0, | ||
isFeatureCollection = layer.type === 'FeatureCollection', | ||
@@ -265,6 +575,9 @@ isFeature = layer.type === 'Feature', | ||
geometry.type === 'MultiPolygon') { | ||
callback(geometry); | ||
callback(geometry, currentIndex); | ||
currentIndex++; | ||
} else if (geometry.type === 'GeometryCollection') { | ||
for (j = 0; j < geometry.geometries.length; j++) | ||
callback(geometry.geometries[j]); | ||
for (j = 0; j < geometry.geometries.length; j++) { | ||
callback(geometry.geometries[j], currentIndex); | ||
currentIndex++; | ||
} | ||
} else { | ||
@@ -277,1 +590,74 @@ throw new Error('Unknown Geometry Type'); | ||
module.exports.geomEach = geomEach; | ||
/** | ||
* 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. | ||
* | ||
* @private | ||
* @callback geomReduceCallback | ||
* @param {*} previousValue The accumulated value previously returned in the last invocation | ||
* of the callback, or initialValue, if supplied. | ||
* @param {*} currentGeometry The current Feature being processed. | ||
* @param {number} currentIndex The index of the current element being processed in the | ||
* array.Starts at index 0, if an initialValue is provided, and at index 1 otherwise. | ||
*/ | ||
/** | ||
* Reduce geometry in any GeoJSON object, similar to Array.reduce(). | ||
* | ||
* @name geomReduce | ||
* @param {Object} layer any GeoJSON object | ||
* @param {Function} callback a method that takes (previousValue, currentGeometry, currentIndex) | ||
* @param {*} [initialValue] Value to use as the first argument to the first call of the callback. | ||
* @returns {*} The value that results from the reduction. | ||
* @example | ||
* var features = { | ||
* "type": "FeatureCollection", | ||
* "features": [ | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"foo": "bar"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [26, 37] | ||
* } | ||
* }, | ||
* { | ||
* "type": "Feature", | ||
* "properties": {"hello": "world"}, | ||
* "geometry": { | ||
* "type": "Point", | ||
* "coordinates": [36, 53] | ||
* } | ||
* } | ||
* ] | ||
* }; | ||
* turf.geomReduce(features, function (previousValue, currentGeometry, currentIndex) { | ||
* //=previousValue | ||
* //=currentGeometry | ||
* //=currentIndex | ||
* return currentGeometry | ||
* }); | ||
*/ | ||
function geomReduce(layer, callback, initialValue) { | ||
var previousValue = initialValue; | ||
featureEach(layer, function (currentGeometry, currentIndex) { | ||
if (currentIndex === 0 && initialValue === undefined) { | ||
previousValue = currentGeometry; | ||
} else { | ||
previousValue = callback(previousValue, currentGeometry, currentIndex); | ||
} | ||
}); | ||
return previousValue; | ||
} | ||
module.exports.geomReduce = geomReduce; |
{ | ||
"name": "@turf/meta", | ||
"version": "3.10.4", | ||
"version": "3.10.5", | ||
"description": "meta and functional programming helpers for turf modules", | ||
@@ -17,3 +17,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@turf/helpers": "^3.10.5", | ||
"@turf/random": "^3.10.0", | ||
"benchmark": "^2.1.3", | ||
"eslint": "^3.14.1", | ||
@@ -20,0 +22,0 @@ "eslint-config-mourner": "^2.0.1", |
336
README.md
@@ -5,4 +5,3 @@ # @turf/meta | ||
Iterate over coordinates in any GeoJSON object, similar to | ||
Array.forEach. | ||
Iterate over coordinates in any GeoJSON object, similar to Array.forEach() | ||
@@ -12,5 +11,5 @@ **Parameters** | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (value) | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentCoords, currentIndex) | ||
- `excludeWrapCoord` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether or not to include | ||
the final coordinate of LinearRings that wraps the ring in its iteration. | ||
the final coordinate of LinearRings that wraps the ring in its iteration. (optional, default `false`) | ||
@@ -20,5 +19,26 @@ **Examples** | ||
```javascript | ||
var point = { type: 'Point', coordinates: [0, 0] }; | ||
turfMeta.coordEach(point, function(coords) { | ||
// coords is equal to [0, 0] | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
turf.coordEach(features, function (currentCoords, currentIndex) { | ||
//=currentCoords | ||
//=currentIndex | ||
}); | ||
@@ -29,5 +49,3 @@ ``` | ||
Reduce coordinates 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 coordinates is unnecessary. | ||
Reduce coordinates in any GeoJSON object, similar to Array.reduce() | ||
@@ -37,14 +55,44 @@ **Parameters** | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (memo, value) and returns | ||
a new memo | ||
- `memo` **Any** the starting value of memo: can be any type. | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentCoords, currentIndex) | ||
- `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. | ||
- `excludeWrapCoord` **\[[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)]** whether or not to include | ||
the final coordinate of LinearRings that wraps the ring in its iteration. | ||
the final coordinate of LinearRings that wraps the ring in its iteration. (optional, default `false`) | ||
Returns **Any** combined value | ||
**Examples** | ||
```javascript | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
turf.coordReduce(features, function (previousValue, currentCoords, currentIndex) { | ||
//=previousValue | ||
//=currentCoords | ||
//=currentIndex | ||
return currentCoords; | ||
}); | ||
``` | ||
Returns **Any** The value that results from the reduction. | ||
# propEach | ||
Iterate over property objects in any GeoJSON object, similar to | ||
Array.forEach. | ||
Iterate over properties in any GeoJSON object, similar to Array.forEach() | ||
@@ -54,3 +102,3 @@ **Parameters** | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (value) | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentProperties, currentIndex) | ||
@@ -60,5 +108,26 @@ **Examples** | ||
```javascript | ||
var point = { type: 'Feature', geometry: null, properties: { foo: 1 } }; | ||
turfMeta.propEach(point, function(props) { | ||
// props is equal to { foo: 1} | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {"foo": "bar"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {"hello": "world"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
turf.propEach(features, function (currentProperties, currentIndex) { | ||
//=currentProperties | ||
//=currentIndex | ||
}); | ||
@@ -76,5 +145,4 @@ ``` | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (memo, coord) and returns | ||
a new memo | ||
- `memo` **Any** the starting value of memo: can be any type. | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentProperties, currentIndex) | ||
- `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. | ||
@@ -84,16 +152,32 @@ **Examples** | ||
```javascript | ||
// an example of an even more advanced function that gives you the | ||
// javascript type of each property of every feature | ||
function propTypes (layer) { | ||
opts = opts || {} | ||
return turfMeta.propReduce(layer, function (prev, props) { | ||
for (var prop in props) { | ||
if (prev[prop]) continue | ||
prev[prop] = typeof props[prop] | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {"foo": "bar"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {"hello": "world"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
}, {}) | ||
} | ||
] | ||
}; | ||
turf.propReduce(features, function (previousValue, currentProperties, currentIndex) { | ||
//=previousValue | ||
//=currentProperties | ||
//=currentIndex | ||
return currentProperties | ||
}); | ||
``` | ||
Returns **Any** combined value | ||
Returns **Any** The value that results from the reduction. | ||
@@ -108,3 +192,3 @@ # featureEach | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (value) | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentFeature, currentIndex) | ||
@@ -114,12 +198,76 @@ **Examples** | ||
```javascript | ||
var feature = { type: 'Feature', geometry: null, properties: {} }; | ||
turfMeta.featureEach(feature, function(feature) { | ||
// feature == feature | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
turf.featureEach(features, function (currentFeature, currentIndex) { | ||
//=currentFeature | ||
//=currentIndex | ||
}); | ||
``` | ||
# featureReduce | ||
Reduce features in any GeoJSON object, similar to Array.reduce(). | ||
**Parameters** | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentFeature, currentIndex) | ||
- `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. | ||
**Examples** | ||
```javascript | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {"foo": "bar"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {"hello": "world"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
turf.featureReduce(features, function (previousValue, currentFeature, currentIndex) { | ||
//=previousValue | ||
//=currentFeature | ||
//=currentIndex | ||
return currentFeature | ||
}); | ||
``` | ||
Returns **Any** The value that results from the reduction. | ||
# coordAll | ||
Get all coordinates from any GeoJSON object, returning an array of coordinate | ||
arrays. | ||
Get all coordinates from any GeoJSON object. | ||
@@ -130,2 +278,30 @@ **Parameters** | ||
**Examples** | ||
```javascript | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
var coords = turf.coordAll(features); | ||
//=coords | ||
``` | ||
Returns **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)>>** coordinate position array | ||
@@ -135,4 +311,3 @@ | ||
Iterate over each geometry in any GeoJSON object, similar to | ||
Array.forEach. | ||
Iterate over each geometry in any GeoJSON object, similar to Array.forEach() | ||
@@ -142,3 +317,3 @@ **Parameters** | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (value) | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (currentGeometry, currentIndex) | ||
@@ -148,12 +323,73 @@ **Examples** | ||
```javascript | ||
var point = { | ||
type: 'Feature', | ||
geometry: { type: 'Point', coordinates: [0, 0] }, | ||
properties: {} | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
turfMeta.geomEach(point, function(geom) { | ||
// geom is the point geometry | ||
turf.geomEach(features, function (currentGeometry, currentIndex) { | ||
//=currentGeometry | ||
//=currentIndex | ||
}); | ||
``` | ||
# geomReduce | ||
Reduce geometry in any GeoJSON object, similar to Array.reduce(). | ||
**Parameters** | ||
- `layer` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** any GeoJSON object | ||
- `callback` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** a method that takes (previousValue, currentGeometry, currentIndex) | ||
- `initialValue` **\[Any]** Value to use as the first argument to the first call of the callback. | ||
**Examples** | ||
```javascript | ||
var features = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {"foo": "bar"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [26, 37] | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {"hello": "world"}, | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [36, 53] | ||
} | ||
} | ||
] | ||
}; | ||
turf.geomReduce(features, function (previousValue, currentGeometry, currentIndex) { | ||
//=previousValue | ||
//=currentGeometry | ||
//=currentIndex | ||
return currentGeometry | ||
}); | ||
``` | ||
Returns **Any** The value that results from the reduction. | ||
<!-- This file is automatically generated. Please don't edit it directly: | ||
@@ -160,0 +396,0 @@ if you find an error, edit the source file (likely index.js), and re-run |
110
test.js
var test = require('tape'); | ||
var lineString = require('@turf/helpers').lineString; | ||
var meta = require('./'); | ||
@@ -72,4 +73,5 @@ | ||
test('coordEach#Point', function (t) { | ||
meta.coordEach(input, function (coord) { | ||
meta.coordEach(input, function (coord, index) { | ||
t.deepEqual(coord, [0, 0]); | ||
t.equal(index, 0); | ||
t.end(); | ||
@@ -83,6 +85,9 @@ }); | ||
var output = []; | ||
meta.coordEach(input, function (coord) { | ||
var lastIndex; | ||
meta.coordEach(input, function (coord, index) { | ||
output.push(coord); | ||
lastIndex = index; | ||
}); | ||
t.deepEqual(output, [[0, 0], [1, 1]]); | ||
t.equal(lastIndex, 1); | ||
t.end(); | ||
@@ -95,6 +100,9 @@ }); | ||
var output = []; | ||
meta.coordEach(input, function (coord) { | ||
var lastIndex; | ||
meta.coordEach(input, function (coord, index) { | ||
output.push(coord); | ||
lastIndex = index; | ||
}); | ||
t.deepEqual(output, [[0, 0], [1, 1], [0, 1], [0, 0]]); | ||
t.equal(lastIndex, 3); | ||
t.end(); | ||
@@ -107,6 +115,9 @@ }); | ||
var output = []; | ||
meta.coordEach(input, function (coord) { | ||
var lastIndex; | ||
meta.coordEach(input, function (coord, index) { | ||
output.push(coord); | ||
lastIndex = index; | ||
}, true); | ||
t.deepEqual(output, [[0, 0], [1, 1], [0, 1]]); | ||
t.equal(lastIndex, 2); | ||
t.end(); | ||
@@ -121,6 +132,9 @@ }); | ||
var output = []; | ||
meta.coordEach(input, function (coord) { | ||
var lastIndex; | ||
meta.coordEach(input, function (coord, index) { | ||
output.push(coord); | ||
lastIndex = index; | ||
}); | ||
t.deepEqual(output, [[0, 0], [1, 1], [0, 1], [0, 0]]); | ||
t.equal(lastIndex, 3); | ||
t.end(); | ||
@@ -133,6 +147,9 @@ }); | ||
var output = []; | ||
meta.coordEach(input, function (coord) { | ||
var lastIndex; | ||
meta.coordEach(input, function (coord, index) { | ||
output.push(coord); | ||
lastIndex = index; | ||
}); | ||
t.deepEqual(output, [[0, 0], [0, 0], [1, 1]]); | ||
t.equal(lastIndex, 2); | ||
t.end(); | ||
@@ -142,2 +159,83 @@ }); | ||
test('coordReduce#initialValue', function (t) { | ||
var lastIndex; | ||
var line = lineString([[126, -11], [129, -21], [135, -31]]); | ||
var sum = meta.coordReduce(line, function (previousValue, currentCoords, currentIndex) { | ||
lastIndex = currentIndex; | ||
return previousValue + currentCoords[0]; | ||
}, 0); | ||
t.equal(lastIndex, 2); | ||
t.equal(sum, 390); | ||
t.end(); | ||
}); | ||
test('Array.reduce()#initialValue', function (t) { | ||
var lastIndex; | ||
var line = [[126, -11], [129, -21], [135, -31]]; | ||
var sum = line.reduce(function (previousValue, currentCoords, currentIndex) { | ||
lastIndex = currentIndex; | ||
return previousValue + currentCoords[0]; | ||
}, 0); | ||
t.equal(lastIndex, 2); | ||
t.equal(sum, 390); | ||
t.end(); | ||
}); | ||
test('coordReduce#previous-coordinates', function (t) { | ||
var lastIndex; | ||
var coords = []; | ||
var line = lineString([[126, -11], [129, -21], [135, -31]]); | ||
meta.coordReduce(line, function (previousCoords, currentCoords, currentIndex) { | ||
lastIndex = currentIndex; | ||
coords.push(currentCoords); | ||
return currentCoords; | ||
}); | ||
t.equal(lastIndex, 2); | ||
t.equal(coords.length, 2); | ||
t.end(); | ||
}); | ||
test('Array.reduce()#previous-coordinates', function (t) { | ||
var lastIndex; | ||
var coords = []; | ||
var line = [[126, -11], [129, -21], [135, -31]]; | ||
line.reduce(function (previousCoords, currentCoords, currentIndex) { | ||
lastIndex = currentIndex; | ||
coords.push(currentCoords); | ||
return currentCoords; | ||
}); | ||
t.equal(lastIndex, 2); | ||
t.equal(coords.length, 2); | ||
t.end(); | ||
}); | ||
test('coordReduce#previous-coordinates+initialValue', function (t) { | ||
var lastIndex; | ||
var coords = []; | ||
var line = lineString([[126, -11], [129, -21], [135, -31]]); | ||
meta.coordReduce(line, function (previousCoords, currentCoords, currentIndex) { | ||
lastIndex = currentIndex; | ||
coords.push(currentCoords); | ||
return currentCoords; | ||
}, line.geometry.coordinates[0]); | ||
t.equal(lastIndex, 2); | ||
t.equal(coords.length, 3); | ||
t.end(); | ||
}); | ||
test('Array.reduce()#previous-coordinates+initialValue', function (t) { | ||
var lastIndex; | ||
var coords = []; | ||
var line = [[126, -11], [129, -21], [135, -31]]; | ||
line.reduce(function (previousCoords, currentCoords, currentIndex) { | ||
lastIndex = currentIndex; | ||
coords.push(currentCoords); | ||
return currentCoords; | ||
}, line[0]); | ||
t.equal(lastIndex, 2); | ||
t.equal(coords.length, 3); | ||
t.end(); | ||
}); | ||
test('unknown', function (t) { | ||
@@ -144,0 +242,0 @@ t.throws(function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51830
1002
404
6
1