@maplat/transform 0.1.5 → 0.2.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/src/edgeutils.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Position } from "geojson";
1
+ import type { Position } from "geojson";
2
2
 
3
3
  /**
4
4
  * エッジの終点を表す型
package/src/geometry.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
2
2
  import { featureCollection } from "@turf/helpers";
3
- import { Feature, FeatureCollection, Polygon, Point, Position } from "geojson";
3
+ import type { Feature, FeatureCollection, Polygon, Point, Position } from "geojson";
4
4
  //import { Tri, Tins, IndexedTins, WeightBuffer, VerticesParams } from "./index";
5
5
  type PropertyTri = { geom: Position; index: number | string };
6
6
  export type PropertyTriKey = "a" | "b" | "c";
@@ -8,7 +8,7 @@ type PropertiesTri = { [key in PropertyTriKey]: PropertyTri };
8
8
  export type Tri = Feature<Polygon, PropertiesTri>;
9
9
  export type Tins = FeatureCollection<Polygon, PropertiesTri>;
10
10
  export type WeightBuffer = { [index: string]: number };
11
- export type VerticesParams = [number[], FeatureCollection<Polygon>[]?];
11
+ export type VerticesParams = [number[], Tins[]?];
12
12
  export interface IndexedTins {
13
13
  gridNum: number;
14
14
  xOrigin: number;
@@ -40,7 +40,7 @@ function hit(point: Feature<Point>, tins: Tins): Tri | undefined {
40
40
  * @param weightBuffer 重み付けバッファ(オプション)
41
41
  * @returns 変換後の座標
42
42
  */
