index.js 2.8 KB

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