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,分别输出它们的几何类型。