index.js 3.7 KB

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