Skip to content

Commit

Permalink
Fix tests for origin
Browse files Browse the repository at this point in the history
  • Loading branch information
keichan34 committed Nov 20, 2024
1 parent 4ca1f37 commit 19f44a9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
35 changes: 26 additions & 9 deletions src/lib/tilebelt_local.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});

Expand All @@ -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]);
});
});
9 changes: 6 additions & 3 deletions src/lib/tilebelt_local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 11 additions & 13 deletions src/local_namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
});
});
Expand Down
6 changes: 2 additions & 4 deletions src/local_spatial_id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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);
});

Expand Down
10 changes: 5 additions & 5 deletions src/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
};
Expand Down

0 comments on commit 19f44a9

Please sign in to comment.