index.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { feature, featureCollection, } from "@turf/helpers";
  2. import { featureEach } from "@turf/meta";
  3. /**
  4. * Combines a {@link FeatureCollection} of {@link Point}, {@link LineString}, or {@link Polygon} features
  5. * into {@link MultiPoint}, {@link MultiLineString}, or {@link MultiPolygon} features.
  6. *
  7. * @name combine
  8. * @param {FeatureCollection<Point|LineString|Polygon>} fc a FeatureCollection of any type
  9. * @returns {FeatureCollection<MultiPoint|MultiLineString|MultiPolygon>} a FeatureCollection of corresponding type to input
  10. * @example
  11. * var fc = turf.featureCollection([
  12. * turf.point([19.026432, 47.49134]),
  13. * turf.point([19.074497, 47.509548])
  14. * ]);
  15. *
  16. * var combined = turf.combine(fc);
  17. *
  18. * //addToMap
  19. * var addToMap = [combined]
  20. */
  21. function combine(fc) {
  22. var groups = {
  23. MultiPoint: {
  24. coordinates: [],
  25. properties: [],
  26. },
  27. MultiLineString: {
  28. coordinates: [],
  29. properties: [],
  30. },
  31. MultiPolygon: {
  32. coordinates: [],
  33. properties: [],
  34. },
  35. };
  36. featureEach(fc, function (feature) {
  37. var _a, _b, _c;
  38. var _d;
  39. switch ((_d = feature.geometry) === null || _d === void 0 ? void 0 : _d.type) {
  40. case "Point":
  41. groups.MultiPoint.coordinates.push(feature.geometry.coordinates);
  42. groups.MultiPoint.properties.push(feature.properties);
  43. break;
  44. case "MultiPoint":
  45. (_a = groups.MultiPoint.coordinates).push.apply(_a, feature.geometry.coordinates);
  46. groups.MultiPoint.properties.push(feature.properties);
  47. break;
  48. case "LineString":
  49. groups.MultiLineString.coordinates.push(feature.geometry.coordinates);
  50. groups.MultiLineString.properties.push(feature.properties);
  51. break;
  52. case "MultiLineString":
  53. (_b = groups.MultiLineString.coordinates).push.apply(_b, feature.geometry.coordinates);
  54. groups.MultiLineString.properties.push(feature.properties);
  55. break;
  56. case "Polygon":
  57. groups.MultiPolygon.coordinates.push(feature.geometry.coordinates);
  58. groups.MultiPolygon.properties.push(feature.properties);
  59. break;
  60. case "MultiPolygon":
  61. (_c = groups.MultiPolygon.coordinates).push.apply(_c, feature.geometry.coordinates);
  62. groups.MultiPolygon.properties.push(feature.properties);
  63. break;
  64. default:
  65. break;
  66. }
  67. });
  68. return featureCollection(Object.keys(groups)
  69. .filter(function (key) {
  70. return groups[key].coordinates.length;
  71. })
  72. .sort()
  73. .map(function (key) {
  74. var geometry = { type: key, coordinates: groups[key].coordinates };
  75. var properties = { collectedProperties: groups[key].properties };
  76. return feature(geometry, properties);
  77. }));
  78. }
  79. export default combine;