@turf/line-to-polygon 7.0.0-alpha.0 → 7.0.0-alpha.110
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -9
- package/dist/cjs/index.cjs +93 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{js → cjs}/index.d.ts +10 -3
- package/dist/esm/index.d.mts +34 -0
- package/dist/esm/index.mjs +93 -0
- package/dist/esm/index.mjs.map +1 -0
- package/package.json +34 -28
- package/dist/es/index.js +0 -129
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -132
package/README.md
CHANGED
|
@@ -45,26 +45,21 @@ Returns **[Feature][2]<([Polygon][7] | [MultiPolygon][8])>** converted to Polygo
|
|
|
45
45
|
|
|
46
46
|
[8]: https://tools.ietf.org/html/rfc7946#section-3.1.7
|
|
47
47
|
|
|
48
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
49
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
50
|
-
./scripts/generate-readmes in the turf project. -->
|
|
48
|
+
<!-- This file is automatically generated. Please don't edit it directly. If you find an error, edit the source file of the module in question (likely index.js or index.ts), and re-run "yarn docs" from the root of the turf project. -->
|
|
51
49
|
|
|
52
50
|
---
|
|
53
51
|
|
|
54
|
-
This module is part of the [Turfjs project](
|
|
55
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
56
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
57
|
-
PRs and issues.
|
|
52
|
+
This module is part of the [Turfjs project](https://turfjs.org/), an open source module collection dedicated to geographic algorithms. It is maintained in the [Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create PRs and issues.
|
|
58
53
|
|
|
59
54
|
### Installation
|
|
60
55
|
|
|
61
|
-
Install this module individually:
|
|
56
|
+
Install this single module individually:
|
|
62
57
|
|
|
63
58
|
```sh
|
|
64
59
|
$ npm install @turf/line-to-polygon
|
|
65
60
|
```
|
|
66
61
|
|
|
67
|
-
Or install the
|
|
62
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
68
63
|
|
|
69
64
|
```sh
|
|
70
65
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
var _bbox = require('@turf/bbox');
|
|
6
|
+
var _invariant = require('@turf/invariant');
|
|
7
|
+
var _helpers = require('@turf/helpers');
|
|
8
|
+
var _clone = require('@turf/clone');
|
|
9
|
+
function lineToPolygon(lines, options = {}) {
|
|
10
|
+
var _a, _b, _c;
|
|
11
|
+
var properties = options.properties;
|
|
12
|
+
var autoComplete = (_a = options.autoComplete) != null ? _a : true;
|
|
13
|
+
var orderCoords = (_b = options.orderCoords) != null ? _b : true;
|
|
14
|
+
var mutate = (_c = options.mutate) != null ? _c : false;
|
|
15
|
+
if (!mutate) {
|
|
16
|
+
lines = _clone.clone.call(void 0, lines);
|
|
17
|
+
}
|
|
18
|
+
switch (lines.type) {
|
|
19
|
+
case "FeatureCollection":
|
|
20
|
+
var coords = [];
|
|
21
|
+
lines.features.forEach(function(line) {
|
|
22
|
+
coords.push(
|
|
23
|
+
_invariant.getCoords.call(void 0, lineStringToPolygon(line, {}, autoComplete, orderCoords))
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
return _helpers.multiPolygon.call(void 0, coords, properties);
|
|
27
|
+
default:
|
|
28
|
+
return lineStringToPolygon(lines, properties, autoComplete, orderCoords);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
__name(lineToPolygon, "lineToPolygon");
|
|
32
|
+
function lineStringToPolygon(line, properties, autoComplete, orderCoords) {
|
|
33
|
+
properties = properties ? properties : line.type === "Feature" ? line.properties : {};
|
|
34
|
+
var geom = _invariant.getGeom.call(void 0, line);
|
|
35
|
+
var coords = geom.coordinates;
|
|
36
|
+
var type = geom.type;
|
|
37
|
+
if (!coords.length)
|
|
38
|
+
throw new Error("line must contain coordinates");
|
|
39
|
+
switch (type) {
|
|
40
|
+
case "LineString":
|
|
41
|
+
if (autoComplete)
|
|
42
|
+
coords = autoCompleteCoords(coords);
|
|
43
|
+
return _helpers.polygon.call(void 0, [coords], properties);
|
|
44
|
+
case "MultiLineString":
|
|
45
|
+
var multiCoords = [];
|
|
46
|
+
var largestArea = 0;
|
|
47
|
+
coords.forEach(function(coord) {
|
|
48
|
+
if (autoComplete)
|
|
49
|
+
coord = autoCompleteCoords(coord);
|
|
50
|
+
if (orderCoords) {
|
|
51
|
+
var area = calculateArea(_bbox.bbox.call(void 0, _helpers.lineString.call(void 0, coord)));
|
|
52
|
+
if (area > largestArea) {
|
|
53
|
+
multiCoords.unshift(coord);
|
|
54
|
+
largestArea = area;
|
|
55
|
+
} else
|
|
56
|
+
multiCoords.push(coord);
|
|
57
|
+
} else {
|
|
58
|
+
multiCoords.push(coord);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return _helpers.polygon.call(void 0, multiCoords, properties);
|
|
62
|
+
default:
|
|
63
|
+
throw new Error("geometry type " + type + " is not supported");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
__name(lineStringToPolygon, "lineStringToPolygon");
|
|
67
|
+
function autoCompleteCoords(coords) {
|
|
68
|
+
var first = coords[0];
|
|
69
|
+
var x1 = first[0];
|
|
70
|
+
var y1 = first[1];
|
|
71
|
+
var last = coords[coords.length - 1];
|
|
72
|
+
var x2 = last[0];
|
|
73
|
+
var y2 = last[1];
|
|
74
|
+
if (x1 !== x2 || y1 !== y2) {
|
|
75
|
+
coords.push(first);
|
|
76
|
+
}
|
|
77
|
+
return coords;
|
|
78
|
+
}
|
|
79
|
+
__name(autoCompleteCoords, "autoCompleteCoords");
|
|
80
|
+
function calculateArea(bbox) {
|
|
81
|
+
var west = bbox[0];
|
|
82
|
+
var south = bbox[1];
|
|
83
|
+
var east = bbox[2];
|
|
84
|
+
var north = bbox[3];
|
|
85
|
+
return Math.abs(west - east) * Math.abs(south - north);
|
|
86
|
+
}
|
|
87
|
+
__name(calculateArea, "calculateArea");
|
|
88
|
+
var turf_line_to_polygon_default = lineToPolygon;
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
exports.default = turf_line_to_polygon_default; exports.lineToPolygon = lineToPolygon;
|
|
93
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AASA,SAAS,QAAQ,gBAAgB;AACjC,SAAS,WAAW,eAAe;AACnC,SAAS,SAAS,cAAc,kBAAkB;AAClD,SAAS,aAAa;AAqBtB,SAAS,cACP,OACA,UAKI,CAAC,GACL;AAzCF;AA2CE,MAAI,aAAa,QAAQ;AACzB,MAAI,gBAAe,aAAQ,iBAAR,YAAwB;AAC3C,MAAI,eAAc,aAAQ,gBAAR,YAAuB;AACzC,MAAI,UAAS,aAAQ,WAAR,YAAkB;AAE/B,MAAI,CAAC,QAAQ;AACX,YAAQ,MAAM,KAAK;AAAA,EACrB;AAEA,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK;AACH,UAAI,SAAyB,CAAC;AAC9B,YAAM,SAAS,QAAQ,SAAU,MAAM;AACrC,eAAO;AAAA,UACL,UAAU,oBAAoB,MAAM,CAAC,GAAG,cAAc,WAAW,CAAC;AAAA,QACpE;AAAA,MACF,CAAC;AACD,aAAO,aAAa,QAAQ,UAAU;AAAA,IACxC;AACE,aAAO,oBAAoB,OAAO,YAAY,cAAc,WAAW;AAAA,EAC3E;AACF;AA/BS;AA2CT,SAAS,oBACP,MACA,YACA,cACA,aACA;AACA,eAAa,aACT,aACA,KAAK,SAAS,YACZ,KAAK,aACL,CAAC;AACP,MAAI,OAAO,QAAQ,IAAI;AACvB,MAAI,SAAoC,KAAK;AAC7C,MAAI,OAAO,KAAK;AAEhB,MAAI,CAAC,OAAO;AAAQ,UAAM,IAAI,MAAM,+BAA+B;AAEnE,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,UAAI;AAAc,iBAAS,mBAAmB,MAAoB;AAClE,aAAO,QAAQ,CAAC,MAAoB,GAAG,UAAU;AAAA,IACnD,KAAK;AACH,UAAI,cAA4B,CAAC;AACjC,UAAI,cAAc;AAElB,MAAC,OAAwB,QAAQ,SAAU,OAAO;AAChD,YAAI;AAAc,kBAAQ,mBAAmB,KAAK;AAGlD,YAAI,aAAa;AACf,cAAI,OAAO,cAAc,SAAS,WAAW,KAAK,CAAC,CAAC;AACpD,cAAI,OAAO,aAAa;AACtB,wBAAY,QAAQ,KAAK;AACzB,0BAAc;AAAA,UAChB;AAAO,wBAAY,KAAK,KAAK;AAAA,QAC/B,OAAO;AACL,sBAAY,KAAK,KAAK;AAAA,QACxB;AAAA,MACF,CAAC;AACD,aAAO,QAAQ,aAAa,UAAU;AAAA,IACxC;AACE,YAAM,IAAI,MAAM,mBAAmB,OAAO,mBAAmB;AAAA,EACjE;AACF;AA3CS;AAoDT,SAAS,mBAAmB,QAAoB;AAC9C,MAAI,QAAQ,OAAO,CAAC;AACpB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,OAAO,OAAO,OAAO,SAAS,CAAC;AACnC,MAAI,KAAK,KAAK,CAAC;AACf,MAAI,KAAK,KAAK,CAAC;AACf,MAAI,OAAO,MAAM,OAAO,IAAI;AAC1B,WAAO,KAAK,KAAK;AAAA,EACnB;AACA,SAAO;AACT;AAXS;AAoBT,SAAS,cAAc,MAAY;AACjC,MAAI,OAAO,KAAK,CAAC;AACjB,MAAI,QAAQ,KAAK,CAAC;AAClB,MAAI,OAAO,KAAK,CAAC;AACjB,MAAI,QAAQ,KAAK,CAAC;AAClB,SAAO,KAAK,IAAI,OAAO,IAAI,IAAI,KAAK,IAAI,QAAQ,KAAK;AACvD;AANS;AAST,IAAO,+BAAQ","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 * @name lineToPolygon\n * @param {FeatureCollection|Feature<LineString|MultiLineString>} 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<Polygon|MultiPolygon>} 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<G extends LineString | MultiLineString>(\n lines: Feature<G> | FeatureCollection<G> | 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<LineString|MultiLineString>} 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<Polygon>} line converted to Polygon\n */\nfunction lineStringToPolygon<G extends LineString | MultiLineString>(\n line: Feature<G> | 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<Array<number>>} coords Coordinates\n * @returns {Array<Array<number>>} 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<number>} 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"]}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as geojson from 'geojson';
|
|
2
|
+
import { LineString, MultiLineString, Feature, FeatureCollection, GeoJsonProperties } from 'geojson';
|
|
3
|
+
|
|
2
4
|
/**
|
|
3
5
|
* Converts (Multi)LineString(s) to Polygon(s).
|
|
4
6
|
*
|
|
@@ -23,5 +25,10 @@ declare function lineToPolygon<G extends LineString | MultiLineString>(lines: Fe
|
|
|
23
25
|
autoComplete?: boolean;
|
|
24
26
|
orderCoords?: boolean;
|
|
25
27
|
mutate?: boolean;
|
|
26
|
-
}): Feature<
|
|
27
|
-
|
|
28
|
+
}): Feature<geojson.MultiPolygon, {
|
|
29
|
+
[name: string]: any;
|
|
30
|
+
} | null> | Feature<geojson.Polygon, {
|
|
31
|
+
[name: string]: any;
|
|
32
|
+
} | null>;
|
|
33
|
+
|
|
34
|
+
export { lineToPolygon as default, lineToPolygon };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as geojson from 'geojson';
|
|
2
|
+
import { LineString, MultiLineString, Feature, FeatureCollection, GeoJsonProperties } from 'geojson';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts (Multi)LineString(s) to Polygon(s).
|
|
6
|
+
*
|
|
7
|
+
* @name lineToPolygon
|
|
8
|
+
* @param {FeatureCollection|Feature<LineString|MultiLineString>} lines Features to convert
|
|
9
|
+
* @param {Object} [options={}] Optional parameters
|
|
10
|
+
* @param {Object} [options.properties={}] translates GeoJSON properties to Feature
|
|
11
|
+
* @param {boolean} [options.autoComplete=true] auto complete linestrings (matches first & last coordinates)
|
|
12
|
+
* @param {boolean} [options.orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates
|
|
13
|
+
* @param {boolean} [options.mutate=false] mutate the original linestring using autoComplete (matches first & last coordinates)
|
|
14
|
+
* @returns {Feature<Polygon|MultiPolygon>} converted to Polygons
|
|
15
|
+
* @example
|
|
16
|
+
* var line = turf.lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]);
|
|
17
|
+
*
|
|
18
|
+
* var polygon = turf.lineToPolygon(line);
|
|
19
|
+
*
|
|
20
|
+
* //addToMap
|
|
21
|
+
* var addToMap = [polygon];
|
|
22
|
+
*/
|
|
23
|
+
declare function lineToPolygon<G extends LineString | MultiLineString>(lines: Feature<G> | FeatureCollection<G> | G, options?: {
|
|
24
|
+
properties?: GeoJsonProperties;
|
|
25
|
+
autoComplete?: boolean;
|
|
26
|
+
orderCoords?: boolean;
|
|
27
|
+
mutate?: boolean;
|
|
28
|
+
}): Feature<geojson.MultiPolygon, {
|
|
29
|
+
[name: string]: any;
|
|
30
|
+
} | null> | Feature<geojson.Polygon, {
|
|
31
|
+
[name: string]: any;
|
|
32
|
+
} | null>;
|
|
33
|
+
|
|
34
|
+
export { lineToPolygon as default, lineToPolygon };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
import { bbox as turfBBox } from "@turf/bbox";
|
|
6
|
+
import { getCoords, getGeom } from "@turf/invariant";
|
|
7
|
+
import { polygon, multiPolygon, lineString } from "@turf/helpers";
|
|
8
|
+
import { clone } from "@turf/clone";
|
|
9
|
+
function lineToPolygon(lines, options = {}) {
|
|
10
|
+
var _a, _b, _c;
|
|
11
|
+
var properties = options.properties;
|
|
12
|
+
var autoComplete = (_a = options.autoComplete) != null ? _a : true;
|
|
13
|
+
var orderCoords = (_b = options.orderCoords) != null ? _b : true;
|
|
14
|
+
var mutate = (_c = options.mutate) != null ? _c : false;
|
|
15
|
+
if (!mutate) {
|
|
16
|
+
lines = clone(lines);
|
|
17
|
+
}
|
|
18
|
+
switch (lines.type) {
|
|
19
|
+
case "FeatureCollection":
|
|
20
|
+
var coords = [];
|
|
21
|
+
lines.features.forEach(function(line) {
|
|
22
|
+
coords.push(
|
|
23
|
+
getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords))
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
return multiPolygon(coords, properties);
|
|
27
|
+
default:
|
|
28
|
+
return lineStringToPolygon(lines, properties, autoComplete, orderCoords);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
__name(lineToPolygon, "lineToPolygon");
|
|
32
|
+
function lineStringToPolygon(line, properties, autoComplete, orderCoords) {
|
|
33
|
+
properties = properties ? properties : line.type === "Feature" ? line.properties : {};
|
|
34
|
+
var geom = getGeom(line);
|
|
35
|
+
var coords = geom.coordinates;
|
|
36
|
+
var type = geom.type;
|
|
37
|
+
if (!coords.length)
|
|
38
|
+
throw new Error("line must contain coordinates");
|
|
39
|
+
switch (type) {
|
|
40
|
+
case "LineString":
|
|
41
|
+
if (autoComplete)
|
|
42
|
+
coords = autoCompleteCoords(coords);
|
|
43
|
+
return polygon([coords], properties);
|
|
44
|
+
case "MultiLineString":
|
|
45
|
+
var multiCoords = [];
|
|
46
|
+
var largestArea = 0;
|
|
47
|
+
coords.forEach(function(coord) {
|
|
48
|
+
if (autoComplete)
|
|
49
|
+
coord = autoCompleteCoords(coord);
|
|
50
|
+
if (orderCoords) {
|
|
51
|
+
var area = calculateArea(turfBBox(lineString(coord)));
|
|
52
|
+
if (area > largestArea) {
|
|
53
|
+
multiCoords.unshift(coord);
|
|
54
|
+
largestArea = area;
|
|
55
|
+
} else
|
|
56
|
+
multiCoords.push(coord);
|
|
57
|
+
} else {
|
|
58
|
+
multiCoords.push(coord);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
return polygon(multiCoords, properties);
|
|
62
|
+
default:
|
|
63
|
+
throw new Error("geometry type " + type + " is not supported");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
__name(lineStringToPolygon, "lineStringToPolygon");
|
|
67
|
+
function autoCompleteCoords(coords) {
|
|
68
|
+
var first = coords[0];
|
|
69
|
+
var x1 = first[0];
|
|
70
|
+
var y1 = first[1];
|
|
71
|
+
var last = coords[coords.length - 1];
|
|
72
|
+
var x2 = last[0];
|
|
73
|
+
var y2 = last[1];
|
|
74
|
+
if (x1 !== x2 || y1 !== y2) {
|
|
75
|
+
coords.push(first);
|
|
76
|
+
}
|
|
77
|
+
return coords;
|
|
78
|
+
}
|
|
79
|
+
__name(autoCompleteCoords, "autoCompleteCoords");
|
|
80
|
+
function calculateArea(bbox) {
|
|
81
|
+
var west = bbox[0];
|
|
82
|
+
var south = bbox[1];
|
|
83
|
+
var east = bbox[2];
|
|
84
|
+
var north = bbox[3];
|
|
85
|
+
return Math.abs(west - east) * Math.abs(south - north);
|
|
86
|
+
}
|
|
87
|
+
__name(calculateArea, "calculateArea");
|
|
88
|
+
var turf_line_to_polygon_default = lineToPolygon;
|
|
89
|
+
export {
|
|
90
|
+
turf_line_to_polygon_default as default,
|
|
91
|
+
lineToPolygon
|
|
92
|
+
};
|
|
93
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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 * @name lineToPolygon\n * @param {FeatureCollection|Feature<LineString|MultiLineString>} 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<Polygon|MultiPolygon>} 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<G extends LineString | MultiLineString>(\n lines: Feature<G> | FeatureCollection<G> | 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<LineString|MultiLineString>} 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<Polygon>} line converted to Polygon\n */\nfunction lineStringToPolygon<G extends LineString | MultiLineString>(\n line: Feature<G> | 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<Array<number>>} coords Coordinates\n * @returns {Array<Array<number>>} 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<number>} 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,SAAS,QAAQ,gBAAgB;AACjC,SAAS,WAAW,eAAe;AACnC,SAAS,SAAS,cAAc,kBAAkB;AAClD,SAAS,aAAa;AAqBtB,SAAS,cACP,OACA,UAKI,CAAC,GACL;AAzCF;AA2CE,MAAI,aAAa,QAAQ;AACzB,MAAI,gBAAe,aAAQ,iBAAR,YAAwB;AAC3C,MAAI,eAAc,aAAQ,gBAAR,YAAuB;AACzC,MAAI,UAAS,aAAQ,WAAR,YAAkB;AAE/B,MAAI,CAAC,QAAQ;AACX,YAAQ,MAAM,KAAK;AAAA,EACrB;AAEA,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK;AACH,UAAI,SAAyB,CAAC;AAC9B,YAAM,SAAS,QAAQ,SAAU,MAAM;AACrC,eAAO;AAAA,UACL,UAAU,oBAAoB,MAAM,CAAC,GAAG,cAAc,WAAW,CAAC;AAAA,QACpE;AAAA,MACF,CAAC;AACD,aAAO,aAAa,QAAQ,UAAU;AAAA,IACxC;AACE,aAAO,oBAAoB,OAAO,YAAY,cAAc,WAAW;AAAA,EAC3E;AACF;AA/BS;AA2CT,SAAS,oBACP,MACA,YACA,cACA,aACA;AACA,eAAa,aACT,aACA,KAAK,SAAS,YACZ,KAAK,aACL,CAAC;AACP,MAAI,OAAO,QAAQ,IAAI;AACvB,MAAI,SAAoC,KAAK;AAC7C,MAAI,OAAO,KAAK;AAEhB,MAAI,CAAC,OAAO;AAAQ,UAAM,IAAI,MAAM,+BAA+B;AAEnE,UAAQ,MAAM;AAAA,IACZ,KAAK;AACH,UAAI;AAAc,iBAAS,mBAAmB,MAAoB;AAClE,aAAO,QAAQ,CAAC,MAAoB,GAAG,UAAU;AAAA,IACnD,KAAK;AACH,UAAI,cAA4B,CAAC;AACjC,UAAI,cAAc;AAElB,MAAC,OAAwB,QAAQ,SAAU,OAAO;AAChD,YAAI;AAAc,kBAAQ,mBAAmB,KAAK;AAGlD,YAAI,aAAa;AACf,cAAI,OAAO,cAAc,SAAS,WAAW,KAAK,CAAC,CAAC;AACpD,cAAI,OAAO,aAAa;AACtB,wBAAY,QAAQ,KAAK;AACzB,0BAAc;AAAA,UAChB;AAAO,wBAAY,KAAK,KAAK;AAAA,QAC/B,OAAO;AACL,sBAAY,KAAK,KAAK;AAAA,QACxB;AAAA,MACF,CAAC;AACD,aAAO,QAAQ,aAAa,UAAU;AAAA,IACxC;AACE,YAAM,IAAI,MAAM,mBAAmB,OAAO,mBAAmB;AAAA,EACjE;AACF;AA3CS;AAoDT,SAAS,mBAAmB,QAAoB;AAC9C,MAAI,QAAQ,OAAO,CAAC;AACpB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,KAAK,MAAM,CAAC;AAChB,MAAI,OAAO,OAAO,OAAO,SAAS,CAAC;AACnC,MAAI,KAAK,KAAK,CAAC;AACf,MAAI,KAAK,KAAK,CAAC;AACf,MAAI,OAAO,MAAM,OAAO,IAAI;AAC1B,WAAO,KAAK,KAAK;AAAA,EACnB;AACA,SAAO;AACT;AAXS;AAoBT,SAAS,cAAc,MAAY;AACjC,MAAI,OAAO,KAAK,CAAC;AACjB,MAAI,QAAQ,KAAK,CAAC;AAClB,MAAI,OAAO,KAAK,CAAC;AACjB,MAAI,QAAQ,KAAK,CAAC;AAClB,SAAO,KAAK,IAAI,OAAO,IAAI,IAAI,KAAK,IAAI,QAAQ,KAAK;AACvD;AANS;AAST,IAAO,+BAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/line-to-polygon",
|
|
3
|
-
"version": "7.0.0-alpha.
|
|
3
|
+
"version": "7.0.0-alpha.110+1411d63a7",
|
|
4
4
|
"description": "turf line-to-polygon module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"contributors": [
|
|
@@ -26,47 +26,53 @@
|
|
|
26
26
|
"linestring",
|
|
27
27
|
"line"
|
|
28
28
|
],
|
|
29
|
-
"
|
|
30
|
-
"
|
|
29
|
+
"type": "commonjs",
|
|
30
|
+
"main": "dist/cjs/index.cjs",
|
|
31
|
+
"module": "dist/esm/index.mjs",
|
|
32
|
+
"types": "dist/cjs/index.d.ts",
|
|
31
33
|
"exports": {
|
|
32
34
|
"./package.json": "./package.json",
|
|
33
35
|
".": {
|
|
34
|
-
"import":
|
|
35
|
-
|
|
36
|
+
"import": {
|
|
37
|
+
"types": "./dist/esm/index.d.mts",
|
|
38
|
+
"default": "./dist/esm/index.mjs"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./dist/cjs/index.d.ts",
|
|
42
|
+
"default": "./dist/cjs/index.cjs"
|
|
43
|
+
}
|
|
36
44
|
}
|
|
37
45
|
},
|
|
38
|
-
"types": "dist/js/index.d.ts",
|
|
39
46
|
"sideEffects": false,
|
|
40
47
|
"files": [
|
|
41
48
|
"dist"
|
|
42
49
|
],
|
|
43
50
|
"scripts": {
|
|
44
|
-
"bench": "
|
|
45
|
-
"build": "
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"test": "npm-run-all test:*",
|
|
50
|
-
"test:tape": "ts-node -r esm test.js",
|
|
51
|
+
"bench": "tsx bench.ts",
|
|
52
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
53
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
54
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
55
|
+
"test:tape": "tsx test.ts",
|
|
51
56
|
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
52
57
|
},
|
|
53
58
|
"devDependencies": {
|
|
54
|
-
"@types/
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
59
|
+
"@types/benchmark": "^2.1.5",
|
|
60
|
+
"@types/tape": "^4.2.32",
|
|
61
|
+
"benchmark": "^2.1.4",
|
|
62
|
+
"load-json-file": "^7.0.1",
|
|
63
|
+
"npm-run-all": "^4.1.5",
|
|
64
|
+
"tape": "^5.7.2",
|
|
65
|
+
"tsup": "^8.0.1",
|
|
66
|
+
"tsx": "^4.6.2",
|
|
67
|
+
"typescript": "^5.2.2",
|
|
68
|
+
"write-json-file": "^5.0.0"
|
|
63
69
|
},
|
|
64
70
|
"dependencies": {
|
|
65
|
-
"@turf/bbox": "^7.0.0-alpha.
|
|
66
|
-
"@turf/clone": "^7.0.0-alpha.
|
|
67
|
-
"@turf/helpers": "^7.0.0-alpha.
|
|
68
|
-
"@turf/invariant": "^7.0.0-alpha.
|
|
69
|
-
"tslib": "^2.
|
|
71
|
+
"@turf/bbox": "^7.0.0-alpha.110+1411d63a7",
|
|
72
|
+
"@turf/clone": "^7.0.0-alpha.110+1411d63a7",
|
|
73
|
+
"@turf/helpers": "^7.0.0-alpha.110+1411d63a7",
|
|
74
|
+
"@turf/invariant": "^7.0.0-alpha.110+1411d63a7",
|
|
75
|
+
"tslib": "^2.6.2"
|
|
70
76
|
},
|
|
71
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "1411d63a74c275c9216fe48e9d3cb2d48a359068"
|
|
72
78
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import turfBBox from "@turf/bbox";
|
|
2
|
-
import { getCoords, getGeom } from "@turf/invariant";
|
|
3
|
-
import { polygon, multiPolygon, lineString } from "@turf/helpers";
|
|
4
|
-
import clone from "@turf/clone";
|
|
5
|
-
/**
|
|
6
|
-
* Converts (Multi)LineString(s) to Polygon(s).
|
|
7
|
-
*
|
|
8
|
-
* @name lineToPolygon
|
|
9
|
-
* @param {FeatureCollection|Feature<LineString|MultiLineString>} lines Features to convert
|
|
10
|
-
* @param {Object} [options={}] Optional parameters
|
|
11
|
-
* @param {Object} [options.properties={}] translates GeoJSON properties to Feature
|
|
12
|
-
* @param {boolean} [options.autoComplete=true] auto complete linestrings (matches first & last coordinates)
|
|
13
|
-
* @param {boolean} [options.orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates
|
|
14
|
-
* @param {boolean} [options.mutate=false] mutate the original linestring using autoComplete (matches first & last coordinates)
|
|
15
|
-
* @returns {Feature<Polygon|MultiPolygon>} converted to Polygons
|
|
16
|
-
* @example
|
|
17
|
-
* var line = turf.lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]);
|
|
18
|
-
*
|
|
19
|
-
* var polygon = turf.lineToPolygon(line);
|
|
20
|
-
*
|
|
21
|
-
* //addToMap
|
|
22
|
-
* var addToMap = [polygon];
|
|
23
|
-
*/
|
|
24
|
-
function lineToPolygon(lines, options = {}) {
|
|
25
|
-
var _a, _b, _c;
|
|
26
|
-
// Optional parameters
|
|
27
|
-
var properties = options.properties;
|
|
28
|
-
var autoComplete = (_a = options.autoComplete) !== null && _a !== void 0 ? _a : true;
|
|
29
|
-
var orderCoords = (_b = options.orderCoords) !== null && _b !== void 0 ? _b : true;
|
|
30
|
-
var mutate = (_c = options.mutate) !== null && _c !== void 0 ? _c : false;
|
|
31
|
-
if (!mutate) {
|
|
32
|
-
lines = clone(lines);
|
|
33
|
-
}
|
|
34
|
-
switch (lines.type) {
|
|
35
|
-
case "FeatureCollection":
|
|
36
|
-
var coords = [];
|
|
37
|
-
lines.features.forEach(function (line) {
|
|
38
|
-
coords.push(getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords)));
|
|
39
|
-
});
|
|
40
|
-
return multiPolygon(coords, properties);
|
|
41
|
-
default:
|
|
42
|
-
return lineStringToPolygon(lines, properties, autoComplete, orderCoords);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* LineString to Polygon
|
|
47
|
-
*
|
|
48
|
-
* @private
|
|
49
|
-
* @param {Feature<LineString|MultiLineString>} line line
|
|
50
|
-
* @param {Object} [properties] translates GeoJSON properties to Feature
|
|
51
|
-
* @param {boolean} [autoComplete=true] auto complete linestrings
|
|
52
|
-
* @param {boolean} [orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates
|
|
53
|
-
* @returns {Feature<Polygon>} line converted to Polygon
|
|
54
|
-
*/
|
|
55
|
-
function lineStringToPolygon(line, properties, autoComplete, orderCoords) {
|
|
56
|
-
properties = properties
|
|
57
|
-
? properties
|
|
58
|
-
: line.type === "Feature"
|
|
59
|
-
? line.properties
|
|
60
|
-
: {};
|
|
61
|
-
var geom = getGeom(line);
|
|
62
|
-
var coords = geom.coordinates;
|
|
63
|
-
var type = geom.type;
|
|
64
|
-
if (!coords.length)
|
|
65
|
-
throw new Error("line must contain coordinates");
|
|
66
|
-
switch (type) {
|
|
67
|
-
case "LineString":
|
|
68
|
-
if (autoComplete)
|
|
69
|
-
coords = autoCompleteCoords(coords);
|
|
70
|
-
return polygon([coords], properties);
|
|
71
|
-
case "MultiLineString":
|
|
72
|
-
var multiCoords = [];
|
|
73
|
-
var largestArea = 0;
|
|
74
|
-
coords.forEach(function (coord) {
|
|
75
|
-
if (autoComplete)
|
|
76
|
-
coord = autoCompleteCoords(coord);
|
|
77
|
-
// Largest LineString to be placed in the first position of the coordinates array
|
|
78
|
-
if (orderCoords) {
|
|
79
|
-
var area = calculateArea(turfBBox(lineString(coord)));
|
|
80
|
-
if (area > largestArea) {
|
|
81
|
-
multiCoords.unshift(coord);
|
|
82
|
-
largestArea = area;
|
|
83
|
-
}
|
|
84
|
-
else
|
|
85
|
-
multiCoords.push(coord);
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
multiCoords.push(coord);
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
return polygon(multiCoords, properties);
|
|
92
|
-
default:
|
|
93
|
-
throw new Error("geometry type " + type + " is not supported");
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Auto Complete Coords - matches first & last coordinates
|
|
98
|
-
*
|
|
99
|
-
* @private
|
|
100
|
-
* @param {Array<Array<number>>} coords Coordinates
|
|
101
|
-
* @returns {Array<Array<number>>} auto completed coordinates
|
|
102
|
-
*/
|
|
103
|
-
function autoCompleteCoords(coords) {
|
|
104
|
-
var first = coords[0];
|
|
105
|
-
var x1 = first[0];
|
|
106
|
-
var y1 = first[1];
|
|
107
|
-
var last = coords[coords.length - 1];
|
|
108
|
-
var x2 = last[0];
|
|
109
|
-
var y2 = last[1];
|
|
110
|
-
if (x1 !== x2 || y1 !== y2) {
|
|
111
|
-
coords.push(first);
|
|
112
|
-
}
|
|
113
|
-
return coords;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* area - quick approximate area calculation (used to sort)
|
|
117
|
-
*
|
|
118
|
-
* @private
|
|
119
|
-
* @param {Array<number>} bbox BBox [west, south, east, north]
|
|
120
|
-
* @returns {number} very quick area calculation
|
|
121
|
-
*/
|
|
122
|
-
function calculateArea(bbox) {
|
|
123
|
-
var west = bbox[0];
|
|
124
|
-
var south = bbox[1];
|
|
125
|
-
var east = bbox[2];
|
|
126
|
-
var north = bbox[3];
|
|
127
|
-
return Math.abs(west - east) * Math.abs(south - north);
|
|
128
|
-
}
|
|
129
|
-
export default lineToPolygon;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const bbox_1 = tslib_1.__importDefault(require("@turf/bbox"));
|
|
5
|
-
const invariant_1 = require("@turf/invariant");
|
|
6
|
-
const helpers_1 = require("@turf/helpers");
|
|
7
|
-
const clone_1 = tslib_1.__importDefault(require("@turf/clone"));
|
|
8
|
-
/**
|
|
9
|
-
* Converts (Multi)LineString(s) to Polygon(s).
|
|
10
|
-
*
|
|
11
|
-
* @name lineToPolygon
|
|
12
|
-
* @param {FeatureCollection|Feature<LineString|MultiLineString>} lines Features to convert
|
|
13
|
-
* @param {Object} [options={}] Optional parameters
|
|
14
|
-
* @param {Object} [options.properties={}] translates GeoJSON properties to Feature
|
|
15
|
-
* @param {boolean} [options.autoComplete=true] auto complete linestrings (matches first & last coordinates)
|
|
16
|
-
* @param {boolean} [options.orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates
|
|
17
|
-
* @param {boolean} [options.mutate=false] mutate the original linestring using autoComplete (matches first & last coordinates)
|
|
18
|
-
* @returns {Feature<Polygon|MultiPolygon>} converted to Polygons
|
|
19
|
-
* @example
|
|
20
|
-
* var line = turf.lineString([[125, -30], [145, -30], [145, -20], [125, -20], [125, -30]]);
|
|
21
|
-
*
|
|
22
|
-
* var polygon = turf.lineToPolygon(line);
|
|
23
|
-
*
|
|
24
|
-
* //addToMap
|
|
25
|
-
* var addToMap = [polygon];
|
|
26
|
-
*/
|
|
27
|
-
function lineToPolygon(lines, options = {}) {
|
|
28
|
-
var _a, _b, _c;
|
|
29
|
-
// Optional parameters
|
|
30
|
-
var properties = options.properties;
|
|
31
|
-
var autoComplete = (_a = options.autoComplete) !== null && _a !== void 0 ? _a : true;
|
|
32
|
-
var orderCoords = (_b = options.orderCoords) !== null && _b !== void 0 ? _b : true;
|
|
33
|
-
var mutate = (_c = options.mutate) !== null && _c !== void 0 ? _c : false;
|
|
34
|
-
if (!mutate) {
|
|
35
|
-
lines = clone_1.default(lines);
|
|
36
|
-
}
|
|
37
|
-
switch (lines.type) {
|
|
38
|
-
case "FeatureCollection":
|
|
39
|
-
var coords = [];
|
|
40
|
-
lines.features.forEach(function (line) {
|
|
41
|
-
coords.push(invariant_1.getCoords(lineStringToPolygon(line, {}, autoComplete, orderCoords)));
|
|
42
|
-
});
|
|
43
|
-
return helpers_1.multiPolygon(coords, properties);
|
|
44
|
-
default:
|
|
45
|
-
return lineStringToPolygon(lines, properties, autoComplete, orderCoords);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* LineString to Polygon
|
|
50
|
-
*
|
|
51
|
-
* @private
|
|
52
|
-
* @param {Feature<LineString|MultiLineString>} line line
|
|
53
|
-
* @param {Object} [properties] translates GeoJSON properties to Feature
|
|
54
|
-
* @param {boolean} [autoComplete=true] auto complete linestrings
|
|
55
|
-
* @param {boolean} [orderCoords=true] sorts linestrings to place outer ring at the first position of the coordinates
|
|
56
|
-
* @returns {Feature<Polygon>} line converted to Polygon
|
|
57
|
-
*/
|
|
58
|
-
function lineStringToPolygon(line, properties, autoComplete, orderCoords) {
|
|
59
|
-
properties = properties
|
|
60
|
-
? properties
|
|
61
|
-
: line.type === "Feature"
|
|
62
|
-
? line.properties
|
|
63
|
-
: {};
|
|
64
|
-
var geom = invariant_1.getGeom(line);
|
|
65
|
-
var coords = geom.coordinates;
|
|
66
|
-
var type = geom.type;
|
|
67
|
-
if (!coords.length)
|
|
68
|
-
throw new Error("line must contain coordinates");
|
|
69
|
-
switch (type) {
|
|
70
|
-
case "LineString":
|
|
71
|
-
if (autoComplete)
|
|
72
|
-
coords = autoCompleteCoords(coords);
|
|
73
|
-
return helpers_1.polygon([coords], properties);
|
|
74
|
-
case "MultiLineString":
|
|
75
|
-
var multiCoords = [];
|
|
76
|
-
var largestArea = 0;
|
|
77
|
-
coords.forEach(function (coord) {
|
|
78
|
-
if (autoComplete)
|
|
79
|
-
coord = autoCompleteCoords(coord);
|
|
80
|
-
// Largest LineString to be placed in the first position of the coordinates array
|
|
81
|
-
if (orderCoords) {
|
|
82
|
-
var area = calculateArea(bbox_1.default(helpers_1.lineString(coord)));
|
|
83
|
-
if (area > largestArea) {
|
|
84
|
-
multiCoords.unshift(coord);
|
|
85
|
-
largestArea = area;
|
|
86
|
-
}
|
|
87
|
-
else
|
|
88
|
-
multiCoords.push(coord);
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
multiCoords.push(coord);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
return helpers_1.polygon(multiCoords, properties);
|
|
95
|
-
default:
|
|
96
|
-
throw new Error("geometry type " + type + " is not supported");
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Auto Complete Coords - matches first & last coordinates
|
|
101
|
-
*
|
|
102
|
-
* @private
|
|
103
|
-
* @param {Array<Array<number>>} coords Coordinates
|
|
104
|
-
* @returns {Array<Array<number>>} auto completed coordinates
|
|
105
|
-
*/
|
|
106
|
-
function autoCompleteCoords(coords) {
|
|
107
|
-
var first = coords[0];
|
|
108
|
-
var x1 = first[0];
|
|
109
|
-
var y1 = first[1];
|
|
110
|
-
var last = coords[coords.length - 1];
|
|
111
|
-
var x2 = last[0];
|
|
112
|
-
var y2 = last[1];
|
|
113
|
-
if (x1 !== x2 || y1 !== y2) {
|
|
114
|
-
coords.push(first);
|
|
115
|
-
}
|
|
116
|
-
return coords;
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* area - quick approximate area calculation (used to sort)
|
|
120
|
-
*
|
|
121
|
-
* @private
|
|
122
|
-
* @param {Array<number>} bbox BBox [west, south, east, north]
|
|
123
|
-
* @returns {number} very quick area calculation
|
|
124
|
-
*/
|
|
125
|
-
function calculateArea(bbox) {
|
|
126
|
-
var west = bbox[0];
|
|
127
|
-
var south = bbox[1];
|
|
128
|
-
var east = bbox[2];
|
|
129
|
-
var north = bbox[3];
|
|
130
|
-
return Math.abs(west - east) * Math.abs(south - north);
|
|
131
|
-
}
|
|
132
|
-
exports.default = lineToPolygon;
|