[Users] Programmatically changing the base map projection

Hugo hfpmartins at gmail.com
Fri Jun 24 16:45:31 CEST 2011


Hi again,
>>
>> I think i've spoken to early. Matt, after digging a little bit more, your
>> code suggestions are givinig me the same results as my initial approach. The
>> strange thing is that, using your code and firbug, i see that the Center
>> coordinates and bounding box for maxExtent are being correctly reprojected.
>> Nevertheless, the map is going to a different place on earth (see attached
>> images: mercator is ok but etrs is completely out).
>
>
http://dl.dropbox.com/u/10817385/mercator.png
http://dl.dropbox.com/u/10817385/etrs.png


> No errors are shown in firebug and i'm a little bit lost. I have defined
>> the etrs layer like the following:
>>
>> var ortos = new OpenLayers.Layer.TileCache(
>>         'Ortofotomapas ARH',
>>         'http://localhost/tilecache',
>>         'Ortos',
>>         {
>>             isBaseLayer: true,
>>             maxExtent: bBoxEtrs,
>>             serverResolutions: mapResolutionsEtrs,
>>             visibility: false,
>>             transparent: true,
>>             scales: [800000, 500000, 250000, 100000, 50000, 25000, 10000,
>> 5000, 2500, 1000],
>>             maxResolution:282.2220733500804,
>>             minResolution: 0.35277758727788067,
>>             units: 'm',
>>             projection: etrs,
>>             restrictedExtent: bBoxEtrs
>>         }
>>     );
>>
>> I'm sure that this tilecache layer is working properly because i've tested
>> it on a simple openlayers app configured with the same options.
>>
>> Any hints on what might be happening??? Unfortunately, i can't see what is
>> going wrong and have no more alternative ideas.
>> Cheers,
>>
>> Hugo
>>
>>
>>
>> On Tue, Jun 21, 2011 at 2:42 PM, Hugo <hfpmartins at gmail.com> wrote:
>>
>>> Hi Matt,
>>>
>>> That works like a charm ;)
>>> Really, thanks a lot for this suggestion and code!
>>>
>>> Cheers,
>>>
>>> Hugo
>>>
>>>
>>>
>>> On Mon, Jun 20, 2011 at 8:29 PM, Matt Priour <
>>> mpriour at kestrelcomputer.com> wrote:
>>>
>>>> **
>>>> 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 <hfpmartins at gmail.com>
>>>> *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"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.geoext.org/pipermail/users/attachments/20110624/4884d95d/attachment-0001.htm 


More information about the Users mailing list