Add and use new proj4.register function

This commit is contained in:
Andreas Hocevar
2017-12-15 18:47:51 +01:00
parent 345ce340e4
commit 9f1ebbb852
20 changed files with 195 additions and 138 deletions

View File

@@ -8,7 +8,6 @@ import EPSG3857 from './proj/EPSG3857.js';
import EPSG4326 from './proj/EPSG4326.js';
import Projection from './proj/Projection.js';
import Units from './proj/Units.js';
import _ol_proj_proj4_ from './proj/proj4.js';
import _ol_proj_projections_ from './proj/projections.js';
import {add as addTransformFunc, clear as clearTransformFuncs, get as getTransformFunc} from './proj/transforms.js';
@@ -29,23 +28,6 @@ export var METERS_PER_UNIT = Units.METERS_PER_UNIT;
var SPHERE = new Sphere(Sphere.DEFAULT_RADIUS);
/**
* Register proj4. If not explicitly registered, it will be assumed that
* proj4js will be loaded in the global namespace. For example in a
* browserify ES6 environment you could use:
*
* import ol from 'openlayers';
* import proj4 from 'proj4';
* ol.proj.setProj4(proj4);
*
* @param {Proj4} proj4 Proj4.
* @api
*/
export function setProj4(proj4) {
_ol_proj_proj4_.set(proj4);
}
/**
* @param {Array.<number>} input Input coordinate array.
* @param {Array.<number>=} opt_output Output array of coordinate values.
@@ -121,13 +103,6 @@ export function get(projectionLike) {
} else if (typeof projectionLike === 'string') {
var code = projectionLike;
projection = _ol_proj_projections_.get(code);
if (!projection) {
var proj4js = _ol_proj_proj4_.get();
if (typeof proj4js == 'function' && proj4js.defs(code) !== undefined) {
projection = new Projection({code: code});
addProjection(projection);
}
}
}
return projection;
}
@@ -390,24 +365,6 @@ export function equivalent(projection1, projection2) {
export function getTransformFromProjections(sourceProjection, destinationProjection) {
var sourceCode = sourceProjection.getCode();
var destinationCode = destinationProjection.getCode();
if (!transformFunc) {
var proj4js = _ol_proj_proj4_.get();
if (typeof proj4js == 'function') {
var sourceDef = proj4js.defs(sourceCode);
var destinationDef = proj4js.defs(destinationCode);
if (sourceDef !== undefined && destinationDef !== undefined) {
if (sourceDef === destinationDef) {
addEquivalentProjections([destinationProjection, sourceProjection]);
} else {
var proj4Transform = proj4js(destinationCode, sourceCode);
addCoordinateTransforms(destinationProjection, sourceProjection,
proj4Transform.forward, proj4Transform.inverse);
}
transformFunc = _ol_proj_transforms_.get(sourceCode, destinationCode);
}
}
}
var transformFunc = getTransformFunc(sourceCode, destinationCode);
if (!transformFunc) {
transformFunc = identityTransform;

View File

@@ -2,7 +2,6 @@
* @module ol/proj/Projection
*/
import _ol_proj_Units_ from '../proj/Units.js';
import _ol_proj_proj4_ from '../proj/proj4.js';
/**
* @classdesc
@@ -105,23 +104,6 @@ var _ol_proj_Projection_ = function(options) {
* @type {number|undefined}
*/
this.metersPerUnit_ = options.metersPerUnit;
var code = options.code;
var proj4js = _ol_proj_proj4_.get();
if (typeof proj4js == 'function') {
var def = proj4js.defs(code);
if (def !== undefined) {
if (def.axis !== undefined && options.axisOrientation === undefined) {
this.axisOrientation_ = def.axis;
}
if (options.metersPerUnit === undefined) {
this.metersPerUnit_ = def.to_meter;
}
if (options.units === undefined) {
this.units_ = def.units;
}
}
}
};

View File

@@ -21,15 +21,14 @@
* for the required projections. These definitions can be obtained from
* {@link https://epsg.io/}, and are a JS function, so can be loaded in a script
* tag (as in the examples) or pasted into your application.
* The first time there is a request for a projection, either with a
* {@link ol.proj.projectionLike} or directly with {@link ol.proj.get}, the
* code will check if the Proj4js library and the necessary definition are
* loaded; if so, it will register the appropriate {@link ol.proj.Projection}
* object and add transform functions between the new projection and all the
* existing ones. See examples/wms-image-custom-proj for an example of this.
* Because the check for presence of the Proj4js library and the definition only
* takes place on the first request for them, this means they can be loaded
* dynamically as needed; for example, with user-supplied data where you don't
*
* After all required projection definitions are added to proj4's registry (by
* using `proj4.defs()`), simply call `register(proj4)` from the `ol/proj/proj4`
* package. Existing transforms are not changed by this function. See
* examples/wms-image-custom-proj for an example of this.
*
* Additional projection definitions can be registered with `proj4.defs()` any
* time. Just make sure to call `register(proj4)` again; for example, with user-supplied data where you don't
* know in advance what projections are needed, you can initially load minimal
* support and then load whichever are requested.
*

View File

@@ -1,30 +1,51 @@
/**
* @module ol/proj/proj4
*/
var _ol_proj_proj4_ = {};
import {addCoordinateTransforms, addProjection, addEquivalentProjections, get} from '../proj.js';
import {get as getTransform} from './transforms.js';
import Projection from './Projection.js';
/**
* @private
* @type {Proj4}
* Make projections defined in proj4 (with `proj4.defs()`) available in
* OpenLayers.
*
* This function should be called whenever changes are made to the proj4
* registry, e.g. after calling `proj4.defs()`. Existing transforms will not be
* modified by this function.
*
* @param {?} proj4 Proj4.
* @api
*/
_ol_proj_proj4_.cache_ = null;
/**
* Store the proj4 function.
* @param {Proj4} proj4 The proj4 function.
*/
_ol_proj_proj4_.set = function(proj4) {
_ol_proj_proj4_.cache_ = proj4;
};
/**
* Get proj4.
* @return {Proj4} The proj4 function set above or available globally.
*/
_ol_proj_proj4_.get = function() {
return _ol_proj_proj4_.cache_ || window['proj4'];
};
export default _ol_proj_proj4_;
export function register(proj4) {
var projCodes = Object.keys(proj4.defs);
var len = projCodes.length;
var i, j;
for (i = 0; i < len; ++i) {
var code = projCodes[i];
if (!get(code)) {
var def = proj4.defs(code);
addProjection(new Projection({
code: code,
axisOrientation: def.axis,
metersPerUnit: def.to_meter,
units: def.units
}));
}
}
for (i = 0; i < len; ++i) {
var code1 = projCodes[i];
var proj1 = get(code1);
for (j = 0; j < len; ++j) {
var code2 = projCodes[j];
var proj2 = get(code2);
if (!getTransform(code1, code2)) {
if (proj4.defs[code1] === proj4.defs[code2]) {
addEquivalentProjections([proj1, proj2]);
} else {
var transform = proj4(code1, code2);
addCoordinateTransforms(proj1, proj2, transform.forward, transform.inverse);
}
}
}
}
}