@maplat/tin 1.0.0 → 1.1.0-rc.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.
@@ -1,114 +0,0 @@
1
- import type {
2
- PropertyTriKey,
3
- TinsBD,
4
- Tri,
5
- WeightBufferBD,
6
- } from "@maplat/transform";
7
-
8
- interface WeightBufferOptions {
9
- tins: TinsBD;
10
- targets: Array<keyof TinsBD>;
11
- includeReciprocals: boolean;
12
- /** Number of boundary vertices (b0..b{N-1}). Defaults to 4 for v2 format. */
13
- numBoundaryVertices?: number;
14
- }
15
-
16
- /**
17
- * Calculate per-point stretch ratios used for non-linear interpolation.
18
- */
19
- export function buildPointsWeightBuffer(
20
- options: WeightBufferOptions,
21
- ): WeightBufferBD {
22
- const { tins, targets, includeReciprocals, numBoundaryVertices = 4 } = options;
23
- const edgeRatios: WeightBufferBD = {};
24
-
25
- targets.forEach((target) => {
26
- const triCollection = tins[target];
27
- if (!triCollection || !triCollection.features) return;
28
-
29
- edgeRatios[target as keyof WeightBufferBD] = {};
30
- const seenEdges: Record<string, boolean> = {};
31
-
32
- triCollection.features.forEach((tri: Tri) => {
33
- const props = ["a", "b", "c"] as PropertyTriKey[];
34
- for (let i = 0; i < 3; i++) {
35
- const j = (i + 1) % 3;
36
- const prop_i = props[i];
37
- const prop_j = props[j];
38
- const index_i = tri.properties![prop_i].index;
39
- const index_j = tri.properties![prop_j].index;
40
- const key = [index_i, index_j].sort().join("-");
41
-
42
- if (seenEdges[key]) continue;
43
- seenEdges[key] = true;
44
-
45
- const i_xy = tri.geometry!.coordinates[0][i];
46
- const j_xy = tri.geometry!.coordinates[0][j];
47
- const i_merc = tri.properties![prop_i].geom;
48
- const j_merc = tri.properties![prop_j].geom;
49
-
50
- const ratio = Math.sqrt(
51
- Math.pow(i_merc[0] - j_merc[0], 2) +
52
- Math.pow(i_merc[1] - j_merc[1], 2),
53
- ) / Math.sqrt(
54
- Math.pow(i_xy[0] - j_xy[0], 2) + Math.pow(i_xy[1] - j_xy[1], 2),
55
- );
56
-
57
- const targetBuffer = edgeRatios[target as keyof WeightBufferBD]!;
58
- targetBuffer[`${index_i}:${key}`] = ratio;
59
- targetBuffer[`${index_j}:${key}`] = ratio;
60
- }
61
- });
62
- });
63
-
64
- const pointsWeightBuffer: WeightBufferBD = {};
65
- if (includeReciprocals) {
66
- pointsWeightBuffer.bakw = {};
67
- }
68
-
69
- targets.forEach((target) => {
70
- const targetBuffer = edgeRatios[target as keyof WeightBufferBD];
71
- pointsWeightBuffer[target as keyof WeightBufferBD] = {};
72
-
73
- if (!targetBuffer) {
74
- return;
75
- }
76
-
77
- const pointWeights: Record<string, number[]> = {};
78
- Object.keys(targetBuffer).forEach((key) => {
79
- const [pointId] = key.split(":");
80
- if (!pointWeights[pointId]) {
81
- pointWeights[pointId] = [];
82
- }
83
- pointWeights[pointId].push(targetBuffer[key]);
84
- });
85
-
86
- Object.keys(pointWeights).forEach((pointId) => {
87
- const weights = pointWeights[pointId];
88
- const avgWeight = weights.reduce((sum, w) => sum + w, 0) / weights.length;
89
-
90
- pointsWeightBuffer[target as keyof WeightBufferBD]![pointId] = avgWeight;
91
-
92
- if (includeReciprocals && pointsWeightBuffer.bakw) {
93
- pointsWeightBuffer.bakw[pointId] = 1 / avgWeight;
94
- }
95
- });
96
-
97
- let centroidSum = 0;
98
- for (let i = 0; i < numBoundaryVertices; i++) {
99
- const key = `b${i}`;
100
- const weight =
101
- pointsWeightBuffer[target as keyof WeightBufferBD]![key] || 0;
102
- centroidSum += weight;
103
- }
104
- pointsWeightBuffer[target as keyof WeightBufferBD]!["c"] =
105
- centroidSum / numBoundaryVertices;
106
-
107
- if (includeReciprocals && pointsWeightBuffer.bakw) {
108
- pointsWeightBuffer.bakw["c"] = 1 /
109
- pointsWeightBuffer[target as keyof WeightBufferBD]!["c"];
110
- }
111
- });
112
-
113
- return pointsWeightBuffer;
114
- }