@maplat/transform 0.2.2 → 0.3.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.
@@ -1,180 +1,180 @@
1
- import { polygon } from "@turf/helpers";
2
- import type { Position } from "geojson";
3
- import { PropertyTriKey, Tri, Tins } from "./geometry.ts";
4
-
5
- /**
6
- * 三角形の頂点の順序を修正する
7
- * 地図外郭の頂点を含む三角形について、頂点の順序を統一する
8
- * @param tins 三角形群
9
- * @returns 頂点順序が修正された三角形群
10
- */
11
- function rotateVerticesTriangle(tins: 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 number)
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
1
+ import { polygon } from "@turf/helpers";
2
+ import type { Position } from "geojson";
3
+ import { PropertyTriKey, Tri, Tins } from "./geometry.ts";
4
+
5
+ /**
6
+ * 三角形の頂点の順序を修正する
7
+ * 地図外郭の頂点を含む三角形について、頂点の順序を統一する
8
+ * @param tins 三角形群
9
+ * @returns 頂点順序が修正された三角形群
10
+ */
11
+ function rotateVerticesTriangle(tins: 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 number)
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
180
  };
package/src/types.ts ADDED
@@ -0,0 +1,122 @@
1
+ import type { Feature, FeatureCollection, Polygon, Point, Position } from "geojson";
2
+ import type {
3
+ IndexedTins,
4
+ Tins,
5
+ VerticesParams,
6
+ WeightBuffer
7
+ } from "./geometry.ts";
8
+ import type { EdgeSet, EdgeSetLegacy } from "./edgeutils.ts";
9
+
10
+ /**
11
+ * Two-way coordinate pair: [source, target].
12
+ */
13
+ export type PointSet = [Position, Position];
14
+
15
+ /**
16
+ * Directional selector shared across the codebase.
17
+ */
18
+ export type BiDirectionKey = "forw" | "bakw";
19
+
20
+ /**
21
+ * Weight buffers indexed by node id for both directions.
22
+ */
23
+ export type WeightBufferBD = { [key in BiDirectionKey]?: WeightBuffer };
24
+
25
+ /**
26
+ * Vertex interpolation modes.
27
+ */
28
+ export type VertexMode = "plain" | "birdeye";
29
+
30
+ /**
31
+ * Strictness flags supported during transformation.
32
+ */
33
+ export type StrictMode = "strict" | "auto" | "loose";
34
+
35
+ /**
36
+ * Result of strictness evaluation.
37
+ */
38
+ export type StrictStatus = "strict" | "strict_error" | "loose";
39
+
40
+ /**
41
+ * Y-axis handling directive.
42
+ */
43
+ export type YaxisMode = "follow" | "invert";
44
+
45
+ export type Centroid = Feature<Point>;
46
+ export type CentroidBD = { [key in BiDirectionKey]?: Centroid };
47
+ export type TinsBD = { [key in BiDirectionKey]?: Tins };
48
+ export type Kinks = FeatureCollection<Point>;
49
+ export type KinksBD = { [key in BiDirectionKey]?: Kinks };
50
+ export type VerticesParamsBD = { [key in BiDirectionKey]?: VerticesParams };
51
+ export type IndexedTinsBD = { [key in BiDirectionKey]?: IndexedTins };
52
+
53
+ /**
54
+ * Serialized structure generated by MaplatTin and consumed by Transform.
55
+ */
56
+ export interface Compiled {
57
+ version?: number;
58
+ points: PointSet[];
59
+ tins_points: (number | string)[][][];
60
+ weight_buffer: WeightBufferBD;
61
+ strict_status?: StrictStatus;
62
+ centroid_point: Position[];
63
+ edgeNodes?: PointSet[];
64
+ kinks_points?: Position[];
65
+ yaxisMode?: YaxisMode;
66
+ vertexMode?: VertexMode;
67
+ strictMode?: StrictMode;
68
+ vertices_params: number[][];
69
+ vertices_points: PointSet[];
70
+ edges: EdgeSet[];
71
+ bounds?: number[][];
72
+ boundsPolygon?: Feature<Polygon>;
73
+ wh?: number[];
74
+ xy?: number[];
75
+ }
76
+
77
+ /**
78
+ * Historical serialization format prior to 2.00703.
79
+ */
80
+ export interface CompiledLegacy extends Compiled {
81
+ tins?: TinsBD;
82
+ centroid?: CentroidBD;
83
+ kinks?: KinksBD;
84
+ vertices_params: number[][] & VerticesParamsBD;
85
+ edges: EdgeSet[] & EdgeSetLegacy[];
86
+ }
87
+
88
+ /**
89
+ * Normalized state derived from modern compiled payloads.
90
+ */
91
+ export interface ModernStatePayload {
92
+ points: PointSet[];
93
+ pointsWeightBuffer: WeightBufferBD;
94
+ strictStatus: StrictStatus;
95
+ verticesParams: VerticesParamsBD;
96
+ centroid: CentroidBD;
97
+ edges: EdgeSet[];
98
+ edgeNodes?: PointSet[];
99
+ tins: TinsBD;
100
+ kinks?: KinksBD;
101
+ yaxisMode: YaxisMode;
102
+ strictMode: StrictMode;
103
+ vertexMode?: VertexMode;
104
+ bounds?: number[][];
105
+ boundsPolygon?: Feature<Polygon>;
106
+ wh?: number[];
107
+ xy?: number[];
108
+ }
109
+
110
+ /**
111
+ * Normalized snapshot captured from legacy payloads.
112
+ */
113
+ export interface LegacyStatePayload {
114
+ compiled: CompiledLegacy;
115
+ tins: TinsBD;
116
+ points: PointSet[];
117
+ strictStatus?: StrictStatus;
118
+ pointsWeightBuffer: WeightBufferBD;
119
+ verticesParams: VerticesParamsBD;
120
+ centroid?: CentroidBD;
121
+ kinks?: KinksBD;
122
+ }