{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n Feature,\n FeatureCollection,\n MultiLineString,\n LineString,\n GeoJsonProperties,\n BBox,\n Position,\n} from \"geojson\";\nimport { bbox as turfBBox } from \"@turf/bbox\";\nimport { getCoords, getGeom } from \"@turf/invariant\";\nimport { polygon, multiPolygon, lineString } from \"@turf/helpers\";\nimport { clone } from \"@turf/clone\";\n\n/**\n * Converts (Multi)LineString(s) to Polygon(s).\n *\n * @function\n * @param {FeatureCollection|Feature} lines Features to convert\n * @param {Object} [options={}] Optional parameters\n * @param {Object} [options.properties={}] translates GeoJSON properties to Feature\n * @param {boolean} [options.autoComplete=true] auto complete linestrings (matches first & last coordinates)\n * @param {boolean} [options.orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates\n * @param {boolean} [options.mutate=false] mutate the original linestring using autoComplete (matches first & last coordinates)\n * @returns {Feature} converted to Polygons\n * @example\n * var line = turf.lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]);\n *\n * var polygon = turf.lineToPolygon(line);\n *\n * //addToMap\n * var addToMap = [polygon];\n */\nfunction lineToPolygon(\n lines: Feature | FeatureCollection | G,\n options: {\n properties?: GeoJsonProperties;\n autoComplete?: boolean;\n orderCoords?: boolean;\n mutate?: boolean;\n } = {}\n) {\n // Optional parameters\n var properties = options.properties;\n var autoComplete = options.autoComplete ?? true;\n var orderCoords = options.orderCoords ?? true;\n var mutate = options.mutate ?? false;\n\n if (!mutate) {\n lines = clone(lines);\n }\n\n switch (lines.type) {\n case \"FeatureCollection\":\n var coords: number[][][][] = [];\n lines.features.forEach(function (line) {\n coords.push(\n getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords))\n );\n });\n return multiPolygon(coords, properties);\n default:\n return lineStringToPolygon(lines, properties, autoComplete, orderCoords);\n }\n}\n\n/**\n * LineString to Polygon\n *\n * @private\n * @param {Feature} line line\n * @param {Object} [properties] translates GeoJSON properties to Feature\n * @param {boolean} [autoComplete=true] auto complete linestrings\n * @param {boolean} [orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates\n * @returns {Feature} line converted to Polygon\n */\nfunction lineStringToPolygon(\n line: Feature | G,\n properties: GeoJsonProperties | undefined,\n autoComplete: boolean,\n orderCoords: boolean\n) {\n properties = properties\n ? properties\n : line.type === \"Feature\"\n ? line.properties\n : {};\n var geom = getGeom(line);\n var coords: Position[] | Position[][] = geom.coordinates;\n var type = geom.type;\n\n if (!coords.length) throw new Error(\"line must contain coordinates\");\n\n switch (type) {\n case \"LineString\":\n if (autoComplete) coords = autoCompleteCoords(coords as Position[]);\n return polygon([coords as Position[]], properties);\n case \"MultiLineString\":\n var multiCoords: number[][][] = [];\n var largestArea = 0;\n\n (coords as Position[][]).forEach(function (coord) {\n if (autoComplete) coord = autoCompleteCoords(coord);\n\n // Largest LineString to be placed in the first position of the coordinates array\n if (orderCoords) {\n var area = calculateArea(turfBBox(lineString(coord)));\n if (area > largestArea) {\n multiCoords.unshift(coord);\n largestArea = area;\n } else multiCoords.push(coord);\n } else {\n multiCoords.push(coord);\n }\n });\n return polygon(multiCoords, properties);\n default:\n throw new Error(\"geometry type \" + type + \" is not supported\");\n }\n}\n\n/**\n * Auto Complete Coords - matches first & last coordinates\n *\n * @private\n * @param {Array>} coords Coordinates\n * @returns {Array>} auto completed coordinates\n */\nfunction autoCompleteCoords(coords: Position[]) {\n var first = coords[0];\n var x1 = first[0];\n var y1 = first[1];\n var last = coords[coords.length - 1];\n var x2 = last[0];\n var y2 = last[1];\n if (x1 !== x2 || y1 !== y2) {\n coords.push(first);\n }\n return coords;\n}\n\n/**\n * area - quick approximate area calculation (used to sort)\n *\n * @private\n * @param {Array} bbox BBox [west, south, east, north]\n * @returns {number} very quick area calculation\n */\nfunction calculateArea(bbox: BBox) {\n var west = bbox[0];\n var south = bbox[1];\n var east = bbox[2];\n var north = bbox[3];\n return Math.abs(west - east) * Math.abs(south - north);\n}\n\nexport { lineToPolygon };\nexport default lineToPolygon;\n"],"mappings":";AASA,SA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