Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How do I add baselayer with two different projection? I have added overlayer with a different projection(EPSG:4326), but I need to add base layer with various projection

  1. Google Map
  2. My vector layer with EPSG:4326 projection

I can see everything is fine with google map but when I switch base layer to EPSG:4326 layer everything is white.

window.onload = function init(){
                var lon = 84;
                var lat = 28;
                var zoom = 8;
                var url = "http://10.6.7.6/geoserver/wms";

                var mapControls = [
                    new OpenLayers.Control.Navigation(),
                    new OpenLayers.Control.PanZoomBar(),
                    new OpenLayers.Control.LayerSwitcher({'ascending':false}),                       
                    new OpenLayers.Control.ScaleLine(),
                    new OpenLayers.Control.MousePosition(),
                    new OpenLayers.Control.OverviewMap(),
                    new OpenLayers.Control.KeyboardDefaults()
                ];

                var map = new OpenLayers.Map(
                'map', 
                {       
                    numZoomLevels: 21,
                    projection: new OpenLayers.Projection("EPSG:900913"),
                    displayProjection: new OpenLayers.Projection("EPSG:4326"),
                    controls: mapControls 
                });


                var dsb = new OpenLayers.Layer.WMS( 
                "Nepal Border",
                url,                
                {layers: 'GisData:IR'}                        
            );  

                var gmap_gs = new OpenLayers.Layer.Google(
                "Google Streets",// the default 
                {'sphericalMercator': true}                 

            );




                var dG = new OpenLayers.Layer.WMS( 
                "2G Down Sites",
                url, 
                {layers: 'GisData:dG', transparent: "true",format: "image/png"},
                {
                    isBaseLayer: false,
                    buffer: 0
                });


                map.addLayers([dsb, dG,gmap_gs]);
                var proj = new OpenLayers.Projection("EPSG:4326");
                var point = new OpenLayers.LonLat(lon,lat);

                point.transform(proj, map.getProjectionObject());

                map.setCenter(point, zoom);

            }
share|improve this question
    
Could you clarify? –  atlefren May 2 '12 at 10:06
    
Why you need a base layer with two projections? Why not use a base layer for each projection? –  cag May 2 '12 at 10:09
    
I have my own dataset with different projection. –  kinkajou May 2 '12 at 10:26
    
@cag I need two base layer one google earth and other my own vector data as base layer. Google earth is working fine but not mine. –  kinkajou May 2 '12 at 11:22
    
@atlefren I have edited the question –  kinkajou May 2 '12 at 11:28
add comment

4 Answers

Sorry to revive this old thread, but as I've been looking into this recently, and this is one of the top search results, I figured i'd add some breadcrumbs for those who come later of my research and experimentation.

Long and Short:

The 2.X line of Open Layers does not support this. The Map, it's properties such as center, resolution and extents are set by the INITIAL base layer of the map. Change to another base layer DOES NOT adjust these properties, so the outcome is unknown. Often, you get a center point/bounds that are not valid for the base layer you are changing to, so you see white tiles. But that is not ALWAYS the outcome (for example, switching from 4326 to 900913 will bring you to a point close to 0degN,0degE).

But, hope is not lost.

Near term:

Some patches were written against open layers that attempt to patch this for the map and many of the layers (see [1]). This comes from a bug filed over 6 (6!!!) years ago [2]. While you'd have to host your own version of OL and patch it by hand (the patch will not apply cleanly to the latest versions, so you have to manually do the edit), this will get you mostly where you want to be. Very nasty, and doesn't handle everything, but overall, gets someone who NEEDS multiple baselayers with different projections on their feet.

Long term:

This is exactly one of the things that OpenLayers 3.0 is hoping to address[3].

One example is the base layer/overlay dichotomy. It is often a challenge to know how and where to set resolutions, maxExtent, etc. for the map. Projection handling between map and layers and the interplay between projection and properties like maxExtent and center are complicated at best.

The Map object in 2.X relied too heavily on the base layer, and that is going to change going forward. So, if you can hold out until late 2013, there may be a pre-made solution for you to use.

[1] http://trac.osgeo.org/openlayers/attachment/ticket/1249/NEW.Reproject.patch
[2] http://trac.osgeo.org/openlayers/ticket/1249
[3] http://openlayers.org/blog/2012/11/14/why-are-we-building-openlayers-3/

share|improve this answer
    
@J Jones nice to hear that :) ! –  kinkajou Jul 15 '13 at 3:22
add comment

there is something as your question here, How to reproject base layers in OpenLayers? and you get some information about transformation, you should look this, Creating Spherical Mercator Raster Images.

to summarize briefly from first post:

  1. OpenLayers can transform vector layers (like WFS).
  2. Raster Layers cannot be transformed by OpenLayers.
  3. Any good WMS should be able to do the reprojection for you.
  4. ArcGIS Server can't provide layers in 900913 because that wkid is unsupported. 3857, on the other hand...!

and from second link:

One of the reasons that the Spherical Mercator projection is so important is that it is the only projection which will allow for overlaying image data on top of commercial layers like Google Maps correctly. When using raster images, in the browser, it is not possible to reproject the images in the same way it might be in a ‘thick’ GIS client. Instead, all images must be in the same projection. How to create Spherical Mercator projected tiles depends on the software you are using to generate your images.

The important thing is here:

using raster images, in the browser, it is not possible to reproject the images
in the same way it might be in a ‘thick’ GIS client.

and for transforming dataset, my answer can help you about this, here.

i hope it helps you...

share|improve this answer
    
I have added the code with proj4js which works for overlayes but not for 2 different base layers any comment on this? –  kinkajou May 3 '12 at 7:58
add comment

Set up your WMS layer with out any reference to 900913 (assuming your data is in 4326). Then when you add the layer to OpenLayers make sure that you have set the projection of the map to 900913 (or just make sure to add Google Maps first). Then when you add the WMS layer, OpenLayers will request the data in the map projection (900913) and GeoServer will reproject from 4326 to 900913 on the fly.

share|improve this answer
    
Added the code. There is problem when switching between these layer. When I load the layer with WGS 4326 first as base layer and maintain the zoom level it's ok after I switch to google layer and pan a map a little it's all gone. –  kinkajou May 3 '12 at 7:57
    
Change map.addLayers([dsb, dG,gmap_gs]); to map.addLayers([gmap_gs, dsb, dG]); –  iant May 3 '12 at 8:12
    
shows google map on first load but still can't switch layer. Error while switching –  kinkajou May 4 '12 at 5:34
add comment

In general this isn't possible as OpenLayers sets the projection of the map from the base layer.

One possible work around is to use a cascading WMS like GeoServer to reproject one of the base layers from one to the other projection.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.