Skip to content

Commit

Permalink
refactor: support xy and yx ordering when generating tile matrixes
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Feb 10, 2025
1 parent 599ce97 commit 4a49ea3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/geo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export { GoogleTms } from './tms/google.js';
export { TileMatrixSets } from './tms/index.js';
export { Nztm2000QuadTms, Nztm2000Tms } from './tms/nztm2000.js';
export { WmtsProvider } from './wmts/wmts.js';
export { getXyOrder } from './xy.order.js';
export { TileMatrixSetType, TileMatrixType } from '@linzjs/tile-matrix-set';
4 changes: 2 additions & 2 deletions packages/geo/src/tile.matrix.set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TileMatrixSetType, TileMatrixType } from '@linzjs/tile-matrix-set';

import { Bounds, Point } from './bounds.js';
import { Epsg } from './epsg.js';
import { getXyOrder, XyOrder } from './xy.order.js';
import { getXyOrder } from './xy.order.js';

export type Tile = Point & { z: number };

Expand Down Expand Up @@ -58,7 +58,7 @@ export class TileMatrixSet {
this.projection = projection;

/** Some projections @see EPSG:2193 are defined with XY Order of */
if (getXyOrder(this.projection) === XyOrder.Yx) {
if (getXyOrder(this.projection) === 'yx') {
this.indexX = 1;
this.indexY = 0;
}
Expand Down
24 changes: 10 additions & 14 deletions packages/geo/src/xy.order.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
import { Epsg } from './epsg.js';
import { Epsg, EpsgCode } from './epsg.js';

/**
* Order of X & Y coordinates when defined as a array
*
* `[x,y]` vs `[y,x]`
*/
export enum XyOrder {
/** [x, y] */
Xy,
/** [y, x] */
Yx,
}
export type XyOrder = 'xy' | 'yx';

/**
* Get the X & Y coordinate order for a given EPSG
* @param epsg EPSG to lookup
*/
export function getXyOrder(epsg: Epsg): XyOrder {
export function getXyOrder(epsg: Epsg | EpsgCode): XyOrder {
const code = typeof epsg === 'number' ? epsg : epsg.code;
/**
* [EPSG:2193](https://www.opengis.net/def/crs/EPSG/0/2193) (NZTM) is defined in [y, x]
* - [EPSG:2193](https://www.opengis.net/def/crs/EPSG/0/2193) (NZTM) is defined in [y, x]
* - [EPSG:3793](https://www.opengis.net/def/crs/EPSG/0/3793) (CITM) is defined in [y, x]
* specified by the coordinate system [cs:4500](https://www.opengis.net/def/cs/EPSG/0/4500)
*/
if (epsg === Epsg.Nztm2000) {
return XyOrder.Yx;
}
if (code === EpsgCode.Nztm2000 || code === EpsgCode.Citm2000) return 'yx';

// TODO there are other projections that are YX
return XyOrder.Xy;
// TODO there are other projections that are YX,
// TileMatrixSet v2 specification includes Xy ordering, https://docs.ogc.org/is/17-083r4/21-066r1.html#_adding_optional_orderedaxes_to_highlight_crs_axis_ordering
return 'xy';
}
17 changes: 9 additions & 8 deletions scripts/debug.tile.matrix.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Bounds, Epsg, Nztm2000QuadTms, Projection, ProjectionLoader } from '@basemaps/geo';
import { Bounds, Epsg, Nztm2000QuadTms, Projection, ProjectionLoader, getXyOrder } from '@basemaps/geo';
import { CliInfo } from '@basemaps/shared/build/cli/info';
import { writeFileSync } from 'fs';

/**
* Attempt to create a debug tile matrix given a projection code.
*
* General flow of the creaion
* - Lookup the projection information from spatialreference
* General flow of the creation
* - Lookup the projection information from spatial reference
* - Attempt to find the bounds of the projection
* - Using the bounds attempt to find a tile matrix zoom level that would cover the entire bounds
* - Create a basic debugging tile matrix from the information
Expand Down Expand Up @@ -77,14 +77,15 @@ async function main() {

const centerProjected = proj.fromWgs84([centerLon, centerLat])

const tileMatrixBounds = new Bounds(centerProjected[0] - halfTileZeroWidth, centerProjected[1] - halfTileZeroWidth, tileZeroWidth, tileZeroWidth);
const outputBounds = proj.boundsToGeoJsonFeature(tileMatrixBounds)

const tileMatrix = structuredClone(Nztm2000QuadTms.def)

const xyOrder = getXyOrder(TargetProjection)

const xy = xyOrder === 'xy' ? { x: 0, y: 1 } : { x: 1, y: 0 }

tileMatrix.title = 'Debug tile matrix for EPSG:' + TargetProjection;
tileMatrix.boundingBox.lowerCorner = [centerProjected[0] - halfTileZeroWidth, centerProjected[1] - halfTileZeroWidth]
tileMatrix.boundingBox.upperCorner = [centerProjected[0] + halfTileZeroWidth, centerProjected[1] + halfTileZeroWidth]
tileMatrix.boundingBox.lowerCorner = [centerProjected[xy.x] - halfTileZeroWidth, centerProjected[xy.y] - halfTileZeroWidth]
tileMatrix.boundingBox.upperCorner = [centerProjected[xy.x] + halfTileZeroWidth, centerProjected[xy.y] + halfTileZeroWidth]

tileMatrix.tileMatrix = tileMatrix.tileMatrix.slice(zOffset).map((x, i) => {
x.identifier = String(i);
Expand Down

0 comments on commit 4a49ea3

Please sign in to comment.