6

I would like to use OpenLayers 3 (I saw similar question for OpenLayers 2) to display a GeoServer layer using WMS, and zoom to the extent of the layer automatically by getting the extent information from this layer. Just like when preview the layer on GeoServer, it displays the layer within the layer's extent and no matter what the CRS of the layer is.

I have installed Opengeo suite 4.5 and created a viewer with the suite-sdk viewer template. I debug with suite-sdk debug -g http/to/geoserver path/to/app

The viewer uses OpenLayers 3 library. I have added OSM layer and my GeoServer layer with ol.source.TileWMS source and display them successfully.

I get the capabilities of my local GeoServer WMS service (ol3 example). With the results, by checking the name of each layer, I would like to zoom to my target layer.

//
// the codes add layers to ol.map and intial the ol.view
//

// TODO: zoom to layer
var featurePrefix = '***';
var featureType = '***';
var url = 'http://localhost:port/geoserver/wms?request=GetCapabilities&service=WMS&version=1.1.1';
var parser = new ol.format.WMSCapabilities();
$.ajax(url).then(function(response) {
   var result = parser.read(response);
   var Layers = result.Capability.Layer.Layer; 
   var extent;
   for (var i=0, len = Layers.length; i<len; i++) {
     var layerobj = Layers[i];
     if (layerobj.Name == featurePrefix + ':' + featureType) {
         extent = layerobj.BoundingBox[0].extent;
         break;
     }
     console.log(layerobj.Name);
   }

   // THIS DOES NOT WORK
   map.getView().fitExtent(extent, map.getSize());
});

With the code above, I can get the BBOX of the layer in its own coordinate system other than ESPG::3857. But the map did not zoom to that layer. I think it is a coordination system problem.

How can I solve it, or another approach to get the extent?

(I found the current code is not nice)

3 Answers 3

3

Just use ol.proj.transformExtent in between to transform your extent. You can also use layer.latlonBBOX from the GetCapabilities parser output so you always know the source bounding box is in EPSG:4326.

2
  • Thanks! I suppose by using the transformExtent, you need to provide projection information if it is not ESPG 4326. So I prefer to use layer.latlonBBOX. However, the parsed result object does not seem to contain this information, but I do have seen that in the XML file. Is there something I miss?
    – stevenhz
    Commented Mar 16, 2015 at 13:37
  • Mind you? If you can give some example on how you implemented it? This is what I need now. Commented Nov 26, 2015 at 3:41
0

Following the suggestions from user23054, I solved the problem like this:

In the geoserver webadmin interface "Edit layer data and publishing" page, section "Coordinate Reference Systems", set the declared SRS to ESPG: 4326, the set "Reproject Native to Declared".

So the code will always get the extent as in ESPG 4326. Then apply ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:4326", "EPSG:3857"));

the ol.proj.transformExtent(extent, "EPSG:4326", "EPSG:3857")does not work for me. I do not know what is wrong here.

1
  • I still prefer to use layer.latlonBBOX without the settings in the Geoserver. The parsed result object does not seem to contain this information, but I do have seen that in the XML file. Is there something I miss?
    – stevenhz
    Commented Mar 16, 2015 at 14:28
-1

Use this:

map.getView().fit(extent, map.getSize());

instead of fitExtent. Only use the function fit Worked for me. i have all my layers already in 3857.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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