index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // index.ts
  2. import { polygon as createPolygon, multiPolygon } from "@turf/helpers";
  3. import * as polyclip from "polyclip-ts";
  4. import { clone } from "@turf/clone";
  5. function mask(polygon, mask2, options) {
  6. var _a;
  7. const mutate = (_a = options == null ? void 0 : options.mutate) != null ? _a : false;
  8. let maskTemplate = mask2;
  9. if (mask2 && mutate === false) {
  10. maskTemplate = clone(mask2);
  11. }
  12. const maskPolygon = createMask(maskTemplate);
  13. let polygonOuters = null;
  14. if (polygon.type === "FeatureCollection") {
  15. polygonOuters = unionFc(polygon);
  16. } else if (polygon.type === "Feature") {
  17. polygonOuters = createGeomFromPolygonClippingOutput(
  18. polyclip.union(polygon.geometry.coordinates)
  19. );
  20. } else {
  21. polygonOuters = createGeomFromPolygonClippingOutput(
  22. polyclip.union(polygon.coordinates)
  23. );
  24. }
  25. polygonOuters.geometry.coordinates.forEach(function(contour) {
  26. maskPolygon.geometry.coordinates.push(contour[0]);
  27. });
  28. return maskPolygon;
  29. }
  30. function unionFc(fc) {
  31. const unioned = fc.features.length === 2 ? polyclip.union(
  32. fc.features[0].geometry.coordinates,
  33. fc.features[1].geometry.coordinates
  34. ) : polyclip.union.apply(
  35. polyclip,
  36. fc.features.map(function(f) {
  37. return f.geometry.coordinates;
  38. })
  39. );
  40. return createGeomFromPolygonClippingOutput(unioned);
  41. }
  42. function createGeomFromPolygonClippingOutput(unioned) {
  43. return multiPolygon(unioned);
  44. }
  45. function createMask(mask2) {
  46. const world = [
  47. [
  48. [180, 90],
  49. [-180, 90],
  50. [-180, -90],
  51. [180, -90],
  52. [180, 90]
  53. ]
  54. ];
  55. let coordinates = world;
  56. if (mask2) {
  57. if (mask2.type === "Feature") {
  58. coordinates = mask2.geometry.coordinates;
  59. } else {
  60. coordinates = mask2.coordinates;
  61. }
  62. }
  63. return createPolygon(coordinates);
  64. }
  65. var turf_mask_default = mask;
  66. export {
  67. turf_mask_default as default,
  68. mask
  69. };
  70. //# sourceMappingURL=index.js.map