geogrid 0.0.1
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.min.js +24 -0
- package/package.json +63 -0
- package/src/grid/diamond.js +50 -0
- package/src/grid/dot.js +36 -0
- package/src/grid/h3.js +48 -0
- package/src/grid/hexbin.js +52 -0
- package/src/grid/random.js +44 -0
- package/src/grid/square.js +45 -0
- package/src/grid/triangle.js +66 -0
- package/src/helpers/rewind.js +45 -0
- package/src/helpers/rewind2.ts +159 -0
- package/src/index.js +21 -0
- package/src/operator/pointstogrid.js +49 -0
- package/src/operator/polygonstogrid.js +68 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { featureCollection } from "@turf/helpers";
|
|
2
|
+
import { intersect } from "@turf/intersect";
|
|
3
|
+
import { geoPath } from "d3-geo";
|
|
4
|
+
import { groups } from "d3-array";
|
|
5
|
+
const d3 = Object.assign({}, { geoPath, groups });
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @function op.polygonstogrid
|
|
9
|
+
* @description The `op.polygonstogrid()` function allows to affect polygons values tu grid cells.
|
|
10
|
+
* @property {object} [polygons] - polygons geoJSON
|
|
11
|
+
* @property {object} [grid] - grid geoJSON
|
|
12
|
+
* @property {string} [var = undefined] - field (absolute quantitative data only)
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export function polygonstogrid(
|
|
16
|
+
opts = {
|
|
17
|
+
grid: undefined,
|
|
18
|
+
polygons: undefined,
|
|
19
|
+
var: undefined,
|
|
20
|
+
}
|
|
21
|
+
) {
|
|
22
|
+
let grid = opts.grid.features;
|
|
23
|
+
let polys = opts.polygons.features;
|
|
24
|
+
let gridbyindex = new Map(grid.map((d, i) => [i, d]));
|
|
25
|
+
|
|
26
|
+
let arr = [];
|
|
27
|
+
polys.forEach((p, i) => {
|
|
28
|
+
const area = d3.geoPath().area(p);
|
|
29
|
+
grid.forEach((g, j) => {
|
|
30
|
+
const f = intersect(featureCollection([p, g]));
|
|
31
|
+
if (f !== null) {
|
|
32
|
+
let areapiece = d3.geoPath().area(f);
|
|
33
|
+
arr.push([i, j, areapiece / area]);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
let accessor;
|
|
39
|
+
if (opts.var) {
|
|
40
|
+
accessor = new Map(
|
|
41
|
+
polys.map((d, i) => [i, parseFloat(d.properties[opts.var]) || 0])
|
|
42
|
+
);
|
|
43
|
+
} else {
|
|
44
|
+
accessor = new Map(polys.map((d, i) => [i, 1]));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let datagrid = d3.groups(arr, (d) => d[1]);
|
|
48
|
+
|
|
49
|
+
function getsum(cell) {
|
|
50
|
+
let sum = 0;
|
|
51
|
+
cell[1].forEach((d) => {
|
|
52
|
+
sum += accessor.get(d[0]) * d[2];
|
|
53
|
+
});
|
|
54
|
+
return sum == 0 ? undefined : sum;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
type: "FeatureCollection",
|
|
59
|
+
features: datagrid.map((d) => {
|
|
60
|
+
let tmp = gridbyindex.get(d[0]);
|
|
61
|
+
return {
|
|
62
|
+
type: tmp.type,
|
|
63
|
+
properties: { ...tmp.properties, sum: getsum(d) },
|
|
64
|
+
geometry: tmp.geometry,
|
|
65
|
+
};
|
|
66
|
+
}),
|
|
67
|
+
};
|
|
68
|
+
}
|