33 */
44
55import VectorLayer from 'ol/layer/Vector.js' ;
6- import {
7- fromEPSGCode ,
8- isRegistered as isProj4Registered ,
9- } from 'ol/proj/proj4.js' ;
6+ import { isRegistered as isProj4Registered } from 'ol/proj/proj4.js' ;
107import Circle from 'ol/style/Circle.js' ;
118import Fill from 'ol/style/Fill.js' ;
129import Stroke from 'ol/style/Stroke.js' ;
@@ -23,6 +20,12 @@ import {STAC} from 'stac-js';
2320/**
2421 * @typedef {import('ol/Feature.js').default } Feature
2522 */
23+ /**
24+ * @typedef {import('ol/proj.js').Projection } Projection
25+ */
26+ /**
27+ * @typedef {import('ol/proj.js').ProjectionLike } ProjectionLike
28+ */
2629
2730/**
2831 * The pattern for the supported versions of the label extension.
@@ -217,29 +220,43 @@ export function getGeoTiffSourceInfoFromAsset(asset, selectedBands) {
217220 return sourceInfo ;
218221}
219222
223+ /**
224+ * Load the projection for the given projection code from the internet.
225+ *
226+ * @param {string } code Projection code, e.g. 'EPSG:1234'
227+ * @return {Promise<Projection|null> } The loaded projection
228+ */
229+ export async function loadProjection ( code ) {
230+ try {
231+ // @ts -ignore - Support both old and new OpenLayers versions
232+ const { fromProjectionCode, fromEPSGCode} = await import ( 'ol/proj/proj4.js' ) ;
233+ if ( typeof fromProjectionCode === 'function' ) {
234+ // Supported since ol v10.8.0
235+ return await fromProjectionCode ( code ) ;
236+ }
237+ // Supported until ol v11.0.0
238+ return await fromEPSGCode ( code ) ;
239+ } catch ( _ ) {
240+ return null ;
241+ }
242+ }
243+
220244/**
221245 * Gets the projection from the asset or link.
222246 * @param {import('stac-js').STACReference } reference The asset or link to read the information from.
223- * @param {import('ol/proj.js'). ProjectionLike } defaultProjection A default projection to use.
224- * @return {Promise<import('ol/proj.js'). ProjectionLike> } The projection, if any.
247+ * @param {ProjectionLike } defaultProjection A default projection to use.
248+ * @return {Promise<ProjectionLike> } The projection, if any.
225249 */
226250export async function getProjection ( reference , defaultProjection = undefined ) {
227- let projection = defaultProjection ;
251+ let projection ;
228252 if ( isProj4Registered ( ) ) {
229253 // TODO: It would be great to handle WKT2 and PROJJSON, but is not supported yet by proj4js.
230254 const code = reference . getMetadata ( 'proj:code' ) ;
231255 if ( code ) {
232- try {
233- if ( code . startsWith ( 'EPSG:' ) ) {
234- const id = parseInt ( code . replace ( 'EPSG:' , '' ) , 10 ) ;
235- projection = await fromEPSGCode ( id ) ;
236- }
237- } catch ( _ ) {
238- // pass
239- }
256+ projection = await loadProjection ( code ) ;
240257 }
241258 }
242- return projection ;
259+ return projection || defaultProjection ;
243260}
244261
245262/**
0 commit comments