{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n Polygon,\n Position,\n MultiPolygon,\n} from \"geojson\";\nimport { polygon as createPolygon, multiPolygon } from \"@turf/helpers\";\nimport * as polyclip from \"polyclip-ts\";\nimport { clone } from \"@turf/clone\";\n\n/**\n * Takes polygons or multipolygons and an optional mask, and returns an exterior\n * ring polygon with holes.\n *\n * @function\n * @param {Polygon|MultiPolygon|Feature|FeatureCollection} polygon GeoJSON polygon used as interior rings or holes\n * @param {Polygon|Feature} [mask] GeoJSON polygon used as the exterior ring (if undefined, the world extent is used)\n * @param {Object} [options={}] Optional parameters\n * @param {boolean} [options.mutate=false] allows the `mask` GeoJSON input to be mutated (performance improvement if true)\n * @returns {Feature} Masked Polygon (exterior ring with holes)\n * @example\n * const polygon = turf.polygon([[[112, -21], [116, -36], [146, -39], [153, -24], [133, -10], [112, -21]]]);\n * const mask = turf.polygon([[[90, -55], [170, -55], [170, 10], [90, 10], [90, -55]]]);\n *\n * const masked = turf.mask(polygon, mask);\n *\n * //addToMap\n * const addToMap = [masked]\n */\nfunction mask(\n polygon: T | Feature | FeatureCollection,\n mask?: Polygon | Feature,\n options?: { mutate?: boolean }\n): Feature {\n const mutate = options?.mutate ?? false; // by default, do not mutate\n\n let maskTemplate = mask;\n if (mask && mutate === false) {\n // Clone mask if requested to avoid side effects\n maskTemplate = clone(mask);\n }\n\n // Define initial mask\n const maskPolygon = createMask(maskTemplate);\n\n let polygonOuters = null;\n if (polygon.type === \"FeatureCollection\") {\n polygonOuters = unionFc(polygon);\n } else if (polygon.type === \"Feature\") {\n // Need to cast below as Position[][] isn't quite as strict as Geom, even\n // though they should be equivalent.\n polygonOuters = createGeomFromPolygonClippingOutput(\n polyclip.union(polygon.geometry.coordinates as polyclip.Geom)\n );\n } else {\n // Geometry\n // Need to cast below as Position[][] isn't quite as strict as Geom, even\n // though they should be equivalent.\n polygonOuters = createGeomFromPolygonClippingOutput(\n polyclip.union(polygon.coordinates as polyclip.Geom)\n );\n }\n\n polygonOuters.geometry.coordinates.forEach(function (contour) {\n maskPolygon.geometry.coordinates.push(contour[0]);\n });\n\n return maskPolygon;\n}\n\nfunction unionFc(fc: FeatureCollection) {\n // Need to cast below as Position[][] isn't quite as strict as Geom, even\n // though they should be equivalent.\n\n // Stick with apply() below as spread operator degrades performance. Have\n // to disable prefer-spread lint rule though.\n /* eslint-disable prefer-spread */\n const unioned =\n fc.features.length === 2\n ? polyclip.union(\n fc.features[0].geometry.coordinates as polyclip.Geom,\n fc.features[1].geometry.coordinates as polyclip.Geom\n )\n : polyclip.union.apply(\n polyclip,\n fc.features.map(function (f) {\n return f.geometry.coordinates;\n }) as [polyclip.Geom, ...polyclip.Geom[]]\n );\n /* eslint-enable */\n return createGeomFromPolygonClippingOutput(unioned);\n}\n\nfunction createGeomFromPolygonClippingOutput(unioned: Position[][][]) {\n return multiPolygon(unioned);\n}\n\n/**\n * Create Mask Coordinates\n *\n * @private\n * @param {Feature} [mask] default to world if undefined\n * @returns {Feature} mask as a polygon\n */\nfunction createMask(mask: Feature | Polygon | undefined) {\n const world = [\n [\n [180, 90],\n [-180, 90],\n [-180, -90],\n [180, -90],\n [180, 90],\n ],\n ];\n let coordinates = world;\n if (mask) {\n if (mask.type === \"Feature\") {\n // polygon feature\n coordinates = mask.geometry.coordinates;\n } else {\n // polygon geometry\n coordinates = mask.coordinates;\n }\n }\n return createPolygon(coordinates);\n}\n\nexport { mask };\nexport default mask;\n"],"mappings":";AAOA,SAAS,WAAW,eAAe,oBAAoB;AACvD,YAAY,cAAc;AAC1B,SAAS,aAAa;AAqBtB,SAAS,KACP,SACAA,OACA,SACkB;AAlCpB;AAmCE,QAAM,UAAS,wCAAS,WAAT,YAAmB;AAElC,MAAI,eAAeA;AACnB,MAAIA,SAAQ,WAAW,OAAO;AAE5B,mBAAe,MAAMA,KAAI;AAAA,EAC3B;AAGA,QAAM,cAAc,WAAW,YAAY;AAE3C,MAAI,gBAAgB;AACpB,MAAI,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