Configure proj4 options and transforms upon construction

This commit is contained in:
Andreas Hocevar
2015-03-26 10:39:26 +01:00
parent 2c04e4c071
commit 338901774b
2 changed files with 42 additions and 39 deletions
+3 -3
View File
@@ -411,7 +411,7 @@ olx.OverlayOptions.prototype.autoPanMargin;
/** /**
* Object literal with config options for the projection. * Object literal with config options for the projection.
* @typedef {{code: string, * @typedef {{code: string,
* units: (ol.proj.Units|string), * units: (ol.proj.Units|string|undefined),
* extent: (ol.Extent|undefined), * extent: (ol.Extent|undefined),
* axisOrientation: (string|undefined), * axisOrientation: (string|undefined),
* global: (boolean|undefined), * global: (boolean|undefined),
@@ -431,8 +431,8 @@ olx.ProjectionOptions.prototype.code;
/** /**
* Units. * Units. Required unless a proj4 projection is defined for `code`.
* @type {ol.proj.Units|string} * @type {ol.proj.Units|string|undefined}
* @api stable * @api stable
*/ */
olx.ProjectionOptions.prototype.units; olx.ProjectionOptions.prototype.units;
+39 -36
View File
@@ -134,6 +134,41 @@ ol.proj.Projection = function(options) {
*/ */
this.defaultTileGrid_ = null; this.defaultTileGrid_ = null;
if (ol.ENABLE_PROJ4JS && typeof proj4 == 'function') {
var code = options.code;
var def = proj4.defs(code);
if (goog.isDef(def)) {
if (goog.isDef(def.axis) && !goog.isDef(options.axisOrientation)) {
this.axisOrientation_ = def.axis;
}
if (!goog.isDef(options.units)) {
var units = def.units;
if (!goog.isDef(units)) {
if (goog.isDef(def.to_meter)) {
units = def.to_meter.toString();
ol.proj.METERS_PER_UNIT[units] = def.to_meter;
}
}
this.units_ = units;
}
var projections = ol.proj.projections_;
var currentCode, currentDef, currentProj, proj4Transform;
for (currentCode in projections) {
currentDef = proj4.defs(currentCode);
if (goog.isDef(currentDef)) {
currentProj = ol.proj.get(currentCode);
if (currentDef === def) {
ol.proj.addEquivalentProjections([currentProj, this]);
} else {
proj4Transform = proj4(currentCode, code);
ol.proj.addCoordinateTransforms(currentProj, this,
proj4Transform.forward, proj4Transform.inverse);
}
}
}
}
}
}; };
@@ -562,43 +597,11 @@ ol.proj.get = function(projectionLike) {
projection = projectionLike; projection = projectionLike;
} else if (goog.isString(projectionLike)) { } else if (goog.isString(projectionLike)) {
var code = projectionLike; var code = projectionLike;
var projections = ol.proj.projections_; projection = ol.proj.projections_[code];
projection = projections[code];
if (ol.ENABLE_PROJ4JS && !goog.isDef(projection) && if (ol.ENABLE_PROJ4JS && !goog.isDef(projection) &&
typeof proj4 == 'function') { typeof proj4 == 'function' && goog.isDef(proj4.defs(code))) {
var def = proj4.defs(code); projection = new ol.proj.Projection({code: code});
if (goog.isDef(def)) { ol.proj.addProjection(projection);
var units = def.units;
if (!goog.isDef(units)) {
if (goog.isDef(def.to_meter)) {
units = def.to_meter.toString();
ol.proj.METERS_PER_UNIT[units] = def.to_meter;
}
}
projection = new ol.proj.Projection({
code: code,
units: units,
axisOrientation: def.axis
});
ol.proj.addProjection(projection);
var currentCode, currentDef, currentProj, proj4Transform;
for (currentCode in projections) {
currentDef = proj4.defs(currentCode);
if (goog.isDef(currentDef)) {
currentProj = ol.proj.get(currentCode);
if (currentDef === def) {
ol.proj.addEquivalentProjections([currentProj, projection]);
} else {
proj4Transform = proj4(currentCode, code);
ol.proj.addCoordinateTransforms(currentProj, projection,
proj4Transform.forward, proj4Transform.inverse);
}
}
}
} else {
goog.asserts.assert(goog.isDef(projection));
projection = null;
}
} }
} else { } else {
projection = null; projection = null;