[OpenLayers-Dev] Alternating base layers

Ringdahl Niklas Niklas.Ringdahl at sweco.se
Tue Jun 9 11:15:06 EDT 2009


Christopher and Trond, thanks for quick responses.

Trond: your code looks very promising, I will try it out ASAP! 

- Niklas




-----Ursprungligt meddelande-----
Från: Trond Michelsen [mailto:trondmm-openlayers at crusaders.no] 
Skickat: den 9 juni 2009 16:51
Till: Ringdahl Niklas; OpenLayers developers list
Ämne: Re: [OpenLayers-Dev] Alternating base layers

On Tue, Jun 09, 2009 at 04:29:21PM +0200, Ringdahl Niklas wrote:
> I have two different raster layers with different coordinate systems; 
> one is a Google Satellite layer and the other is a tiled layer in 
> epsg:2400. Depending on scale, I would like to select either as the 
> base layer and keep the WMS and WFS overlays independent of which base 
> layer is used.
> 
> The way I envision this is to add them both to the map and use the 
> map.setBaseLayer method to alternate. Is this approach complete, or 
> are there other considerations to make, keeping the different 
> reference systems in mind? What kind of trouble will I get myself into with this?
> 
> I would be happy for any pointer in the right direction on this. Thanks!

I've done something similar to what you want. I've just implemented it as three buttons you can click to switch projection, but I don't think it should be too hard to trigger it by a layer change. 

As Christopher said, vector layers also needs to be reprojected, and I've made no effort to do this in my solution. Adding a
setProjection() function to the Vector Layer class, that does The Right Thing (tm), should do the trick, though.

Here is all the relevant code (I think...) needed:

OpenLayers.Map.prototype.setCenterProj = function (center, zoom, projection, params) {
    this.setProjection(projection, params);
    this.setCenter(center, zoom, true, true); }

OpenLayers.Map.prototype.setProjection = function (projection, params) {
    this.projection = projection;

    for (var key in params) {
        this[key] = params[key];
    }
    for (var i=0; i< this.layers.length; i++) {
        this.layers[i].setProjection(projection, params);
    }
};

OpenLayers.Layer.WMS.prototype.setProjection = function (projection, params) {
    this.projection = projection;
    for (var key in params) {
        this[key] = params[key];
    }
    this.initResolutions();
};

OpenLayers.Layer.WMS.Untiled.prototype.setProjection = OpenLayers.Layer.WMS.prototype.setProjection;

GLOBAL.projections = {
  mercator: {projparams: {maxExtent: new OpenLayers.Bounds(-20014.0868, -20014.0868, 20016.0868, 20016.0868),
                          maxResolution: 50.037717,
                          units: "km"},
             projcode: "EPSG:41000",
             center: new OpenLayers.LonLat(0,0),
             zoom: 0},

  npole:  {projparams: {maxExtent: new OpenLayers.Bounds(-11888450, -11888450, 11888450, 11888450),
                        maxResolution: 29721.125/2,
                        units: "m"},
           projcode: "EPSG:42000",
           center: new OpenLayers.LonLat(0,0),
           zoom: 1},

  spole:  {projparams: {maxExtent: new OpenLayers.Bounds(-11888450, -11888450, 11888450, 11888450),
                        maxResolution: 29721.125/2,
                        units: "m"},
           projcode: "EPSG:43000",
           center: new OpenLayers.LonLat(0,0),
           zoom: 1}
};

function reproject (projname) {
    var map    = GLOBAL.map;
    var params = GLOBAL.projections[ projname ];
    var proj   = new OpenLayers.Projection(params.projcode);
    map.setCenterProj(params.center, params.zoom, proj, params.projparams); }

and then in the webpage, there are three buttons like this:

<button id="bt_npol"  name="npole"    value="npole"    onClick="reproject(&quot;npole&quot;)">North Pole</button>
<button id="bt_merc"  name="mercator" value="Mercator" onClick="reproject(&quot;mercator&quot;)">Global</button>
<button id="bt_spol"  name="spole"    value="spole"    onClick="reproject(&quot;spole&quot;)">South Pole</button>

--
Trond Michelsen



More information about the Dev mailing list