index.cjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _cleancoords = require('@turf/clean-coords');
  3. var _clone = require('@turf/clone');
  4. var _meta = require('@turf/meta');
  5. var _helpers = require('@turf/helpers');
  6. // lib/simplify.js
  7. function getSqDist(p1, p2) {
  8. var dx = p1[0] - p2[0], dy = p1[1] - p2[1];
  9. return dx * dx + dy * dy;
  10. }
  11. function getSqSegDist(p, p1, p2) {
  12. var x = p1[0], y = p1[1], dx = p2[0] - x, dy = p2[1] - y;
  13. if (dx !== 0 || dy !== 0) {
  14. var t = ((p[0] - x) * dx + (p[1] - y) * dy) / (dx * dx + dy * dy);
  15. if (t > 1) {
  16. x = p2[0];
  17. y = p2[1];
  18. } else if (t > 0) {
  19. x += dx * t;
  20. y += dy * t;
  21. }
  22. }
  23. dx = p[0] - x;
  24. dy = p[1] - y;
  25. return dx * dx + dy * dy;
  26. }
  27. function simplifyRadialDist(points, sqTolerance) {
  28. var prevPoint = points[0], newPoints = [prevPoint], point;
  29. for (var i = 1, len = points.length; i < len; i++) {
  30. point = points[i];
  31. if (getSqDist(point, prevPoint) > sqTolerance) {
  32. newPoints.push(point);
  33. prevPoint = point;
  34. }
  35. }
  36. if (prevPoint !== point) newPoints.push(point);
  37. return newPoints;
  38. }
  39. function simplifyDPStep(points, first, last, sqTolerance, simplified) {
  40. var maxSqDist = sqTolerance, index;
  41. for (var i = first + 1; i < last; i++) {
  42. var sqDist = getSqSegDist(points[i], points[first], points[last]);
  43. if (sqDist > maxSqDist) {
  44. index = i;
  45. maxSqDist = sqDist;
  46. }
  47. }
  48. if (maxSqDist > sqTolerance) {
  49. if (index - first > 1)
  50. simplifyDPStep(points, first, index, sqTolerance, simplified);
  51. simplified.push(points[index]);
  52. if (last - index > 1)
  53. simplifyDPStep(points, index, last, sqTolerance, simplified);
  54. }
  55. }
  56. function simplifyDouglasPeucker(points, sqTolerance) {
  57. var last = points.length - 1;
  58. var simplified = [points[0]];
  59. simplifyDPStep(points, 0, last, sqTolerance, simplified);
  60. simplified.push(points[last]);
  61. return simplified;
  62. }
  63. function simplify(points, tolerance, highestQuality) {
  64. if (points.length <= 2) return points;
  65. var sqTolerance = tolerance !== void 0 ? tolerance * tolerance : 1;
  66. points = highestQuality ? points : simplifyRadialDist(points, sqTolerance);
  67. points = simplifyDouglasPeucker(points, sqTolerance);
  68. return points;
  69. }
  70. // index.ts
  71. function simplify2(geojson, options = {}) {
  72. var _a, _b, _c;
  73. options = options != null ? options : {};
  74. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  75. const tolerance = (_a = options.tolerance) != null ? _a : 1;
  76. const highQuality = (_b = options.highQuality) != null ? _b : false;
  77. const mutate = (_c = options.mutate) != null ? _c : false;
  78. if (!geojson) throw new Error("geojson is required");
  79. if (tolerance && tolerance < 0) throw new Error("invalid tolerance");
  80. if (mutate !== true) geojson = _clone.clone.call(void 0, geojson);
  81. _meta.geomEach.call(void 0, geojson, function(geom) {
  82. simplifyGeom(geom, tolerance, highQuality);
  83. });
  84. return geojson;
  85. }
  86. function simplifyGeom(geometry, tolerance, highQuality) {
  87. const type = geometry.type;
  88. if (type === "Point" || type === "MultiPoint") return geometry;
  89. _cleancoords.cleanCoords.call(void 0, geometry, { mutate: true });
  90. if (type !== "GeometryCollection") {
  91. switch (type) {
  92. case "LineString":
  93. geometry.coordinates = simplify(
  94. geometry.coordinates,
  95. tolerance,
  96. highQuality
  97. );
  98. break;
  99. case "MultiLineString":
  100. geometry.coordinates = geometry.coordinates.map(
  101. (lines) => simplify(lines, tolerance, highQuality)
  102. );
  103. break;
  104. case "Polygon":
  105. geometry.coordinates = simplifyPolygon(
  106. geometry.coordinates,
  107. tolerance,
  108. highQuality
  109. );
  110. break;
  111. case "MultiPolygon":
  112. geometry.coordinates = geometry.coordinates.map(
  113. (rings) => simplifyPolygon(rings, tolerance, highQuality)
  114. );
  115. }
  116. }
  117. return geometry;
  118. }
  119. function simplifyPolygon(coordinates, tolerance, highQuality) {
  120. return coordinates.map(function(ring) {
  121. if (ring.length < 4) {
  122. throw new Error("invalid polygon");
  123. }
  124. let ringTolerance = tolerance;
  125. let simpleRing = simplify(ring, ringTolerance, highQuality);
  126. while (!checkValidity(simpleRing)) {
  127. ringTolerance -= ringTolerance * 0.01;
  128. simpleRing = simplify(ring, ringTolerance, highQuality);
  129. }
  130. if (simpleRing[simpleRing.length - 1][0] !== simpleRing[0][0] || simpleRing[simpleRing.length - 1][1] !== simpleRing[0][1]) {
  131. simpleRing.push(simpleRing[0]);
  132. }
  133. return simpleRing;
  134. });
  135. }
  136. function checkValidity(ring) {
  137. if (ring.length < 3) return false;
  138. return !(ring.length === 3 && ring[2][0] === ring[0][0] && ring[2][1] === ring[0][1]);
  139. }
  140. var turf_simplify_default = simplify2;
  141. exports.default = turf_simplify_default; exports.simplify = simplify2;
  142. //# sourceMappingURL=index.cjs.map