{"version":3,"sources":["../../index.ts"],"sourcesContent":["import { booleanIntersects as intersect } from \"@turf/boolean-intersects\";\nimport {\n BBox,\n Feature,\n FeatureCollection,\n MultiPolygon,\n Polygon,\n GeoJsonProperties,\n} from \"geojson\";\nimport {\n convertLength,\n featureCollection,\n polygon,\n Units,\n} from \"@turf/helpers\";\n/**\n * Creates a grid of rectangular polygons with width and height consistent in degrees\n *\n * @function\n * @param {BBox} bbox extent of grid in [minX, minY, maxX, maxY] order. If the grid does not fill the bbox perfectly, it is centered.\n * @param {number} cellWidth width of each cell, in units\n * @param {number} cellHeight height of each cell, in units\n * @param {Object} [options={}] Optional parameters\n * @param {Units} [options.units='kilometers'] the units of the cell width and height value.\n * Supports all valid Turf {@link https://github.com/Turfjs/turf/blob/master/packages/turf-helpers/README_UNITS.md Units}.\n * If you are looking for rectangles with equal width and height in linear units (e.g. kilometers) this is not the module for you.\n * The cellWidth and cellHeight is converted from units provided to degrees internally, so the width and height of resulting polygons will be consistent only in degrees.\n * @param {Feature} [options.mask] if passed a Polygon or MultiPolygon,\n * the grid Points will be created only inside it\n * @param {Object} [options.properties={}] passed to each point of the grid\n * @returns {FeatureCollection} a grid of polygons\n * @example\n * var bbox = [-95, 30 ,-85, 40];\n * var cellWidth = 50;\n * var cellHeight = 20;\n * var options = {units: 'miles'};\n *\n * var rectangleGrid = turf.rectangleGrid(bbox, cellWidth, cellHeight, options);\n *\n * //addToMap\n * var addToMap = [rectangleGrid]\n */\nfunction rectangleGrid

(\n bbox: BBox,\n cellWidth: number,\n cellHeight: number,\n options: {\n units?: Units;\n properties?: P;\n mask?: Feature | Polygon | MultiPolygon;\n } = {}\n): FeatureCollection {\n // Containers\n const results = [];\n const west = bbox[0];\n const south = bbox[1];\n const east = bbox[2];\n const north = bbox[3];\n\n const bboxWidth = east - west;\n const cellWidthDeg = convertLength(cellWidth, options.units, \"degrees\");\n\n const bboxHeight = north - south;\n const cellHeightDeg = convertLength(cellHeight, options.units, \"degrees\");\n\n const columns = Math.floor(Math.abs(bboxWidth) / cellWidthDeg);\n const rows = Math.floor(Math.abs(bboxHeight) / cellHeightDeg);\n\n // if the grid does not fill the bbox perfectly, center it.\n const deltaX = (bboxWidth - columns * cellWidthDeg) / 2;\n const deltaY = (bboxHeight - rows * cellHeightDeg) / 2;\n\n // iterate over columns & rows\n let currentX = west + deltaX;\n for (let column = 0; column < columns; column++) {\n let currentY = south + deltaY;\n for (let row = 0; row < rows; row++) {\n const cellPoly = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeightDeg],\n [currentX + cellWidthDeg, currentY + cellHeightDeg],\n [currentX + cellWidthDeg, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n if (options.mask) {\n if (intersect(options.mask, cellPoly)) {\n results.push(cellPoly);\n }\n } else {\n results.push(cellPoly);\n }\n\n currentY += cellHeightDeg;\n }\n currentX += cellWidthDeg;\n }\n return featureCollection(results);\n}\n\nexport { rectangleGrid };\nexport default rectangleGrid;\n"],"mappings":";AAAA,SAAS,qBAAqB,iBAAiB;AAS/C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AA4BP,SAAS,cACP,MACA,WACA,YACA,UAII,CAAC,GAC0B;AAE/B,QAAM,UAAU,CAAC;AACjB,QAAM,OAAO,KAAK,CAAC;AACnB,QAAM,QAAQ,KAAK,CAAC;AACpB,QAAM,OAAO,KAAK,CAAC;AACnB,QAAM,QAAQ,KAAK,CAAC;AAEpB,QAAM,YAAY,OAAO;AACzB,QAAM,eAAe,cAAc,WAAW,QAAQ,OAAO,SAAS;AAEtE,QAAM,aAAa,QAAQ;AAC3B,QAAM,gBAAgB,cAAc,YAAY,QAAQ,OAAO,SAAS;AAExE,QAAM,UAAU,KAAK,MAAM,KAAK,IAAI,SAAS,IAAI,YAAY;AAC7D,QAAM,OAAO,KAAK,MAAM,KAAK,IAAI,UAAU,IAAI,aAAa;AAG5D,QAAM,UAAU,YAAY,UAAU,gBAAgB;AACtD,QAAM,UAAU,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