@maplat/transform 0.1.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.
@@ -0,0 +1,180 @@
1
+ import { polygon } from "@turf/helpers";
2
+ import { Position } from "geojson";
3
+ import { PropertyTriKey, Tri, Tins } from "./geometry";
4
+
5
+ /**
6
+ * 三角形の頂点の順序を修正する
7
+ * 地図外郭の頂点を含む三角形について、頂点の順序を統一する
8
+ * @param tins 三角形群
9
+ * @returns 頂点順序が修正された三角形群
10
+ */
11
+ function rotateVerticesTriangle(tins: Tins) {
12
+ const features = tins.features;
13
+ for (let i = 0; i < features.length; i++) {
14
+ const feature = features[i];
15
+ if (
16
+ `${feature.properties!.a.index}`.substring(0, 1) === "b" &&
17
+ `${feature.properties!.b.index}`.substring(0, 1) === "b"
18
+ ) {
19
+ features[i] = {
20
+ geometry: {
21
+ type: "Polygon",
22
+ coordinates: [
23
+ [
24
+ feature.geometry!.coordinates[0][2],
25
+ feature.geometry!.coordinates[0][0],
26
+ feature.geometry!.coordinates[0][1],
27
+ feature.geometry!.coordinates[0][2]
28
+ ]
29
+ ]
30
+ },
31
+ properties: {
32
+ a: {
33
+ geom: feature.properties!.c.geom,
34
+ index: feature.properties!.c.index
35
+ },
36
+ b: {
37
+ geom: feature.properties!.a.geom,
38
+ index: feature.properties!.a.index
39
+ },
40
+ c: {
41
+ geom: feature.properties!.b.geom,
42
+ index: feature.properties!.b.index
43
+ }
44
+ },
45
+ type: "Feature"
46
+ };
47
+ } else if (
48
+ `${feature.properties!.c.index}`.substring(0, 1) === "b" &&
49
+ `${feature.properties!.a.index}`.substring(0, 1) === "b"
50
+ ) {
51
+ features[i] = {
52
+ geometry: {
53
+ type: "Polygon",
54
+ coordinates: [
55
+ [
56
+ feature.geometry!.coordinates[0][1],
57
+ feature.geometry!.coordinates[0][2],
58
+ feature.geometry!.coordinates[0][0],
59
+ feature.geometry!.coordinates[0][1]
60
+ ]
61
+ ]
62
+ },
63
+ properties: {
64
+ a: {
65
+ geom: feature.properties!.b.geom,
66
+ index: feature.properties!.b.index
67
+ },
68
+ b: {
69
+ geom: feature.properties!.c.geom,
70
+ index: feature.properties!.c.index
71
+ },
72
+ c: {
73
+ geom: feature.properties!.a.geom,
74
+ index: feature.properties!.a.index
75
+ }
76
+ },
77
+ type: "Feature"
78
+ };
79
+ }
80
+ }
81
+ return tins;
82
+ }
83
+
84
+ /**
85
+ * 三角形の頂点の座標系を反転する
86
+ * @param tri 元の三角形
87
+ * @returns 座標系が反転された三角形
88
+ */
89
+ function counterTri(tri: Tri): Tri {
90
+ const coordinates = (["a", "b", "c", "a"] as PropertyTriKey[]).map(
91
+ key => tri.properties![key].geom
92
+ );
93
+ const geoms = tri.geometry!.coordinates[0];
94
+ const props = tri.properties!;
95
+ const properties = {
96
+ a: { geom: geoms[0], index: props["a"].index },
97
+ b: { geom: geoms[1], index: props["b"].index },
98
+ c: { geom: geoms[2], index: props["c"].index }
99
+ };
100
+ return polygon([coordinates], properties);
101
+ }
102
+
103
+ /**
104
+ * 頂点座標群から三角形を生成する
105
+ * @param points 頂点座標群 [座標, インデックス][]
106
+ * @returns 生成された三角形
107
+ */
108
+ function buildTri(points: [Position[], string | number][]): Tri {
109
+ const coordinates = [0, 1, 2, 0].map(i => points[i][0][0]);
110
+ const properties = {
111
+ a: { geom: points[0][0][1], index: points[0][1] },
112
+ b: { geom: points[1][0][1], index: points[1][1] },
113
+ c: { geom: points[2][0][1], index: points[2][1] }
114
+ };
115
+ return polygon([coordinates], properties);
116
+ }
117
+
118
+ /**
119
+ * インデックス配列から三角形を生成する
120
+ * @param indexes インデックス配列
121
+ * @param points 点群座標
122
+ * @param edgeNodes エッジノード座標
123
+ * @param cent 重心座標
124
+ * @param bboxes バウンディングボックス座標
125
+ * @param bakw 座標系を反転するかどうか
126
+ * @param version バージョン番号(オプション)
127
+ * @returns 生成された三角形
128
+ */
129
+ function indexesToTri(
130
+ indexes: (number | string)[],
131
+ points: Position[][],
132
+ edgeNodes: Position[][],
133
+ cent: Position[],
134
+ bboxes: Position[][],
135
+ bakw = false,
136
+ version?: number
137
+ ): Tri {
138
+ const points_: [Position[], string | number][] = indexes.map(
139
+ (index: number | string) => {
140
+ if (!version || version < 2.00703) index = normalizeNodeKey(index);
141
+ const point_base = isFinite(index as any)
142
+ ? points[index as number]
143
+ : index === "c"
144
+ ? cent
145
+ : index === "b0"
146
+ ? bboxes[0]
147
+ : index === "b1"
148
+ ? bboxes[1]
149
+ : index === "b2"
150
+ ? bboxes[2]
151
+ : index === "b3"
152
+ ? bboxes[3]
153
+ : (function () {
154
+ const match = (index as string).match(/e(\d+)/);
155
+ if (match) {
156
+ const nodeIndex = parseInt(match[1]);
157
+ return edgeNodes[nodeIndex];
158
+ }
159
+ throw "Bad index value for indexesToTri";
160
+ })();
161
+ return bakw
162
+ ? [[point_base![1], point_base![0]], index]
163
+ : [[point_base![0], point_base![1]], index];
164
+ }
165
+ );
166
+ return buildTri(points_);
167
+ }
168
+
169
+ // After 7.0.3 Normalizing node index key
170
+ function normalizeNodeKey(index: number | string) {
171
+ if (typeof index === "number") return index;
172
+ return index.replace(/^(c|e|b)(?:ent|dgeNode|box)(\d+)?$/, "$1$2");
173
+ }
174
+
175
+ export {
176
+ rotateVerticesTriangle,
177
+ counterTri,
178
+ indexesToTri,
179
+ normalizeNodeKey
180
+ };