[GIS] WMS Geoserver layer over OSM

boundless-suitegeoserverjavascriptopenlayers-2trajectory

I have a Geoserver with a layer in a EPSG:32632 projection. I want to display this layer over Open Street Map tiles.

When I use the following html code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="http://www.openlayers.org/api/2.11/OpenLayers.js"></script>
    <script>

        var map, layer;

        function init(){
            var epsg900913 = new OpenLayers.Projection("EPSG:900913");
            var epsg4326 = new OpenLayers.Projection("EPSG:4326");
            var cent=new OpenLayers.LonLat(6.59,46.39).transform(epsg4326,epsg900913);

            var osm = new OpenLayers.Layer.OSM("Open Street Map");

            bwms = new OpenLayers.Layer.WMS("Trajectory", "http://server.domain.com:8080/geoserver/test/wms",
                {
                    version: "1.3.0",
                    layers: "test:v_trajectory",
                    transparent: true,
                    tiled: true
                },
                {
                    visibility: true,
                    sphericalMercator: true,
                    displayOutsideMaxExtent: true // Will not appear with certain zooms without this option
                }
            );
            map = new OpenLayers.Map("map",{
                layers:[osm,bwms],
                center: cent,
                zoom: 10
            });
//          map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
            map.addControl( new OpenLayers.Control.LayerSwitcher() );

        }

    </script>
</head>
<body onload="init()">
    <div style="width:500px; height:500px" id="map"></div>
</body>
</html>

The OSM tiles are correctly displayed but the other layer does not appear on the map. I guess it is a projection problem, since the OSM tiles are in EPSG:900913, but isn't Geoserver supposed to feed me a reprojected map directly? With Firebug, I can see that the request has correctly been treated, but return blank 256×256 tiles. The request is also made for a SRC EPSG:900913, so the problem must be server-side.

What am I missing? The layer is feeded to Geoserver via PostGIS (in the OpenGeo framework). Am I supposed to put some specific settings client-side?

As a consistency check, I also used the Geoserver "preview layer" tool with OpenLayers, to see if the layer is correctly dispayed, and it is. However, if I declare a SRS 900913 in addition to the native 32632, I also get blank tiles in the tool. So I guess the answer is probably related to Geoserver and not OpenLayers. I really don't understand what I'm missing… Any help would be most welcome!

Best Answer

I finally found the answer to my question:

What I needed to do was to reproject from EPSG:32632 to 4326. This is done by defining a "declared layer" to 4326 and setting the option "reproject native to declare". This is not however the end of the story. You still need to recompute the associated BBox. On the geoserver GUI, this is done by clicking on "compute from data" AND "compute from native bounds".

The problem was that the query from OpenLayers was done with the correct BBOX in the projection 900913, but since the BBox on geoserver were incorrect, it was looking for an area where there were no data, ie return blank tiles.

Related Question