meta

coordAll

npm install @turf/meta

接收任意的 GeoJSON 对象(包括要素集),以二维数组的形式返回它们的坐标

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象

返回

Array

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. var coords = turf.coordAll(features);
  6. //= [[26, 37], [36, 53]]
  7. var coords = turf.coordAll(turf.point([26, 37], { foo: "bar" }));
  8. //= [[26, 37]] 虽然只有一个要素,还是以二维数组的形式返回

coordEach

npm install @turf/meta

遍历接收的 GeoJSON 对象,功能类似 Array.forEach()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 currentCoord、coordIndex、featureIndex、multiFeatureIndex
excludeWrapCoord Boolean 是否包含最后一个坐标对(针对闭环线、面要素等坐标是首位闭合的情况),默认为 false

返回

undefined

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.coordEach(features, function (
  6. currentCoord,
  7. coordIndex,
  8. featureIndex,
  9. multiFeatureIndex,
  10. geometryIndex
  11. ) {
  12. //=currentCoord
  13. //=coordIndex
  14. //=featureIndex
  15. //=multiFeatureIndex
  16. //=geometryIndex
  17. });

coordReduce

npm install @turf/meta

接收任意 GeoJSON 对象(包括要素集),遍历累加操作。功能类似 Array.reduce()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 previousValue、currentCoord、coordIndex、featureIndex、multiFeatureIndex、geometryIndex
initial (*) 初始值
excludeWrapCoord Boolean 是否包含最后一个坐标对(针对闭环线、面要素等坐标是首位闭合的情况),默认为 false

返回

*

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.coordReduce(features, function (
  6. previousValue,
  7. currentCoord,
  8. coordIndex,
  9. featureIndex,
  10. multiFeatureIndex,
  11. geometryIndex
  12. ) {
  13. //=previousValue
  14. //=currentCoord
  15. //=coordIndex
  16. //=featureIndex
  17. //=multiFeatureIndex
  18. //=geometryIndex
  19. return currentCoord;
  20. });

featureEach

npm install @turf/meta

遍历接收的 GeoJSON 对象,功能类似 Array.forEach()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 currentFeature、featureIndex

返回

undefined

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.featureEach(features, function (currentFeature, featureIndex) {
  6. //=currentFeature
  7. //=featureIndex
  8. });

featureReduce

npm install @turf/meta

接收任意 GeoJSON 对象(包括要素集),遍历累加操作。功能类似 Array.reduce()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 previousValue、currentFeature、featureIndex
initialValue (*) 初始值

返回

*

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.featureReduce(features, function (previousValue, currentFeature, featureIndex) {
  6. //=previousValue
  7. //=currentFeature
  8. //=featureIndex
  9. return currentFeature;
  10. });

flattenEach

npm install @turf/meta

遍历接收的任意 GeoJSON 对象(包括要素集)。功能类似 Array.forEach()

值得注意的是,如果是多要素集,做扁平处理再遍历,功能类似 Array.flat()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 currentFeature、featureIndex、multiFeatureIndex

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.multiPoint(
  4. [
  5. [40, 30],
  6. [36, 53]
  7. ],
  8. { hello: "world" }
  9. )
  10. ]);
  11. turf.flattenEach(features, function (currentFeature, featureIndex, multiFeatureIndex) {
  12. //=currentFeature
  13. //=featureIndex
  14. //=multiFeatureIndex
  15. console.log(currentFeature);
  16. });
  17. /* 输出三个点要素
  18. {
  19. type: "Feature",
  20. geometry: {
  21. coordinates: [26, 37],
  22. type: "Point"
  23. },
  24. properties: { foo: "bar" }
  25. }
  26. {
  27. type: "Feature",
  28. geometry: {
  29. coordinates: [40, 30],
  30. type: "Point"
  31. },
  32. properties: { hello: "world" }
  33. }
  34. {
  35. type: "Feature",
  36. geometry: {
  37. coordinates: [36, 53],
  38. type: "Point"
  39. },
  40. properties: { hello: "world" }
  41. }
  42. */

flattenReduce

npm install @turf/meta

接收任意 GeoJSON 对象(包括要素集),遍历累加操作。功能类似 Array.reduce()

值得注意的是,如果是多要素集,做扁平处理再遍历,功能类似 Array.flat()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 previousValue、currentFeature、featureIndex、multiFeatureIndex
initialValue (*) 初始值

返回