43
- function transformTinArr(of: any, tri: any, weightBuffer: any) {
43
+ function transformTinArr(of: Feature<Point>, tri: Tri, weightBuffer: WeightBuffer | undefined) {
44
44
  const a = tri.geometry.coordinates[0][0];
45
45
  const b = tri.geometry.coordinates[0][1];
46
46
  const c = tri.geometry.coordinates[0][2];
@@ -97,7 +97,10 @@ function useVerticesArr(
97
97
  const centCoord = centroid.geometry!.coordinates;
98
98
  const radian = Math.atan2(coord[0] - centCoord[0], coord[1] - centCoord[1]);
99
99
  const index = decideUseVertex(radian, verticesParams[0]);
100
- const tin = verticesParams[1]![index as any];
100
+ if (index === undefined) {
101
+ throw new Error("Unable to determine vertex index");
102
+ }
103
+ const tin = verticesParams[1]![index];
101
104
  return transformTinArr(o, tin.features[0], weightBuffer);
102
105
  }
103
106
 
@@ -145,7 +148,7 @@ function transformArr(
145
148
  ? gridCache[normX][normY]
146
149
  : []
147
150
  : [];
148
- tins = featureCollection(tinsKey.map((key: any) => tins.features[key]));
151
+ tins = featureCollection(tinsKey.map((key: number) => tins.features[key]));
149
152
  }
150
153
  tin = hit(point, tins);
151
154
  }
@@ -189,7 +192,7 @@ function unitCalc(
189
192
  * const index = decideUseVertex(0.5, [0, Math.PI/2, Math.PI, Math.PI*3/2]);
190
193
  * // returns 0 (最初の頂点が最も近い)
191
194
  */
192
- function decideUseVertex(radian: any, radianList: any) {
195
+ function decideUseVertex(radian: number, radianList: number[]): number | undefined {
193
196
  // 最初の頂点との角度差を正規化
194
197
  let idel = normalizeRadian(radian - radianList[0]);
195
198
  let minTheta = Math.PI * 2; // 最小角度差の初期値
@@ -226,13 +229,13 @@ function decideUseVertex(radian: any, radianList: any) {
226
229
  * // [0, 2π)の範囲に正規化
227
230
  * normalizeRadian(3 * Math.PI, true); // returns π
228
231
  */
229
- function normalizeRadian(target: any, noNegative = false) {
232
+ function normalizeRadian(target: number, noNegative = false): number {
230
233
  // 正規化の範囲を決定する関数
231
234
  const rangeFunc = noNegative
232
- ? function (val: any) {
235
+ ? function (val: number) {
233
236
  return !(val >= 0 && val < Math.PI * 2); // [0, 2π)の範囲外
234
237
  }
235
- : function (val: any) {
238
+ : function (val: number) {
236
239
  return !(val > -1 * Math.PI && val <= Math.PI); // (-π, π]の範囲外
237
240
  };
238
241
 
package/src/index.ts CHANGED
@@ -1,20 +1,20 @@
1
1
  import booleanPointInPolygon from "@turf/boolean-point-in-polygon";
2
2
  import { featureCollection, point } from "@turf/helpers";
3
3
  import { getCoords } from "@turf/invariant";
4
- import { indexesToTri, normalizeNodeKey } from "./triangulation";
5
- import { Feature, Polygon, Position, Point, FeatureCollection } from "geojson";
6
- import { normalizeEdges } from "./edgeutils";
4
+ import { indexesToTri, normalizeNodeKey } from "./triangulation.ts";
5
+ import type { Feature, Polygon, Position, Point, FeatureCollection } from "geojson";
6
+ import { normalizeEdges } from "./edgeutils.ts";
7
7
  import type {
8
8
  WeightBuffer, Tins, VerticesParams, PropertyTriKey,
9
9
  IndexedTins, Tri
10
- } from "./geometry";
11
- import { unitCalc, transformArr } from "./geometry";
12
- import type { EdgeSet, EdgeSetLegacy } from "./edgeutils";
13
- export type { Tins, Tri, PropertyTriKey } from './geometry';
14
- export { transformArr } from './geometry';
15
- export { rotateVerticesTriangle, counterTri } from './triangulation';
16
- export type { Edge, EdgeSet, EdgeSetLegacy } from './edgeutils';
17
- export { normalizeEdges } from './edgeutils';
10
+ } from "./geometry.ts";
11
+ 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
18
 
19
19
  /**
20
20
  * 座標ペアの型定義。[ソース座標, ターゲット座標] の形式
@@ -164,10 +164,10 @@ export class Transform {
164
164
  * 3. TINネットワークの再構築
165
165
  * 4. インデックスの作成
166
166
  */
167
- setCompiled(compiled: Compiled | CompiledLegacy) {
167
+ setCompiled(compiled: Compiled | CompiledLegacy): void {
168
168
  if (
169
169
  compiled.version ||
170
- (!(compiled as any).tins && compiled.points && compiled.tins_points)
170
+ (!(compiled as CompiledLegacy).tins && compiled.points && compiled.tins_points)
171
171
  ) {
172
172
  // 新コンパイルロジック
173
173
  // pointsはそのままpoints
@@ -250,7 +250,7 @@ export class Transform {
250
250
  const bakwI = compiled.tins_points.length == 1 ? 0 : 1;
251
251
  this.tins = {
252
252
  forw: featureCollection(
253
- compiled.tins_points[0].map((idxes: any) =>
253
+ compiled.tins_points[0].map((idxes: (number | string)[]) =>
254
254
  indexesToTri(
255
255
  idxes,
256
256
  compiled.points,
@@ -263,7 +263,7 @@ export class Transform {
263
263
  )
264
264
  ),
265
265
  bakw: featureCollection(
266
- compiled.tins_points[bakwI].map((idxes: any) =>
266
+ compiled.tins_points[bakwI].map((idxes: (number | string)[]) =>
267
267
  indexesToTri(
268
268
  idxes,
269
269
  compiled.points,
@@ -324,27 +324,20 @@ export class Transform {
324
324
  this.vertices_params = compiled.vertices_params as VerticesParamsBD;
325
325
  this.centroid = (compiled as CompiledLegacy).centroid;
326
326
  this.kinks = (compiled as CompiledLegacy).kinks;
327
- const points: any = [];
327
+ const points: PointSet[] = [];
328
328
  for (let i = 0; i < this.tins!.forw!.features.length; i++) {
329
329
  const tri = this.tins!.forw!.features[i];
330
330
  (["a", "b", "c"] as PropertyTriKey[]).map((key, idx) => {
331
331
  const forw = tri.geometry!.coordinates[0][idx];
332
332
  const bakw = tri.properties![key].geom;
333
333
  const pIdx = tri.properties![key].index;
334
- points[pIdx] = [forw, bakw];
334
+ if (typeof pIdx === 'number') {
335
+ points[pIdx] = [forw, bakw];
336
+ }
335
337
  });
336
338
  }
337
339
  this.points = points;
338
340
  }
339
- // 翻訳したオブジェクトを返す
340
- return {
341
- tins: this.tins,
342
- strict_status: this.strict_status,
343
- weight_buffer: this.pointsWeightBuffer,
344
- vertices_params: this.vertices_params,
345
- centroid: this.centroid,
346
- kinks: this.kinks
347
- };
348
341
  }
349
342
 
350
343
  /**
@@ -451,7 +444,7 @@ export class Transform {
451
444
  const bakwXUnit = (bakwBound[1][0] - bakwBound[0][0]) / gridNum;
452
445
  const bakwYUnit = (bakwBound[1][1] - bakwBound[0][1]) / gridNum;
453
446
  const bakwGridCache = bakwEachBound.reduce(
454
- (prev: any, bound: any, index: number) => {
447
+ (prev: number[][][], bound: Position[], index: number) => {
455
448
  const normXMin = unitCalc(
456
449
  bound[0][0],
457
450
  bakwBound[0][0],
@@ -517,7 +510,7 @@ export class Transform {
517
510
  *
518
511
  * @throws {Error} 逆方向変換が許可されていない状態での逆変換時
519
512
  */
520
- transform(apoint: number[], backward?: boolean, ignoreBounds?: boolean) {
513
+ transform(apoint: number[], backward?: boolean, ignoreBounds?: boolean): number[] | false {
521
514
  if (backward && this.strict_status == Transform.STATUS_ERROR)
522
515
  throw 'Backward transform is not allowed if strict_status == "strict_error"';
523
516
  // if (!this.tins) this.updateTin();
@@ -1,6 +1,6 @@
1
1
  import { polygon } from "@turf/helpers";
2
- import { Position } from "geojson";
3
- import { PropertyTriKey, Tri, Tins } from "./geometry";
2
+ import type { Position } from "geojson";
3
+ import { PropertyTriKey, Tri, Tins } from "./geometry.ts";
4
4
 
5
5
  /**
6
6
  * 三角形の頂点の順序を修正する
@@ -8,7 +8,7 @@ import { PropertyTriKey, Tri, Tins } from "./geometry";
8
8
  * @param tins 三角形群
9
9
  * @returns 頂点順序が修正された三角形群
10
10
  */
11
- function rotateVerticesTriangle(tins: Tins) {
11
+ function rotateVerticesTriangle(tins: Tins): Tins {
12
12
  const features = tins.features;
13
13
  for (let i = 0; i < features.length; i++) {
14
14
  const feature = features[i];
@@ -138,7 +138,7 @@ function indexesToTri(
138
138
  const points_: [Position[], string | number][] = indexes.map(
139
139
  (index: number | string) => {
140
140
  if (!version || version < 2.00703) index = normalizeNodeKey(index);
141
- const point_base = isFinite(index as any)
141
+ const point_base = isFinite(index as number)
142
142
  ? points[index as number]
143
143
  : index === "c"
144
144
  ? cent