@turf/line-to-polygon
Advanced tools
Comparing version 5.1.5 to 6.0.0
106
index.js
@@ -1,5 +0,6 @@ | ||
import turfBBox from '@turf/bbox'; | ||
import { getCoords, getType } from '@turf/invariant'; | ||
import { polygon, multiPolygon, lineString, isObject } from '@turf/helpers'; | ||
"use strict"; | ||
exports.__esModule = true; | ||
var bbox_1 = require("@turf/bbox"); | ||
var invariant_1 = require("@turf/invariant"); | ||
var helpers_1 = require("@turf/helpers"); | ||
/** | ||
@@ -24,30 +25,21 @@ * Converts (Multi)LineString(s) to Polygon(s). | ||
function lineToPolygon(lines, options) { | ||
if (options === void 0) { options = {}; } | ||
// Optional parameters | ||
options = options || {}; | ||
if (!isObject(options)) throw new Error('options is invalid'); | ||
var properties = options.properties; | ||
var autoComplete = options.autoComplete; | ||
var orderCoords = options.orderCoords; | ||
// validation | ||
if (!lines) throw new Error('lines is required'); | ||
// default params | ||
autoComplete = (autoComplete !== undefined) ? autoComplete : true; | ||
orderCoords = (orderCoords !== undefined) ? orderCoords : true; | ||
var type = getType(lines); | ||
switch (type) { | ||
case 'FeatureCollection': | ||
case 'GeometryCollection': | ||
var coords = []; | ||
var features = (lines.features) ? lines.features : lines.geometries; | ||
features.forEach(function (line) { | ||
coords.push(getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords))); | ||
}); | ||
return multiPolygon(coords, properties); | ||
switch (lines.type) { | ||
case 'FeatureCollection': | ||
var coords = []; | ||
lines.features.forEach(function (line) { | ||
coords.push(invariant_1.getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords))); | ||
}); | ||
return helpers_1.multiPolygon(coords, properties); | ||
default: | ||
return lineStringToPolygon(lines, properties, autoComplete, orderCoords); | ||
} | ||
return lineStringToPolygon(lines, properties, autoComplete, orderCoords); | ||
} | ||
/** | ||
@@ -64,36 +56,38 @@ * LineString to Polygon | ||
function lineStringToPolygon(line, properties, autoComplete, orderCoords) { | ||
properties = properties || line.properties || {}; | ||
var coords = getCoords(line); | ||
var type = getType(line); | ||
if (!coords.length) throw new Error('line must contain coordinates'); | ||
properties = properties ? properties : (line.type === 'Feature') ? line.properties : {}; | ||
var geom = invariant_1.getGeom(line); | ||
var coords = geom.coordinates; | ||
var type = geom.type; | ||
if (!coords.length) | ||
throw new Error('line must contain coordinates'); | ||
switch (type) { | ||
case 'LineString': | ||
if (autoComplete) coords = autoCompleteCoords(coords); | ||
return polygon([coords], properties); | ||
case 'MultiLineString': | ||
var multiCoords = []; | ||
var largestArea = 0; | ||
coords.forEach(function (coord) { | ||
if (autoComplete) coord = autoCompleteCoords(coord); | ||
// Largest LineString to be placed in the first position of the coordinates array | ||
if (orderCoords) { | ||
var area = calculateArea(turfBBox(lineString(coord))); | ||
if (area > largestArea) { | ||
multiCoords.unshift(coord); | ||
largestArea = area; | ||
} else multiCoords.push(coord); | ||
} else { | ||
multiCoords.push(coord); | ||
} | ||
}); | ||
return polygon(multiCoords, properties); | ||
default: | ||
throw new Error('geometry type ' + type + ' is not supported'); | ||
case 'LineString': | ||
if (autoComplete) | ||
coords = autoCompleteCoords(coords); | ||
return helpers_1.polygon([coords], properties); | ||
case 'MultiLineString': | ||
var multiCoords = []; | ||
var largestArea = 0; | ||
coords.forEach(function (coord) { | ||
if (autoComplete) | ||
coord = autoCompleteCoords(coord); | ||
// Largest LineString to be placed in the first position of the coordinates array | ||
if (orderCoords) { | ||
var area = calculateArea(bbox_1["default"](helpers_1.lineString(coord))); | ||
if (area > largestArea) { | ||
multiCoords.unshift(coord); | ||
largestArea = area; | ||
} | ||
else | ||
multiCoords.push(coord); | ||
} | ||
else { | ||
multiCoords.push(coord); | ||
} | ||
}); | ||
return helpers_1.polygon(multiCoords, properties); | ||
default: | ||
throw new Error('geometry type ' + type + ' is not supported'); | ||
} | ||
} | ||
/** | ||
@@ -118,3 +112,2 @@ * Auto Complete Coords - matches first & last coordinates | ||
} | ||
/** | ||
@@ -134,3 +127,2 @@ * area - quick approximate area calculation (used to sort) | ||
} | ||
export default lineToPolygon; | ||
exports["default"] = lineToPolygon; |
{ | ||
"name": "@turf/line-to-polygon", | ||
"version": "5.1.5", | ||
"version": "6.0.0", | ||
"description": "turf line-to-polygon module", | ||
"main": "main.js", | ||
"module": "main.es.js", | ||
"types": "index.d.ts", | ||
"main": "index", | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"main.js", | ||
"main.es.js" | ||
"index.ts" | ||
], | ||
"scripts": { | ||
"pretest": "rollup -c ../../rollup.config.js", | ||
"test": "node -r @std/esm test.js", | ||
"posttest": "node -r @std/esm ../../scripts/validate-es5-dependencies.js", | ||
"bench": "node -r @std/esm bench.js", | ||
"pretest": "tsc", | ||
"test": "node test.js", | ||
"bench": "node bench.js", | ||
"docs": "node ../../scripts/generate-readmes" | ||
@@ -42,6 +37,5 @@ }, | ||
"devDependencies": { | ||
"@std/esm": "*", | ||
"benchmark": "*", | ||
"load-json-file": "*", | ||
"rollup": "*", | ||
"typescript": "*", | ||
"tape": "*", | ||
@@ -52,9 +46,5 @@ "write-json-file": "*" | ||
"@turf/bbox": "^5.1.5", | ||
"@turf/helpers": "^5.1.5", | ||
"@turf/invariant": "^5.1.5" | ||
}, | ||
"@std/esm": { | ||
"esm": "js", | ||
"cjs": true | ||
"@turf/helpers": "6.x", | ||
"@turf/invariant": "6.x" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
5
13907
5
249
1
+ Added@turf/helpers@6.5.0(transitive)
+ Added@turf/invariant@6.5.0(transitive)
- Removed@turf/invariant@5.2.0(transitive)
Updated@turf/helpers@6.x
Updated@turf/invariant@6.x