Do not require an extent for global projections

This commit is contained in:
Andreas Hocevar
2015-04-21 08:47:36 +02:00
parent 4f8dca92ba
commit 40feabc3c8
5 changed files with 61 additions and 7 deletions

View File

@@ -120,9 +120,13 @@ ol.proj.Projection = function(options) {
* @type {boolean} * @type {boolean}
*/ */
this.global_ = goog.isDef(options.global) ? options.global : false; this.global_ = goog.isDef(options.global) ? options.global : false;
if (this.global_) {
goog.asserts.assert(!goog.isNull(this.extent_));
} /**
* @private
* @type {boolean}
*/
this.canWrapX_ = this.global_ && !goog.isNull(this.extent_);
/** /**
* @private * @private
@@ -175,6 +179,14 @@ ol.proj.Projection = function(options) {
}; };
/**
* @return {boolean} The projection is suitable for wrapping the x-axis
*/
ol.proj.Projection.prototype.canWrapX = function() {
return this.canWrapX_;
};
/** /**
* Get the code for this projection, e.g. 'EPSG:4326'. * Get the code for this projection, e.g. 'EPSG:4326'.
* @return {string} Code. * @return {string} Code.
@@ -258,6 +270,7 @@ ol.proj.Projection.prototype.isGlobal = function() {
*/ */
ol.proj.Projection.prototype.setGlobal = function(global) { ol.proj.Projection.prototype.setGlobal = function(global) {
this.global_ = global; this.global_ = global;
this.canWrapX_ = global && !goog.isNull(this.extent_);
}; };
@@ -284,6 +297,7 @@ ol.proj.Projection.prototype.setDefaultTileGrid = function(tileGrid) {
*/ */
ol.proj.Projection.prototype.setExtent = function(extent) { ol.proj.Projection.prototype.setExtent = function(extent) {
this.extent_ = extent; this.extent_ = extent;
this.canWrapX_ = this.global_ && !goog.isNull(extent);
}; };

View File

@@ -108,7 +108,7 @@ ol.renderer.canvas.Map.prototype.dispatchComposeEvent_ =
var projectionExtent = projection.getExtent(); var projectionExtent = projection.getExtent();
var resolution = viewState.resolution; var resolution = viewState.resolution;
var rotation = viewState.rotation; var rotation = viewState.rotation;
var repeatReplay = (wrapX && projection.isGlobal() && var repeatReplay = (wrapX && projection.canWrapX() &&
!ol.extent.containsExtent(projectionExtent, extent)); !ol.extent.containsExtent(projectionExtent, extent));
var skippedFeaturesHash = {}; var skippedFeaturesHash = {};

View File

@@ -110,7 +110,7 @@ ol.renderer.canvas.VectorLayer.prototype.composeFrame =
replayGroup.replay( replayGroup.replay(
replayContext, pixelRatio, transform, rotation, skippedFeatureUids); replayContext, pixelRatio, transform, rotation, skippedFeatureUids);
if (vectorSource.getWrapX() && projection.isGlobal() && if (vectorSource.getWrapX() && projection.canWrapX() &&
!ol.extent.containsExtent(projectionExtent, frameState.extent)) { !ol.extent.containsExtent(projectionExtent, frameState.extent)) {
var startX = extent[0]; var startX = extent[0];
var worldWidth = ol.extent.getWidth(projectionExtent); var worldWidth = ol.extent.getWidth(projectionExtent);
@@ -229,7 +229,7 @@ ol.renderer.canvas.VectorLayer.prototype.prepareFrame =
vectorLayerRenderBuffer * resolution); vectorLayerRenderBuffer * resolution);
var projectionExtent = viewState.projection.getExtent(); var projectionExtent = viewState.projection.getExtent();
if (vectorSource.getWrapX() && viewState.projection.isGlobal() && if (vectorSource.getWrapX() && viewState.projection.canWrapX() &&
!ol.extent.containsExtent(projectionExtent, frameState.extent)) { !ol.extent.containsExtent(projectionExtent, frameState.extent)) {
// do not clip when the view crosses the -180° or 180° meridians // do not clip when the view crosses the -180° or 180° meridians
extent[0] = projectionExtent[0]; extent[0] = projectionExtent[0];

View File

@@ -157,7 +157,7 @@ ol.renderer.Map.prototype.forEachFeatureAtCoordinate =
var projection = viewState.projection; var projection = viewState.projection;
var translatedX; var translatedX;
if (projection.isGlobal()) { if (projection.canWrapX()) {
var projectionExtent = projection.getExtent(); var projectionExtent = projection.getExtent();
var worldWidth = ol.extent.getWidth(projectionExtent); var worldWidth = ol.extent.getWidth(projectionExtent);
var x = coordinate[0]; var x = coordinate[0];

View File

@@ -138,6 +138,46 @@ describe('ol.proj', function() {
}); });
}); });
describe('canWrapX()', function() {
it('requires an extent for allowing wrapX', function() {
var proj = new ol.proj.Projection({
code: 'foo',
global: true
});
expect(proj.canWrapX()).to.be(false);
proj.setExtent([1, 2, 3, 4]);
expect(proj.canWrapX()).to.be(true);
proj = new ol.proj.Projection({
code: 'foo',
global: true,
extent: [1, 2, 3, 4]
});
expect(proj.canWrapX()).to.be(true);
proj.setExtent(null);
expect(proj.canWrapX()).to.be(false);
});
it('requires global to be true for allowing wrapX', function() {
var proj = new ol.proj.Projection({
code: 'foo',
extent: [1, 2, 3, 4]
});
expect(proj.canWrapX()).to.be(false);
proj.setGlobal(true);
expect(proj.canWrapX()).to.be(true);
proj = new ol.proj.Projection({
code: 'foo',
global: true,
extent: [1, 2, 3, 4]
});
expect(proj.canWrapX()).to.be(true);
proj.setGlobal(false);
expect(proj.canWrapX()).to.be(false);
});
});
describe('transformExtent()', function() { describe('transformExtent()', function() {
it('transforms an extent given projection identifiers', function() { it('transforms an extent given projection identifiers', function() {