[Users] Programmatically changing the base map projection

Matt Priour mpriour at kestrelcomputer.com
Mon Jun 20 21:29:27 CEST 2011


I've had similar problems and I've solved it by adding an event listener to the map's baselayerchange event.
you would put mapOptionsEtrs properties on the Etrs base layer rather than on the map. Map options are most ignored and overwritten by base layer options.

here is my baselayerchange event handler, which is called with the map as scope.

function onBaseLayerChange(evtObj){
   var mapProj, baseProj, map, newBase, reproject;
   map = this;
   newBase = evtObj.layer;
   mapProj = (map.projection && map.projection instanceof OpenLayers.Projection) ? map.projection : new OpenLayers.Projection(map.projection);
   baseProj = newBase.projection;
   reproject = !(baseProj.equals(mapProj));
   if (reproject) {
      var center, maxExt;
      //calc proper reporojected center
      center = map.getCenter().transform(mapProj, baseProj);
      //calc correct reprojected extents
      maxExt = newBase.maxExtent;
      //set map projection, extent, & center of map to proper values
      map.projection = baseProj;
      map.maxExtent = maxExt;
      map.setCenter(center);
   }
}

Matt Priour
Kestrel Computer Consulting


From: Hugo 
Sent: Monday, June 20, 2011 12:45 PM
To: users at geoext.org 
Subject: [Users] Programmatically changing the base map projection


Dear Geoext users,

I'm trying to develop a functionality that will allow the user to change the base map projection. The initial map is using the spherical mercator projection so that OSM and Google/Yahoo/Bing layers can be displayed. Because the users will need to edit features at specific scales (1:25.000; 1:10.000) i need to develop a function that will change the projection from spherical mercator to ETRS PTM06 (EPSG:3763). I think i'm almost there but somehow, after reprojection the base map i get my map full extent much higher in latitude (so, more to the north) and much smaller in longitude (more to the west).

My steps are:
1. Remove all OSM/Google/Yahoo/Bing layers from the map.
2. Add new options to the map
3. Merge new options (projection) to a blank layer and set it as basellayer
4. Loop the remaining layers and add new options.

The relevant part of the code is below:

//Map options
var mapOptions = {
    maxExtent: bBox,
    maxResolution: 305.74811309814453,
    minResolution: 0.29858214169740677,
    resolutions: mapResolutions,
    units: 'm',
    projection: baseMercator,
    displayProjection: wgs,
    restrictedExtent: bBox,
    panMethod: OpenLayers.Easing.Quad.easeOut,
    numZoomLevels: 20,
    controls: []
};

var mapOptionsEtrs = {
    maxExtent: bBoxEtrs,
    maxResolution: 282.2220698223045,
    minResolution: 0.35277758727788067,
    resolutions: mapResolutionsEtrs,
    units: 'm',
    projection: etrs,
    displayProjection: etrs,
    restrictedExtent: bBoxEtrs,
    panMethod: OpenLayers.Easing.Quad.easeOut,
    numZoomLevels: 10,
};


//Reprojection logic
var radioValue = Ext.getCmp('srsradiogroup').getValue().getGroupValue();
var mapProj = map.getProjection();
var baseCart = treePanel.getNodeById('basecartography');
if (radioValue != mapProj && radioValue == 'EPSG:3763') {
    baseCart.cascade(function () {
        var childNodes = this.childNodes;
        for (i = 0; i < childNodes.length; i++) {
            switch (childNodes[i].text) {
            case 'OpenStreetMap':
                childNodes[i].disable();
                map.removeLayer(osm);
                break;
            case 'Google Streets':
                childNodes[i].disable();
                map.removeLayer(gstreets);
                break;
                // and so on...
            }
        }
    });

    map.setOptions(mapOptionsEtrs);
    map.layerContainerOrigin.transform(baseMercator, etrs);

    blankLayer.addOptions({
        projection: etrs
    });
    map.setBaseLayer(blankLayer);

    for (i = 0; i < mapPanel.map.layers.length; i++) {
        var className = mapPanel.map.layers[i].CLASS_NAME;
        if (className == 'OpenLayers.Layer.WMS') {
            mapPanel.map.layers[i].addOptions({
                srs: etrs
            });
        } else if (className == 'OpenLayers.Layer.Vector') {
            mapPanel.map.layers[i].projection = etrs;
        } else {
            //here i will control other types of layers like tilecache
        }
    }
}

Checking the map and layers projection through firebug, it seems everything is correct.
I've also tried to get the initial extent of the map, reproject it and zoom to it after the reprojection but the it still goes to the same place.

What might be the issues here?? Am i doing something completely wrong?
Cheers,

Hugo
                   


-- 
Hugo Martins
LabNT - ISEGI UNL
Campus de Campolide
1070-312 Lisboa
N 38°43'56.84", W 9°9'35.74"



--------------------------------------------------------------------------------


_______________________________________________
Users mailing list
Users at geoext.org
http://www.geoext.org/cgi-bin/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.geoext.org/pipermail/users/attachments/20110620/18cd8f32/attachment.htm 


More information about the Users mailing list