Skip to content

Commit

Permalink
refacto(Crs): remove tms/epsg family of functions
Browse files Browse the repository at this point in the history
BREAKING CHANGE: CRS.isEPSG and CRS.isTMS have been removed
BREAKING CHANGE: CRS.formatToESPG and CRS.formatToTMS have been removed
  • Loading branch information
Desplandis committed Nov 27, 2024
1 parent 2fdf15a commit 83eb0d9
Show file tree
Hide file tree
Showing 25 changed files with 58 additions and 129 deletions.
3 changes: 1 addition & 2 deletions src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three';
import * as Crs from 'Core/Geographic/Crs';
import Earcut from 'earcut';
import { FEATURE_TYPES } from 'Core/Feature';
import ReferLayerProperties from 'Layer/ReferencingLayerProperties';
Expand Down Expand Up @@ -64,7 +63,7 @@ class FeatureMesh extends THREE.Group {
} else {
// calculate the scale transformation to transform the feature.extent
// to feature.extent.as(crs)
coord.crs = Crs.formatToEPSG(this.#originalCrs);
coord.crs = this.#originalCrs;
// TODO: An extent here could be either a geographic extent (for
// features from WFS) or a tiled extent (for features from MVT).
// Unify both behavior.
Expand Down
3 changes: 1 addition & 2 deletions src/Converter/textureConverter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three';
import * as CRS from 'Core/Geographic/Crs';
import Feature2Texture from 'Converter/Feature2Texture';
import Extent from 'Core/Geographic/Extent';

Expand Down Expand Up @@ -27,7 +26,7 @@ export default {
new THREE.Color(backgroundLayer.paint['background-color']) :
undefined;

destinationTile.toExtent(CRS.formatToEPSG(layer.crs), extentTexture);
destinationTile.toExtent(layer.crs, extentTexture);
texture = Feature2Texture.createTextureFromFeature(data, extentTexture, layer.subdivisionThreshold, layer.style, backgroundColor);
texture.features = data;
texture.extent = destinationTile;
Expand Down
3 changes: 1 addition & 2 deletions src/Core/Feature.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three';
import * as CRS from 'Core/Geographic/Crs';
import Extent from 'Core/Geographic/Extent';
import Coordinates from 'Core/Geographic/Coordinates';
import Style from 'Core/Style';
Expand Down Expand Up @@ -347,7 +346,7 @@ export class FeatureCollection extends THREE.Object3D {
constructor(options) {
super();
this.isFeatureCollection = true;
this.crs = CRS.formatToEPSG(options.accurate || !options.source?.crs ? options.crs : options.source.crs);
this.crs = options.accurate || !options.source?.crs ? options.crs : options.source.crs;
this.features = [];
this.mergeFeatures = options.mergeFeatures === undefined ? true : options.mergeFeatures;
this.size = options.structure == '3d' ? 3 : 2;
Expand Down
53 changes: 1 addition & 52 deletions src/Core/Geographic/Crs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,6 @@ function mustBeString(crs: string) {
}
}

/**
* Checks that a given CRS identifier follows the TMS:XXXX format.
* @internal
*
* @param crs - The CRS identifier string.
*/
export function isTms(crs: string) {
return isString(crs) && crs.startsWith('TMS');
}

/**
* Checks that a given CRS identifier follows the EPSG:XXXX format.
* @internal
*
* @param crs - The CRS identifier string.
*/
export function isEpsg(crs: string) {
return isString(crs) && crs.startsWith('EPSG');
}

/**
* Converts the input to the tile matrix set notation: TMS:XXXX.
* @internal
*
* @param crs - The input to format.
*/
export function formatToTms(crs: string) {
mustBeString(crs);
return isTms(crs) ? crs : `TMS:${crs.match(/\d+/)?.[0]}`;
}

/**
* Converts the input to European Petroleum Survey Group notation: EPSG:XXXX.
* @internal
*
* @param crs - The input to format.
*/
export function formatToEPSG(crs: string) {
mustBeString(crs);
return isEpsg(crs) ? crs : `EPSG:${crs.match(/\d+/)?.[0]}`;
}

/**
* System units supported for a coordinate system. See
* [proj](https://proj4.org/en/9.5/operations/conversions/unitconvert.html#angular-units).
Expand All @@ -79,15 +37,6 @@ export const UNIT = {
METER: 2,
} as const;

/**
* @internal
*/
export const tms_3857 = 'TMS:3857';
/**
* @internal
*/
export const tms_4326 = 'TMS:4326';

/**
* Checks that the CRS is EPSG:4326.
* @internal
Expand Down Expand Up @@ -120,7 +69,7 @@ function unitFromProj4Unit(proj: ProjectionDefinition) {
*/
export function getUnit(crs: ProjectionLike) {
mustBeString(crs);
const p = proj4.defs(formatToEPSG(crs));
const p = proj4.defs(crs);
if (!p) {
return undefined;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Core/Geographic/Extent.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ class Extent {
throw new Error(`${crs} is a geocentric projection, it doesn't make sense with a geographical extent`);
}

if (CRS.isTms(crs)) {
throw new Error(`${crs} is a tiled projection, use Tile instead`);
}

this.isExtent = true;
this.crs = crs;

Expand Down
9 changes: 4 additions & 5 deletions src/Core/Prefab/Globe/GlobeLayer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three';
import * as CRS from 'Core/Geographic/Crs';
import TiledGeometryLayer from 'Layer/TiledGeometryLayer';
import { ellipsoidSizes } from 'Core/Math/Ellipsoid';
import { globalExtentTMS, schemeTiles } from 'Core/Tile/TileGrid';
Expand Down Expand Up @@ -48,13 +47,13 @@ class GlobeLayer extends TiledGeometryLayer {
*/
constructor(id, object3d, config = {}) {
// Configure tiles
const scheme = schemeTiles.get(CRS.tms_4326);
const scheme = schemeTiles.get('EPSG:4326');
const schemeTile = globalExtentTMS.get('EPSG:4326').subdivisionByScheme(scheme);

// Supported tile matrix set for color/elevation layer
config.tileMatrixSets = [
CRS.tms_4326,
CRS.tms_3857,
'EPSG:4326',
'EPSG:3857',
];
const uvCount = config.tileMatrixSets.length;
const builder = new BuilderEllipsoidTile({ crs: 'EPSG:4978', uvCount });
Expand Down Expand Up @@ -104,7 +103,7 @@ class GlobeLayer extends TiledGeometryLayer {

subdivision(context, layer, node) {
if (node.level == 5) {
const row = node.getExtentsByProjection(CRS.tms_4326)[0].row;
const row = node.getExtentsByProjection('EPSG:4326')[0].row;
if (row == 31 || row == 0) {
// doesn't subdivise the pole
return false;
Expand Down
6 changes: 2 additions & 4 deletions src/Core/Prefab/GlobeView.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as THREE from 'three';

import * as CRS from 'Core/Geographic/Crs';

import View, { VIEW_EVENTS } from 'Core/View';
import GlobeControls from 'Controls/GlobeControls';
import Coordinates from 'Core/Geographic/Coordinates';
Expand Down Expand Up @@ -139,11 +137,11 @@ class GlobeView extends View {
return Promise.reject(new Error('Add Layer type object'));
}
if (layer.isColorLayer) {
if (!this.tileLayer.tileMatrixSets.includes(CRS.formatToTms(layer.source.crs))) {
if (!this.tileLayer.tileMatrixSets.includes(layer.source.crs)) {
return layer._reject(`Only ${this.tileLayer.tileMatrixSets} tileMatrixSet are currently supported for color layers`);
}
} else if (layer.isElevationLayer) {
if (CRS.formatToTms(layer.source.crs) !== this.tileLayer.tileMatrixSets[0]) {
if (layer.source.crs !== this.tileLayer.tileMatrixSets[0]) {
return layer._reject(`Only ${this.tileLayer.tileMatrixSets[0]} tileMatrixSet is currently supported for elevation layers`);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Core/Prefab/Planar/PlanarLayer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as THREE from 'three';

import * as CRS from 'Core/Geographic/Crs';
import TiledGeometryLayer from 'Layer/TiledGeometryLayer';
import { globalExtentTMS } from 'Core/Tile/TileGrid';
import PlanarTileBuilder from './PlanarTileBuilder';
Expand Down Expand Up @@ -35,8 +34,7 @@ class PlanarLayer extends TiledGeometryLayer {
* @throws {Error} `object3d` must be a valid `THREE.Object3d`.
*/
constructor(id, extent, object3d, config = {}) {
const tms = CRS.formatToTms(extent.crs);
const tileMatrixSets = [tms];
const tileMatrixSets = [extent.crs];
if (!globalExtentTMS.get(extent.crs)) {
// Add new global extent for this new crs projection.
globalExtentTMS.set(extent.crs, extent);
Expand Down
6 changes: 3 additions & 3 deletions src/Core/Tile/Tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ class Tile {
* @returns {Tile[]}
*/
export function tiledCovering(e, tms) {
if (e.crs == 'EPSG:4326' && tms == CRS.tms_3857) {
if (e.crs == 'EPSG:4326' && tms == 'EPSG:3857') {
const WMTS_PM = [];
const extent = _extent.copy(e).as(CRS.formatToEPSG(tms), _extent2);
const { globalExtent, globalDimension, sTs } = getInfoTms(CRS.formatToEPSG(tms));
const extent = _extent.copy(e).as(tms, _extent2);
const { globalExtent, globalDimension, sTs } = getInfoTms(tms);
extent.clampByExtent(globalExtent);
extent.planarDimensions(_dimensionTile);

Expand Down
17 changes: 7 additions & 10 deletions src/Core/Tile/TileGrid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three';
import * as CRS from '../Geographic/Crs';
import Extent from '../Geographic/Extent';

const _countTiles = new THREE.Vector2();
Expand All @@ -19,27 +18,25 @@ extent3857.clampSouthNorth(extent3857.west, extent3857.east);
globalExtentTMS.set('EPSG:3857', extent3857);

schemeTiles.set('default', new THREE.Vector2(1, 1));
schemeTiles.set(CRS.tms_3857, schemeTiles.get('default'));
schemeTiles.set(CRS.tms_4326, new THREE.Vector2(2, 1));
schemeTiles.set('EPSG:3857', schemeTiles.get('default'));
schemeTiles.set('EPSG:4326', new THREE.Vector2(2, 1));

export function getInfoTms(/** @type {string} */ crs) {
const epsg = CRS.formatToEPSG(crs);
const globalExtent = globalExtentTMS.get(epsg);
const globalExtent = globalExtentTMS.get(crs);
const globalDimension = globalExtent.planarDimensions(_dim);
const tms = CRS.formatToTms(crs);
const sTs = schemeTiles.get(tms) || schemeTiles.get('default');
const sTs = schemeTiles.get(crs) || schemeTiles.get('default');
// The isInverted parameter is to be set to the correct value, true or false
// (default being false) if the computation of the coordinates needs to be
// inverted to match the same scheme as OSM, Google Maps or other system.
// See link below for more information
// https://alastaira.wordpress.com/2011/07/06/converting-tms-tile-coordinates-to-googlebingosm-tile-coordinates/
// in crs includes ':NI' => tms isn't inverted (NOT INVERTED)
const isInverted = !tms.includes(':NI');
return { epsg, globalExtent, globalDimension, sTs, isInverted };
const isInverted = !crs.includes(':NI');
return { epsg: crs, globalExtent, globalDimension, sTs, isInverted };
}

export function getCountTiles(/** @type {string} */ crs, /** @type {number} */ zoom) {
const sTs = schemeTiles.get(CRS.formatToTms(crs)) || schemeTiles.get('default');
const sTs = schemeTiles.get(crs) || schemeTiles.get('default');
const count = 2 ** zoom;
_countTiles.set(count, count).multiply(sTs);
return _countTiles;
Expand Down
3 changes: 1 addition & 2 deletions src/Core/TileMesh.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three';
import * as CRS from 'Core/Geographic/Crs';
import { geoidLayerIsVisible } from 'Layer/GeoidLayer';
import { tiledCovering } from 'Core/Tile/Tile';

Expand Down Expand Up @@ -77,7 +76,7 @@ class TileMesh extends THREE.Mesh {
}

getExtentsByProjection(crs) {
return this.#_tms.get(CRS.formatToTms(crs));
return this.#_tms.get(crs);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Core/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function _preprocessLayer(view, layer, parentLayer) {
// Find crs projection layer, this is projection destination
layer.crs = view.referenceCrs;
} else if (!layer.crs) {
if (parentLayer && parentLayer.tileMatrixSets && parentLayer.tileMatrixSets.includes(CRS.formatToTms(source.crs))) {
if (parentLayer && parentLayer.tileMatrixSets && parentLayer.tileMatrixSets.includes(source.crs)) {
layer.crs = source.crs;
} else {
layer.crs = parentLayer && parentLayer.extent.crs;
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer/OBB.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class OBB extends THREE.Object3D {
this.position.copy(position);
this.quaternion.copy(quaternion);
this.updateMatrixWorld(true);
} else if (!CRS.isTms(extent.crs) && CRS.isMetricUnit(extent.crs)) {
} else if (CRS.isMetricUnit(extent.crs)) {
extent.center(coord).toVector3(this.position);
extent.planarDimensions(dimension);
size.set(dimension.x, dimension.y, Math.abs(maxHeight - minHeight));
Expand Down
3 changes: 1 addition & 2 deletions src/Renderer/RasterTile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as THREE from 'three';
import * as CRS from 'Core/Geographic/Crs';
import { ELEVATION_MODES } from 'Renderer/LayeredMaterial';
import { checkNodeElevationTextureValidity, insertSignificantValuesFromParent, computeMinMaxElevation } from 'Parser/XbilParser';

Expand Down Expand Up @@ -32,7 +31,7 @@ class RasterTile extends THREE.EventDispatcher {
constructor(material, layer) {
super();
this.layer = layer;
this.crs = layer.parent.tileMatrixSets.indexOf(CRS.formatToTms(layer.crs));
this.crs = layer.parent.tileMatrixSets.indexOf(layer.crs);
if (this.crs == -1) {
console.error('Unknown crs:', layer.crs);
}
Expand Down
3 changes: 1 addition & 2 deletions src/Source/FileSource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as CRS from 'Core/Geographic/Crs';
import Source from 'Source/Source';
import Cache from 'Core/Scheduler/Cache';

Expand Down Expand Up @@ -159,7 +158,7 @@ class FileSource extends Source {
if (!features) {
options.out.buildExtent = this.crs != 'EPSG:4978';
if (options.out.buildExtent) {
options.out.forcedExtentCrs = options.out.crs != 'EPSG:4978' ? options.out.crs : CRS.formatToEPSG(this.crs);
options.out.forcedExtentCrs = options.out.crs != 'EPSG:4978' ? options.out.crs : this.crs;
}
features = this.parser(this.fetchedData, options);
this._featuresCaches[options.out.crs].setByArray(features, [0]);
Expand Down
5 changes: 2 additions & 3 deletions src/Source/TMSSource.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as CRS from 'Core/Geographic/Crs';
import Source from 'Source/Source';
import URLBuilder from 'Provider/URLBuilder';
import Extent from 'Core/Geographic/Extent';
import Tile from 'Core/Tile/Tile';
import { globalExtentTMS } from 'Core/Tile/TileGrid';

const _tile = new Tile(CRS.tms_4326, 0, 0, 0);
const _tile = new Tile('EPSG:4326', 0, 0, 0);

/**
* An object defining the source of resources to get from a
Expand Down Expand Up @@ -95,7 +94,7 @@ class TMSSource extends Source {
this.zoom = source.zoom;

this.isInverted = source.isInverted || false;
this.crs = CRS.formatToTms(source.crs);
this.crs = source.crs;
this.tileMatrixSetLimits = source.tileMatrixSetLimits;
this.extentSetlimits = {};
this.tileMatrixCallback = source.tileMatrixCallback || (zoomLevel => zoomLevel);
Expand Down
3 changes: 1 addition & 2 deletions src/Source/WFSSource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as CRS from 'Core/Geographic/Crs';
import Source from 'Source/Source';
import URLBuilder from 'Provider/URLBuilder';
import Extent from 'Core/Geographic/Extent';
Expand Down Expand Up @@ -161,7 +160,7 @@ class WFSSource extends Source {
}

requestToKey(extent) {
if (CRS.isTms(extent.crs)) {
if (extent.isTile) {
return super.requestToKey(extent);
} else {
return [extent.zoom, extent.south, extent.west];
Expand Down
2 changes: 1 addition & 1 deletion test/functional/vector_tile_3d_mesh_mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('vector_tile_3d_mesh_mapbox', function _describe() {
it('should correctly load building features on a given TMS tile', async function _it() {
const featuresCollection = await page.evaluate(async function _() {
const layers = view.getLayers(l => l.source && l.source.isVectorSource);
const res = await layers[0].source.loadData({ zoom: 15, row: 11634, col: 16859 }, { crs: 'EPSG:4978', source: { crs: 'TMS:3857' } });
const res = await layers[0].source.loadData({ zoom: 15, row: 11634, col: 16859 }, { crs: 'EPSG:4978', source: { crs: 'EPSG:3857' } });
return res;
});
assert.ok(featuresCollection.isFeatureCollection);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/layeredmaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('material state vs layer state', function () {
view.tileLayer.colorLayersOrder = [layer.id];
view.addLayer(layer);

const extent = new Tile('TMS:4326', 3, 0, 0).toExtent('EPSG:4326');
const extent = new Tile('EPSG:4326', 3, 0, 0).toExtent('EPSG:4326');
material = new LayeredMaterial();
const geom = new THREE.BufferGeometry();
geom.OBB = new OBB(new THREE.Vector3(), new THREE.Vector3(1, 1, 1));
Expand Down
8 changes: 4 additions & 4 deletions test/unit/layeredmaterialnodeprocessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ describe('updateLayeredMaterialNodeImagery', function () {
crs: 'EPSG:4326',
info: { update: () => {} },
tileMatrixSets: [
'TMS:4326',
'TMS:3857',
'EPSG:4326',
'EPSG:3857',
],
parent: { tileMatrixSets: [
'TMS:4326',
'TMS:3857',
'EPSG:4326',
'EPSG:3857',
],
},
});
Expand Down
Loading

0 comments on commit 83eb0d9

Please sign in to comment.