*

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.multiPoint(
  4. [
  5. [40, 30],
  6. [36, 53]
  7. ],
  8. { hello: "world" }
  9. )
  10. ]);
  11. turf.flattenReduce(features, function (
  12. previousValue,
  13. currentFeature,
  14. featureIndex,
  15. multiFeatureIndex
  16. ) {
  17. //=previousValue
  18. //=currentFeature
  19. //=featureIndex
  20. //=multiFeatureIndex
  21. return currentFeature;
  22. });

getCoord

npm install @turf/invariant

接收类型为 Point 的 GeoJSON,返回该要素的 coordinates 经纬度坐标

值得注意的是,如果入参是数组,只能是一维数组,否则抛出异常错误

参数

入参 类型 描述
coord Array\ Geometry<Point>\ Feature<Point> GeoJSON 或一维数组

返回

Array - coordinates

范例

  1. var pt = turf.point([10, 10]);
  2. var coord = turf.getCoord(pt);
  3. //= [10, 10]
  4. var pt2 = turf.getCoord([1, 2, 3, 4, 5]); // [1,2,3,4,5]

getCoords

npm install @turf/invariant

接收任意类型的 GeoJSON 或 Geometry,返回该要素的 coordinates 经纬度坐标

参数

入参 类型 描述
coords Array\ Feature\ Geometry 任意 GeoJSON 对象

返回

Array - coordinates

范例

  1. var poly = turf.polygon([
  2. [
  3. [119.32, -8.7],
  4. [119.55, -8.69],
  5. [119.51, -8.54],
  6. [119.32, -8.7]
  7. ]
  8. ]);
  9. var coords = turf.getCoords(poly);
  10. //= [[[119.32, -8.7], [119.55, -8.69], [119.51, -8.54], [119.32, -8.7]]]
  11. var coords2 = turf.getCoords({
  12. // 也可以是Geometry
  13. type: "LineString",
  14. coordinates: [
  15. [1, 2],
  16. [3, 4]
  17. ]
  18. }); // [[1,2], [3,4]]

getGeom

npm install @turf/invariant

接收任意类型的 GeoJSON,返回该要素的 Geometry

值得注意的是,如果不是 GeoJSON,该方法返回入参本身的值

参数

入参 类型 描述
geojson Feature\ Geometry 任意 GeoJSON 对象

返回

Geometry|AnyValue

范例

  1. var point = {
  2. type: "Feature",
  3. properties: {},
  4. geometry: {
  5. type: "Point",
  6. coordinates: [110, 40]
  7. }
  8. };
  9. var geom = turf.getGeom(point);
  10. //={"type": "Point", "coordinates": [110, 40]}
  11. turf.getGeom("wrong"); // 'wrong'

getType

npm install @turf/invariant

接收任意类型的 GeoJSON,返回该要素的类型

值得注意的是,如果是要素,返回类型优先是 Geometry 的 type,如果是要素集,返回类型优先是要素集的 type

参数

入参 类型 描述
geojson GeoJSON 任意 GeoJSON 对象
name String 在报错消息中展示的变量名,目前暂未使用

返回

string - GeoJSON type

范例

  1. var point = {
  2. type: "Feature",
  3. properties: {},
  4. geometry: {
  5. type: "Point",
  6. coordinates: [110, 40]
  7. }
  8. };
  9. var geom = turf.getType(point);
  10. //="Point"
  11. turf.getType({
  12. type: "FeatureCollection",
  13. faetures: [
  14. {
  15. type: "Feature",
  16. geometry: {
  17. type: "Point",
  18. coordinates: [1, 2]
  19. }
  20. },
  21. {
  22. type: "Feature",
  23. geometry: {
  24. type: "LineString",
  25. coordinates: [
  26. [1, 2],
  27. [3, 4]
  28. ]
  29. }
  30. }
  31. ]
  32. }); // "FeatureCollection"

geomEach

npm install @turf/meta

接收任意类型的 GeoJSON,遍历它的每个 Geometry。功能类似 Array.forEach()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 currentGeometry、featureIndex、featureProperties、featureBBox、featureId

返回

undefined

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.geomEach(features, function (
  6. currentGeometry,
  7. featureIndex,
  8. featureProperties,
  9. featureBBox,
  10. featureId
  11. ) {
  12. //=currentGeometry
  13. //=featureIndex
  14. //=featureProperties
  15. //=featureBBox
  16. //=featureId
  17. });

geomReduce

npm install @turf/meta

接收任意 GeoJSON 对象(包括要素集),遍历它的每个 Geometry 并累加操作。功能类似 Array.reduce()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 previousValue、currentGeometry、featureIndex、featureProperties、featureBBox、featureId
initialValue (*) 初始值

