index.cjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _helpers = require('@turf/helpers');
  3. var _invariant = require('@turf/invariant');
  4. // lib/lineclip.ts
  5. function lineclip(points, bbox, result) {
  6. var len = points.length, codeA = bitCode(points[0], bbox), part = [], i, codeB, lastCode;
  7. let a;
  8. let b;
  9. if (!result) result = [];
  10. for (i = 1; i < len; i++) {
  11. a = points[i - 1];
  12. b = points[i];
  13. codeB = lastCode = bitCode(b, bbox);
  14. while (true) {
  15. if (!(codeA | codeB)) {
  16. part.push(a);
  17. if (codeB !== lastCode) {
  18. part.push(b);
  19. if (i < len - 1) {
  20. result.push(part);
  21. part = [];
  22. }
  23. } else if (i === len - 1) {
  24. part.push(b);
  25. }
  26. break;
  27. } else if (codeA & codeB) {
  28. break;
  29. } else if (codeA) {
  30. a = intersect(a, b, codeA, bbox);
  31. codeA = bitCode(a, bbox);
  32. } else {
  33. b = intersect(a, b, codeB, bbox);
  34. codeB = bitCode(b, bbox);
  35. }
  36. }
  37. codeA = lastCode;
  38. }
  39. if (part.length) result.push(part);
  40. return result;
  41. }
  42. function polygonclip(points, bbox) {
  43. var result, edge, prev, prevInside, i, p, inside;
  44. for (edge = 1; edge <= 8; edge *= 2) {
  45. result = [];
  46. prev = points[points.length - 1];
  47. prevInside = !(bitCode(prev, bbox) & edge);
  48. for (i = 0; i < points.length; i++) {
  49. p = points[i];
  50. inside = !(bitCode(p, bbox) & edge);
  51. if (inside !== prevInside) result.push(intersect(prev, p, edge, bbox));
  52. if (inside) result.push(p);
  53. prev = p;
  54. prevInside = inside;
  55. }
  56. points = result;
  57. if (!points.length) break;
  58. }
  59. return result;
  60. }
  61. function intersect(a, b, edge, bbox) {
  62. return edge & 8 ? [a[0] + (b[0] - a[0]) * (bbox[3] - a[1]) / (b[1] - a[1]), bbox[3]] : edge & 4 ? [a[0] + (b[0] - a[0]) * (bbox[1] - a[1]) / (b[1] - a[1]), bbox[1]] : edge & 2 ? [bbox[2], a[1] + (b[1] - a[1]) * (bbox[2] - a[0]) / (b[0] - a[0])] : edge & 1 ? [bbox[0], a[1] + (b[1] - a[1]) * (bbox[0] - a[0]) / (b[0] - a[0])] : null;
  63. }
  64. function bitCode(p, bbox) {
  65. var code = 0;
  66. if (p[0] < bbox[0]) code |= 1;
  67. else if (p[0] > bbox[2]) code |= 2;
  68. if (p[1] < bbox[1]) code |= 4;
  69. else if (p[1] > bbox[3]) code |= 8;
  70. return code;
  71. }
  72. // index.ts
  73. function bboxClip(feature, bbox) {
  74. const geom = _invariant.getGeom.call(void 0, feature);
  75. const type = geom.type;
  76. const properties = feature.type === "Feature" ? feature.properties : {};
  77. let coords = geom.coordinates;
  78. switch (type) {
  79. case "LineString":
  80. case "MultiLineString": {
  81. const lines = [];
  82. if (type === "LineString") {
  83. coords = [coords];
  84. }
  85. coords.forEach((line) => {
  86. lineclip(line, bbox, lines);
  87. });
  88. if (lines.length === 1) {
  89. return _helpers.lineString.call(void 0, lines[0], properties);
  90. }
  91. return _helpers.multiLineString.call(void 0, lines, properties);
  92. }
  93. case "Polygon":
  94. return _helpers.polygon.call(void 0, clipPolygon(coords, bbox), properties);
  95. case "MultiPolygon":
  96. return _helpers.multiPolygon.call(void 0,
  97. coords.map((poly) => {
  98. return clipPolygon(poly, bbox);
  99. }),
  100. properties
  101. );
  102. default:
  103. throw new Error("geometry " + type + " not supported");
  104. }
  105. }
  106. function clipPolygon(rings, bbox) {
  107. const outRings = [];
  108. for (const ring of rings) {
  109. const clipped = polygonclip(ring, bbox);
  110. if (clipped.length > 0) {
  111. if (clipped[0][0] !== clipped[clipped.length - 1][0] || clipped[0][1] !== clipped[clipped.length - 1][1]) {
  112. clipped.push(clipped[0]);
  113. }
  114. if (clipped.length >= 4) {
  115. outRings.push(clipped);
  116. }
  117. }
  118. }
  119. return outRings;
  120. }
  121. var turf_bbox_clip_default = bboxClip;
  122. exports.bboxClip = bboxClip; exports.default = turf_bbox_clip_default;
  123. //# sourceMappingURL=index.cjs.map