Skip to content

描述 ​

归约任何 GeoJSON 对象中的 2 顶点线段,类似于 Array.reduce()。 (Multi)Point 几何体不包含线段,因此在操作期间被忽略。

参数 ​

名称类型描述
geojsonFeatureCollection | Feature | Geometry任何 GeoJSON
callbacksegmentReduceCallback接收 (previousValue, currentSegment, currentIndex) 的方法
initialValue?Reducer作为回调函数第一个参数首次调用的值。

返回值 ​

Reducer

示例 ​

javascript
var polygon = turf.polygon([
  [
    [-50, 5],
    [-40, -10],
    [-50, -10],
    [-40, 5],
    [-50, 5],
  ],
]);

// 按 2 顶点线段遍历 GeoJSON
turf.segmentReduce(
  polygon,
  function (
    previousSegment,
    currentSegment,
    featureIndex,
    multiFeatureIndex,
    geometryIndex,
    segmentIndex,
  ) {
    //= previousSegment
    //= currentSegment
    //= featureIndex
    //= multiFeatureIndex
    //= geometryIndex
    //= segmentIndex
    return currentSegment;
  },
);

// 计算线段总数
var initialValue = 0;
var total = turf.segmentReduce(
  polygon,
  function (previousValue) {
    previousValue++;
    return previousValue;
  },
  initialValue,
);

安装 ​

javascript
$ npm install @turf/meta

import { segmentReduce } from "@turf/meta";
const result = segmentReduce(...);
javascript
$ npm install @turf/turf

import * as turf from "@turf/turf";
const result = turf.segmentReduce(...);