返回

* - reduce 产生的值

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.geomReduce(features, function (
  6. previousValue,
  7. currentGeometry,
  8. featureIndex,
  9. featureProperties,
  10. featureBBox,
  11. featureId
  12. ) {
  13. //=previousValue
  14. //=currentGeometry
  15. //=featureIndex
  16. //=featureProperties
  17. //=featureBBox
  18. //=featureId
  19. return currentGeometry;
  20. });

propEach

npm install @turf/meta

接收任意类型的 GeoJSON,遍历它的每个要素的 properties。功能类似 Array.forEach()

参数

入参 类型 描述
geojson FeatureCollection\ Feature 任意 GeoJSON 对象
callback Function 回调,参数依次是 currentProperties、featureIndex

返回

undefined

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.propEach(features, function (currentProperties, featureIndex) {
  6. //=currentProperties
  7. //=featureIndex
  8. });

propReduce

npm install @turf/meta

接收任意类型的 GeoJSON,遍历它的每个要素的 properties 并累加操作。功能类似 Array.reduce()

参数

入参 类型 描述
geojson FeatureCollection\ Feature 任意 GeoJSON 对象
callback Function 回调,参数依次是 previousValue、currentProperties、featureIndex
initialValue (*) 初始值

返回

* - reduce 产生的值

范例

  1. var features = turf.featureCollection([
  2. turf.point([26, 37], { foo: "bar" }),
  3. turf.point([36, 53], { hello: "world" })
  4. ]);
  5. turf.propReduce(features, function (previousValue, currentProperties, featureIndex) {
  6. //=previousValue
  7. console.log(currentProperties);
  8. //=featureIndex
  9. return currentProperties;
  10. });
  11. /*
  12. { foo: "bar" }
  13. { hello: "world" }
  14. */

segmentEach

npm install @turf/meta

接收任意类型的 GeoJSON,遍历它的 2-vertex 线段。功能类似 Array.forEach()

值得注意的是,(Multi)Point 的要素不包含线段,不进行操作

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 currentSegment、featureIndex、multiFeatureIndex、geometryIndex、segmentIndex

返回

undefined

范例

  1. var polygon = turf.polygon([
  2. [
  3. [-50, 5],
  4. [-40, -10],
  5. [-50, -10],
  6. [-40, 5],
  7. [-50, 5]
  8. ]
  9. ]);
  10. // Iterate over GeoJSON by 2-vertex segments
  11. turf.segmentEach(polygon, function (
  12. currentSegment,
  13. featureIndex,
  14. multiFeatureIndex,
  15. geometryIndex,
  16. segmentIndex
  17. ) {
  18. //=currentSegment
  19. //=featureIndex
  20. //=multiFeatureIndex
  21. //=geometryIndex
  22. //=segmentIndex
  23. });
  24. // Calculate the total number of segments
  25. var total = 0;
  26. turf.segmentEach(polygon, function () {
  27. total++;
  28. });

segmentReduce

npm install @turf/meta

接收任意类型的 GeoJSON,遍历它的 2-vertex 线段并累加操作。功能类似 Array.reduce()

参数

入参 类型 描述
geojson FeatureCollection\ Feature\ Geometry 任意 GeoJSON 对象
callback Function 回调,参数依次是 previousValue、currentProperties、featureIndex
initialValue (*) 初始值

返回

undefined

范例

  1. var polygon = turf.polygon([
  2. [
  3. [-50, 5],
  4. [-40, -10],
  5. [-50, -10],
  6. [-40, 5],
  7. [-50, 5]
  8. ]
  9. ]);
  10. // Iterate over GeoJSON by 2-vertex segments
  11. turf.segmentReduce(polygon, function (
  12. previousSegment,
  13. currentSegment,
  14. featureIndex,
  15. multiFeatureIndex,
  16. geometryIndex,
  17. segmentIndex
  18. ) {
  19. //= previousSegment
  20. //= currentSegment
  21. //= featureIndex
  22. //= multiFeatureIndex
  23. //= geometryIndex
  24. //= segmentInex
  25. return currentSegment;
  26. });
  27. // Calculate the total number of segments
  28. var initialValue = 0;
  29. var total = turf.segmentReduce(
  30. polygon,
  31. function (previousValue) {
  32. previousValue++;
  33. return previousValue;
  34. },
  35. initialValue
  36. );

getCluster

npm install @turf/clusters

