Handle projections with different aliases: smarter Projection.equals and Layer.WMS.getFullRequestString methods. r=tschaub (closes #2914)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@10869 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -252,7 +252,10 @@ OpenLayers.Layer.WMS = OpenLayers.Class(OpenLayers.Layer.Grid, {
|
|||||||
* {String}
|
* {String}
|
||||||
*/
|
*/
|
||||||
getFullRequestString:function(newParams, altUrl) {
|
getFullRequestString:function(newParams, altUrl) {
|
||||||
var projectionCode = this.map.getProjection();
|
var mapProjection = this.map.getProjectionObject();
|
||||||
|
var projectionCode = this.projection.equals(mapProjection) ?
|
||||||
|
this.projection.getCode() :
|
||||||
|
mapProjection.getCode();
|
||||||
var value = (projectionCode == "none") ? null : projectionCode
|
var value = (projectionCode == "none") ? null : projectionCode
|
||||||
if (parseFloat(this.params.VERSION) >= 1.3) {
|
if (parseFloat(this.params.VERSION) >= 1.3) {
|
||||||
this.params.CRS = value;
|
this.params.CRS = value;
|
||||||
|
|||||||
@@ -27,6 +27,12 @@ OpenLayers.Projection = OpenLayers.Class({
|
|||||||
*/
|
*/
|
||||||
projCode: null,
|
projCode: null,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Property: titleRegEx
|
||||||
|
* {RegEx} regular expression to strip the title from a proj4js definition
|
||||||
|
*/
|
||||||
|
titleRegEx: /\+title=[^\+]*/,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor: OpenLayers.Projection
|
* Constructor: OpenLayers.Projection
|
||||||
* This class offers several methods for interacting with a wrapped
|
* This class offers several methods for interacting with a wrapped
|
||||||
@@ -92,11 +98,20 @@ OpenLayers.Projection = OpenLayers.Class({
|
|||||||
* {Boolean} The two projections are equivalent.
|
* {Boolean} The two projections are equivalent.
|
||||||
*/
|
*/
|
||||||
equals: function(projection) {
|
equals: function(projection) {
|
||||||
if (projection && projection.getCode) {
|
var p = projection, equals = false;
|
||||||
return this.getCode() == projection.getCode();
|
if (p) {
|
||||||
} else {
|
if (window.Proj4js && this.proj.defData && p.proj.defData) {
|
||||||
return false;
|
equals = this.proj.defData.replace(this.titleRegEx, "") ==
|
||||||
|
p.proj.defData.replace(this.titleRegEx, "");
|
||||||
|
} else if (p.getCode) {
|
||||||
|
var source = this.getCode(), target = p.getCode();
|
||||||
|
equals = source == target ||
|
||||||
|
!!OpenLayers.Projection.transforms[source] &&
|
||||||
|
OpenLayers.Projection.transforms[source][target] ===
|
||||||
|
OpenLayers.Projection.nullTransform;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return equals;
|
||||||
},
|
},
|
||||||
|
|
||||||
/* Method: destroy
|
/* Method: destroy
|
||||||
@@ -176,3 +191,23 @@ OpenLayers.Projection.transform = function(point, source, dest) {
|
|||||||
}
|
}
|
||||||
return point;
|
return point;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIFunction: nullTransform
|
||||||
|
* A null transformation - useful for defining projection aliases when
|
||||||
|
* proj4js is not available:
|
||||||
|
*
|
||||||
|
* (code)
|
||||||
|
* OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:3857",
|
||||||
|
* OpenLayers.Layer.SphericalMercator.projectForward);
|
||||||
|
* OpenLayers.Projection.addTransform("EPSG:3857", "EPSG:3857",
|
||||||
|
* OpenLayers.Layer.SphericalMercator.projectInverse);
|
||||||
|
* OpenLayers.Projection.addTransform("EPSG:3857", "EPSG:900913",
|
||||||
|
* OpenLayers.Projection.nullTransform);
|
||||||
|
* OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:3857",
|
||||||
|
* OpenLayers.Projection.nullTransform);
|
||||||
|
* (end)
|
||||||
|
*/
|
||||||
|
OpenLayers.Projection.nullTransform = function(point) {
|
||||||
|
return point;
|
||||||
|
};
|
||||||
|
|||||||
+18
-4
@@ -230,15 +230,15 @@
|
|||||||
function test_Layer_WMS_getFullRequestString (t) {
|
function test_Layer_WMS_getFullRequestString (t) {
|
||||||
|
|
||||||
|
|
||||||
t.plan( 2 );
|
t.plan( 3 );
|
||||||
var map = new OpenLayers.Map('map');
|
var map = new OpenLayers.Map('map');
|
||||||
map.projection = "xx";
|
map.projection = "xx";
|
||||||
tUrl = "http://octo.metacarta.com/cgi-bin/mapserv";
|
var tUrl = "http://octo.metacarta.com/cgi-bin/mapserv";
|
||||||
tParams = { layers: 'basic',
|
var tParams = { layers: 'basic',
|
||||||
format: 'image/png'};
|
format: 'image/png'};
|
||||||
var tLayer = new OpenLayers.Layer.WMS(name, tUrl, tParams);
|
var tLayer = new OpenLayers.Layer.WMS(name, tUrl, tParams);
|
||||||
map.addLayer(tLayer);
|
map.addLayer(tLayer);
|
||||||
str = tLayer.getFullRequestString();
|
var str = tLayer.getFullRequestString();
|
||||||
var tParams = {
|
var tParams = {
|
||||||
LAYERS: "basic", FORMAT: "image/png", SERVICE: "WMS",
|
LAYERS: "basic", FORMAT: "image/png", SERVICE: "WMS",
|
||||||
VERSION: "1.1.1", REQUEST: "GetMap", STYLES: "",
|
VERSION: "1.1.1", REQUEST: "GetMap", STYLES: "",
|
||||||
@@ -258,6 +258,20 @@
|
|||||||
"getFullRequestString() by default does *not* add SRS value if projection is 'none'");
|
"getFullRequestString() by default does *not* add SRS value if projection is 'none'");
|
||||||
map.destroy();
|
map.destroy();
|
||||||
|
|
||||||
|
map = new OpenLayers.Map("map", {projection: "EPSG:4326"});
|
||||||
|
var layerProj = new OpenLayers.Projection("FOO", {
|
||||||
|
equals: function() {return true},
|
||||||
|
getCode: function() {return "FOO"}
|
||||||
|
});
|
||||||
|
tLayer = new OpenLayers.Layer.WMS(name, tUrl, tParams, {projection: layerProj});
|
||||||
|
map.addLayer(tLayer);
|
||||||
|
str = tLayer.getFullRequestString();
|
||||||
|
tParams.SRS = "FOO";
|
||||||
|
t.eq(str,
|
||||||
|
tUrl + "?" + OpenLayers.Util.getParameterString(tParams),
|
||||||
|
"getFullRequestString() uses the layer projection if it equals the map projection");
|
||||||
|
map.destroy();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_Layer_WMS_setOpacity (t) {
|
function test_Layer_WMS_setOpacity (t) {
|
||||||
|
|||||||
@@ -23,6 +23,46 @@
|
|||||||
t.eq(projection.equals({}), false, "equals on null projection object returns false (doesn't call getCode)");
|
t.eq(projection.equals({}), false, "equals on null projection object returns false (doesn't call getCode)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_Projection_equals(t) {
|
||||||
|
t.plan(8);
|
||||||
|
var origTransforms = OpenLayers.Util.extend({}, OpenLayers.Projection.transforms);
|
||||||
|
OpenLayers.Projection.addTransform("EPSG:4326", "FOO", OpenLayers.Projection.nullTransform);
|
||||||
|
OpenLayers.Projection.addTransform("FOO", "EPSG:4326", OpenLayers.Projection.nullTransform);
|
||||||
|
var projection = new OpenLayers.Projection("FOO");
|
||||||
|
t.eq(projection.equals(new OpenLayers.Projection("EPSG:4326")), true, "EPSG:4326 and FOO are equal without proj4js");
|
||||||
|
t.eq(projection.equals(new OpenLayers.Projection("EPSG:900913")), false, "EPSG:900913 and FOO are not equal without proj4js");
|
||||||
|
t.eq(new OpenLayers.Projection("EPSG:4326").equals(new OpenLayers.Projection("EPSG:4326")), true, "EPSG:4326 and EPSG:4326 are equal without proj4js");
|
||||||
|
t.eq(new OpenLayers.Projection("BAR").equals(new OpenLayers.Projection("EPSG:4326")), false, "Projection.equals() returns false for unknown projections withoug proj4js");
|
||||||
|
OpenLayers.Projection.transforms = origTransforms;
|
||||||
|
|
||||||
|
var proj1 = new OpenLayers.Projection("EPSG:4326");
|
||||||
|
var proj2 = new OpenLayers.Projection("FOO");
|
||||||
|
var proj3 = new OpenLayers.Projection("EPSG:900913");
|
||||||
|
var proj4 = new OpenLayers.Projection("EPSG:4326");
|
||||||
|
var proj5 = new OpenLayers.Projection("BAR");
|
||||||
|
|
||||||
|
// conditionally mock up proj4js
|
||||||
|
var hasProj = !!window.Proj4js;
|
||||||
|
if (!hasProj) {
|
||||||
|
window.Proj4js = true;
|
||||||
|
}
|
||||||
|
proj1.proj = {defData: "+title= WGS84 +foo=bar +x=0"};
|
||||||
|
proj2.proj = {defData: "+title=FOO +foo=bar +x=0", srsCode: "FOO"};
|
||||||
|
proj3.proj = {defData: "+title=Web Mercator +foo=bar +x=0 +I=am-different"};
|
||||||
|
proj4.proj = proj1.proj;
|
||||||
|
proj5.proj = {srsCode: "BAR"};
|
||||||
|
|
||||||
|
t.eq(proj2.equals(proj1), true, "EPSG:4326 and FOO are equal with proj4js");
|
||||||
|
t.eq(proj2.equals(proj3), false, "EPSG:900913 and FOO are not equal with proj4js");
|
||||||
|
t.eq(proj1.equals(proj4), true, "EPSG:4326 and EPSG:4326 are equal with proj4js");
|
||||||
|
t.eq(proj2.equals(proj5), false, "Projection.equals() returns false for unknown projections with proj4js");
|
||||||
|
|
||||||
|
if (!hasProj) {
|
||||||
|
delete window.Proj4js
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user