turf-line-dissolve.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var clone_1 = __importDefault(require("@turf/clone"));
  7. var helpers_1 = require("@turf/helpers");
  8. var invariant_1 = require("@turf/invariant");
  9. var meta_1 = require("@turf/meta");
  10. /**
  11. * Merges all connected (non-forking, non-junctioning) line strings into single lineStrings.
  12. * [LineString] -> LineString|MultiLineString
  13. *
  14. * @param {FeatureCollection<LineString|MultiLineString>} geojson Lines to dissolve
  15. * @param {Object} [options={}] Optional parameters
  16. * @param {boolean} [options.mutate=false] Prevent input mutation
  17. * @returns {Feature<LineString|MultiLineString>} Dissolved lines
  18. */
  19. function lineDissolve(geojson, options) {
  20. if (options === void 0) { options = {}; }
  21. // Optional parameters
  22. options = options || {};
  23. if (!helpers_1.isObject(options)) {
  24. throw new Error("options is invalid");
  25. }
  26. var mutate = options.mutate;
  27. // Validation
  28. if (invariant_1.getType(geojson) !== "FeatureCollection") {
  29. throw new Error("geojson must be a FeatureCollection");
  30. }
  31. if (!geojson.features.length) {
  32. throw new Error("geojson is empty");
  33. }
  34. // Clone geojson to avoid side effects
  35. if (mutate === false || mutate === undefined) {
  36. geojson = clone_1.default(geojson);
  37. }
  38. var result = [];
  39. var lastLine = meta_1.lineReduce(geojson, function (previousLine, currentLine) {
  40. // Attempt to merge this LineString with the other LineStrings, updating
  41. // the reference as it is merged with others and grows.
  42. var merged = mergeLineStrings(previousLine, currentLine);
  43. // Accumulate the merged LineString
  44. if (merged) {
  45. return merged;
  46. // Put the unmerged LineString back into the list
  47. }
  48. else {
  49. result.push(previousLine);
  50. return currentLine;
  51. }
  52. });
  53. // Append the last line
  54. if (lastLine) {
  55. result.push(lastLine);
  56. }
  57. // Return null if no lines were dissolved
  58. if (!result.length) {
  59. return null;
  60. // Return LineString if only 1 line was dissolved
  61. }
  62. else if (result.length === 1) {
  63. return result[0];
  64. // Return MultiLineString if multiple lines were dissolved with gaps
  65. }
  66. else {
  67. return helpers_1.multiLineString(result.map(function (line) {
  68. return line.coordinates;
  69. }));
  70. }
  71. }
  72. // [Number, Number] -> String
  73. function coordId(coord) {
  74. return coord[0].toString() + "," + coord[1].toString();
  75. }
  76. /**
  77. * LineString, LineString -> LineString
  78. *
  79. * @private
  80. * @param {Feature<LineString>} a line1
  81. * @param {Feature<LineString>} b line2
  82. * @returns {Feature<LineString>|null} Merged LineString
  83. */
  84. function mergeLineStrings(a, b) {
  85. var coords1 = a.geometry.coordinates;
  86. var coords2 = b.geometry.coordinates;
  87. var s1 = coordId(coords1[0]);
  88. var e1 = coordId(coords1[coords1.length - 1]);
  89. var s2 = coordId(coords2[0]);
  90. var e2 = coordId(coords2[coords2.length - 1]);
  91. // TODO: handle case where more than one of these is true!
  92. var coords;
  93. if (s1 === e2) {
  94. coords = coords2.concat(coords1.slice(1));
  95. }
  96. else if (s2 === e1) {
  97. coords = coords1.concat(coords2.slice(1));
  98. }
  99. else if (s1 === s2) {
  100. coords = coords1.slice(1).reverse().concat(coords2);
  101. }
  102. else if (e1 === e2) {
  103. coords = coords1.concat(coords2.reverse().slice(1));
  104. }
  105. else {
  106. return null;
  107. }
  108. return helpers_1.lineString(coords);
  109. }
  110. exports.default = lineDissolve;