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

sample

sample 方法随机从给定的 feature 集合中返回一个或多个 feature 实例

参数

  • fc (FeatureCollection): 需要从中选择元素的 feature 集合
  • num (number): 选择要返回的元素的数量。默认为 1。

返回

根据指定的数量,返回一个或多个随机 feature 实例。

示例

import { featureCollection, point } from '@turf/turf';
import { sample } from '@turf/turf';

const fc = featureCollection([
  point([10.195312, 43.755225]),
  point([10.404052, 43.842451]),
  point([10.579833, 43.659924]),
  point([10.360107, 43.516688]),
  point([10.14038, 43.588348]),
  point([10.195312, 43.755225])
]);

// 样本数量为一
const randomFeature = sample(fc);
console.log(randomFeature);

// 样本数量为两
const randomFeatures = sample(fc, 2);
console.log(randomFeatures);

参见