index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. var helpers = require('@turf/helpers');
  3. var polygonClipping = require('polygon-clipping');
  4. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  5. var polygonClipping__default = /*#__PURE__*/_interopDefaultLegacy(polygonClipping);
  6. /**
  7. * Takes any type of {@link Polygon|polygon} and an optional mask and returns a {@link Polygon|polygon} exterior ring with holes.
  8. *
  9. * @name mask
  10. * @param {FeatureCollection|Feature<Polygon|MultiPolygon>} polygon GeoJSON Polygon used as interior rings or holes.
  11. * @param {Feature<Polygon>} [mask] GeoJSON Polygon used as the exterior ring (if undefined, the world extent is used)
  12. * @returns {Feature<Polygon>} Masked Polygon (exterior ring with holes).
  13. * @example
  14. * var polygon = turf.polygon([[[112, -21], [116, -36], [146, -39], [153, -24], [133, -10], [112, -21]]]);
  15. * var mask = turf.polygon([[[90, -55], [170, -55], [170, 10], [90, 10], [90, -55]]]);
  16. *
  17. * var masked = turf.mask(polygon, mask);
  18. *
  19. * //addToMap
  20. * var addToMap = [masked]
  21. */
  22. function mask(polygon, mask) {
  23. // Define mask
  24. var maskPolygon = createMask(mask);
  25. var polygonOuters = null;
  26. if (polygon.type === "FeatureCollection") polygonOuters = unionFc(polygon);
  27. else
  28. polygonOuters = createGeomFromPolygonClippingOutput(
  29. polygonClipping__default['default'].union(polygon.geometry.coordinates)
  30. );
  31. polygonOuters.geometry.coordinates.forEach(function (contour) {
  32. maskPolygon.geometry.coordinates.push(contour[0]);
  33. });
  34. return maskPolygon;
  35. }
  36. function unionFc(fc) {
  37. var unioned =
  38. fc.features.length === 2
  39. ? polygonClipping__default['default'].union(
  40. fc.features[0].geometry.coordinates,
  41. fc.features[1].geometry.coordinates
  42. )
  43. : polygonClipping__default['default'].union.apply(
  44. polygonClipping__default['default'],
  45. fc.features.map(function (f) {
  46. return f.geometry.coordinates;
  47. })
  48. );
  49. return createGeomFromPolygonClippingOutput(unioned);
  50. }
  51. function createGeomFromPolygonClippingOutput(unioned) {
  52. return helpers.multiPolygon(unioned);
  53. }
  54. /**
  55. * Create Mask Coordinates
  56. *
  57. * @private
  58. * @param {Feature<Polygon>} [mask] default to world if undefined
  59. * @returns {Feature<Polygon>} mask coordinate
  60. */
  61. function createMask(mask) {
  62. var world = [
  63. [
  64. [180, 90],
  65. [-180, 90],
  66. [-180, -90],
  67. [180, -90],
  68. [180, 90],
  69. ],
  70. ];
  71. var coordinates = (mask && mask.geometry.coordinates) || world;
  72. return helpers.polygon(coordinates);
  73. }
  74. module.exports = mask;
  75. module.exports.default = mask;