MEASUREMENT
COORDINATE MUTATION
TRANSFORMATION
FEATURE_CONVERSION
MISC
HELPER
RANDOM
GRIDS
AGGREGATION
META
ASSERTIONS
BOOLEANS
UNIT CONVERSION
DATA
JOINS
CLASSIFICATION

collectionOf

描述

collectionOf 是一个 Turf 的断言函数,用于验证传递的参数是否为一个 Feature Collection,并且其中的每个 Feature 都满足特定的测试条件。

函数定义

collectionOf(featureCollection, validator)

参数

  • featureCollection (FeatureCollection): 待测试的 Feature Collection。
  • validator (Function): 对 Feature 的元素进行测试的函数。

返回值

  • 如果所有 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 是否满足测试条件。