Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript
https://turfjs.org/
MIT License
9.3k stars 941 forks source link

Add featureIndex & subIndex to coordEach/coordReduce callback @turf/meta #867

Closed DenisCarriere closed 7 years ago

DenisCarriere commented 7 years ago

Add featureIndex & subIndex callback params

Most of the @turf/meta callback modules all have a way to retrace back to the original Feature in the FeatureCollection using the index or subIndex callback parameters.

To-Do

Examples (Current State)

const features = turf.featureCollection([
    turf.lineString([[0, 0], [1, 1]]), // index 0
    turf.multiPoint([[4, 4], [0, 0]]), // index 1 & subIndex 0, 1
    turf.point([3, 3]),                // index 2
    turf.lineString([[2, 2], [1, 1]])  // index 3
]);

turf.featureEach(features, (feature, featureIndex) => {
    // featureIndex => 0, 1, 2, 3
})

turf.geomEach(features, (geometry, featureIndex) => {
    // featureIndex => 0, 1, 2, 3
})

turf.flattenEach(features, (feature, featureIndex, subIndex) => {
    // featureIndex => 0, 1, 1, 2, 3   (1 = MultiPoint)
    // subIndex     => 0, 0, 1, 0, 0
})

turf.coordEach(features, (coord, coordIndex) => {
    // coordIndex => 0, 1, 2, 3, 4, 5, 6
    // No way to reference back to original FeatureCollection
})

Proposed

turf.coordEach(geojson, (coord, coordIndex, featureIndex, subIndex) => {
    // coordIndex   => 0, 1, 2, 3, 4, 5, 6
    // featureIndex => 0, 0, 1, 1, 2, 3, 3
    // subIndex     => 0, 1, 0, 1, 0, 0, 1
})
rowanwins commented 7 years ago

you're my hero @DenisCarriere !

DenisCarriere commented 7 years ago

🤣 Thanks @rowanwins It's a big team effort! ❤️

DenisCarriere commented 7 years ago

@rowanwins Just pushed a PR for this, have a look and let me what you think about it.

DenisCarriere commented 7 years ago

Implemented PR https://github.com/Turfjs/turf/issues/872