[GIS] OpenLayers weird problem with GoogleMaps base layer and WMS overlay

google mapsopenlayers-2wms

I'm trying to overlay a WMS layer over GoogleMaps base one. The WMS
layer is defined like this:

 var catastro_layer = new OpenLayers.Layer.WMS(
           "WMS Catastro",
           http://ovc.catastro.meh.es/Cartografia/WMS/ServidorWMS.aspx?SERVICE=WMS&VERSION=1.1.1,
           {
                   layers: "catastro",
                   format:"image/png",
                   srs: "EPSG:3785",
                   transparent: false
           }
   );

GoogleMaps layers are defined just like here
http://openlayers.org/blog/2010/07/10/google-maps-v3-for-openlayers/.

As result, my WMS layer appears as one base layer more. So, I can
select it instead of the other base layers, not over them. This
screenshots shows what I mean:
http://dl.dropbox.com/u/6599273/capa_wms1.png

The WMS requests are ok:

http://ovc.catastro.meh.es/Cartografia/WMS/ServidorWMS.aspx?SERVICE=WMS&VERSION=1.1.1&LAYERS=catastro&FORMAT=image%2Fpng&SRS=EPSG%3A3785&TRANSPARENT=false&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&BBOX=-1721973.3737,3228700.0733875,-1702405.4944625,3248267.952625&WIDTH=256&HEIGHT=256

But this isn't what I need. I want a GoogleMaps base layer and a
transparent WMS overlay.

Now, if I change the WMS layer defintion replacing transparent: false
by transparent: true:

 var catastro_layer = new OpenLayers.Layer.WMS(
           "WMS Catastro",
           http://ovc.catastro.meh.es/Cartografia/WMS/ServidorWMS.aspx?SERVICE=WMS&VERSION=1.1.1,
           {
                   layers: "catastro",
                   format:"image/png",
                   srs: "EPSG:3785",
                   transparent: true
           }
   );

My WMS layer appears as a overlay:
http://dl.dropbox.com/u/6599273/capa_wms2.png, just what I want.

But now, the requests are incorrect:

http://ovc.catastro.meh.es/Cartografia/WMS/ServidorWMS.aspx?SERVICE=WMS&VERSION=1.1.1&LAYERS=catastro&FORMAT=image%2Fpng&SRS=EPSG%3A900913&TRANSPARENT=true&REQUEST=GetMap&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&BBOX=-1721973.3737,3228700.0733875,-1702405.4944625,3248267.952625&WIDTH=256&HEIGHT=256

As you can see, the epsg:3785 has been replaced by epsg:900913. But
the only change I've made is "transparent: false" by "transparent:
true". Why is this happening?

Many thanks in advance, and best regards

Best Answer

In OpenLayers, the base layer dictates what is going to be the "base spatial reference". So any overlay will be projected to the base srid. Since any base layer that comes from the consumer-based providers (google, bing, yahoo, etc) supports only 900913, then OpenLayers is doing the right thing for your.

If you do a GetCapabilities call on your server you will notice the following section:

<Layer queryable="1" opaque="0" noSubsets="0">
<Name>Catastro</Name>
<Title>Catastro</Title>
<Style>
<Name>Default</Name>
<Title>Default</Title>
<LegendURL width="160" height="500">
<Format>image/png</Format>
<OnlineResource xlink:type="simple" xlink:href="http://ovc.catastro.meh.es/Cartografia/WMS/simbolos.png"/>
</LegendURL>
</Style>
<BoundingBox SRS="EPSG:4230" minx="-18.409876" miny="26.275447" maxx="5.22598" maxy="44.85536"/>
<BoundingBox SRS="EPSG:4326" minx="-18.409876" miny="26.275447" maxx="5.22598" maxy="44.85536"/>
<BoundingBox SRS="EPSG:4258" minx="-18.409876" miny="26.275447" maxx="5.22598" maxy="44.85536"/>
<BoundingBox SRS="EPSG:32627" minx="770000" miny="3000000" maxx="2700000" maxy="5000000"/>
<BoundingBox SRS="EPSG:32628" minx="180000" miny="3000000" maxx="2170000" maxy="5000000"/>
<BoundingBox SRS="EPSG:23029" minx="-410000" miny="3000000" maxx="1650000" maxy="5000000"/>
<BoundingBox SRS="EPSG:23030" minx="-1050000" miny="3000000" maxx="1150000" maxy="5000000"/>
<BoundingBox SRS="EPSG:23031" minx="-1615000" miny="3000000" maxx="620000" maxy="5000000"/>
<BoundingBox SRS="EPSG:32629" minx="-410000" miny="3000000" maxx="1650000" maxy="5000000"/>
<BoundingBox SRS="EPSG:32630" minx="-1050000" miny="3000000" maxx="1150000" maxy="5000000"/>
<BoundingBox SRS="EPSG:32631" minx="-1615000" miny="3000000" maxx="620000" maxy="5000000"/>
<BoundingBox SRS="EPSG:25829" minx="-410000" miny="3000000" maxx="1650000" maxy="5000000"/>
<BoundingBox SRS="EPSG:25830" minx="-1050000" miny="3000000" maxx="1150000" maxy="5000000"/>
<BoundingBox SRS="EPSG:25831" minx="-1615000" miny="3000000" maxx="620000" maxy="5000000"/>
<BoundingBox SRS="EPSG:3785" minx="-2100000" miny="3000000" maxx="500000" maxy="5600000"/>
</Layer>

Lo and behold, 900913 is not an option. You may need to either:

  • modify the WMS server definition to make sure that 900913 is allowed as a valid reprojection
  • add a definition for that new srid (depends on what web server you are using)
  • a combination of thse

By looking at the URL, it seems you are using an ASP .NET WMS server, since Geoserver is java and Mapserver if cgi, and ArcGIS Server is .NET, I am going guess this is an ESRI server, so the instructions on how to modify this is here.

Update: As mkennedy points out, the official EPSG code for SphericalMercator (aka Google's projection) is 3857 and a previous assigned number was 3785. If you are wondering why there are so many numbers that refer to the same spatial reference, crschmidt's blog post will give you a clue...

Ian pointed out the relevant OpenLayer's documentation section to force OpenLayers to use that EPSG code instead.

Since your GetCapabilities section shows 3785, the code from the OL documentation won't work as is, you will need to replace all the 3857 for 3785.

Related Question