@turf/triangle-grid 7.0.0-alpha.2 → 7.0.0
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 +138 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +31 -0
- package/dist/{js → esm}/index.d.ts +6 -4
- package/dist/esm/index.js +138 -0
- package/dist/esm/index.js.map +1 -0
- package/package.json +36 -31
- package/dist/es/index.js +0 -138
- package/dist/es/package.json +0 -1
- package/dist/js/index.js +0 -141
package/README.md
CHANGED
|
@@ -47,26 +47,21 @@ Returns **[FeatureCollection][8]<[Polygon][7]>** grid of polygons
|
|
|
47
47
|
|
|
48
48
|
[8]: https://tools.ietf.org/html/rfc7946#section-3.3
|
|
49
49
|
|
|
50
|
-
<!-- This file is automatically generated. Please don't edit it directly
|
|
51
|
-
if you find an error, edit the source file (likely index.js), and re-run
|
|
52
|
-
./scripts/generate-readmes in the turf project. -->
|
|
50
|
+
<!-- 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. -->
|
|
53
51
|
|
|
54
52
|
---
|
|
55
53
|
|
|
56
|
-
This module is part of the [Turfjs project](
|
|
57
|
-
module collection dedicated to geographic algorithms. It is maintained in the
|
|
58
|
-
[Turfjs/turf](https://github.com/Turfjs/turf) repository, where you can create
|
|
59
|
-
PRs and issues.
|
|
54
|
+
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.
|
|
60
55
|
|
|
61
56
|
### Installation
|
|
62
57
|
|
|
63
|
-
Install this module individually:
|
|
58
|
+
Install this single module individually:
|
|
64
59
|
|
|
65
60
|
```sh
|
|
66
61
|
$ npm install @turf/triangle-grid
|
|
67
62
|
```
|
|
68
63
|
|
|
69
|
-
Or install the
|
|
64
|
+
Or install the all-encompassing @turf/turf module that includes all modules as functions:
|
|
70
65
|
|
|
71
66
|
```sh
|
|
72
67
|
$ npm install @turf/turf
|
|
@@ -0,0 +1,138 @@
|
|
|
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 _distance = require('@turf/distance');
|
|
6
|
+
var _intersect = require('@turf/intersect');
|
|
7
|
+
var _helpers = require('@turf/helpers');
|
|
8
|
+
function triangleGrid(bbox, cellSide, options = {}) {
|
|
9
|
+
var results = [];
|
|
10
|
+
var xFraction = cellSide / _distance.distance.call(void 0, [bbox[0], bbox[1]], [bbox[2], bbox[1]], options);
|
|
11
|
+
var cellWidth = xFraction * (bbox[2] - bbox[0]);
|
|
12
|
+
var yFraction = cellSide / _distance.distance.call(void 0, [bbox[0], bbox[1]], [bbox[0], bbox[3]], options);
|
|
13
|
+
var cellHeight = yFraction * (bbox[3] - bbox[1]);
|
|
14
|
+
var xi = 0;
|
|
15
|
+
var currentX = bbox[0];
|
|
16
|
+
while (currentX <= bbox[2]) {
|
|
17
|
+
var yi = 0;
|
|
18
|
+
var currentY = bbox[1];
|
|
19
|
+
while (currentY <= bbox[3]) {
|
|
20
|
+
var cellTriangle1 = null;
|
|
21
|
+
var cellTriangle2 = null;
|
|
22
|
+
if (xi % 2 === 0 && yi % 2 === 0) {
|
|
23
|
+
cellTriangle1 = _helpers.polygon.call(void 0,
|
|
24
|
+
[
|
|
25
|
+
[
|
|
26
|
+
[currentX, currentY],
|
|
27
|
+
[currentX, currentY + cellHeight],
|
|
28
|
+
[currentX + cellWidth, currentY],
|
|
29
|
+
[currentX, currentY]
|
|
30
|
+
]
|
|
31
|
+
],
|
|
32
|
+
options.properties
|
|
33
|
+
);
|
|
34
|
+
cellTriangle2 = _helpers.polygon.call(void 0,
|
|
35
|
+
[
|
|
36
|
+
[
|
|
37
|
+
[currentX, currentY + cellHeight],
|
|
38
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
39
|
+
[currentX + cellWidth, currentY],
|
|
40
|
+
[currentX, currentY + cellHeight]
|
|
41
|
+
]
|
|
42
|
+
],
|
|
43
|
+
options.properties
|
|
44
|
+
);
|
|
45
|
+
} else if (xi % 2 === 0 && yi % 2 === 1) {
|
|
46
|
+
cellTriangle1 = _helpers.polygon.call(void 0,
|
|
47
|
+
[
|
|
48
|
+
[
|
|
49
|
+
[currentX, currentY],
|
|
50
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
51
|
+
[currentX + cellWidth, currentY],
|
|
52
|
+
[currentX, currentY]
|
|
53
|
+
]
|
|
54
|
+
],
|
|
55
|
+
options.properties
|
|
56
|
+
);
|
|
57
|
+
cellTriangle2 = _helpers.polygon.call(void 0,
|
|
58
|
+
[
|
|
59
|
+
[
|
|
60
|
+
[currentX, currentY],
|
|
61
|
+
[currentX, currentY + cellHeight],
|
|
62
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
63
|
+
[currentX, currentY]
|
|
64
|
+
]
|
|
65
|
+
],
|
|
66
|
+
options.properties
|
|
67
|
+
);
|
|
68
|
+
} else if (yi % 2 === 0 && xi % 2 === 1) {
|
|
69
|
+
cellTriangle1 = _helpers.polygon.call(void 0,
|
|
70
|
+
[
|
|
71
|
+
[
|
|
72
|
+
[currentX, currentY],
|
|
73
|
+
[currentX, currentY + cellHeight],
|
|
74
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
75
|
+
[currentX, currentY]
|
|
76
|
+
]
|
|
77
|
+
],
|
|
78
|
+
options.properties
|
|
79
|
+
);
|
|
80
|
+
cellTriangle2 = _helpers.polygon.call(void 0,
|
|
81
|
+
[
|
|
82
|
+
[
|
|
83
|
+
[currentX, currentY],
|
|
84
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
85
|
+
[currentX + cellWidth, currentY],
|
|
86
|
+
[currentX, currentY]
|
|
87
|
+
]
|
|
88
|
+
],
|
|
89
|
+
options.properties
|
|
90
|
+
);
|
|
91
|
+
} else if (yi % 2 === 1 && xi % 2 === 1) {
|
|
92
|
+
cellTriangle1 = _helpers.polygon.call(void 0,
|
|
93
|
+
[
|
|
94
|
+
[
|
|
95
|
+
[currentX, currentY],
|
|
96
|
+
[currentX, currentY + cellHeight],
|
|
97
|
+
[currentX + cellWidth, currentY],
|
|
98
|
+
[currentX, currentY]
|
|
99
|
+
]
|
|
100
|
+
],
|
|
101
|
+
options.properties
|
|
102
|
+
);
|
|
103
|
+
cellTriangle2 = _helpers.polygon.call(void 0,
|
|
104
|
+
[
|
|
105
|
+
[
|
|
106
|
+
[currentX, currentY + cellHeight],
|
|
107
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
108
|
+
[currentX + cellWidth, currentY],
|
|
109
|
+
[currentX, currentY + cellHeight]
|
|
110
|
+
]
|
|
111
|
+
],
|
|
112
|
+
options.properties
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (options.mask) {
|
|
116
|
+
if (_intersect.intersect.call(void 0, _helpers.featureCollection.call(void 0, [options.mask, cellTriangle1])))
|
|
117
|
+
results.push(cellTriangle1);
|
|
118
|
+
if (_intersect.intersect.call(void 0, _helpers.featureCollection.call(void 0, [options.mask, cellTriangle2])))
|
|
119
|
+
results.push(cellTriangle2);
|
|
120
|
+
} else {
|
|
121
|
+
results.push(cellTriangle1);
|
|
122
|
+
results.push(cellTriangle2);
|
|
123
|
+
}
|
|
124
|
+
currentY += cellHeight;
|
|
125
|
+
yi++;
|
|
126
|
+
}
|
|
127
|
+
xi++;
|
|
128
|
+
currentX += cellWidth;
|
|
129
|
+
}
|
|
130
|
+
return _helpers.featureCollection.call(void 0, results);
|
|
131
|
+
}
|
|
132
|
+
__name(triangleGrid, "triangleGrid");
|
|
133
|
+
var turf_triangle_grid_default = triangleGrid;
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
exports.default = turf_triangle_grid_default; exports.triangleGrid = triangleGrid;
|
|
138
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"names":[],"mappings":";;;;AAOA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAC1B,SAAS,SAAS,yBAAgC;AAuBlD,SAAS,aACP,MACA,UACA,UAII,CAAC,GAC0B;AAE/B,MAAI,UAAiC,CAAC;AAYtC,MAAI,YACF,WAAW,SAAS,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,OAAO;AACrE,MAAI,YAAY,aAAa,KAAK,CAAC,IAAI,KAAK,CAAC;AAC7C,MAAI,YACF,WAAW,SAAS,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,OAAO;AACrE,MAAI,aAAa,aAAa,KAAK,CAAC,IAAI,KAAK,CAAC;AAE9C,MAAI,KAAK;AACT,MAAI,WAAW,KAAK,CAAC;AACrB,SAAO,YAAY,KAAK,CAAC,GAAG;AAC1B,QAAI,KAAK;AACT,QAAI,WAAW,KAAK,CAAC;AACrB,WAAO,YAAY,KAAK,CAAC,GAAG;AAC1B,UAAI,gBAAgB;AACpB,UAAI,gBAAgB;AAEpB,UAAI,KAAK,MAAM,KAAK,KAAK,MAAM,GAAG;AAChC,wBAAgB;AAAA,UACd;AAAA,YACE;AAAA,cACE,CAAC,UAAU,QAAQ;AAAA,cACnB,CAAC,UAAU,WAAW,UAAU;AAAA,cAChC,CAAC,WAAW,WAAW,QAAQ;AAAA,cAC/B,CAAC,UAAU,QAAQ;AAAA,YACrB;AAAA,UACF;AAAA,UACA,QAAQ;AAAA,QACV;AACA,wBAAgB;AAAA,UACd;AAAA,YACE;AAAA,cACE,CAAC,UAAU,WAAW,UAAU;AAAA,cAChC,CAAC,WA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BAAgB;AAAA,UACd;AAAA,YACE;AAAA,cACE,CAAC,UAAU,QAAQ;AAAA,cACnB,CAAC,UAAU,WAAW,UAAU;AAAA,cAChC,CAAC,WAAW,WAAW,QAAQ;AAAA,cAC/B,CAAC,UAAU,QAAQ;AAAA,YACrB;AAAA,UACF;AAAA,UACA,QAAQ;AAAA,QACV;AACA,wBAAgB;AAAA,UACd;AAAA,YACE;AAAA,cACE,CAAC,UAAU,WAAW,UAAU;AAAA,cAChC,CAAC,WAAW,WAAW,WAAW,UAAU;AAAA,cAC5C,CAAC,WAAW,WAAW,QAAQ;AAAA,cAC/B,CAAC,UAAU,WAAW,UAAU;AAAA,YAClC;AAAA,UACF;AAAA,UACA,QAAQ;AAAA,QACV;AAAA,MACF;AACA,UAAI,QAAQ,MAAM;AAChB,YAAI,UAAU,kBAAkB,CAAC,QAAQ,MAAM,aAAc,CAAC,CAAC;AAC7D,kBAAQ,KAAK,aAAc;AAC7B,YAAI,UAAU,kBAAkB,CAAC,QAAQ,MAAM,aAAc,CAAC,CAAC;AAC7D,kBAAQ,KAAK,aAAc;AAAA,MAC/B,OAAO;AACL,gBAAQ,KAAK,aAAc;AAC3B,gBAAQ,KAAK,aAAc;AAAA,MAC7B;AAEA,kBAAY;AACZ;AAAA,IACF;AACA;AACA,gBAAY;AAAA,EACd;AACA,SAAO,kBAAkB,OAAO;AAClC;AApJS;AAuJT,IAAO,6BAAQ","sourcesContent":["import {\n BBox,\n Feature,\n Polygon,\n FeatureCollection,\n GeoJsonProperties,\n} from \"geojson\";\nimport { distance } from \"@turf/distance\";\nimport { intersect } from \"@turf/intersect\";\nimport { polygon, featureCollection, Units } from \"@turf/helpers\";\n\n/**\n * Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid.\n *\n * @name triangleGrid\n * @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order\n * @param {number} cellSide dimension of each cell\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers\n * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it\n * @param {Object} [options.properties={}] passed to each point of the grid\n * @returns {FeatureCollection<Polygon>} grid of polygons\n * @example\n * var bbox = [-95, 30 ,-85, 40];\n * var cellSide = 50;\n * var options = {units: 'miles'};\n *\n * var triangleGrid = turf.triangleGrid(bbox, cellSide, options);\n *\n * //addToMap\n * var addToMap = [triangleGrid];\n */\nfunction triangleGrid<P extends GeoJsonProperties = GeoJsonProperties>(\n bbox: BBox,\n cellSide: number,\n options: {\n units?: Units;\n properties?: P;\n mask?: Feature<Polygon>;\n } = {}\n): FeatureCollection<Polygon, P> {\n // Containers\n var results: Feature<Polygon, P>[] = [];\n\n // Input Validation is being handled by Typescript\n\n // if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');\n // if (!isNumber(cellSide)) throw new Error('cellSide is invalid');\n // if (!bbox) throw new Error('bbox is required');\n // if (!Array.isArray(bbox)) throw new Error('bbox must be array');\n // if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');\n // if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');\n\n // Main\n var xFraction =\n cellSide / distance([bbox[0], bbox[1]], [bbox[2], bbox[1]], options);\n var cellWidth = xFraction * (bbox[2] - bbox[0]);\n var yFraction =\n cellSide / distance([bbox[0], bbox[1]], [bbox[0], bbox[3]], options);\n var cellHeight = yFraction * (bbox[3] - bbox[1]);\n\n var xi = 0;\n var currentX = bbox[0];\n while (currentX <= bbox[2]) {\n var yi = 0;\n var currentY = bbox[1];\n while (currentY <= bbox[3]) {\n var cellTriangle1 = null;\n var cellTriangle2 = null;\n\n if (xi % 2 === 0 && yi % 2 === 0) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY + cellHeight],\n ],\n ],\n options.properties\n );\n } else if (xi % 2 === 0 && yi % 2 === 1) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n } else if (yi % 2 === 0 && xi % 2 === 1) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n } else if (yi % 2 === 1 && xi % 2 === 1) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY + cellHeight],\n ],\n ],\n options.properties\n );\n }\n if (options.mask) {\n if (intersect(featureCollection([options.mask, cellTriangle1!])))\n results.push(cellTriangle1!);\n if (intersect(featureCollection([options.mask, cellTriangle2!])))\n results.push(cellTriangle2!);\n } else {\n results.push(cellTriangle1!);\n results.push(cellTriangle2!);\n }\n\n currentY += cellHeight;\n yi++;\n }\n xi++;\n currentX += cellWidth;\n }\n return featureCollection(results);\n}\n\nexport { triangleGrid };\nexport default triangleGrid;\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { GeoJsonProperties, BBox, Feature, Polygon, FeatureCollection } from 'geojson';
|
|
2
|
+
import { Units } from '@turf/helpers';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid.
|
|
6
|
+
*
|
|
7
|
+
* @name triangleGrid
|
|
8
|
+
* @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
|
|
9
|
+
* @param {number} cellSide dimension of each cell
|
|
10
|
+
* @param {Object} [options={}] Optional parameters
|
|
11
|
+
* @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers
|
|
12
|
+
* @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
|
|
13
|
+
* @param {Object} [options.properties={}] passed to each point of the grid
|
|
14
|
+
* @returns {FeatureCollection<Polygon>} grid of polygons
|
|
15
|
+
* @example
|
|
16
|
+
* var bbox = [-95, 30 ,-85, 40];
|
|
17
|
+
* var cellSide = 50;
|
|
18
|
+
* var options = {units: 'miles'};
|
|
19
|
+
*
|
|
20
|
+
* var triangleGrid = turf.triangleGrid(bbox, cellSide, options);
|
|
21
|
+
*
|
|
22
|
+
* //addToMap
|
|
23
|
+
* var addToMap = [triangleGrid];
|
|
24
|
+
*/
|
|
25
|
+
declare function triangleGrid<P extends GeoJsonProperties = GeoJsonProperties>(bbox: BBox, cellSide: number, options?: {
|
|
26
|
+
units?: Units;
|
|
27
|
+
properties?: P;
|
|
28
|
+
mask?: Feature<Polygon>;
|
|
29
|
+
}): FeatureCollection<Polygon, P>;
|
|
30
|
+
|
|
31
|
+
export { triangleGrid as default, triangleGrid };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { BBox, Feature, Polygon, FeatureCollection
|
|
2
|
-
import { Units } from
|
|
1
|
+
import { GeoJsonProperties, BBox, Feature, Polygon, FeatureCollection } from 'geojson';
|
|
2
|
+
import { Units } from '@turf/helpers';
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid.
|
|
5
6
|
*
|
|
@@ -21,9 +22,10 @@ import { Units } from "@turf/helpers";
|
|
|
21
22
|
* //addToMap
|
|
22
23
|
* var addToMap = [triangleGrid];
|
|
23
24
|
*/
|
|
24
|
-
declare function triangleGrid<P = GeoJsonProperties>(bbox: BBox, cellSide: number, options?: {
|
|
25
|
+
declare function triangleGrid<P extends GeoJsonProperties = GeoJsonProperties>(bbox: BBox, cellSide: number, options?: {
|
|
25
26
|
units?: Units;
|
|
26
27
|
properties?: P;
|
|
27
28
|
mask?: Feature<Polygon>;
|
|
28
29
|
}): FeatureCollection<Polygon, P>;
|
|
29
|
-
|
|
30
|
+
|
|
31
|
+
export { triangleGrid as default, triangleGrid };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// index.ts
|
|
5
|
+
import { distance } from "@turf/distance";
|
|
6
|
+
import { intersect } from "@turf/intersect";
|
|
7
|
+
import { polygon, featureCollection } from "@turf/helpers";
|
|
8
|
+
function triangleGrid(bbox, cellSide, options = {}) {
|
|
9
|
+
var results = [];
|
|
10
|
+
var xFraction = cellSide / distance([bbox[0], bbox[1]], [bbox[2], bbox[1]], options);
|
|
11
|
+
var cellWidth = xFraction * (bbox[2] - bbox[0]);
|
|
12
|
+
var yFraction = cellSide / distance([bbox[0], bbox[1]], [bbox[0], bbox[3]], options);
|
|
13
|
+
var cellHeight = yFraction * (bbox[3] - bbox[1]);
|
|
14
|
+
var xi = 0;
|
|
15
|
+
var currentX = bbox[0];
|
|
16
|
+
while (currentX <= bbox[2]) {
|
|
17
|
+
var yi = 0;
|
|
18
|
+
var currentY = bbox[1];
|
|
19
|
+
while (currentY <= bbox[3]) {
|
|
20
|
+
var cellTriangle1 = null;
|
|
21
|
+
var cellTriangle2 = null;
|
|
22
|
+
if (xi % 2 === 0 && yi % 2 === 0) {
|
|
23
|
+
cellTriangle1 = polygon(
|
|
24
|
+
[
|
|
25
|
+
[
|
|
26
|
+
[currentX, currentY],
|
|
27
|
+
[currentX, currentY + cellHeight],
|
|
28
|
+
[currentX + cellWidth, currentY],
|
|
29
|
+
[currentX, currentY]
|
|
30
|
+
]
|
|
31
|
+
],
|
|
32
|
+
options.properties
|
|
33
|
+
);
|
|
34
|
+
cellTriangle2 = polygon(
|
|
35
|
+
[
|
|
36
|
+
[
|
|
37
|
+
[currentX, currentY + cellHeight],
|
|
38
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
39
|
+
[currentX + cellWidth, currentY],
|
|
40
|
+
[currentX, currentY + cellHeight]
|
|
41
|
+
]
|
|
42
|
+
],
|
|
43
|
+
options.properties
|
|
44
|
+
);
|
|
45
|
+
} else if (xi % 2 === 0 && yi % 2 === 1) {
|
|
46
|
+
cellTriangle1 = polygon(
|
|
47
|
+
[
|
|
48
|
+
[
|
|
49
|
+
[currentX, currentY],
|
|
50
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
51
|
+
[currentX + cellWidth, currentY],
|
|
52
|
+
[currentX, currentY]
|
|
53
|
+
]
|
|
54
|
+
],
|
|
55
|
+
options.properties
|
|
56
|
+
);
|
|
57
|
+
cellTriangle2 = polygon(
|
|
58
|
+
[
|
|
59
|
+
[
|
|
60
|
+
[currentX, currentY],
|
|
61
|
+
[currentX, currentY + cellHeight],
|
|
62
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
63
|
+
[currentX, currentY]
|
|
64
|
+
]
|
|
65
|
+
],
|
|
66
|
+
options.properties
|
|
67
|
+
);
|
|
68
|
+
} else if (yi % 2 === 0 && xi % 2 === 1) {
|
|
69
|
+
cellTriangle1 = polygon(
|
|
70
|
+
[
|
|
71
|
+
[
|
|
72
|
+
[currentX, currentY],
|
|
73
|
+
[currentX, currentY + cellHeight],
|
|
74
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
75
|
+
[currentX, currentY]
|
|
76
|
+
]
|
|
77
|
+
],
|
|
78
|
+
options.properties
|
|
79
|
+
);
|
|
80
|
+
cellTriangle2 = polygon(
|
|
81
|
+
[
|
|
82
|
+
[
|
|
83
|
+
[currentX, currentY],
|
|
84
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
85
|
+
[currentX + cellWidth, currentY],
|
|
86
|
+
[currentX, currentY]
|
|
87
|
+
]
|
|
88
|
+
],
|
|
89
|
+
options.properties
|
|
90
|
+
);
|
|
91
|
+
} else if (yi % 2 === 1 && xi % 2 === 1) {
|
|
92
|
+
cellTriangle1 = polygon(
|
|
93
|
+
[
|
|
94
|
+
[
|
|
95
|
+
[currentX, currentY],
|
|
96
|
+
[currentX, currentY + cellHeight],
|
|
97
|
+
[currentX + cellWidth, currentY],
|
|
98
|
+
[currentX, currentY]
|
|
99
|
+
]
|
|
100
|
+
],
|
|
101
|
+
options.properties
|
|
102
|
+
);
|
|
103
|
+
cellTriangle2 = polygon(
|
|
104
|
+
[
|
|
105
|
+
[
|
|
106
|
+
[currentX, currentY + cellHeight],
|
|
107
|
+
[currentX + cellWidth, currentY + cellHeight],
|
|
108
|
+
[currentX + cellWidth, currentY],
|
|
109
|
+
[currentX, currentY + cellHeight]
|
|
110
|
+
]
|
|
111
|
+
],
|
|
112
|
+
options.properties
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
if (options.mask) {
|
|
116
|
+
if (intersect(featureCollection([options.mask, cellTriangle1])))
|
|
117
|
+
results.push(cellTriangle1);
|
|
118
|
+
if (intersect(featureCollection([options.mask, cellTriangle2])))
|
|
119
|
+
results.push(cellTriangle2);
|
|
120
|
+
} else {
|
|
121
|
+
results.push(cellTriangle1);
|
|
122
|
+
results.push(cellTriangle2);
|
|
123
|
+
}
|
|
124
|
+
currentY += cellHeight;
|
|
125
|
+
yi++;
|
|
126
|
+
}
|
|
127
|
+
xi++;
|
|
128
|
+
currentX += cellWidth;
|
|
129
|
+
}
|
|
130
|
+
return featureCollection(results);
|
|
131
|
+
}
|
|
132
|
+
__name(triangleGrid, "triangleGrid");
|
|
133
|
+
var turf_triangle_grid_default = triangleGrid;
|
|
134
|
+
export {
|
|
135
|
+
turf_triangle_grid_default as default,
|
|
136
|
+
triangleGrid
|
|
137
|
+
};
|
|
138
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../index.ts"],"sourcesContent":["import {\n BBox,\n Feature,\n Polygon,\n FeatureCollection,\n GeoJsonProperties,\n} from \"geojson\";\nimport { distance } from \"@turf/distance\";\nimport { intersect } from \"@turf/intersect\";\nimport { polygon, featureCollection, Units } from \"@turf/helpers\";\n\n/**\n * Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid.\n *\n * @name triangleGrid\n * @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order\n * @param {number} cellSide dimension of each cell\n * @param {Object} [options={}] Optional parameters\n * @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers\n * @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it\n * @param {Object} [options.properties={}] passed to each point of the grid\n * @returns {FeatureCollection<Polygon>} grid of polygons\n * @example\n * var bbox = [-95, 30 ,-85, 40];\n * var cellSide = 50;\n * var options = {units: 'miles'};\n *\n * var triangleGrid = turf.triangleGrid(bbox, cellSide, options);\n *\n * //addToMap\n * var addToMap = [triangleGrid];\n */\nfunction triangleGrid<P extends GeoJsonProperties = GeoJsonProperties>(\n bbox: BBox,\n cellSide: number,\n options: {\n units?: Units;\n properties?: P;\n mask?: Feature<Polygon>;\n } = {}\n): FeatureCollection<Polygon, P> {\n // Containers\n var results: Feature<Polygon, P>[] = [];\n\n // Input Validation is being handled by Typescript\n\n // if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');\n // if (!isNumber(cellSide)) throw new Error('cellSide is invalid');\n // if (!bbox) throw new Error('bbox is required');\n // if (!Array.isArray(bbox)) throw new Error('bbox must be array');\n // if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');\n // if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');\n\n // Main\n var xFraction =\n cellSide / distance([bbox[0], bbox[1]], [bbox[2], bbox[1]], options);\n var cellWidth = xFraction * (bbox[2] - bbox[0]);\n var yFraction =\n cellSide / distance([bbox[0], bbox[1]], [bbox[0], bbox[3]], options);\n var cellHeight = yFraction * (bbox[3] - bbox[1]);\n\n var xi = 0;\n var currentX = bbox[0];\n while (currentX <= bbox[2]) {\n var yi = 0;\n var currentY = bbox[1];\n while (currentY <= bbox[3]) {\n var cellTriangle1 = null;\n var cellTriangle2 = null;\n\n if (xi % 2 === 0 && yi % 2 === 0) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY + cellHeight],\n ],\n ],\n options.properties\n );\n } else if (xi % 2 === 0 && yi % 2 === 1) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n } else if (yi % 2 === 0 && xi % 2 === 1) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n } else if (yi % 2 === 1 && xi % 2 === 1) {\n cellTriangle1 = polygon(\n [\n [\n [currentX, currentY],\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY],\n ],\n ],\n options.properties\n );\n cellTriangle2 = polygon(\n [\n [\n [currentX, currentY + cellHeight],\n [currentX + cellWidth, currentY + cellHeight],\n [currentX + cellWidth, currentY],\n [currentX, currentY + cellHeight],\n ],\n ],\n options.properties\n );\n }\n if (options.mask) {\n if (intersect(featureCollection([options.mask, cellTriangle1!])))\n results.push(cellTriangle1!);\n if (intersect(featureCollection([options.mask, cellTriangle2!])))\n results.push(cellTriangle2!);\n } else {\n results.push(cellTriangle1!);\n results.push(cellTriangle2!);\n }\n\n currentY += cellHeight;\n yi++;\n }\n xi++;\n currentX += cellWidth;\n }\n return featureCollection(results);\n}\n\nexport { triangleGrid };\nexport default triangleGrid;\n"],"mappings":";;;;AAOA,SAAS,gBAAgB;AACzB,SAAS,iBAAiB;AAC1B,SAAS,SAAS,yBAAgC;AAuBlD,SAAS,aACP,MACA,UACA,UAII,CAAC,GAC0B;AAE/B,MAAI,UAAiC,CAAC;AAYtC,MAAI,YACF,WAAW,SAAS,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,OAAO;AACrE,MAAI,YAAY,aAAa,KAAK,CAAC,IAAI,KAAK,CAAC;AAC7C,MAAI,YACF,WAAW,SAAS,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,OAAO;AACrE,MAAI,aAAa,aAAa,KAAK,CAAC,IAAI,KAAK,CAAC;AAE9C,MAAI,KA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;AAAA,MAC/B,OAAO;AACL,gBAAQ,KAAK,aAAc;AAC3B,gBAAQ,KAAK,aAAc;AAAA,MAC7B;AAEA,kBAAY;AACZ;AAAA,IACF;AACA;AACA,gBAAY;AAAA,EACd;AACA,SAAO,kBAAkB,OAAO;AAClC;AApJS;AAuJT,IAAO,6BAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@turf/triangle-grid",
|
|
3
|
-
"version": "7.0.0
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"description": "turf triangle-grid module",
|
|
5
5
|
"author": "Turf Authors",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,49 +24,54 @@
|
|
|
24
24
|
"analysis",
|
|
25
25
|
"gis"
|
|
26
26
|
],
|
|
27
|
-
"
|
|
28
|
-
"
|
|
27
|
+
"type": "module",
|
|
28
|
+
"main": "dist/cjs/index.cjs",
|
|
29
|
+
"module": "dist/esm/index.js",
|
|
30
|
+
"types": "dist/esm/index.d.ts",
|
|
29
31
|
"exports": {
|
|
30
32
|
"./package.json": "./package.json",
|
|
31
33
|
".": {
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
"import": {
|
|
35
|
+
"types": "./dist/esm/index.d.ts",
|
|
36
|
+
"default": "./dist/esm/index.js"
|
|
37
|
+
},
|
|
38
|
+
"require": {
|
|
39
|
+
"types": "./dist/cjs/index.d.cts",
|
|
40
|
+
"default": "./dist/cjs/index.cjs"
|
|
41
|
+
}
|
|
35
42
|
}
|
|
36
43
|
},
|
|
37
|
-
"types": "dist/js/index.d.ts",
|
|
38
44
|
"sideEffects": false,
|
|
39
45
|
"files": [
|
|
40
46
|
"dist"
|
|
41
47
|
],
|
|
42
48
|
"scripts": {
|
|
43
|
-
"bench": "tsx bench.
|
|
44
|
-
"build": "
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"test": "
|
|
49
|
-
"test:tape": "tsx test.js",
|
|
50
|
-
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts"
|
|
49
|
+
"bench": "tsx bench.ts",
|
|
50
|
+
"build": "tsup --config ../../tsup.config.ts",
|
|
51
|
+
"docs": "tsx ../../scripts/generate-readmes.ts",
|
|
52
|
+
"test": "npm-run-all --npm-path npm test:*",
|
|
53
|
+
"test:tape": "tsx test.ts",
|
|
54
|
+
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
|
|
51
55
|
},
|
|
52
56
|
"devDependencies": {
|
|
53
|
-
"@turf/bbox-polygon": "^7.0.0
|
|
54
|
-
"@turf/truncate": "^7.0.0
|
|
55
|
-
"@types/
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
57
|
+
"@turf/bbox-polygon": "^7.0.0",
|
|
58
|
+
"@turf/truncate": "^7.0.0",
|
|
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"
|
|
64
69
|
},
|
|
65
70
|
"dependencies": {
|
|
66
|
-
"@turf/distance": "^7.0.0
|
|
67
|
-
"@turf/helpers": "^7.0.0
|
|
68
|
-
"@turf/intersect": "^7.0.0
|
|
69
|
-
"tslib": "^2.
|
|
71
|
+
"@turf/distance": "^7.0.0",
|
|
72
|
+
"@turf/helpers": "^7.0.0",
|
|
73
|
+
"@turf/intersect": "^7.0.0",
|
|
74
|
+
"tslib": "^2.6.2"
|
|
70
75
|
},
|
|
71
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "3d3a7917025fbabe191dbddbc89754b86f9c7739"
|
|
72
77
|
}
|
package/dist/es/index.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import distance from "@turf/distance";
|
|
2
|
-
import intersect from "@turf/intersect";
|
|
3
|
-
import { polygon, featureCollection } from "@turf/helpers";
|
|
4
|
-
/**
|
|
5
|
-
* Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid.
|
|
6
|
-
*
|
|
7
|
-
* @name triangleGrid
|
|
8
|
-
* @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
|
|
9
|
-
* @param {number} cellSide dimension of each cell
|
|
10
|
-
* @param {Object} [options={}] Optional parameters
|
|
11
|
-
* @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers
|
|
12
|
-
* @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
|
|
13
|
-
* @param {Object} [options.properties={}] passed to each point of the grid
|
|
14
|
-
* @returns {FeatureCollection<Polygon>} grid of polygons
|
|
15
|
-
* @example
|
|
16
|
-
* var bbox = [-95, 30 ,-85, 40];
|
|
17
|
-
* var cellSide = 50;
|
|
18
|
-
* var options = {units: 'miles'};
|
|
19
|
-
*
|
|
20
|
-
* var triangleGrid = turf.triangleGrid(bbox, cellSide, options);
|
|
21
|
-
*
|
|
22
|
-
* //addToMap
|
|
23
|
-
* var addToMap = [triangleGrid];
|
|
24
|
-
*/
|
|
25
|
-
function triangleGrid(bbox, cellSide, options = {}) {
|
|
26
|
-
// Containers
|
|
27
|
-
var results = [];
|
|
28
|
-
// Input Validation is being handled by Typescript
|
|
29
|
-
// if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');
|
|
30
|
-
// if (!isNumber(cellSide)) throw new Error('cellSide is invalid');
|
|
31
|
-
// if (!bbox) throw new Error('bbox is required');
|
|
32
|
-
// if (!Array.isArray(bbox)) throw new Error('bbox must be array');
|
|
33
|
-
// if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');
|
|
34
|
-
// if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');
|
|
35
|
-
// Main
|
|
36
|
-
var xFraction = cellSide / distance([bbox[0], bbox[1]], [bbox[2], bbox[1]], options);
|
|
37
|
-
var cellWidth = xFraction * (bbox[2] - bbox[0]);
|
|
38
|
-
var yFraction = cellSide / distance([bbox[0], bbox[1]], [bbox[0], bbox[3]], options);
|
|
39
|
-
var cellHeight = yFraction * (bbox[3] - bbox[1]);
|
|
40
|
-
var xi = 0;
|
|
41
|
-
var currentX = bbox[0];
|
|
42
|
-
while (currentX <= bbox[2]) {
|
|
43
|
-
var yi = 0;
|
|
44
|
-
var currentY = bbox[1];
|
|
45
|
-
while (currentY <= bbox[3]) {
|
|
46
|
-
var cellTriangle1 = null;
|
|
47
|
-
var cellTriangle2 = null;
|
|
48
|
-
if (xi % 2 === 0 && yi % 2 === 0) {
|
|
49
|
-
cellTriangle1 = polygon([
|
|
50
|
-
[
|
|
51
|
-
[currentX, currentY],
|
|
52
|
-
[currentX, currentY + cellHeight],
|
|
53
|
-
[currentX + cellWidth, currentY],
|
|
54
|
-
[currentX, currentY],
|
|
55
|
-
],
|
|
56
|
-
], options.properties);
|
|
57
|
-
cellTriangle2 = polygon([
|
|
58
|
-
[
|
|
59
|
-
[currentX, currentY + cellHeight],
|
|
60
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
61
|
-
[currentX + cellWidth, currentY],
|
|
62
|
-
[currentX, currentY + cellHeight],
|
|
63
|
-
],
|
|
64
|
-
], options.properties);
|
|
65
|
-
}
|
|
66
|
-
else if (xi % 2 === 0 && yi % 2 === 1) {
|
|
67
|
-
cellTriangle1 = polygon([
|
|
68
|
-
[
|
|
69
|
-
[currentX, currentY],
|
|
70
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
71
|
-
[currentX + cellWidth, currentY],
|
|
72
|
-
[currentX, currentY],
|
|
73
|
-
],
|
|
74
|
-
], options.properties);
|
|
75
|
-
cellTriangle2 = polygon([
|
|
76
|
-
[
|
|
77
|
-
[currentX, currentY],
|
|
78
|
-
[currentX, currentY + cellHeight],
|
|
79
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
80
|
-
[currentX, currentY],
|
|
81
|
-
],
|
|
82
|
-
], options.properties);
|
|
83
|
-
}
|
|
84
|
-
else if (yi % 2 === 0 && xi % 2 === 1) {
|
|
85
|
-
cellTriangle1 = polygon([
|
|
86
|
-
[
|
|
87
|
-
[currentX, currentY],
|
|
88
|
-
[currentX, currentY + cellHeight],
|
|
89
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
90
|
-
[currentX, currentY],
|
|
91
|
-
],
|
|
92
|
-
], options.properties);
|
|
93
|
-
cellTriangle2 = polygon([
|
|
94
|
-
[
|
|
95
|
-
[currentX, currentY],
|
|
96
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
97
|
-
[currentX + cellWidth, currentY],
|
|
98
|
-
[currentX, currentY],
|
|
99
|
-
],
|
|
100
|
-
], options.properties);
|
|
101
|
-
}
|
|
102
|
-
else if (yi % 2 === 1 && xi % 2 === 1) {
|
|
103
|
-
cellTriangle1 = polygon([
|
|
104
|
-
[
|
|
105
|
-
[currentX, currentY],
|
|
106
|
-
[currentX, currentY + cellHeight],
|
|
107
|
-
[currentX + cellWidth, currentY],
|
|
108
|
-
[currentX, currentY],
|
|
109
|
-
],
|
|
110
|
-
], options.properties);
|
|
111
|
-
cellTriangle2 = polygon([
|
|
112
|
-
[
|
|
113
|
-
[currentX, currentY + cellHeight],
|
|
114
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
115
|
-
[currentX + cellWidth, currentY],
|
|
116
|
-
[currentX, currentY + cellHeight],
|
|
117
|
-
],
|
|
118
|
-
], options.properties);
|
|
119
|
-
}
|
|
120
|
-
if (options.mask) {
|
|
121
|
-
if (intersect(featureCollection([options.mask, cellTriangle1])))
|
|
122
|
-
results.push(cellTriangle1);
|
|
123
|
-
if (intersect(featureCollection([options.mask, cellTriangle2])))
|
|
124
|
-
results.push(cellTriangle2);
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
results.push(cellTriangle1);
|
|
128
|
-
results.push(cellTriangle2);
|
|
129
|
-
}
|
|
130
|
-
currentY += cellHeight;
|
|
131
|
-
yi++;
|
|
132
|
-
}
|
|
133
|
-
xi++;
|
|
134
|
-
currentX += cellWidth;
|
|
135
|
-
}
|
|
136
|
-
return featureCollection(results);
|
|
137
|
-
}
|
|
138
|
-
export default triangleGrid;
|
package/dist/es/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"type":"module"}
|
package/dist/js/index.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const distance_1 = tslib_1.__importDefault(require("@turf/distance"));
|
|
5
|
-
const intersect_1 = tslib_1.__importDefault(require("@turf/intersect"));
|
|
6
|
-
const helpers_1 = require("@turf/helpers");
|
|
7
|
-
/**
|
|
8
|
-
* Takes a bounding box and a cell depth and returns a set of triangular {@link Polygon|polygons} in a grid.
|
|
9
|
-
*
|
|
10
|
-
* @name triangleGrid
|
|
11
|
-
* @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
|
|
12
|
-
* @param {number} cellSide dimension of each cell
|
|
13
|
-
* @param {Object} [options={}] Optional parameters
|
|
14
|
-
* @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers
|
|
15
|
-
* @param {Feature<Polygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
|
|
16
|
-
* @param {Object} [options.properties={}] passed to each point of the grid
|
|
17
|
-
* @returns {FeatureCollection<Polygon>} grid of polygons
|
|
18
|
-
* @example
|
|
19
|
-
* var bbox = [-95, 30 ,-85, 40];
|
|
20
|
-
* var cellSide = 50;
|
|
21
|
-
* var options = {units: 'miles'};
|
|
22
|
-
*
|
|
23
|
-
* var triangleGrid = turf.triangleGrid(bbox, cellSide, options);
|
|
24
|
-
*
|
|
25
|
-
* //addToMap
|
|
26
|
-
* var addToMap = [triangleGrid];
|
|
27
|
-
*/
|
|
28
|
-
function triangleGrid(bbox, cellSide, options = {}) {
|
|
29
|
-
// Containers
|
|
30
|
-
var results = [];
|
|
31
|
-
// Input Validation is being handled by Typescript
|
|
32
|
-
// if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');
|
|
33
|
-
// if (!isNumber(cellSide)) throw new Error('cellSide is invalid');
|
|
34
|
-
// if (!bbox) throw new Error('bbox is required');
|
|
35
|
-
// if (!Array.isArray(bbox)) throw new Error('bbox must be array');
|
|
36
|
-
// if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');
|
|
37
|
-
// if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');
|
|
38
|
-
// Main
|
|
39
|
-
var xFraction = cellSide / distance_1.default([bbox[0], bbox[1]], [bbox[2], bbox[1]], options);
|
|
40
|
-
var cellWidth = xFraction * (bbox[2] - bbox[0]);
|
|
41
|
-
var yFraction = cellSide / distance_1.default([bbox[0], bbox[1]], [bbox[0], bbox[3]], options);
|
|
42
|
-
var cellHeight = yFraction * (bbox[3] - bbox[1]);
|
|
43
|
-
var xi = 0;
|
|
44
|
-
var currentX = bbox[0];
|
|
45
|
-
while (currentX <= bbox[2]) {
|
|
46
|
-
var yi = 0;
|
|
47
|
-
var currentY = bbox[1];
|
|
48
|
-
while (currentY <= bbox[3]) {
|
|
49
|
-
var cellTriangle1 = null;
|
|
50
|
-
var cellTriangle2 = null;
|
|
51
|
-
if (xi % 2 === 0 && yi % 2 === 0) {
|
|
52
|
-
cellTriangle1 = helpers_1.polygon([
|
|
53
|
-
[
|
|
54
|
-
[currentX, currentY],
|
|
55
|
-
[currentX, currentY + cellHeight],
|
|
56
|
-
[currentX + cellWidth, currentY],
|
|
57
|
-
[currentX, currentY],
|
|
58
|
-
],
|
|
59
|
-
], options.properties);
|
|
60
|
-
cellTriangle2 = helpers_1.polygon([
|
|
61
|
-
[
|
|
62
|
-
[currentX, currentY + cellHeight],
|
|
63
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
64
|
-
[currentX + cellWidth, currentY],
|
|
65
|
-
[currentX, currentY + cellHeight],
|
|
66
|
-
],
|
|
67
|
-
], options.properties);
|
|
68
|
-
}
|
|
69
|
-
else if (xi % 2 === 0 && yi % 2 === 1) {
|
|
70
|
-
cellTriangle1 = helpers_1.polygon([
|
|
71
|
-
[
|
|
72
|
-
[currentX, currentY],
|
|
73
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
74
|
-
[currentX + cellWidth, currentY],
|
|
75
|
-
[currentX, currentY],
|
|
76
|
-
],
|
|
77
|
-
], options.properties);
|
|
78
|
-
cellTriangle2 = helpers_1.polygon([
|
|
79
|
-
[
|
|
80
|
-
[currentX, currentY],
|
|
81
|
-
[currentX, currentY + cellHeight],
|
|
82
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
83
|
-
[currentX, currentY],
|
|
84
|
-
],
|
|
85
|
-
], options.properties);
|
|
86
|
-
}
|
|
87
|
-
else if (yi % 2 === 0 && xi % 2 === 1) {
|
|
88
|
-
cellTriangle1 = helpers_1.polygon([
|
|
89
|
-
[
|
|
90
|
-
[currentX, currentY],
|
|
91
|
-
[currentX, currentY + cellHeight],
|
|
92
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
93
|
-
[currentX, currentY],
|
|
94
|
-
],
|
|
95
|
-
], options.properties);
|
|
96
|
-
cellTriangle2 = helpers_1.polygon([
|
|
97
|
-
[
|
|
98
|
-
[currentX, currentY],
|
|
99
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
100
|
-
[currentX + cellWidth, currentY],
|
|
101
|
-
[currentX, currentY],
|
|
102
|
-
],
|
|
103
|
-
], options.properties);
|
|
104
|
-
}
|
|
105
|
-
else if (yi % 2 === 1 && xi % 2 === 1) {
|
|
106
|
-
cellTriangle1 = helpers_1.polygon([
|
|
107
|
-
[
|
|
108
|
-
[currentX, currentY],
|
|
109
|
-
[currentX, currentY + cellHeight],
|
|
110
|
-
[currentX + cellWidth, currentY],
|
|
111
|
-
[currentX, currentY],
|
|
112
|
-
],
|
|
113
|
-
], options.properties);
|
|
114
|
-
cellTriangle2 = helpers_1.polygon([
|
|
115
|
-
[
|
|
116
|
-
[currentX, currentY + cellHeight],
|
|
117
|
-
[currentX + cellWidth, currentY + cellHeight],
|
|
118
|
-
[currentX + cellWidth, currentY],
|
|
119
|
-
[currentX, currentY + cellHeight],
|
|
120
|
-
],
|
|
121
|
-
], options.properties);
|
|
122
|
-
}
|
|
123
|
-
if (options.mask) {
|
|
124
|
-
if (intersect_1.default(helpers_1.featureCollection([options.mask, cellTriangle1])))
|
|
125
|
-
results.push(cellTriangle1);
|
|
126
|
-
if (intersect_1.default(helpers_1.featureCollection([options.mask, cellTriangle2])))
|
|
127
|
-
results.push(cellTriangle2);
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
results.push(cellTriangle1);
|
|
131
|
-
results.push(cellTriangle2);
|
|
132
|
-
}
|
|
133
|
-
currentY += cellHeight;
|
|
134
|
-
yi++;
|
|
135
|
-
}
|
|
136
|
-
xi++;
|
|
137
|
-
currentX += cellWidth;
|
|
138
|
-
}
|
|
139
|
-
return helpers_1.featureCollection(results);
|
|
140
|
-
}
|
|
141
|
-
exports.default = triangleGrid;
|