geogrid 0.0.1 → 0.0.3
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 +6 -0
- package/dist/index.min.js +2 -2
- package/package.json +10 -5
- package/src/grid/diamond.js +37 -13
- package/src/grid/dot.js +26 -10
- package/src/grid/h3.js +3 -3
- package/src/grid/hexbin.js +58 -31
- package/src/grid/random.js +29 -8
- package/src/grid/square.js +35 -10
- package/src/grid/triangle.js +92 -24
- package/src/helpers/createSteppedArray.js +7 -0
- package/src/index.js +9 -21
- package/src/operator/pointstogrid.js +44 -21
- package/src/operator/polygonstogrid.js +56 -34
- package/src/operator/project.js +14 -0
- package/src/helpers/rewind.js +0 -45
- package/src/helpers/rewind2.ts +0 -159
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "geogrid",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "Regular and irregular geoJSON grids ",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
7
7
|
"jsdelivr": "dist/index.min.js",
|
|
@@ -24,9 +24,10 @@
|
|
|
24
24
|
"url": "git+https://github.com/neocarto/geogrid.git"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
27
|
+
"grid",
|
|
28
|
+
"regular mesh",
|
|
29
|
+
"maps",
|
|
30
|
+
"geo"
|
|
30
31
|
],
|
|
31
32
|
"author": "Nicolas Lambert",
|
|
32
33
|
"license": "MIT",
|
|
@@ -35,16 +36,20 @@
|
|
|
35
36
|
},
|
|
36
37
|
"homepage": "https://github.com/neocarto/geogrid#readme",
|
|
37
38
|
"dependencies": {
|
|
39
|
+
"@turf/bbox": "^7.2.0",
|
|
38
40
|
"@turf/boolean-point-in-polygon": "^7.1.0",
|
|
39
41
|
"@turf/helpers": "^7.1.0",
|
|
40
42
|
"@turf/intersect": "^7.1.0",
|
|
41
43
|
"d3-array": "^3.2.4",
|
|
42
44
|
"d3-delaunay": "^6.0.4",
|
|
43
45
|
"d3-geo": "^3.1.1",
|
|
46
|
+
"d3-geo-projection": "^4.0.0",
|
|
44
47
|
"docs": "^0.3.2-canary.0",
|
|
45
48
|
"documentation": "^14.0.3",
|
|
46
49
|
"geojson2h3": "^1.2.0",
|
|
50
|
+
"geotoolbox": "^3.0.3",
|
|
47
51
|
"h3-js": "^4.1.0",
|
|
52
|
+
"rbush": "^4.0.1",
|
|
48
53
|
"rollup": "^4.10.0"
|
|
49
54
|
},
|
|
50
55
|
"devDependencies": {
|
package/src/grid/diamond.js
CHANGED
|
@@ -1,26 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const d3 = Object.assign({}, { range });
|
|
1
|
+
import { createSteppedArray } from "../helpers/createSteppedArray.js";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
4
|
+
* @function diamond
|
|
5
|
+
* @summary Compute a diamond grid.
|
|
6
|
+
* @description The `diamond()` function allows to create a diamond grid in SVG coordinates.
|
|
7
|
+
* @param {number} [step = 50] - Step of the grid.
|
|
8
|
+
* @param {array} [start = [0,0]] - Positioning coordinates [x,y].
|
|
9
|
+
* @param {number} [width = 1000] - Width of the grid
|
|
10
|
+
* @param {number} [height = 500] - Height of the grid
|
|
11
|
+
* @param {boolean} [overflow = true] - Depending on the step you choose, the grid may be smaller than the bounding box defined by with and height. With overflow = true, the grid is allowed to exceed the bounding box.
|
|
12
|
+
* @returns {object} - A GeoJSON FeatureCollection
|
|
13
|
+
* @example
|
|
14
|
+
* geogrid.diamond({step:30})
|
|
10
15
|
*/
|
|
11
|
-
export function diamond({
|
|
16
|
+
export function diamond({
|
|
17
|
+
start = [0, 0],
|
|
18
|
+
width = 1000,
|
|
19
|
+
height = 500,
|
|
20
|
+
step = 50,
|
|
21
|
+
overflow = true,
|
|
22
|
+
} = {}) {
|
|
12
23
|
let size = step * Math.sqrt(2);
|
|
13
24
|
|
|
14
|
-
|
|
15
|
-
let
|
|
16
|
-
let
|
|
25
|
+
let x0 = overflow ? start[0] : start[0] + size / 2;
|
|
26
|
+
let y0 = overflow ? start[1] : start[1] + size / 2;
|
|
27
|
+
let xend = start[0] + width + size / 2;
|
|
28
|
+
let yend = start[1] + height + size / 2;
|
|
29
|
+
|
|
30
|
+
let x = createSteppedArray(x0, xend, size);
|
|
31
|
+
let y = createSteppedArray(y0, yend, size / 2, false);
|
|
17
32
|
let grid = x.map((x) => y.map((y, j) => [x, y, j % 2])).flat();
|
|
18
33
|
grid = grid.map((d) => {
|
|
19
|
-
return d[2]
|
|
34
|
+
return d[2] === 1 ? [d[0] + size / 2, d[1]] : [d[0], d[1]];
|
|
20
35
|
});
|
|
21
36
|
|
|
37
|
+
grid = overflow
|
|
38
|
+
? grid.filter((d) => d[0] <= start[0] + width + size / 2)
|
|
39
|
+
: grid.filter((d) => d[0] <= start[0] + width - size / 2);
|
|
40
|
+
|
|
41
|
+
grid = overflow
|
|
42
|
+
? grid.filter((d) => d[1] <= start[1] + height + size / 2)
|
|
43
|
+
: grid.filter((d) => d[1] <= start[1] + height - size / 2);
|
|
44
|
+
|
|
22
45
|
let s = size / 2;
|
|
23
|
-
|
|
46
|
+
|
|
24
47
|
let result = grid.map((d, i) => {
|
|
25
48
|
return {
|
|
26
49
|
type: "Feature",
|
|
@@ -41,6 +64,7 @@ export function diamond({ step = 50, width = 1000, height = 500 } = {}) {
|
|
|
41
64
|
},
|
|
42
65
|
};
|
|
43
66
|
});
|
|
67
|
+
|
|
44
68
|
return {
|
|
45
69
|
type: "FeatureCollection",
|
|
46
70
|
grid: "diamond",
|
package/src/grid/dot.js
CHANGED
|
@@ -1,17 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const d3 = Object.assign({}, { range });
|
|
1
|
+
import { createSteppedArray } from "../helpers/createSteppedArray.js";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
4
|
+
* @function dot
|
|
5
|
+
* @summary Compute a dot grid.
|
|
6
|
+
* @description The `dot()` function allows to create a square grid in SVG coordinates.
|
|
7
|
+
* @param {number} [step = 50] - Step of the grid.
|
|
8
|
+
* @param {array} [start = [0,0]] - Positioning coordinates [x,y].
|
|
9
|
+
* @param {number} [width = 1000] - Width of the grid
|
|
10
|
+
* @param {number} [height = 500] - Height of the grid
|
|
11
|
+
* @param {boolean} [overflow = true] - Depending on the step you choose, the grid may be smaller than the bounding box defined by with and height. With overflow = true, the grid is allowed to exceed the bounding box.
|
|
12
|
+
* @returns {object} - A GeoJSON FeatureCollection
|
|
13
|
+
* @example
|
|
14
|
+
* geogrid.dot({step:30})
|
|
10
15
|
*/
|
|
11
|
-
export function dot({
|
|
16
|
+
export function dot({
|
|
17
|
+
step = 30,
|
|
18
|
+
start = [0, 0],
|
|
19
|
+
width = 1000,
|
|
20
|
+
height = 500,
|
|
21
|
+
overflow = true,
|
|
22
|
+
} = {}) {
|
|
12
23
|
// build grid
|
|
13
|
-
let
|
|
14
|
-
let
|
|
24
|
+
let x0 = overflow ? start[0] - step / 2 : start[0];
|
|
25
|
+
let y0 = overflow ? start[1] - step / 2 : start[1];
|
|
26
|
+
let xend = overflow ? start[0] + width + step : start[0] + width;
|
|
27
|
+
let yend = overflow ? start[1] + height + step : start[1] + height;
|
|
28
|
+
let x = createSteppedArray(x0, xend, step);
|
|
29
|
+
let y = createSteppedArray(y0, yend, step, true);
|
|
30
|
+
|
|
15
31
|
let grid = x.map((x) => y.map((y) => [x, y])).flat();
|
|
16
32
|
let s = step / 2;
|
|
17
33
|
// build object
|
package/src/grid/h3.js
CHANGED
|
@@ -6,11 +6,11 @@ import {
|
|
|
6
6
|
} from "h3-js";
|
|
7
7
|
|
|
8
8
|
import { featureToH3Set, h3SetToFeatureCollection } from "geojson2h3";
|
|
9
|
-
import { rewind as rrewind } from "
|
|
9
|
+
import { rewind as rrewind } from "geotoolbox";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* @function
|
|
13
|
-
* @description The `
|
|
12
|
+
* @function h3
|
|
13
|
+
* @description The `h3()` function allows to create a hexbin geoJSON grid in geographical coordinates.
|
|
14
14
|
* @property {number} [level = 0] - level of the grid. Form 0 (large hexagons) to 4 (small hexagons). See: https://h3geo.org
|
|
15
15
|
* @property {object} [domain] - a geoJSON to define an extent
|
|
16
16
|
* @property {boolen} [rewind] - to rewind the output
|
package/src/grid/hexbin.js
CHANGED
|
@@ -1,52 +1,79 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const d3 = Object.assign({}, { range, max });
|
|
3
|
-
|
|
1
|
+
import { createSteppedArray } from "../helpers/createSteppedArray.js";
|
|
4
2
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
3
|
+
* @function hexbin
|
|
4
|
+
* @summary Compute a hexbin grid.
|
|
5
|
+
* @description The `hexbin()` function allows to create a hexbin grid in SVG coordinates.
|
|
6
|
+
* @param {number} [step = 50] - Step of the grid.
|
|
7
|
+
* @param {array} [start = [0,0]] - Positioning coordinates [x,y].
|
|
8
|
+
* @param {number} [width = 1000] - Width of the grid
|
|
9
|
+
* @param {number} [height = 500] - Height of the grid
|
|
10
|
+
* @param {boolean} [overflow = true] - Depending on the step you choose, the grid may be smaller than the bounding box defined by with and height. With overflow = true, the grid is allowed to exceed the bounding box.
|
|
11
|
+
* @returns {object} - A GeoJSON FeatureCollection
|
|
12
|
+
* @example
|
|
13
|
+
* geogrid.hexbin({step:30})
|
|
10
14
|
*/
|
|
11
|
-
export function hexbin({
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
export function hexbin({
|
|
16
|
+
step = 50,
|
|
17
|
+
width = 1000,
|
|
18
|
+
height = 500,
|
|
19
|
+
start = [0, 0],
|
|
20
|
+
overflow = true,
|
|
21
|
+
} = {}) {
|
|
22
|
+
const w = step;
|
|
23
|
+
const size = w / Math.sqrt(3);
|
|
24
|
+
const h = size * 1.5;
|
|
25
|
+
|
|
26
|
+
const x0 = overflow ? start[0] - w / 2 : start[0];
|
|
27
|
+
//const x0 = start[0];
|
|
28
|
+
const y0 = overflow ? start[1] - size : start[1];
|
|
29
|
+
|
|
30
|
+
const yEnd = overflow ? height + y0 + size * 2 : height + y0 - size;
|
|
31
|
+
const y = createSteppedArray(y0 + size, yEnd, h);
|
|
32
|
+
|
|
33
|
+
const grid = [];
|
|
34
|
+
|
|
35
|
+
// Pour chaque ligne Y
|
|
36
|
+
y.forEach((yy, row) => {
|
|
37
|
+
const offset = (row % 2) * (w / 2);
|
|
38
|
+
let xEnd = overflow ? width + x0 : width + x0 - w - w / 2;
|
|
39
|
+
|
|
40
|
+
const x = createSteppedArray(x0 + offset + w / 2, xEnd, w);
|
|
41
|
+
|
|
42
|
+
x.forEach((xx) => {
|
|
43
|
+
if (xx <= width + x0) {
|
|
44
|
+
grid.push([xx, yy]);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (x[x.length - 1] < width + x0) {
|
|
49
|
+
grid.push([x[x.length - 1] + w, yy]);
|
|
50
|
+
}
|
|
25
51
|
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
let
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
hex.push([d[0] + size * Math.cos(ang), d[1] + size * Math.sin(ang)]);
|
|
52
|
+
|
|
53
|
+
const features = grid.map(([cx, cy], i) => {
|
|
54
|
+
const hex = [];
|
|
55
|
+
for (let a = 0; a < 6; a++) {
|
|
56
|
+
const angle = (Math.PI / 180) * (60 * a - 30);
|
|
57
|
+
hex.push([cx + size * Math.cos(angle), cy + size * Math.sin(angle)]);
|
|
33
58
|
}
|
|
59
|
+
hex.push(hex[0]);
|
|
34
60
|
|
|
35
61
|
return {
|
|
36
62
|
type: "Feature",
|
|
37
63
|
geometry: {
|
|
38
64
|
type: "Polygon",
|
|
39
|
-
coordinates: [
|
|
65
|
+
coordinates: [hex],
|
|
40
66
|
},
|
|
41
67
|
properties: {
|
|
42
68
|
index: i,
|
|
43
69
|
},
|
|
44
70
|
};
|
|
45
71
|
});
|
|
72
|
+
|
|
46
73
|
return {
|
|
47
74
|
type: "FeatureCollection",
|
|
48
75
|
grid: "hexbin",
|
|
49
76
|
geo: false,
|
|
50
|
-
features:
|
|
77
|
+
features: features,
|
|
51
78
|
};
|
|
52
79
|
}
|
package/src/grid/random.js
CHANGED
|
@@ -2,25 +2,44 @@ import { Delaunay } from "d3-delaunay";
|
|
|
2
2
|
const d3 = Object.assign({}, { Delaunay });
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
5
|
+
* @function hexbin
|
|
6
|
+
* @summary Compute a hexbin grid.
|
|
7
|
+
* @description The `hexbin()` function allows to create a hexbin grid in SVG coordinates.
|
|
8
|
+
* @param {number} [step = 50] - Step of the grid.
|
|
9
|
+
* @param {array} [start = [0,0]] - Positioning coordinates [x,y].
|
|
10
|
+
* @param {number} [width = 1000] - Width of the grid
|
|
11
|
+
* @param {number} [height = 500] - Height of the grid
|
|
12
|
+
* @param {boolean} [overflow = true] - Depending on the step you choose, the grid may be smaller than the bounding box defined by with and height. With overflow = true, the grid is allowed to exceed the bounding box.
|
|
13
|
+
* @returns {object} - A GeoJSON FeatureCollection
|
|
14
|
+
* @example
|
|
15
|
+
* geogrid.hexbin({step:30})
|
|
10
16
|
*/
|
|
11
17
|
|
|
12
|
-
export function random({
|
|
18
|
+
export function random({
|
|
19
|
+
start = [0, 0],
|
|
20
|
+
width = 1000,
|
|
21
|
+
height = 500,
|
|
22
|
+
step = 50,
|
|
23
|
+
overflow = false,
|
|
24
|
+
} = {}) {
|
|
25
|
+
let x0 = overflow ? start[0] - step : start[0];
|
|
26
|
+
let y0 = overflow ? start[1] - step : start[1];
|
|
27
|
+
let xend = overflow ? start[0] + width + step : x0 + width;
|
|
28
|
+
let yend = overflow ? start[1] + height + step : y0 + height;
|
|
29
|
+
|
|
30
|
+
let w = overflow ? width + step * 2 : width;
|
|
31
|
+
let h = overflow ? height + step * 2 : height;
|
|
13
32
|
let grid = [];
|
|
14
33
|
let nb = Math.round((width / step) * (height / step));
|
|
15
34
|
for (let i = 0; i < nb; i++) {
|
|
16
|
-
grid.push([Math.random() *
|
|
35
|
+
grid.push([Math.random() * w + x0, Math.random() * h + y0]);
|
|
17
36
|
}
|
|
18
37
|
|
|
19
38
|
let voronoi = d3.Delaunay.from(
|
|
20
39
|
grid,
|
|
21
40
|
(d) => d[0],
|
|
22
41
|
(d) => d[1]
|
|
23
|
-
).voronoi([
|
|
42
|
+
).voronoi([x0, y0, xend, yend]);
|
|
24
43
|
|
|
25
44
|
// build object
|
|
26
45
|
let result = grid.map((d, i) => {
|
|
@@ -35,6 +54,8 @@ export function random({ step = 50, width = 1000, height = 500 } = {}) {
|
|
|
35
54
|
},
|
|
36
55
|
};
|
|
37
56
|
});
|
|
57
|
+
|
|
58
|
+
// return result;
|
|
38
59
|
return {
|
|
39
60
|
type: "FeatureCollection",
|
|
40
61
|
grid: "random",
|
package/src/grid/square.js
CHANGED
|
@@ -1,17 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const d3 = Object.assign({}, { range });
|
|
1
|
+
import { createSteppedArray } from "../helpers/createSteppedArray.js";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
4
|
+
* @function square
|
|
5
|
+
* @summary Compute a square grid.
|
|
6
|
+
* @description The `square()` function allows to create a square grid in SVG coordinates.
|
|
7
|
+
* @param {number} [step = 50] - Step of the grid.
|
|
8
|
+
* @param {array} [start = [0,0]] - Positioning coordinates [x,y].
|
|
9
|
+
* @param {number} [width = 1000] - Width of the grid
|
|
10
|
+
* @param {number} [height = 500] - Height of the grid
|
|
11
|
+
* @param {boolean} [overflow = true] - Depending on the step you choose, the grid may be smaller than the bounding box defined by with and height. With overflow = true, the grid is allowed to exceed the bounding box.
|
|
12
|
+
* @returns {object} - A GeoJSON FeatureCollection
|
|
13
|
+
* @example
|
|
14
|
+
* geogrid.square({step:30})
|
|
10
15
|
*/
|
|
11
|
-
export function square({
|
|
16
|
+
export function square({
|
|
17
|
+
start = [0, 0],
|
|
18
|
+
width = 1000,
|
|
19
|
+
height = 500,
|
|
20
|
+
step = 50,
|
|
21
|
+
overflow = true,
|
|
22
|
+
} = {}) {
|
|
12
23
|
// build grid
|
|
13
|
-
|
|
14
|
-
let
|
|
24
|
+
|
|
25
|
+
let x0 = overflow ? start[0] : start[0] + step / 2;
|
|
26
|
+
let y0 = overflow ? start[1] : start[1] + step / 2;
|
|
27
|
+
|
|
28
|
+
let y = createSteppedArray(y0, start[1] + height - step / 2, step, true);
|
|
29
|
+
let x = createSteppedArray(x0, start[0] + width - step / 2, step);
|
|
30
|
+
|
|
31
|
+
if (overflow) {
|
|
32
|
+
if (y[0] + step / 2 < start[1] + height) {
|
|
33
|
+
y.unshift(y[0] + step);
|
|
34
|
+
}
|
|
35
|
+
if (x.at(-1) + step / 2 < start[0] + width) {
|
|
36
|
+
x.push(x.at(-1) + step);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
15
40
|
let grid = x.map((x) => y.map((y) => [x, y])).flat();
|
|
16
41
|
|
|
17
42
|
let s = step / 2;
|
package/src/grid/triangle.js
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
const d3 = Object.assign({}, { range, max });
|
|
1
|
+
import { createSteppedArray } from "../helpers/createSteppedArray.js";
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
* @function
|
|
6
|
-
* @
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
4
|
+
* @function triangle
|
|
5
|
+
* @summary Compute a triangle grid.
|
|
6
|
+
* @description The `triangle()` function allows to create a triangle grid in SVG coordinates.
|
|
7
|
+
* @param {number} [step = 50] - Step of the grid.
|
|
8
|
+
* @param {array} [start = [0,0]] - Positioning coordinates [x,y].
|
|
9
|
+
* @param {number} [width = 1000] - Width of the grid
|
|
10
|
+
* @param {number} [height = 500] - Height of the grid
|
|
11
|
+
* @param {boolean} [overflow = true] - Depending on the step you choose, the grid may be smaller than the bounding box defined by with and height. With overflow = true, the grid is allowed to exceed the bounding box.
|
|
12
|
+
* @returns {object} - A GeoJSON FeatureCollection
|
|
13
|
+
* @example
|
|
14
|
+
* geogrid.triangle({step:30})
|
|
10
15
|
*/
|
|
11
|
-
export function triangle({
|
|
16
|
+
export function triangle({
|
|
17
|
+
start = [0, 0],
|
|
18
|
+
width = 1000,
|
|
19
|
+
height = 500,
|
|
20
|
+
step = 50,
|
|
21
|
+
overflow = true,
|
|
22
|
+
} = {}) {
|
|
12
23
|
let triangletop = (p, size) => {
|
|
13
24
|
let h = (Math.sqrt(3) / 2) * size;
|
|
14
25
|
let p1 = [p[0] + size / 2, p[1]];
|
|
@@ -29,12 +40,18 @@ export function triangle({ step = 50, width = 1000, height = 500 } = {}) {
|
|
|
29
40
|
let h = (Math.sqrt(3) / 2) * step;
|
|
30
41
|
|
|
31
42
|
// build grid
|
|
43
|
+
let x0 = overflow ? start[0] - step / 2 : start[0];
|
|
44
|
+
let y0 = overflow ? start[1] - h / 2 : start[1];
|
|
45
|
+
let xend = start[0] + width + h;
|
|
46
|
+
let yend = x0 + height + h;
|
|
47
|
+
|
|
48
|
+
let x = createSteppedArray(x0, xend, step);
|
|
49
|
+
let y = createSteppedArray(y0, yend, h, true);
|
|
32
50
|
|
|
33
|
-
let y = d3.range(0, height + size, h).reverse();
|
|
34
51
|
if (y.length % 2) {
|
|
35
|
-
y.unshift(
|
|
52
|
+
y.unshift(y[0] + h);
|
|
36
53
|
}
|
|
37
|
-
|
|
54
|
+
|
|
38
55
|
let grid = x.map((x, i) => y.map((y) => [x, y])).flat();
|
|
39
56
|
grid = grid.map((d, i) => {
|
|
40
57
|
return i % 2 == 1 ? [d[0] + step / 2, d[1]] : d;
|
|
@@ -43,20 +60,71 @@ export function triangle({ step = 50, width = 1000, height = 500 } = {}) {
|
|
|
43
60
|
let nb = grid.length;
|
|
44
61
|
grid = grid.concat(grid);
|
|
45
62
|
|
|
46
|
-
//
|
|
47
|
-
let
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
// Build triangles
|
|
64
|
+
let triangles = [];
|
|
65
|
+
grid.forEach((d, i) => {
|
|
66
|
+
if (overflow) {
|
|
67
|
+
// triangle top
|
|
68
|
+
if (i < nb) {
|
|
69
|
+
if (
|
|
70
|
+
d[0] <= x0 + width + step &&
|
|
71
|
+
d[0] > x0 &&
|
|
72
|
+
d[1] >= y0 + step / 2 &&
|
|
73
|
+
d[1] <= start[1] + height + h
|
|
74
|
+
) {
|
|
75
|
+
triangles.push(triangletop(d, step));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//triangle bottom
|
|
79
|
+
else {
|
|
80
|
+
if (
|
|
81
|
+
d[0] <= x0 + width + step &&
|
|
82
|
+
d[0] > x0 &&
|
|
83
|
+
d[1] <= start[1] + height
|
|
84
|
+
) {
|
|
85
|
+
triangles.push(trianglebottom(d, step));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
// triangle top
|
|
90
|
+
if (i < nb) {
|
|
91
|
+
if (
|
|
92
|
+
d[0] <= x0 + width - step / 2 &&
|
|
93
|
+
d[0] > x0 &&
|
|
94
|
+
d[1] <= y0 + height &&
|
|
95
|
+
d[1] > y0
|
|
96
|
+
) {
|
|
97
|
+
triangles.push(triangletop(d, step));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//triangle bottom
|
|
101
|
+
else {
|
|
102
|
+
if (
|
|
103
|
+
d[0] <= x0 + width - step / 2 &&
|
|
104
|
+
d[0] > x0 &&
|
|
105
|
+
d[1] <= y0 + height - h
|
|
106
|
+
) {
|
|
107
|
+
triangles.push(trianglebottom(d, step));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
59
111
|
});
|
|
112
|
+
|
|
113
|
+
// Build geojson
|
|
114
|
+
|
|
115
|
+
let result = triangles.map((d, i) => ({
|
|
116
|
+
type: "Feature",
|
|
117
|
+
geometry: {
|
|
118
|
+
type: "Polygon",
|
|
119
|
+
//coordinates: d
|
|
120
|
+
coordinates: [d],
|
|
121
|
+
},
|
|
122
|
+
properties: {
|
|
123
|
+
index: i,
|
|
124
|
+
coords: d,
|
|
125
|
+
},
|
|
126
|
+
}));
|
|
127
|
+
|
|
60
128
|
return {
|
|
61
129
|
type: "FeatureCollection",
|
|
62
130
|
grid: "triangle",
|
package/src/index.js
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
square,
|
|
11
|
-
triangle,
|
|
12
|
-
dot,
|
|
13
|
-
diamond,
|
|
14
|
-
random,
|
|
15
|
-
hexbin,
|
|
16
|
-
h3,
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
import { pointstogrid } from "./operator/pointstogrid.js";
|
|
20
|
-
import { polygonstogrid } from "./operator/polygonstogrid.js";
|
|
21
|
-
export let op = { pointstogrid, polygonstogrid };
|
|
1
|
+
export { square } from "./grid/square.js";
|
|
2
|
+
export { triangle } from "./grid/triangle.js";
|
|
3
|
+
export { dot } from "./grid/dot.js";
|
|
4
|
+
export { random } from "./grid/random.js";
|
|
5
|
+
export { diamond } from "./grid/diamond.js";
|
|
6
|
+
export { hexbin } from "./grid/hexbin.js";
|
|
7
|
+
export { h3 } from "./grid/h3.js";
|
|
8
|
+
export { pointstogrid } from "./operator/pointstogrid.js";
|
|
9
|
+
export { polygonstogrid } from "./operator/polygonstogrid.js";
|