Have Layer.WMS support WMS version 1.3 with the axis order sequence, r=elemoine,crschmidt,ahocevar (closes #2284)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@9775 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
bartvde
2009-11-03 14:01:51 +00:00
parent 1e87b26029
commit e003972959
4 changed files with 232 additions and 61 deletions

52
examples/wms-v13.html Normal file
View File

@@ -0,0 +1,52 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="../lib/OpenLayers.js"></script>
<script type="text/javascript">
var map, map2;
function init(){
// clear array to simulate a wrong axis order request
map = new OpenLayers.Map( 'map' );
var layer = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://demo.cubewerx.com/demo/cubeserv/cubeserv.cgi?",
{layers: 'Foundation.GTOPO30', version: '1.3.0'},
{singleTile: true, yx: []}
);
map.addLayer(layer);
map.zoomToMaxExtent();
map2 = new OpenLayers.Map( 'map2' );
var layer2 = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://demo.cubewerx.com/demo/cubeserv/cubeserv.cgi?",
{layers: 'Foundation.GTOPO30', version: '1.3.0'},
{singleTile: true}
);
map2.addLayer(layer2);
map2.zoomToMaxExtent();
}
</script>
</head>
<body onload="init()">
<h1 id="title"> WMS version 1.3 (axis order) Example</h1>
<div id="tags">
</div>
<p id="shortdesc">
Shows an example of the influence of axis order on WMS 1.3 GetMap requests.
</p>
<div id="map" class="smallmap"></div>
<div id="map2" class="smallmap"></div>
<div id="docs">
WMS version 1.3 introduced the axis order sequence, so that for e.g. EPSG:4326 the bbox coordinate
values need to be flipped (LatLon instead of LonLat). The first map uses the incorrect (WMS 1.1) axis
order against a WMS 1.3 service, resulting in corrupted maps. The second map shows how to correctly
request a map in EPSG:4326 against a WMS 1.3 service.
</div>
</body>
</html>

View File

