From 220e7cbea4015af5c2f757f8066f66f3bd679b8a Mon Sep 17 00:00:00 2001 From: arieger Date: Wed, 10 Oct 2018 20:36:41 +0200 Subject: [PATCH] provide default crs definitions for ProjectionUtil --- src/ProjectionUtil/ProjectionUtil.js | 92 +++++++++++++++++++---- src/ProjectionUtil/ProjectionUtil.spec.js | 20 ++--- 2 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/ProjectionUtil/ProjectionUtil.js b/src/ProjectionUtil/ProjectionUtil.js index 2aa8ff6a5..58416a85e 100644 --- a/src/ProjectionUtil/ProjectionUtil.js +++ b/src/ProjectionUtil/ProjectionUtil.js @@ -1,6 +1,8 @@ import proj4 from 'proj4'; import { register } from 'ol/proj/proj4'; +import isEmpty from 'lodash/isEmpty'; + /** * Helper class for ol/proj4 projection handling. * @@ -9,32 +11,92 @@ import { register } from 'ol/proj/proj4'; export class ProjectionUtil { /** - * Registers custom crs definitions to the application. + * Default proj4 CRS definitions. + */ + static defaultProj4CrsDefinitions = { + 'EPSG:25832': '+proj=utm +zone=32 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs', + 'EPSG:31466': '+proj=tmerc +lat_0=0 +lon_0=6 +k=1 +x_0=2500000 +y_0=0 +ellps=bessel +towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 +units=m +no_defs', + 'EPSG:31467': '+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0 +ellps=bessel +towgs84=598.1,73.7,418.2,0.202,0.045,-2.455,6.7 +units=m +no_defs' + } + + /** + * Default mappings for CRS identifiers (e.g. "urn:ogc:def:crs:EPSG::25832"). + */ + static defaultProj4CrsMappings = { + 'urn:ogc:def:crs:EPSG::3857': 'EPSG:3857', + 'urn:ogc:def:crs:EPSG::25832': 'EPSG:25832', + 'urn:ogc:def:crs:EPSG::31466': 'EPSG:31466', + 'urn:ogc:def:crs:EPSG::31467': 'EPSG:31467' + } + + /** + * Registers custom CRS definitions to the application. * - * @param {Object} proj4CrsDefinitions The (custom) proj4 definition strings. + * @param {Object} customCrsDefs The custom `proj4` definition strings + * which should be registered additionally to default avaliable CRS (s. + * `defaultProj4CrsDefinitions` above) as well. + * Further CRS definitions in proj4 format can be checked under + * http://epsg.io (e.g. http://epsg.io/3426.proj4). + * @param {Boolean} registerDefaults Whether the default CRS should be + * registered or not. Default is true. */ - static initProj4Definitions(proj4CrsDefinitions) { - for (let [projCode, projDefinition] of Object.entries(proj4CrsDefinitions)) { - proj4.defs(projCode, projDefinition); + static initProj4Definitions(customCrsDefs, registerDefaults = true) { + let proj4CrsDefinitions; + + if (registerDefaults) { + proj4CrsDefinitions = ProjectionUtil.defaultProj4CrsDefinitions; + } + + if (!isEmpty(customCrsDefs)) { + Object.keys(customCrsDefs).forEach(crsKey => { + if (!(crsKey in proj4CrsDefinitions)) { + proj4CrsDefinitions[crsKey] = customCrsDefs[crsKey]; + } + }); + } + + if (!isEmpty(proj4CrsDefinitions)) { + for (let [projCode, projDefinition] of Object.entries(proj4CrsDefinitions)) { + proj4.defs(projCode, projDefinition); + } + register(proj4); } - register(proj4); } /** - * Registers custom crs mappings to allow automatic crs detection. Sometimes + * Registers custom CRS mappings to allow automatic CRS detection. Sometimes * FeatureCollections returned by the GeoServer may be associated with - * crs identifiers (e.g. "urn:ogc:def:crs:EPSG::25832") that aren't - * supported by proj4 and/or ol per default. Add appropriate - * mappings to allow automatic crs detection by ol here. + * CRS identifiers (e.g. "urn:ogc:def:crs:EPSG::25832") that aren't + * supported by `proj4` and `OpenLayers` per default. Add appropriate + * mappings to allow automatic CRS detection by `OpenLayers` here. * - * @param {Object} proj4CrsMappings The crs mappings. + * @param {Object} customCrsMappings The custom CRS mappings which will be + * added additionally to the by default avaliable (s. `defaultProj4CrsMappings` + * above). + * @param {Boolean} useDefaultMappings Whether the default CRS should be mapped + * as well or not. Default is true. */ - static initProj4DefinitionMappings(proj4CrsMappings) { - for (let [aliasProjCode, projCode] of Object.entries(proj4CrsMappings)) { - proj4.defs(aliasProjCode, proj4.defs(projCode)); + static initProj4DefinitionMappings(customCrsMappings, useDefaultMappings = true) { + let proj4CrsMappings; + + if (useDefaultMappings) { + proj4CrsMappings = ProjectionUtil.defaultProj4CrsMappings; + } + + if (!isEmpty(customCrsMappings)) { + Object.keys(customCrsMappings).forEach(crsKey => { + if (!(crsKey in proj4CrsMappings)) { + proj4CrsMappings[crsKey] = customCrsMappings[crsKey]; + } + }); } - } + if (!isEmpty(proj4CrsMappings)) { + for (let [aliasProjCode, projCode] of Object.entries(proj4CrsMappings)) { + proj4.defs(aliasProjCode, proj4.defs(projCode)); + } + } + } } export default ProjectionUtil; diff --git a/src/ProjectionUtil/ProjectionUtil.spec.js b/src/ProjectionUtil/ProjectionUtil.spec.js index a31760321..07b99e34d 100644 --- a/src/ProjectionUtil/ProjectionUtil.spec.js +++ b/src/ProjectionUtil/ProjectionUtil.spec.js @@ -6,15 +6,6 @@ import ProjectionUtil from './ProjectionUtil.js'; describe('ProjectionUtil', () => { - const testCrsDefinition = { - 'EPSG:25832': '+proj=utm +zone=32 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs' - }; - - const testCrsMappings = { - 'urn:ogc:def:crs:EPSG::3857': 'EPSG:3857', - 'urn:ogc:def:crs:EPSG::25832': 'EPSG:25832' - }; - describe('Basic test', () => { it('is defined', () => { expect(ProjectionUtil).not.toBeUndefined(); @@ -29,9 +20,10 @@ describe('ProjectionUtil', () => { it('it registers the given CRS definitions in proj4 and ol', () => { OlProj4.register = jest.fn(); const proj4Spy = jest.spyOn(proj4, 'defs'); + const length = Object.keys(ProjectionUtil.defaultProj4CrsDefinitions).length; - ProjectionUtil.initProj4Definitions(testCrsDefinition); - expect(proj4Spy).toHaveBeenCalled(); + ProjectionUtil.initProj4Definitions(); + expect(proj4Spy).toHaveBeenCalledTimes(length); expect(OlProj4.register).toHaveBeenCalled(); proj4Spy.mockReset(); @@ -45,14 +37,14 @@ describe('ProjectionUtil', () => { }); it('it registers the given CRS mappings in proj4', () => { const proj4Spy = jest.spyOn(proj4, 'defs'); + const length = Object.keys(ProjectionUtil.defaultProj4CrsMappings).length; - ProjectionUtil.initProj4DefinitionMappings(testCrsMappings); - expect(proj4Spy).toHaveBeenCalledTimes(4); + ProjectionUtil.initProj4DefinitionMappings(ProjectionUtil.defaultProj4CrsMappings); + expect(proj4Spy).toHaveBeenCalledTimes(length * 2); proj4Spy.mockReset(); proj4Spy.mockRestore(); }); }); }); - });