coordinate mutation

cleanCoords

npm install @turf/clean-coords

接收任意 GeoJSON,删除冗余坐标并返回

参数

入参 类型 描述
line Geometry\ Feature Geometry 或 Feature
options Object 可配置项

options

属性 类型 默认值 描述
mutate Boolean false 是否返回入参的 GeoJSON

返回

Geometry|Feature

范例

  1. var line = turf.lineString([
  2. [0, 0],
  3. [0, 2],
  4. [0, 5],
  5. [0, 8],
  6. [0, 8],
  7. [0, 10]
  8. ]);
  9. var multiPoint = turf.multiPoint([
  10. [0, 0],
  11. [0, 0],
  12. [2, 2]
  13. ]);
  14. turf.cleanCoords(line).geometry.coordinates;
  15. //= [[0, 0], [0, 10]]
  16. turf.cleanCoords(multiPoint).geometry.coordinates;
  17. //= [[0, 0], [2, 2]]
  18. // 也可以使用 Geometry 对象
  19. var geometry = {
  20. type: "LineString",
  21. coordinates: [
  22. [0, 0],
  23. [0, 0],
  24. [2, 2]
  25. ]
  26. };
  27. var result = turf.cleanCoords(geometry, { mutate: true });
  28. /*
  29. geometry、result均为
  30. {
  31. coordinates: [[0, 0], [2, 2]],
  32. type: "LineString"
  33. }
  34. */

flip

npm install @turf/flip

接收入参要素并将其所有坐标从[x,y]翻转为[y,x]

参数

入参 类型 描述
geojson GeoJSON 一个任意类型的 GeoJSON
options Object 可配置项

options

属性 类型 默认值 描述
mutate Boolean false 是否返回入参的 GeoJSON,为 true 性能能显著提高

返回

GeoJSON

范例

  1. var serbia = turf.point([20.566406, 43.421008]);
  2. var saudiArabia = turf.flip(serbia);
  3. /*
  4. {
  5. type: "Feature",
  6. geometry: {
  7. type: "Point",
  8. coordinates: [43.421008, 20.566406]
  9. },
  10. properties: {}
  11. }
  12. */

rewind

npm install @turf/rewind

接收(Multi)LineString(Multi)Polygon,将坐标的顺序修改为顺时针,内圈顺序修改为逆时针

参数

入参 类型 描述
geojson GeoJSON 类型为 Polygon 的 GeoJSON
options Object 可配置项

options

属性 类型 默认值 描述
reverse Boolean false 启用反向绕组,即坐标组使用 reverse 方法
mutate Boolean false 是否返回入参的 GeoJSON,为 true 性能能显著提高

返回

GeoJSON

范例

  1. var polygon = turf.polygon([
  2. [
  3. [121, -29],
  4. [138, -29],
  5. [138, -18],
  6. [121, -18],
  7. [121, -29]
  8. ]
  9. ]);
  10. var rewind = turf.rewind(polygon);
  11. /*
  12. 本来就是顺时针顺序,所以坐标顺序未发生变化
  13. {
  14. type: "Feature",
  15. geometry: {
  16. type: "Polygon",
  17. coordinates: [
  18. [
  19. [121, -29],
  20. [138, -29],
  21. [138, -18],
  22. [121, -18],
  23. [121, -29]
  24. ]
  25. ]
  26. },
  27. properties: {}
  28. }
  29. */

round

npm install @turf/helpers
值得注意的是,round 是 helpers 模块的一部分

接收入参的数字和精确度数,返回四舍五入后的数字

参数

入参 类型 描述
num Number 需要四舍五入的数字
precision Number 坐标的小数点精确位数,不传则不保留

返回

Number

范例

  1. turf.round(120.4321);
  2. //=120
  3. turf.round(120.4321, 2);
  4. //=120.43

truncate

npm install @turf/truncate

接收入参要素(Feature)或要素集(FeatureCollection),返回精确后的要素

参数

入参 类型 描述
geojson GeoJSON 一个 GeoJSON 或要素集
options Object 可配置项

options

属性 类型 默认值 描述
precision Number 6 坐标的小数点精确位数
coordinates Number 3 最大坐标数(主要用于删除 z 坐标)
mutate Boolean false 是否返回入参的 GeoJSON,为 true 性能能显著提高

返回

GeoJSON

范例

  1. var point = turf.point([70.46923055566859, 58.11088890802906, 1508]);
  2. var options = { precision: 3, coordinates: 2 };
  3. var truncated = turf.truncate(point, options);
  4. //=truncated.geometry.coordinates => [70.469, 58.111]