@maplat/transform 0.2.3 → 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.
package/src/index.ts CHANGED
@@ -1,110 +1,55 @@
1
+ import type { Feature, Polygon, Position } from "geojson";
1
2
  import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
2
- import { featureCollection, point } from "@turf/helpers";
3
+ import { point } from "@turf/helpers";
3
4
  import { getCoords } from "@turf/invariant";
4
- import { indexesToTri, normalizeNodeKey } from "./triangulation.ts";
5
- import type { Feature, Polygon, Position, Point, FeatureCollection } from "geojson";
6
- import { normalizeEdges } from "./edgeutils.ts";
7
- import type {
8
- WeightBuffer, Tins, VerticesParams, PropertyTriKey,
9
- IndexedTins, Tri
10
- } from "./geometry.ts";
11
5
  import { unitCalc, transformArr } from "./geometry.ts";
12
- import type { EdgeSet, EdgeSetLegacy } from "./edgeutils.ts";
13
- export type { Tins, Tri, PropertyTriKey } from './geometry.ts';
14
- export { transformArr } from './geometry.ts';
15
- export { rotateVerticesTriangle, counterTri } from './triangulation.ts';
16
- export type { Edge, EdgeSet, EdgeSetLegacy } from './edgeutils.ts';
17
- export { normalizeEdges } from './edgeutils.ts';
18
-
19
- /**
20
- * 座標ペアの型定義。[ソース座標, ターゲット座標] の形式
21
- */
22
- export type PointSet = [Position, Position];
23
-
24
- /**
25
- * 変換の方向を示す型定義
26
- * - forw: 順方向(ソース → ターゲット)
27
- * - bakw: 逆方向(ターゲット → ソース)
28
- */
29
- export type BiDirectionKey = "forw" | "bakw";
30
-
31
- /**
32
- * 両方向の重み付けバッファの型定義
33
- */
34
- export type WeightBufferBD = { [key in BiDirectionKey]?: WeightBuffer };
35
-
36
- /**
37
- * 頂点モードの型定義
38
- * - plain: 通常モード
39
- * - birdeye: 鳥瞰図モード
40
- */
41
- export type VertexMode = "plain" | "birdeye";
42
-
43
- /**
44
- * 厳密性モードの型定義
45
- * - strict: 厳密モード(交差なしを保証)
46
- * - auto: 自動モード(可能な限り厳密に)
47
- * - loose: 緩和モード(交差を許容)
48
- */
49
- export type StrictMode = "strict" | "auto" | "loose";
50
-
51
- /**
52
- * 厳密性モードの生成結果
53
- * - strict: 厳密モード生成成功
54
- * - auto: 厳密モードでエラー
55
- * - loose: 緩和モード
56
- */
57
- export type StrictStatus = "strict" | "strict_error" | "loose";
58
-
59
- /**
60
- * Y軸の向きの型定義
61
- * - follow: Y軸の向きを維持
62
- * - invert: Y軸の向きを反転
63
- */
64
- export type YaxisMode = "follow" | "invert";
65
- export type Centroid = Feature<Point>;
66
- export type CentroidBD = { [key in BiDirectionKey]?: Centroid };
67
- export type TinsBD = { [key in BiDirectionKey]?: Tins };
68
- export type Kinks = FeatureCollection<Point>;
69
- export type KinksBD = { [key in BiDirectionKey]?: Kinks };
70
- export type VerticesParamsBD = { [key in BiDirectionKey]?: VerticesParams };
71
- export type IndexedTinsBD = { [key in BiDirectionKey]?: IndexedTins };
72
-
73
- export const format_version = 2.00703; //(Version 2 format for library version 0.7.3)
74
-
75
- /**
76
- * コンパイルされた設定の型定義
77
- * 変換に必要な全ての情報を含む
78
- */
79
- export interface Compiled {
80
- version?: number;
81
- points: PointSet[];
82
- tins_points: (number | string)[][][];
83
- weight_buffer: WeightBufferBD;
84
- strict_status?: StrictStatus;
85
- centroid_point: Position[];
86
- edgeNodes?: PointSet[];
87
- kinks_points?: Position[];
88
- yaxisMode?: YaxisMode;
89
- vertexMode?: VertexMode;
90
- strictMode?: StrictMode;
91
- vertices_params: number[][];
92
- vertices_points: PointSet[];
93
- edges: EdgeSet[];
94
- bounds?: number[][];
95
- boundsPolygon?: Feature<Polygon>;
96
- wh?: number[];
97
- xy?: number[];
98
- }
99
-
100
- // For old Interface
101
- export interface CompiledLegacy extends Compiled {
102
- tins?: TinsBD;
103
- centroid?: CentroidBD;
104
- kinks?: KinksBD;
105
- vertices_params: number[][] & VerticesParamsBD;
106
- edges: EdgeSet[] & EdgeSetLegacy[];
107
- }
6
+ import type { Tri } from "./geometry.ts";
7
+ import {
8
+ FORMAT_VERSION,
9
+ isModernCompiled,
10
+ restoreLegacyState,
11
+ restoreModernState
12
+ } from "./compiled-state.ts";
13
+ import type { EdgeSet } from "./edgeutils.ts";
14
+ import type {
15
+ Compiled,
16
+ CompiledLegacy,
17
+ IndexedTinsBD,
18
+ KinksBD,
19
+ LegacyStatePayload,
20
+ ModernStatePayload,
21
+ PointSet,
22
+ StrictMode,
23
+ StrictStatus,
24
+ TinsBD,
25
+ VertexMode,
26
+ VerticesParamsBD,
27
+ WeightBufferBD,
28
+ YaxisMode,
29
+ CentroidBD
30
+ } from "./types.ts";
31
+ export type {
32
+ PointSet,
33
+ BiDirectionKey,
34
+ WeightBufferBD,
35
+ VertexMode,
36
+ StrictMode,
37
+ StrictStatus,
38
+ YaxisMode,
39
+ CentroidBD,
40
+ TinsBD,
41
+ KinksBD,
42
+ VerticesParamsBD,
43
+ IndexedTinsBD,
44
+ Compiled,
45
+ CompiledLegacy
46
+ } from "./types.ts";
47
+ export type { Tins, Tri, PropertyTriKey } from "./geometry.ts";
48
+ export { transformArr } from "./geometry.ts";
49
+ export { rotateVerticesTriangle, counterTri } from "./triangulation.ts";
50
+ export type { Edge, EdgeSet, EdgeSetLegacy } from "./edgeutils.ts";
51
+ export { normalizeEdges } from "./edgeutils.ts";
52
+ export const format_version = FORMAT_VERSION;
108
53
 
