From 19f44a9f3ba2b7a29a8ce0d4c7ee014e48c8dd3a Mon Sep 17 00:00:00 2001 From: Keita Kobayashi Date: Wed, 20 Nov 2024 16:51:27 +0900 Subject: [PATCH] Fix tests for origin --- src/lib/tilebelt_local.test.ts | 35 +++++++++++++++++++++++++--------- src/lib/tilebelt_local.ts | 9 ++++++--- src/local_namespace.test.ts | 24 +++++++++++------------ src/local_spatial_id.test.ts | 6 ++---- src/shared.test.ts | 10 +++++----- 5 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/lib/tilebelt_local.test.ts b/src/lib/tilebelt_local.test.ts index b7e35d9..b2c9ffd 100644 --- a/src/lib/tilebelt_local.test.ts +++ b/src/lib/tilebelt_local.test.ts @@ -24,13 +24,19 @@ describe('tile2meters', () => { // // calculateLocalZFXY describe('calculateLocalZFXY', () => { test('Basic test, init with point', () => { + // assert.deepStrictEqual(tb.calculateLocalZFXY(1, { x: 0, y: 0, alt: 0 }, 0), { z: 0, f: 0, x: 0, y: 0 }); + // assert.deepStrictEqual(tb.calculateLocalZFXY(1024, { x: 0, y: 0, alt: 0 }, 10), { z: 10, f: 0, x: 512, y: 512 }); + assert.deepStrictEqual(tb.calculateLocalZFXY(1, { x: 0, y: 0, alt: 0 }, 0), { z: 0, f: 0, x: 0, y: 0 }); - assert.deepStrictEqual(tb.calculateLocalZFXY(1024, { x: 0, y: 0, alt: 0 }, 10), { z: 10, f: 0, x: 512, y: 512 }); + assert.deepStrictEqual(tb.calculateLocalZFXY(1024, { x: 0, y: 0, alt: 0 }, 10), { z: 10, f: 0, x: 0, y: 0 }); }); test('Basic test, init with bbox', () => { - assert.deepStrictEqual(tb.calculateLocalZFXY(1, [0, 0, 0, 0, 0, 0], 0), { z: 0, f: 0, x: 0, y: 0 }); - assert.deepStrictEqual(tb.calculateLocalZFXY(1024, [0, 0, 0, 0, 0, 0], 10), { z: 10, f: 0, x: 512, y: 512 }); + // assert.deepStrictEqual(tb.calculateLocalZFXY(1, [0, 0, 0, 0, 0, 0], 0), { z: 0, f: 0, x: 0, y: 0 }); + // assert.deepStrictEqual(tb.calculateLocalZFXY(1024, [0, 0, 0, 0, 0, 0], 10), { z: 10, f: 0, x: 512, y: 512 }); + + assert.deepStrictEqual(tb.calculateLocalZFXY(1, { x: 0, y: 0, alt: 0 }, 0), { z: 0, f: 0, x: 0, y: 0 }); + assert.deepStrictEqual(tb.calculateLocalZFXY(1024, { x: 0, y: 0, alt: 0 }, 10), { z: 10, f: 0, x: 0, y: 0 }); }); }); @@ -51,13 +57,24 @@ describe('calculateLocalZFXY', () => { // pointToLocalTileFraction describe('pointToLocalTileFraction', () => { test('Basic test', () => { - assert.deepStrictEqual(tb.pointToLocalTileFraction(1, 0, 0, 0, 0), [0.5, 0.5, 0, 0]); + // for center point + // assert.deepStrictEqual(tb.pointToLocalTileFraction(1, 0, 0, 0, 0), [0.5, 0.5, 0, 0]); + + // assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 0, 0, 0, 10), [512, 512, 0, 10]); + // assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 0, 0, 10), [1023, 512, 0, 10]); + // assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 10), [1023, 1023, 0, 10]); + + // assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 11), [2046, 2046, 0, 11]); + // assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 9), [511.5, 511.5, 0, 9]); + + // for top-left point + assert.deepStrictEqual(tb.pointToLocalTileFraction(1, 0, 0, 0, 0), [0, 0, 0, 0]); - assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 0, 0, 0, 10), [512, 512, 0, 10]); - assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 0, 0, 10), [1023, 512, 0, 10]); - assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 10), [1023, 1023, 0, 10]); + assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 0, 0, 0, 10), [0, 0, 0, 10]); + assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 0, 0, 10), [511, 0, 0, 10]); + assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 10), [511, 511, 0, 10]); - assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 11), [2046, 2046, 0, 11]); - assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 9), [511.5, 511.5, 0, 9]); + assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 11), [1022, 1022, 0, 11]); + assert.deepStrictEqual(tb.pointToLocalTileFraction(1024, 511, 511, 0, 9), [255.5, 255.5, 0, 9]); }); }); diff --git a/src/lib/tilebelt_local.ts b/src/lib/tilebelt_local.ts index 6329095..9625a63 100644 --- a/src/lib/tilebelt_local.ts +++ b/src/lib/tilebelt_local.ts @@ -73,12 +73,15 @@ export function pointToLocalTile(scale: number, x: number, y: number, alt: numbe */ export function pointToLocalTileFraction(scale: number, xMeters: number, yMeters: number, altMeters: number, z: number): XYFZTile { const z2 = Math.pow(2, z); - const z2_1 = Math.pow(2, z - 1); + // const z2_1 = Math.pow(2, z - 1); // transform input points (in meters) to tile coordinates at the zoom level `z` // note that the origin point is at the center of the tile in the x/y plane, so we have to add z2/2 to the x and y coordinates - const x = (z2 * (xMeters / scale)) + z2_1; - const y = (z2 * (yMeters / scale)) + z2_1; + // const x = (z2 * (xMeters / scale)) + z2_1; + // const y = (z2 * (yMeters / scale)) + z2_1; + + const x = z2 * (xMeters / scale); + const y = z2 * (yMeters / scale); const f = z2 * (altMeters / scale); // Detect out-of-bounds coordinates diff --git a/src/local_namespace.test.ts b/src/local_namespace.test.ts index 312eaaf..bce06ee 100644 --- a/src/local_namespace.test.ts +++ b/src/local_namespace.test.ts @@ -25,22 +25,20 @@ describe('LocalNamespace', () => { }); describe("Georeferencing (origin)", () => { - test("boundingSpaceFromGeoJSON", () => { - const namespace = namespaces.tokyo; - const geojson = geoJsons["tokyo/shinjuku-gyoen"]; - const boundingSpace = namespace.boundingSpaceFromGeoJSON(geojson.geometry); - assert.strictEqual(boundingSpace.zfxyStr, '/2/0/2/2'); - }); - test("spacesFromGeoJSON", () => { const namespace = namespaces.tokyo; const geojson = geoJsons["tokyo/shinjuku-gyoen"]; - const spaces = namespace.spacesFromGeoJSON(3, geojson.geometry); - assert.deepStrictEqual(spaces.map((space) => space.zfxyStr), [ - '/3/0/4/4', - '/3/0/5/4', - '/3/1/4/4', - '/3/1/5/4', + const boundingSpace = namespace.spacesFromGeoJSON(4, geojson.geometry); + const zfxys = boundingSpace.map((space) => space.zfxyStr); + assert.deepStrictEqual(zfxys, [ + '/4/0/8/6', + '/4/0/9/6', + '/4/0/8/7', + '/4/0/9/7', + '/4/1/8/6', + '/4/1/9/6', + '/4/1/8/7', + '/4/1/9/7', ]); }); }); diff --git a/src/local_spatial_id.test.ts b/src/local_spatial_id.test.ts index fc42d4a..63ef9b3 100644 --- a/src/local_spatial_id.test.ts +++ b/src/local_spatial_id.test.ts @@ -22,8 +22,7 @@ describe('LocalSpatialId', () => { const bbox = space.toWGS84BBox(); // a bounding box of 10km2, with the center at 東京都庁 - // http://bboxfinder.com/#35.644424126412915,139.63645566328384,35.7345521481874,139.7469336572898 - const referenceBbox: BBox3D = [ 139.63645566328384, 35.644424126412915, 0, 139.7469957955975, 35.7345521481874, 10000 ]; + const referenceBbox: BBox3D = [ 139.64657, 35.63581115931438, 0, 139.7570982609969, 35.72599000000001, 10000 ]; assert.deepStrictEqual(bbox, referenceBbox); }); @@ -33,8 +32,7 @@ describe('LocalSpatialId', () => { const bbox = space.toWGS84BBox(); // a bounding box of 10km2, with the center at 東京都庁 - // http://bboxfinder.com/#35.644424126412915,139.63645566328384,35.7345521481874,139.7469336572898 - const referenceBbox: BBox3D = [ 139.61360603969516, 35.6257704895902, 0, 139.76984541918617, 35.753230793225924, 10000 ]; + const referenceBbox: BBox3D = [ 139.61377266711673, 35.61310942775181, 0, 139.76998733288326, 35.74057, 10000 ]; assert.deepStrictEqual(bbox, referenceBbox); }); diff --git a/src/shared.test.ts b/src/shared.test.ts index 4b74084..539a9d0 100644 --- a/src/shared.test.ts +++ b/src/shared.test.ts @@ -4,13 +4,13 @@ export const namespaces: { [key: string]: LocalNamespace } = { // 東京都庁: 35.68950097945576, 139.69172572944066 "tokyo": new LocalNamespace({ scale: 10e3, // 10km - origin_latitude: 35.68950097945576, - origin_longitude: 139.69172572944066, + origin_latitude: 35.72599, // 35.68950097945576, + origin_longitude: 139.64657, // 139.69172572944066, }), "tokyo_45deg": new LocalNamespace({ - scale: 10e3, // 1km - origin_latitude: 35.68950097945576, - origin_longitude: 139.69172572944066, + scale: 10e3, // 10km + origin_latitude: 35.74057, // 35.68950097945576, + origin_longitude: 139.69188, // 139.69172572944066, origin_angle: 45, }), };