index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var helpers_1 = require("@turf/helpers");
  4. var invariant_1 = require("@turf/invariant");
  5. /**
  6. * Converts a {@link Polygon} to {@link LineString|(Multi)LineString} or {@link MultiPolygon} to a
  7. * {@link FeatureCollection} of {@link LineString|(Multi)LineString}.
  8. *
  9. * @name polygonToLine
  10. * @param {Feature<Polygon|MultiPolygon>} poly Feature to convert
  11. * @param {Object} [options={}] Optional parameters
  12. * @param {Object} [options.properties={}] translates GeoJSON properties to Feature
  13. * @returns {FeatureCollection|Feature<LineString|MultiLinestring>} converted (Multi)Polygon to (Multi)LineString
  14. * @example
  15. * var poly = turf.polygon([[[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]]);
  16. *
  17. * var line = turf.polygonToLine(poly);
  18. *
  19. * //addToMap
  20. * var addToMap = [line];
  21. */
  22. function default_1(poly, options) {
  23. if (options === void 0) { options = {}; }
  24. var geom = invariant_1.getGeom(poly);
  25. if (!options.properties && poly.type === "Feature") {
  26. options.properties = poly.properties;
  27. }
  28. switch (geom.type) {
  29. case "Polygon":
  30. return polygonToLine(geom, options);
  31. case "MultiPolygon":
  32. return multiPolygonToLine(geom, options);
  33. default:
  34. throw new Error("invalid poly");
  35. }
  36. }
  37. exports.default = default_1;
  38. /**
  39. * @private
  40. */
  41. function polygonToLine(poly, options) {
  42. if (options === void 0) { options = {}; }
  43. var geom = invariant_1.getGeom(poly);
  44. var coords = geom.coordinates;
  45. var properties = options.properties
  46. ? options.properties
  47. : poly.type === "Feature"
  48. ? poly.properties
  49. : {};
  50. return coordsToLine(coords, properties);
  51. }
  52. exports.polygonToLine = polygonToLine;
  53. /**
  54. * @private
  55. */
  56. function multiPolygonToLine(multiPoly, options) {
  57. if (options === void 0) { options = {}; }
  58. var geom = invariant_1.getGeom(multiPoly);
  59. var coords = geom.coordinates;
  60. var properties = options.properties
  61. ? options.properties
  62. : multiPoly.type === "Feature"
  63. ? multiPoly.properties
  64. : {};
  65. var lines = [];
  66. coords.forEach(function (coord) {
  67. lines.push(coordsToLine(coord, properties));
  68. });
  69. return helpers_1.featureCollection(lines);
  70. }
  71. exports.multiPolygonToLine = multiPolygonToLine;
  72. /**
  73. * @private
  74. */
  75. function coordsToLine(coords, properties) {
  76. if (coords.length > 1) {
  77. return helpers_1.multiLineString(coords, properties);
  78. }
  79. return helpers_1.lineString(coords[0], properties);
  80. }
  81. exports.coordsToLine = coordsToLine;