Dedicated module for proj4 access

This commit is contained in:
Tim Schaub
2016-12-04 10:37:33 -08:00
parent b54342dfb8
commit a33caa598c
2 changed files with 31 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ goog.provide('ol.proj.Projection');
goog.require('ol');
goog.require('ol.extent');
goog.require('ol.proj.Units');
goog.require('ol.proj.proj4');
goog.require('ol.proj.projections');
goog.require('ol.proj.transforms');
goog.require('ol.sphere.NORMAL');
@@ -24,13 +25,6 @@ ol.proj.METERS_PER_UNIT[ol.proj.Units.METERS] = 1;
ol.proj.METERS_PER_UNIT[ol.proj.Units.USFEET] = 1200 / 3937;
/**
* @private
* @type {proj4}
*/
ol.proj.proj4_ = null;
if (ol.ENABLE_PROJ4JS) {
/**
* Register proj4. If not explicitly registered, it will be assumed that
@@ -47,7 +41,7 @@ if (ol.ENABLE_PROJ4JS) {
ol.proj.setProj4 = function(proj4) {
ol.DEBUG && console.assert(typeof proj4 == 'function',
'proj4 argument should be a function');
ol.proj.proj4_ = proj4;
ol.proj.proj4.set(proj4);
};
}
@@ -151,7 +145,7 @@ ol.proj.Projection = function(options) {
ol.DEBUG && console.assert(code !== undefined,
'Option "code" is required for constructing instance');
if (ol.ENABLE_PROJ4JS) {
var proj4js = ol.proj.proj4_ || window['proj4'];
var proj4js = ol.proj.proj4.get();
if (typeof proj4js == 'function' && !ol.proj.projections.get(code)) {
var def = proj4js.defs(code);
if (def !== undefined) {
@@ -579,7 +573,7 @@ ol.proj.get = function(projectionLike) {
var code = projectionLike;
projection = ol.proj.projections.get(code);
if (ol.ENABLE_PROJ4JS) {
var proj4js = ol.proj.proj4_ || window['proj4'];
var proj4js = ol.proj.proj4.get();
if (!projection && typeof proj4js == 'function' &&
proj4js.defs(code) !== undefined) {
projection = new ol.proj.Projection({code: code});
@@ -648,7 +642,7 @@ ol.proj.getTransformFromProjections = function(sourceProjection, destinationProj
var destinationCode = destinationProjection.getCode();
var transform = ol.proj.transforms.get(sourceCode, destinationCode);
if (ol.ENABLE_PROJ4JS && !transform) {
var proj4js = ol.proj.proj4_ || window['proj4'];
var proj4js = ol.proj.proj4.get();
if (typeof proj4js == 'function') {
var sourceDef = proj4js.defs(sourceCode);
var destinationDef = proj4js.defs(destinationCode);

26
src/ol/proj/proj4.js Normal file
View File

@@ -0,0 +1,26 @@
goog.provide('ol.proj.proj4');
/**
* @private
* @type {proj4}
*/
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'];
};