Merge pull request #304 from bbinet/projection-improvements

Some projection improvements
This commit is contained in:
Bruno Binet
2013-03-06 16:53:14 -08:00
7 changed files with 131 additions and 38 deletions
+6 -3
View File
@@ -13,9 +13,12 @@ goog.require('ol.source.SingleImageWMS');
goog.require('ol.source.TiledWMS'); goog.require('ol.source.TiledWMS');
var epsg21781 = new ol.Projection('EPSG:21781', ol.ProjectionUnits.METERS, var epsg21781 = new ol.Projection({
// Validity extent from http://spatialreference.org code: 'EPSG:21781',
new ol.Extent(485869.5728, 76443.1884, 837076.5648, 299941.7864)); units: ol.ProjectionUnits.METERS,
// Validity extent from http://spatialreference.org
extent: new ol.Extent(485869.5728, 76443.1884, 837076.5648, 299941.7864)
});
ol.projection.addProjection(epsg21781); ol.projection.addProjection(epsg21781);
var extent = new ol.Extent(420000, 30000, 900000, 350000); var extent = new ol.Extent(420000, 30000, 900000, 350000);
+12
View File
@@ -153,3 +153,15 @@
@exportObjectLiteral ol.tilegrid.XYZOptions @exportObjectLiteral ol.tilegrid.XYZOptions
@exportObjectLiteralProperty ol.tilegrid.XYZOptions.maxZoom number @exportObjectLiteralProperty ol.tilegrid.XYZOptions.maxZoom number
@exportObjectLiteral ol.ProjectionOptions
@exportObjectLiteralProperty ol.ProjectionOptions.code string
@exportObjectLiteralProperty ol.ProjectionOptions.units ol.ProjectionUnits
@exportObjectLiteralProperty ol.ProjectionOptions.extent ol.Extent
@exportObjectLiteralProperty ol.ProjectionOptions.axisOrientation string|undefined
@exportObjectLiteralProperty ol.ProjectionOptions.global boolean|undefined
@exportObjectLiteral ol.Proj4jsProjectionOptions
@exportObjectLiteralProperty ol.Proj4jsProjectionOptions.code string
@exportObjectLiteralProperty ol.Proj4jsProjectionOptions.extent ol.Extent
@exportObjectLiteralProperty ol.Proj4jsProjectionOptions.global boolean|undefined
+4 -1
View File
@@ -1,9 +1,11 @@
@exportSymbol ol.Projection @exportClass ol.Projection ol.ProjectionOptions
@exportProperty ol.Projection.prototype.getAxisOrientation @exportProperty ol.Projection.prototype.getAxisOrientation
@exportProperty ol.Projection.prototype.getCode @exportProperty ol.Projection.prototype.getCode
@exportProperty ol.Projection.prototype.getExtent @exportProperty ol.Projection.prototype.getExtent
@exportProperty ol.Projection.prototype.getPointResolution @exportProperty ol.Projection.prototype.getPointResolution
@exportProperty ol.Projection.prototype.getUnits @exportProperty ol.Projection.prototype.getUnits
@exportProperty ol.Projection.prototype.getMetersPerUnit
@exportProperty ol.Projection.prototype.isGlobal
@exportSymbol ol.ProjectionUnits @exportSymbol ol.ProjectionUnits
@exportProperty ol.ProjectionUnits.DEGREES @exportProperty ol.ProjectionUnits.DEGREES
@@ -16,3 +18,4 @@
@exportSymbol ol.projection.getTransformFromCodes @exportSymbol ol.projection.getTransformFromCodes
@exportSymbol ol.projection.transform @exportSymbol ol.projection.transform
@exportSymbol ol.projection.transformWithCodes @exportSymbol ol.projection.transformWithCodes
@exportSymbol ol.projection.configureProj4jsProjection
+65 -28
View File
@@ -46,37 +46,41 @@ ol.METERS_PER_UNIT[ol.ProjectionUnits.METERS] = 1;
/** /**
* @constructor * @constructor
* @param {string} code Code. * @param {ol.ProjectionOptions} options Options object.
* @param {ol.ProjectionUnits} units Units.
* @param {ol.Extent} extent Extent.
* @param {string=} opt_axisOrientation Axis orientation.
*/ */
ol.Projection = function(code, units, extent, opt_axisOrientation) { ol.Projection = function(options) {
/** /**
* @private * @private
* @type {string} * @type {string}
*/ */
this.code_ = code; this.code_ = options.code;
/** /**
* @private * @private
* @type {ol.ProjectionUnits} * @type {ol.ProjectionUnits}
*/ */
this.units_ = units; this.units_ = options.units;
/** /**
* @private * @private
* @type {ol.Extent} * @type {ol.Extent}
*/ */
this.extent_ = extent; this.extent_ = options.extent;
/** /**
* @private * @private
* @type {string} * @type {string}
*/ */
this.axisOrientation_ = goog.isDef(opt_axisOrientation) ? this.axisOrientation_ = goog.isDef(options.axisOrientation) ?
opt_axisOrientation : 'enu'; options.axisOrientation : 'enu';
/**
* @private
* @type {boolean}
*/
this.global_ = goog.isDef(options.global) ?
options.global : false;
/** /**
* @private * @private
@@ -103,14 +107,6 @@ ol.Projection.prototype.getExtent = function() {
}; };
/**
* @param {ol.Extent} extent Extent.
*/
ol.Projection.prototype.setExtent = function(extent) {
this.extent_ = extent;
};
/** /**
* @param {number} resolution Resolution. * @param {number} resolution Resolution.
* @param {ol.Coordinate} point Point. * @param {ol.Coordinate} point Point.
@@ -143,6 +139,14 @@ ol.Projection.prototype.getAxisOrientation = function() {
}; };
/**
* @return {boolean} Wether the projection is global.
*/
ol.Projection.prototype.isGlobal = function() {
return this.global_;
};
/** /**
* @return {ol.tilegrid.TileGrid} The default tile grid. * @return {ol.tilegrid.TileGrid} The default tile grid.
*/ */
@@ -163,15 +167,21 @@ ol.Projection.prototype.setDefaultTileGrid = function(tileGrid) {
/** /**
* @constructor * @constructor
* @extends {ol.Projection} * @extends {ol.Projection}
* @param {string} code Code.
* @param {Proj4js.Proj} proj4jsProj Proj4js projection. * @param {Proj4js.Proj} proj4jsProj Proj4js projection.
* @param {ol.Proj4jsProjectionOptions} options Projection config options.
* @private * @private
*/ */
ol.Proj4jsProjection_ = function(code, proj4jsProj) { ol.Proj4jsProjection_ = function(proj4jsProj, options) {
var units = /** @type {ol.ProjectionUnits} */ (proj4jsProj.units); var units = /** @type {ol.ProjectionUnits} */ (proj4jsProj.units);
goog.base(this, code, units, null, proj4jsProj.axis); var config = /** @type {ol.ProjectionOptions} */ ({
units: units,
axisOrientation: proj4jsProj.axis
});
goog.object.extend(config, options);
goog.base(this, config);
/** /**
* @private * @private
@@ -202,7 +212,10 @@ ol.Proj4jsProjection_.prototype.getPointResolution =
// average of the width and height. // average of the width and height.
if (goog.isNull(this.toEPSG4326_)) { if (goog.isNull(this.toEPSG4326_)) {
this.toEPSG4326_ = ol.projection.getTransform( this.toEPSG4326_ = ol.projection.getTransform(
this, ol.projection.getProj4jsProjectionFromCode_('EPSG:4326')); this, ol.projection.getProj4jsProjectionFromCode_({
code: 'EPSG:4326',
extent: null
}));
} }
var vertices = [ var vertices = [
point.x - resolution / 2, point.y, point.x - resolution / 2, point.y,
@@ -416,7 +429,10 @@ ol.projection.removeTransform = function(source, destination) {
ol.projection.getFromCode = function(code) { ol.projection.getFromCode = function(code) {
var projection = ol.projection.projections_[code]; var projection = ol.projection.projections_[code];
if (ol.HAVE_PROJ4JS && !goog.isDef(projection)) { if (ol.HAVE_PROJ4JS && !goog.isDef(projection)) {
projection = ol.projection.getProj4jsProjectionFromCode_(code); projection = ol.projection.getProj4jsProjectionFromCode_({
code: code,
extent: null
});
} }
if (!goog.isDef(projection)) { if (!goog.isDef(projection)) {
goog.asserts.assert(goog.isDef(projection)); goog.asserts.assert(goog.isDef(projection));
@@ -427,11 +443,12 @@ ol.projection.getFromCode = function(code) {
/** /**
* @param {string} code Code. * @param {ol.Proj4jsProjectionOptions} options Projection config options.
* @private * @private
* @return {ol.Proj4jsProjection_} Proj4js projection. * @return {ol.Proj4jsProjection_} Proj4js projection.
*/ */
ol.projection.getProj4jsProjectionFromCode_ = function(code) { ol.projection.getProj4jsProjectionFromCode_ = function(options) {
var code = options.code;
var proj4jsProjections = ol.projection.proj4jsProjections_; var proj4jsProjections = ol.projection.proj4jsProjections_;
var proj4jsProjection = proj4jsProjections[code]; var proj4jsProjection = proj4jsProjections[code];
if (!goog.isDef(proj4jsProjection)) { if (!goog.isDef(proj4jsProjection)) {
@@ -439,7 +456,10 @@ ol.projection.getProj4jsProjectionFromCode_ = function(code) {
var srsCode = proj4jsProj.srsCode; var srsCode = proj4jsProj.srsCode;
proj4jsProjection = proj4jsProjections[srsCode]; proj4jsProjection = proj4jsProjections[srsCode];
if (!goog.isDef(proj4jsProjection)) { if (!goog.isDef(proj4jsProjection)) {
proj4jsProjection = new ol.Proj4jsProjection_(srsCode, proj4jsProj); var config = /** @type {ol.Proj4jsProjectionOptions} */
(goog.object.clone(options));
config.code = srsCode;
proj4jsProjection = new ol.Proj4jsProjection_(proj4jsProj, config);
proj4jsProjections[srsCode] = proj4jsProjection; proj4jsProjections[srsCode] = proj4jsProjection;
} }
proj4jsProjections[code] = proj4jsProjection; proj4jsProjections[code] = proj4jsProjection;
@@ -492,7 +512,10 @@ ol.projection.getTransform = function(source, destination) {
proj4jsSource = source; proj4jsSource = source;
} else { } else {
proj4jsSource = proj4jsSource =
ol.projection.getProj4jsProjectionFromCode_(source.getCode()); ol.projection.getProj4jsProjectionFromCode_({
code: source.getCode(),
extent: null
});
} }
var sourceProj4jsProj = proj4jsSource.getProj4jsProj(); var sourceProj4jsProj = proj4jsSource.getProj4jsProj();
var proj4jsDestination; var proj4jsDestination;
@@ -500,7 +523,10 @@ ol.projection.getTransform = function(source, destination) {
proj4jsDestination = destination; proj4jsDestination = destination;
} else { } else {
proj4jsDestination = proj4jsDestination =
ol.projection.getProj4jsProjectionFromCode_(destination.getCode()); ol.projection.getProj4jsProjectionFromCode_({
code: destination.getCode(),
extent: null
});
} }
var destinationProj4jsProj = proj4jsDestination.getProj4jsProj(); var destinationProj4jsProj = proj4jsDestination.getProj4jsProj();
transform = transform =
@@ -629,3 +655,14 @@ ol.projection.transformWithCodes =
vertex = transformFn(vertex, vertex, 2); vertex = transformFn(vertex, vertex, 2);
return new ol.Coordinate(vertex[0], vertex[1]); return new ol.Coordinate(vertex[0], vertex[1]);
}; };
/**
* @param {ol.Proj4jsProjectionOptions} options Projection config options.
* @return {ol.Proj4jsProjection_} Proj4js projection.
*/
ol.projection.configureProj4jsProjection = function(options) {
goog.asserts.assert(!goog.object.containsKey(
ol.projection.proj4jsProjections_, options.code));
return ol.projection.getProj4jsProjectionFromCode_(options);
};
+6 -2
View File
@@ -15,8 +15,12 @@ goog.require('ol.projection');
* @param {string} code Code. * @param {string} code Code.
*/ */
ol.projection.EPSG3857 = function(code) { ol.projection.EPSG3857 = function(code) {
goog.base( goog.base(this, {
this, code, ol.ProjectionUnits.METERS, ol.projection.EPSG3857.EXTENT); code: code,
units: ol.ProjectionUnits.METERS,
extent: ol.projection.EPSG3857.EXTENT,
global: true
});
}; };
goog.inherits(ol.projection.EPSG3857, ol.Projection); goog.inherits(ol.projection.EPSG3857, ol.Projection);
+7 -2
View File
@@ -14,8 +14,13 @@ goog.require('ol.projection');
* @param {string=} opt_axisOrientation Axis orientation. * @param {string=} opt_axisOrientation Axis orientation.
*/ */
ol.projection.EPSG4326 = function(code, opt_axisOrientation) { ol.projection.EPSG4326 = function(code, opt_axisOrientation) {
goog.base(this, code, ol.ProjectionUnits.DEGREES, goog.base(this, {
ol.projection.EPSG4326.EXTENT, opt_axisOrientation); code: code,
units: ol.ProjectionUnits.DEGREES,
extent: ol.projection.EPSG4326.EXTENT,
axisOrientation: opt_axisOrientation,
global: true
});
}; };
goog.inherits(ol.projection.EPSG4326, ol.Projection); goog.inherits(ol.projection.EPSG4326, ol.Projection);
+31 -2
View File
@@ -305,8 +305,16 @@ describe('ol.projection', function() {
var units = ol.ProjectionUnits.DEGREES; var units = ol.ProjectionUnits.DEGREES;
it('removes functions cached by addTransform', function() { it('removes functions cached by addTransform', function() {
var foo = new ol.Projection('foo', units, extent); var foo = new ol.Projection({
var bar = new ol.Projection('bar', units, extent); code: 'foo',
units: units,
extent: extent
});
var bar = new ol.Projection({
code: 'bar',
units: units,
extent: extent
});
var transform = function(input, output, dimension) {return input}; var transform = function(input, output, dimension) {return input};
ol.projection.addTransform(foo, bar, transform); ol.projection.addTransform(foo, bar, transform);
expect(ol.projection.transforms_).not.toBeUndefined(); expect(ol.projection.transforms_).not.toBeUndefined();
@@ -329,6 +337,27 @@ describe('ol.projection', function() {
}); });
describe('ol.projection.configureProj4jsProjection()', function() {
beforeEach(function() {
ol.projection.proj4jsProjections_ = {};
});
it('returns a configured projection', function() {
var extent = new ol.Extent(
485869.5728, 76443.1884, 837076.5648, 299941.7864);
var epsg21781 = ol.projection.configureProj4jsProjection({
code: 'EPSG:21781',
extent: extent
});
expect(epsg21781.getCode()).toEqual('EPSG:21781');
expect(epsg21781.getExtent()).toBe(extent);
expect(epsg21781.getUnits()).toBe(ol.ProjectionUnits.METERS);
expect(epsg21781.isGlobal()).toBeFalsy();
});
});
}); });
goog.require('goog.array'); goog.require('goog.array');