index.js 3.1 KB

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