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

geomEach

geomEach 函数可以对 geom 几何对象中的所有子对象进行迭代,并对每个子对象执行指定的回调函数。

参数

  • geom (Geometry): 执行迭代的目标 geom 几何对象。
  • callback (Function(feature, index)): 执行的回调函数,接收两个参数:
    • feature (Feature): 每个子对象的 feature。
    • index (Number): 每个子对象在 geom 中的索引。

返回值

  • 无返回值,回调函数执行完毕后结束函数。

示例

import {geomEach} from "@turf/turf";

const point = {
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [0, 0]
  },
  "properties": {
    "name": "center"
  }
};

const line = {
  "type": "Feature",
  "geometry": {
    "type": "LineString",
    "coordinates": [
      [1, 1],
      [2, 2],
      [3, 3]
    ]
  },
  "properties": {
    "name": "route"
  }
};

const polygon = {
  "type": "Feature",
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [0, 0],
        [0, 10],
        [10, 10],
        [10, 0],
        [0, 0]
      ]
    ]
  },
  "properties": {
    "name": "boundary"
  }
};

const geomCollection = {
  "type": "FeatureCollection",
  "features": [point, line, polygon]
};

geomEach(geomCollection, (feature, index) => {
  console.log(`Feature ${index} has geometry type ${feature.geometry.type}`);
});

上述代码将会遍历 geomCollection 中的所有 feature,分别输出它们的几何类型。

另请参阅

  • featureEach: 遍历 feature 集合中的每个 feature。
  • coordEach: 遍历几何对象中的每个坐标点。