Skip to content

Commit

Permalink
Merge pull request #41 from annarieger/projection-util-default-defs
Browse files Browse the repository at this point in the history
Default CRS definitions for ProjectionUtil
  • Loading branch information
annarieger committed Oct 11, 2018
2 parents bb45b64 + 220e7cb commit 1fe246b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 29 deletions.
92 changes: 77 additions & 15 deletions src/ProjectionUtil/ProjectionUtil.js
Original file line number Diff line number Diff line change
@@ -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.
*
Expand All @@ -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;
20 changes: 6 additions & 14 deletions src/ProjectionUtil/ProjectionUtil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
});
});
});

});

0 comments on commit 1fe246b

Please sign in to comment.