index.cjs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } }// index.ts
  2. var _helpers = require('@turf/helpers');
  3. var _polyclipts = require('polyclip-ts'); var polyclip = _interopRequireWildcard(_polyclipts);
  4. var _clone = require('@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.clone.call(void 0, 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 _helpers.multiPolygon.call(void 0, 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 _helpers.polygon.call(void 0, coordinates);
  64. }
  65. var turf_mask_default = mask;
  66. exports.default = turf_mask_default; exports.mask = mask;
  67. //# sourceMappingURL=index.cjs.map