collectionOf
是一个 Turf 的断言函数,用于验证传递的参数是否为一个 Feature Collection,并且其中的每个 Feature 都满足特定的测试条件。
collectionOf(featureCollection, validator)
featureCollection
(FeatureCollection): 待测试的 Feature Collection。validator
(Function): 对 Feature 的元素进行测试的函数。true
,否则返回 false
。const { collectionOf } = require('@turf/helpers');
const { booleanContains } = require('@turf/turf');
const fc = collectionOf({
type: 'FeatureCollection',
features: [
{ type: 'Feature', geometry: {
type: 'Point', coordinates: [0, 0]
}
},
{ type: 'Feature', geometry: {
type: 'Polygon', coordinates: [
[[-2, -2], [2, -2], [2, 2], [-2, 2], [-2, -2]]
]
}
}
]
})
console.log(fc); // true
const fc2 = collectionOf({
type: 'FeatureCollection',
features: [
{ type: 'Feature', geometry: {
type: 'Point', coordinates: [0, 0]
}
},
{ type: 'Feature', geometry: {
type: 'Polygon', coordinates: [
[[-2, -2], [2, -2], [2, 2], [-2, 2], [-2, -2]]
]
}
}
]
}, (feature) => booleanContains(feature, {
type: 'Feature', geometry: {
type: 'Point', coordinates: [1, 1]
}
}));
console.log(fc2); // false
validator
函数必须返回一个 Boolean 类型的值,用于决定 Feature 是否满足测试条件。