接收要素集FeatureCollection,过滤指定属性的要素并返回要素集

参数

入参 类型 描述
geojson FeatureCollection GeoJSON 要素集
filter (*) 初始值

返回

FeatureCollection - 过滤后的要素集

范例

  1. var geojson = turf.featureCollection([
  2. turf.point([0, 0], { "marker-symbol": "circle" }),
  3. turf.point([2, 4], { "marker-symbol": "star" }),
  4. turf.point([3, 6], { "marker-symbol": "star" }),
  5. turf.point([5, 1], { "marker-symbol": "square" }),
  6. turf.point([4, 2], { "marker-symbol": "circle" })
  7. ]);
  8. // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  9. var clustered = turf.clustersKmeans(geojson);
  10. // Retrieve first cluster (0)
  11. var cluster = turf.getCluster(clustered, { cluster: 0 });
  12. //= cluster
  13. // Retrieve cluster based on custom properties
  14. turf.getCluster(clustered, { "marker-symbol": "circle" }).length;
  15. //= 2
  16. turf.getCluster(clustered, { "marker-symbol": "square" }).length;
  17. //= 1

clusterEach

npm install @turf/clusters

接收要素集FeatureCollection,遍历通过指定属性过滤后的要素集集群

参数

入参 类型 描述
geojson FeatureCollection GeoJSON 要素集
property String\ Number 用于过滤、区分要素集的属性值
callback Function 回调,参数依次是 previousValue、currentProperties、featureIndex

返回

undefined

范例

  1. var geojson = turf.featureCollection([
  2. turf.point([0, 0]),
  3. turf.point([2, 4]),
  4. turf.point([3, 6]),
  5. turf.point([5, 1]),
  6. turf.point([4, 2])
  7. ]);
  8. // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  9. var clustered = turf.clustersKmeans(geojson); // cluster为0有1个要素,cluster为1有4个要素
  10. // Iterate over each cluster
  11. turf.clusterEach(clustered, "cluster", function (cluster, clusterValue, currentIndex) {
  12. //= cluster
  13. //= clusterValue
  14. //= currentIndex
  15. console.log("cluster: ", cluster, clusterValue);
  16. });
  17. /*
  18. cluster: { type: "FeatureCollection", features: Array(1) }, "0"
  19. cluster: { type: "FeatureCollection", features: Array(4) }, "1"
  20. */
  21. // Calculate the total number of clusters
  22. var total = 0;
  23. turf.clusterEach(clustered, "cluster", function () {
  24. total++;
  25. });
  26. // Create an Array of all the values retrieved from the 'cluster' property
  27. var values = [];
  28. turf.clusterEach(clustered, "cluster", function (cluster, clusterValue) {
  29. values.push(clusterValue);
  30. });

clusterReduce

npm install @turf/meta

接收要素集FeatureCollection,遍历通过指定属性过滤后的要素集集群并累加操作

参数

入参 类型 描述
geojson FeatureCollection GeoJSON 要素集
property String\ Number 用于过滤、区分要素集的属性值
callback Function 回调,参数依次是 previousValue、cluster、clusterValue、currentIndex
initialValue (*) 初始值

返回

* - reduce 产生的值

范例

  1. var geojson = turf.featureCollection([
  2. turf.point([0, 0]),
  3. turf.point([2, 4]),
  4. turf.point([3, 6]),
  5. turf.point([5, 1]),
  6. turf.point([4, 2])
  7. ]);
  8. // Create a cluster using K-Means (adds `cluster` to GeoJSON properties)
  9. var clustered = turf.clustersKmeans(geojson);
  10. // Iterate over each cluster and perform a calculation
  11. var initialValue = 0;
  12. turf.clusterReduce(
  13. clustered,
  14. "cluster",
  15. function (previousValue, cluster, clusterValue, currentIndex) {
  16. //=previousValue
  17. //=cluster
  18. //=clusterValue
  19. //=currentIndex
  20. return previousValue++;
  21. },
  22. initialValue
  23. );
  24. // Calculate the total number of clusters
  25. var total = turf.clusterReduce(
  26. clustered,
  27. "cluster",
  28. function (previousValue) {
  29. return previousValue++;
  30. },
  31. 0
  32. );
  33. // Create an Array of all the values retrieved from the 'cluster' property
  34. var values = turf.clusterReduce(
  35. clustered,
  36. "cluster",
  37. function (previousValue, cluster, clusterValue) {
  38. return previousValue.concat(clusterValue);
  39. },
  40. []
  41. );