From fce97421bd6c946f4c70b3e2f9bf9f2874c1aa4a Mon Sep 17 00:00:00 2001 From: Benjamin Gerber Date: Mon, 23 Sep 2024 09:34:16 +0200 Subject: [PATCH] Better returns for proj registration --- src/proj/EPSG_2056.js | 7 +++---- src/proj/EPSG_21781.js | 7 +++---- src/proj/utils.js | 36 ++++++++++++++++++++++-------------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/proj/EPSG_2056.js b/src/proj/EPSG_2056.js index 829832448026..b156920c2cd8 100644 --- a/src/proj/EPSG_2056.js +++ b/src/proj/EPSG_2056.js @@ -19,7 +19,7 @@ // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -import create from 'ngeo/proj/utils'; +import {createProjection} from 'ngeo/proj/utils'; export const code = 'EPSG:2056'; @@ -37,8 +37,7 @@ const def = [ ]; const extent = [2420000, 1030000, 2900000, 1350000]; -const projections = {}; -projections[code] = {'definition': def, 'extent': extent}; -export const proj = create(projections); +const projection = {'definition': def, 'extent': extent}; +export const proj = createProjection(code, projection); export default code; diff --git a/src/proj/EPSG_21781.js b/src/proj/EPSG_21781.js index c68faf9f0dd5..f58a0c77f5ed 100644 --- a/src/proj/EPSG_21781.js +++ b/src/proj/EPSG_21781.js @@ -19,7 +19,7 @@ // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -import create from 'ngeo/proj/utils'; +import {createProjection} from 'ngeo/proj/utils'; export const code = 'EPSG:21781'; @@ -37,8 +37,7 @@ const def = [ ]; const extent = [420000, 30000, 900000, 350000]; -const projections = {}; -projections[code] = {'definition': def, 'extent': extent}; -export const proj = create(projections); +const projection = {'definition': def, 'extent': extent}; +export const proj = createProjection(code, projection); export default code; diff --git a/src/proj/utils.js b/src/proj/utils.js index 5415fadb31b2..99d0226fc860 100644 --- a/src/proj/utils.js +++ b/src/proj/utils.js @@ -19,25 +19,33 @@ // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -import {get as getProjection, add as addProjection} from 'ol/proj'; +import {get as getProjection} from 'ol/proj'; import {register} from 'ol/proj/proj4'; import proj4 from 'proj4'; -/** @type {import('gmf/options').gmfProjectionsOptions} */ -export default function createProjections(projections) { - for (const code in projections) { - proj4.defs(code, projections[code].definition.join(' ').trim()); - const match = /^EPSG:(\d+)$/.exec(code); - if (match !== null) { - proj4.defs( - 'http://www.opengis.net/gml/srs/epsg.xml#' + match[1], //NOSONAR - proj4.defs(code), - ); - } +/** + * @type {string} the projection code. + * @type {import('gmf/options').Projection} the projection settings. + * @returns {import('ol/proj/Projection').default} the registered projection. + */ +export function createProjection(code, projection) { + proj4.defs(code, projection.definition.join(' ').trim()); + const match = /^EPSG:(\d+)$/.exec(code); + if (match !== null) { + proj4.defs( + 'http://www.opengis.net/gml/srs/epsg.xml#' + match[1], //NOSONAR + proj4.defs(code), + ); } register(proj4); + const proj = getProjection(code); + proj.setExtent(projection.extent); + return proj; +} + +/** @type {import('gmf/options').gmfProjectionsOptions} */ +export default function createProjections(projections) { for (const code in projections) { - const proj = getProjection(code); - proj.setExtent(projections[code].extent); + createProjection(code, projections[code]); } }