109
54
  /**
110
55
  * 座標変換の基本機能を提供するクラス
@@ -165,181 +110,51 @@ export class Transform {
165
110
  * 4. インデックスの作成
166
111
  */
167
112
  setCompiled(compiled: Compiled | CompiledLegacy): void {
168
- if (
169
- compiled.version ||
170
- (!(compiled as CompiledLegacy).tins && compiled.points && compiled.tins_points)
171
- ) {
172
- // 新コンパイルロジック
173
- // pointsはそのままpoints
174
- this.points = compiled.points;
175
- // After 0.7.3 Normalizing old formats for weightBuffer
176
- this.pointsWeightBuffer =
177
- !compiled.version || compiled.version < 2.00703
178
- ? (["forw", "bakw"] as BiDirectionKey[]).reduce((bd, forb) => {
179
- const base = compiled.weight_buffer[forb];
180
- if (base) {
181
- bd[forb] = Object.keys(base!).reduce((buffer, key) => {
182
- const normKey = normalizeNodeKey(key);
183
- buffer[normKey] = base![key];
184
- return buffer;
185
- }, {} as WeightBuffer);
186
- }
187
- return bd;
188
- }, {} as WeightBufferBD)
189
- : compiled.weight_buffer;
190
- // kinksやtinsの存在状況でstrict_statusを判定
191
- if (compiled.strict_status) {
192
- this.strict_status = compiled.strict_status;
193
- } else if (compiled.kinks_points) {
194
- this.strict_status = Transform.STATUS_ERROR;
195
- } else if (compiled.tins_points.length == 2) {
196
- this.strict_status = Transform.STATUS_LOOSE;
197
- } else {
198
- this.strict_status = Transform.STATUS_STRICT;
199
- }
200
- // vertices_paramsを復元
201
- this.vertices_params = {
202
- forw: [(compiled.vertices_params as number[][])[0]],
203
- bakw: [(compiled.vertices_params as number[][])[1]]
204
- };
205
- this.vertices_params.forw![1] = [0, 1, 2, 3].map(idx => {
206
- const idxNxt = (idx + 1) % 4;
207
- const tri = indexesToTri(
208
- ["c", `b${idx}`, `b${idxNxt}`],
209
- compiled.points,
210
- compiled.edgeNodes || [],
211
- compiled.centroid_point,
212
- compiled.vertices_points,
213
- false,
214
- format_version
215
- );
216
- return featureCollection([tri]);
217
- });
218
- this.vertices_params.bakw![1] = [0, 1, 2, 3].map(idx => {
219
- const idxNxt = (idx + 1) % 4;
220
- const tri = indexesToTri(
221
- ["c", `b${idx}`, `b${idxNxt}`],
222
- compiled.points,
223
- compiled.edgeNodes || [],
224
- compiled.centroid_point,
225
- compiled.vertices_points,
226
- true,
227
- format_version
228
- );
229
- return featureCollection([tri]);
230
- });
231
- // centroidを復元
232
- this.centroid = {
233
- forw: point(compiled.centroid_point[0], {
234
- target: {
235
- geom: compiled.centroid_point[1],
236
- index: "c"
237
- }
238
- }),
239
- bakw: point(compiled.centroid_point[1], {
240
- target: {
241
- geom: compiled.centroid_point[0],
242
- index: "c"
243
- }
244
- })
245
- };
246
- // edgesを復元
247
- this.edges = normalizeEdges(compiled.edges || []);
248
- this.edgeNodes = compiled.edgeNodes || [];
249
- // tinsを復元
250
- const bakwI = compiled.tins_points.length == 1 ? 0 : 1;
251
- this.tins = {
252
- forw: featureCollection(
253
- compiled.tins_points[0].map((idxes: (number | string)[]) =>
254
- indexesToTri(
255
- idxes,
256
- compiled.points,
257
- compiled.edgeNodes || [],
258
- compiled.centroid_point,
259
- compiled.vertices_points,
260
- false,
261
- compiled.version
262
- )
263
- )
264
- ),
265
- bakw: featureCollection(
266
- compiled.tins_points[bakwI].map((idxes: (number | string)[]) =>
267
- indexesToTri(
268
- idxes,
269
- compiled.points,
270
- compiled.edgeNodes || [],
271
- compiled.centroid_point,
272
- compiled.vertices_points,
273
- true,
274
- compiled.version
275
- )
276
- )
277
- )
278
- };
279
- this.addIndexedTin();
280
- // kinksを復元
281
- if (compiled.kinks_points) {
282
- this.kinks = {
283
- bakw: featureCollection(
284
- compiled.kinks_points.map((coord: Position) => point(coord))
285
- )
286
- };
287
- }
288
- // yaxisModeを復元
289
- if (compiled.yaxisMode) {
290
- this.yaxisMode = compiled.yaxisMode;
291
- } else {
292
- this.yaxisMode = Transform.YAXIS_INVERT;
293
- }
294
- // After 0.7.3: Restore strict_mode & vertex_mode
295
- if (compiled.vertexMode) {
296
- this.vertexMode = compiled.vertexMode;
297
- }
298
- if (compiled.strictMode) {
299
- this.strictMode = compiled.strictMode;
300
- }
301
- // boundsを復元
302
- if (compiled.bounds) {
303
- this.bounds = compiled.bounds;
304
- this.boundsPolygon = compiled.boundsPolygon;
305
- this.xy = compiled.xy;
306
- this.wh = compiled.wh;
307
- } else {
308
- this.xy = [0, 0];
309
- if (compiled.wh) this.wh = compiled.wh;
113
+ if (isModernCompiled(compiled)) {
114
+ this.applyModernState(restoreModernState(compiled));
115
+ return;
116
+ }
117
+ this.applyLegacyState(restoreLegacyState(compiled as CompiledLegacy));
118
+ }
119
+
120
+ private applyModernState(state: ModernStatePayload): void {
121
+ this.points = state.points;
122
+ this.pointsWeightBuffer = state.pointsWeightBuffer;
123
+ this.strict_status = state.strictStatus;
124
+ this.vertices_params = state.verticesParams;
125
+ this.centroid = state.centroid;
126
+ this.edges = state.edges;
127
+ this.edgeNodes = state.edgeNodes || [];
128
+ this.tins = state.tins;
129
+ this.addIndexedTin();
130
+ this.kinks = state.kinks;
131
+ this.yaxisMode = state.yaxisMode ?? Transform.YAXIS_INVERT;
132
+ this.vertexMode = state.vertexMode ?? Transform.VERTEX_PLAIN;
133
+ this.strictMode = state.strictMode ?? Transform.MODE_AUTO;
134
+ if (state.bounds) {
135
+ this.bounds = state.bounds;
136
+ this.boundsPolygon = state.boundsPolygon;
137
+ this.xy = state.xy;
138
+ this.wh = state.wh;
139
+ } else {
310
140
  this.bounds = undefined;
311
141
  this.boundsPolygon = undefined;
312
- }
313
- } else {
314
- // 旧コンパイルロジック
315
- compiled = JSON.parse(
316
- JSON.stringify(compiled)
317
- .replace('"cent"', '"c"')
318
- .replace(/"bbox(\d+)"/g, '"b$1"')
319
- );
320
- this.tins = (compiled as CompiledLegacy).tins;
321
- this.addIndexedTin();
322
- this.strict_status = compiled.strict_status;
323
- this.pointsWeightBuffer = compiled.weight_buffer;
324
- this.vertices_params = compiled.vertices_params as VerticesParamsBD;
325
- this.centroid = (compiled as CompiledLegacy).centroid;
326
- this.kinks = (compiled as CompiledLegacy).kinks;
327
- const points: PointSet[] = [];
328
- for (let i = 0; i < this.tins!.forw!.features.length; i++) {
329
- const tri = this.tins!.forw!.features[i];
330
- (["a", "b", "c"] as PropertyTriKey[]).map((key, idx) => {
331
- const forw = tri.geometry!.coordinates[0][idx];
332
- const bakw = tri.properties![key].geom;
333
- const pIdx = tri.properties![key].index;
334
- if (typeof pIdx === 'number') {
335
- points[pIdx] = [forw, bakw];
336
- }
337
- });
338
- }
339
- this.points = points;
142
+ this.xy = state.xy ?? [0, 0];
143
+ if (state.wh) this.wh = state.wh;
340
144
  }
341
145
  }
342
146
 
147
+ private applyLegacyState(state: LegacyStatePayload): void {
148
+ this.tins = state.tins;
149
+ this.addIndexedTin();
150
+ this.strict_status = state.strictStatus;
151
+ this.pointsWeightBuffer = state.pointsWeightBuffer;
152
+ this.vertices_params = state.verticesParams;
153
+ this.centroid = state.centroid;
154
+ this.kinks = state.kinks;
155
+ this.points = state.points;
156
+ }
157
+
343
158
  /**
344
159
  * TINネットワークのインデックスを作成します
345
160
  *
@@ -564,4 +379,4 @@ export class Transform {
564
379
  return ret;
565
380
  }
566
381
 
567
- }
382
+ }
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
+ }