index.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. var meta_1 = require("@turf/meta");
  6. /**
  7. * Creates a {@link FeatureCollection} of 2-vertex {@link LineString} segments from a
  8. * {@link LineString|(Multi)LineString} or {@link Polygon|(Multi)Polygon}.
  9. *
  10. * @name lineSegment
  11. * @param {GeoJSON} geojson GeoJSON Polygon or LineString
  12. * @returns {FeatureCollection<LineString>} 2-vertex line segments
  13. * @example
  14. * var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]);
  15. * var segments = turf.lineSegment(polygon);
  16. *
  17. * //addToMap
  18. * var addToMap = [polygon, segments]
  19. */
  20. function lineSegment(geojson) {
  21. if (!geojson) {
  22. throw new Error("geojson is required");
  23. }
  24. var results = [];
  25. meta_1.flattenEach(geojson, function (feature) {
  26. lineSegmentFeature(feature, results);
  27. });
  28. return helpers_1.featureCollection(results);
  29. }
  30. /**
  31. * Line Segment
  32. *
  33. * @private
  34. * @param {Feature<LineString|Polygon>} geojson Line or polygon feature
  35. * @param {Array} results push to results
  36. * @returns {void}
  37. */
  38. function lineSegmentFeature(geojson, results) {
  39. var coords = [];
  40. var geometry = geojson.geometry;
  41. if (geometry !== null) {
  42. switch (geometry.type) {
  43. case "Polygon":
  44. coords = invariant_1.getCoords(geometry);
  45. break;
  46. case "LineString":
  47. coords = [invariant_1.getCoords(geometry)];
  48. }
  49. coords.forEach(function (coord) {
  50. var segments = createSegments(coord, geojson.properties);
  51. segments.forEach(function (segment) {
  52. segment.id = results.length;
  53. results.push(segment);
  54. });
  55. });
  56. }
  57. }
  58. /**
  59. * Create Segments from LineString coordinates
  60. *
  61. * @private
  62. * @param {Array<Array<number>>} coords LineString coordinates
  63. * @param {*} properties GeoJSON properties
  64. * @returns {Array<Feature<LineString>>} line segments
  65. */
  66. function createSegments(coords, properties) {
  67. var segments = [];
  68. coords.reduce(function (previousCoords, currentCoords) {
  69. var segment = helpers_1.lineString([previousCoords, currentCoords], properties);
  70. segment.bbox = bbox(previousCoords, currentCoords);
  71. segments.push(segment);
  72. return currentCoords;
  73. });
  74. return segments;
  75. }
  76. /**
  77. * Create BBox between two coordinates (faster than @turf/bbox)
  78. *
  79. * @private
  80. * @param {Array<number>} coords1 Point coordinate
  81. * @param {Array<number>} coords2 Point coordinate
  82. * @returns {BBox} [west, south, east, north]
  83. */
  84. function bbox(coords1, coords2) {
  85. var x1 = coords1[0];
  86. var y1 = coords1[1];
  87. var x2 = coords2[0];
  88. var y2 = coords2[1];
  89. var west = x1 < x2 ? x1 : x2;
  90. var south = y1 < y2 ? y1 : y2;
  91. var east = x1 > x2 ? x1 : x2;
  92. var north = y1 > y2 ? y1 : y2;
  93. return [west, south, east, north];
  94. }
  95. exports.default = lineSegment;