@@ -128,11 +128,18 @@ OpenLayers.Bounds = OpenLayers.Class({
/**
* APIMethod: toArray
*
* Parameters:
* reverseAxisOrder - {Boolean} Should we reverse the axis order?
*
* Returns:
* {Array} array of left, bottom, right, top
*/
toArray: function() {
return [this.left, this.bottom, this.right, this.top];
toArray: function(reverseAxisOrder) {
if (reverseAxisOrder === true) {
return [this.bottom, this.left, this.top, this.right];
} else {
return [this.left, this.bottom, this.right, this.top];
}
},
/**
@@ -141,22 +148,26 @@ OpenLayers.Bounds = OpenLayers.Class({
* Parameters:
* decimal - {Integer} How many significant digits in the bbox coords?
* Default is 6
* reverseAxisOrder - {Boolean} Should we reverse the axis order?
*
* Returns:
* {String} Simple String representation of bounds object.
* (ex. <i>"5,42,10,45"</i>)
*/
toBBOX:function(decimal) {
toBBOX:function(decimal, reverseAxisOrder) {
if (decimal== null) {
decimal = 6;
}
var mult = Math.pow(10, decimal);
var bbox = Math.round(this.left * mult) / mult + "," +
Math.round(this.bottom * mult) / mult + "," +
Math.round(this.right * mult) / mult + "," +
Math.round(this.top * mult) / mult;
return bbox;
var xmin = Math.round(this.left * mult) / mult;
var ymin = Math.round(this.bottom * mult) / mult;
var xmax = Math.round(this.right * mult) / mult;
var ymax = Math.round(this.top * mult) / mult;
if (reverseAxisOrder === true) {
return ymin + "," + xmin + "," + ymax + "," + xmax;
} else {
return xmin + "," + ymin + "," + xmax + "," + ymax;
}
},
/**

View File

@@ -62,7 +62,15 @@ OpenLayers.Layer.WMS = OpenLayers.Class(OpenLayers.Layer.Grid, {
* TRANSPARENT=TRUE. Also isBaseLayer will not changed by the
* constructor. Default false.
*/
noMagic: false,
noMagic: false,
/**
* Property: yx
* {Array} Array of strings with the EPSG codes for which the axis order
* is to be reversed (yx instead of xy, LatLon instead of LonLat). This
* is only relevant for WMS versions >= 1.3.0.
*/
yx: ['EPSG:4326'],
/**
* Constructor: OpenLayers.Layer.WMS
@@ -164,12 +172,21 @@ OpenLayers.Layer.WMS = OpenLayers.Class(OpenLayers.Layer.Grid, {
getURL: function (bounds) {
bounds = this.adjustBounds(bounds);
var imageSize = this.getImageSize();
var newParams = {
'BBOX': this.encodeBBOX ? bounds.toBBOX() : bounds.toArray(),
'WIDTH': imageSize.w,
'HEIGHT': imageSize.h
};
var imageSize = this.getImageSize();
var newParams = {};
// WMS 1.3 introduced axis order
if (parseFloat(this.params.VERSION) >= 1.3 &&
OpenLayers.Util.indexOf(this.yx,
this.map.getProjectionObject().getCode()) !== -1) {
newParams.BBOX = this.encodeBBOX ? bounds.toBBOX(null, true) :
bounds.toArray(true);
} else {
newParams.BBOX = this.encodeBBOX ? bounds.toBBOX() :
bounds.toArray();
}
newParams.WIDTH = imageSize.w;
newParams.HEIGHT = imageSize.h;
var requestString = this.getFullRequestString(newParams);
return requestString;
},
@@ -225,7 +242,12 @@ OpenLayers.Layer.WMS = OpenLayers.Class(OpenLayers.Layer.Grid, {
*/
getFullRequestString:function(newParams, altUrl) {
var projectionCode = this.map.getProjection();
this.params.SRS = (projectionCode == "none") ? null : projectionCode;
var value = (projectionCode == "none") ? null : projectionCode
if (parseFloat(this.params.VERSION) >= 1.3) {
this.params.CRS = value;
} else {
this.params.SRS = value;
}
return OpenLayers.Layer.Grid.prototype.getFullRequestString.apply(
this, arguments);

View File

@@ -6,20 +6,20 @@
<script src="../../lib/OpenLayers.js"></script>
<script type="text/javascript">
var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
var layer;
var layer;
var name = 'Test Layer';
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
var params = { map: '/mapdata/vmap_wms.map',
layers: 'basic',
var params = { map: '/mapdata/vmap_wms.map',
layers: 'basic',
format: 'image/jpeg'};
function test_Layer_WMS_constructor (t) {
t.plan( 15 );
var trans_format = "image/png";
if (OpenLayers.Util.alphaHack()) { trans_format = "image/gif"; }
if (OpenLayers.Util.alphaHack()) { trans_format = "image/gif"; }
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.WMS(name, url, params);
t.ok( layer instanceof OpenLayers.Layer.WMS, "new OpenLayers.Layer.WMS returns object" );
@@ -64,10 +64,10 @@
params.TRANSPARENT = false;
}
function test_Layer_WMS_addtile (t) {
t.plan( 6 );
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.WMS(name, url, params);
var map = new OpenLayers.Map('map');
@@ -103,10 +103,10 @@
t.eq( tile.position.toString(), "x=5,y=6", "Position of tile is set correctly." );
map.destroy();
}
function test_Layer_WMS_bboxEncoding (t) {
t.plan( 6 );
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.WMS(name, url, params, {encodeBBOX:true});
var map = new OpenLayers.Map('map');
@@ -142,7 +142,7 @@
t.eq( tile.position.toString(), "x=5,y=6", "Position of tile is set correctly." );
map.destroy();
}
function test_Layer_WMS_inittiles (t) {
t.plan( 2 );
var map = new OpenLayers.Map('map');
@@ -157,14 +157,14 @@
function test_Layer_WMS_clone (t) {
t.plan(4);
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
var options = {tileSize: new OpenLayers.Size(500,50)};
var map = new OpenLayers.Map('map', options);
layer = new OpenLayers.Layer.WMS(name, url, params);
map.addLayer(layer);
layer.grid = [ [6, 7],
layer.grid = [ [6, 7],
[8, 9]];
var clone = layer.clone();
@@ -185,7 +185,7 @@
function test_Layer_WMS_isBaseLayer(t) {
t.plan(3);
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.WMS(name, url, params);
t.ok( layer.isBaseLayer, "baselayer is true by default");
@@ -205,8 +205,8 @@
var map = new OpenLayers.Map("map");
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.WMS(name, url, params);
var newParams = { layers: 'sooper',
var newParams = { layers: 'sooper',
chickpeas: 'image/png'};
map.addLayer(layer);
@@ -217,10 +217,10 @@
}
layer.mergeNewParams(newParams);
t.eq( layer.params.LAYERS, "sooper", "mergeNewParams() overwrites well");
t.eq( layer.params.CHICKPEAS, "image/png", "mergeNewParams() adds well");
newParams.CHICKPEAS = 151;
t.eq( layer.params.CHICKPEAS, "image/png", "mergeNewParams() makes clean copy of hashtable");
@@ -229,12 +229,12 @@
function test_Layer_WMS_getFullRequestString (t) {
t.plan( 2 );
var map = new OpenLayers.Map('map');
map.projection = "xx";
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
tParams = { layers: 'basic',
format: 'image/png'};
var tLayer = new OpenLayers.Layer.WMS(name, tUrl, tParams);
map.addLayer(tLayer);
@@ -247,7 +247,7 @@
t.eq(str,
tUrl + "?" + OpenLayers.Util.getParameterString(tParams),
"getFullRequestString() adds SRS value");
map.removeLayer(tLayer);
tLayer.projection = "none";
map.addLayer(tLayer);
@@ -257,7 +257,7 @@
tUrl + "?" + OpenLayers.Util.getParameterString(tParams),
"getFullRequestString() by default does *not* add SRS value if projection is 'none'");
map.destroy();
}
function test_Layer_WMS_setOpacity (t) {
@@ -266,9 +266,9 @@
var map = new OpenLayers.Map('map');
map.projection = "xx";
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv";
tParams = { layers: 'basic',
tParams = { layers: 'basic',
format: 'image/png'};
tOptions = { 'opacity': '0.5' };
tOptions = { 'opacity': '0.5' };
var tLayer = new OpenLayers.Layer.WMS(name, tUrl, tParams, tOptions);
map.addLayer(tLayer);
map.zoomToMaxExtent();
@@ -283,8 +283,8 @@
t.eq(parseFloat(tile.imgDiv.style.opacity), 0.6, "Tile opacity is set correctly");
map.destroy();
}
}
function test_Layer_WMS_Reproject (t) {
var validkey = (window.location.protocol == "file:") ||
(window.location.host == "localhost") ||
@@ -304,17 +304,17 @@
map.addLayer(wmslayer);
map.setCenter(new OpenLayers.LonLat(0,0), 5);
var tile = wmslayer.grid[0][0];
t.eq( tile.bounds.left, -22.5, "left side matches" );
t.eq( tile.bounds.right, -11.25, "right side matches" );
t.eq( tile.bounds.bottom.toFixed(6), '11.178402', "bottom side matches" );
t.eq( tile.bounds.top.toFixed(6), '21.943046', "top side matches" );
t.eq( tile.bounds.left, -22.5, "left side matches" );
t.eq( tile.bounds.right, -11.25, "right side matches" );
t.eq( tile.bounds.bottom.toFixed(6), '11.178402', "bottom side matches" );
t.eq( tile.bounds.top.toFixed(6), '21.943046', "top side matches" );
map.destroy();
} else {
t.plan(1);
t.debug_print("can't test google layer from " +
window.location.host);
}
var map = new OpenLayers.Map('map');
layer = new OpenLayers.Layer.WMS(name, url, params);
map.addLayer(layer);
@@ -324,8 +324,8 @@
map.destroy();
}
function test_Layer_WMS_noGutters (t) {
function test_Layer_WMS_noGutters (t) {
t.plan(2);
var map = new OpenLayers.Map('map');
var layer = new OpenLayers.Layer.WMS("no gutter layer", url, params, {gutter: 0});
@@ -340,12 +340,12 @@
t.eq(parseInt(args['HEIGHT']),
tile.size.h,
"layer without gutter requests images that are as tall as the tile");
layer.destroy();
map.destroy();
}
function test_Layer_WMS_gutters (t) {
function test_Layer_WMS_gutters (t) {
t.plan(2);
var gutter = 15;
var map = new OpenLayers.Map('map');
@@ -368,16 +368,16 @@
}
// DEPRECATED -- REMOVE IN 3.0
function test_Layer_Untiled_WMS(t) {
function test_Layer_Untiled_WMS(t) {
t.plan(1);
var layer = new OpenLayers.Layer.WMS.Untiled();
var clone = layer.clone();
t.ok(clone.singleTile, "regression test: clone works. this is for #1013");
}
function test_Layer_WMS_destroy (t) {
t.plan( 1 );
@@ -389,15 +389,101 @@
map.setCenter(new OpenLayers.LonLat(0,0), 5);
//grab a reference to one of the tiles
var tile = layer.grid[0][0];
var tile = layer.grid[0][0];
layer.destroy();
// checks to make sure superclass (grid) destroy() was called
// checks to make sure superclass (grid) destroy() was called
t.ok( layer.grid == null, "grid set to null");
}
function test_Layer_WMS_v13(t) {
t.plan(5);
var lon = 5;
var lat = 40;
var zoom = 5;
var map = new OpenLayers.Map( 'map' );
var layer = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://myserver.org/wms?",
{layers: 'mylayer', version: '1.3.0'},
{singleTile: true}
);
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
var url = layer.getURL(map.getExtent());
var params = url.split("&");
var bbox;
for (var i=0, len=params.length; i<len; i++) {
var param = params[i];
var a = param.split('=');
if (a[0] === 'BBOX') {
bbox = a[1];
break;
}
}
t.eq(layer.params.CRS, "EPSG:4326", "In WMS 1.3 SRS is now CRS");
t.eq(bbox, "27.9150390625,-5.986328125,52.0849609375,15.986328125", "Axis sequence is lat lon for EPSG:4326 in WMS 1.3.0");
var layer2 = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://myserver.org/wms?",
{layers: 'mylayer', version: '1.1.1'},
{singleTile: true}
);
map.addLayer(layer2);
var url = layer2.getURL(map.getExtent());
var params = url.split("&");
var bbox;
for (var i=0, len=params.length; i<len; i++) {
var param = params[i];
var a = param.split('=');
if (a[0] === 'BBOX') {
bbox = a[1];
break;
}
}
t.eq(layer2.params.SRS, "EPSG:4326", "In WMS 1.1.1 parameter is called SRS");
t.eq(bbox, "-5.986328125,27.9150390625,15.986328125,52.0849609375", "Axis sequence is lon lat for EPSG:4326 in WMS 1.1.1");
map.destroy();
// CRS:84 has normal axis sequence (lon lat)
var map = new OpenLayers.Map( 'map', {projection: 'CRS:84'} );
var layer = new OpenLayers.Layer.WMS(
"OpenLayers WMS",
"http://myserver.org/wms?",
{layers: 'mylayer', version: '1.3.0'},
{singleTile: true}
);
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
var url = layer.getURL(map.getExtent());
var params = url.split("&");
var bbox;
for (var i=0, len=params.length; i<len; i++) {
var param = params[i];
var a = param.split('=');
if (a[0] === 'BBOX') {
bbox = a[1];
break;
}
}
t.eq(bbox, "-5.986328125,27.9150390625,15.986328125,52.0849609375", "Axis sequence for CRS:84 is lon lat");
map.destroy();
}
</script>
</head>