Skip to content

Commit 9750c63

Browse files
authored
Use fromProjectionCode instead of fromEPSGCode (#21)
* Use fromEPSGCode or fromProjectionCode depending on ol version
1 parent 06b0739 commit 9750c63

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/ol/source/GeoTIFF.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {error as logError} from 'ol/console.js';
1212
import {applyTransform, getCenter, getIntersection} from 'ol/extent.js';
1313
import {clamp} from 'ol/math.js';
1414
import {fromCode as unitsFromCode} from 'ol/proj/Units.js';
15-
import {fromEPSGCode} from 'ol/proj/proj4.js';
1615
import {
1716
Projection,
1817
createTransformFromCoordinateTransform,
@@ -28,6 +27,7 @@ import {
2827
makeInverse,
2928
multiply as multiplyTransform,
3029
} from 'ol/transform.js';
30+
import {loadProjection} from '../util.js';
3131

3232
/**
3333
* Determine if an image type is a mask.
@@ -196,11 +196,7 @@ async function getProjectionFromKeys(
196196
const code = 'EPSG:' + value;
197197
let projection = getCachedProjection(code);
198198
if (!projection && loadMissingProjection) {
199-
try {
200-
projection = await fromEPSGCode(value);
201-
} catch {
202-
// pass
203-
}
199+
projection = await loadProjection(code);
204200
}
205201
if (!projection) {
206202
const units = unitsFromCode(geoKeys[unitKey]);

src/ol/util.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
*/
44

55
import 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';
107
import Circle from 'ol/style/Circle.js';
118
import Fill from 'ol/style/Fill.js';
129
import 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
*/
226250
export 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

Comments
 (0)