[GIS] How to get native bounding box (minx,miny,maxx,maxy) using OpenLayers

geoserveropenlayers-2

I am trying to develop a GIS application using GeoServer and OpenLayers. I want to get the bounds from GeoServer Native Bounding Box data of a layer instead of manually hard code:

var map;
var key = 0;
var fromNo = "";
var toNo = "";
var colorId = "";
var count = 0;
function init(){
format = 'image/png';   
if(map)
map.destroy();
var bounds = new OpenLayers.Bounds(
    8568700.4305088, 1677704.30921461,
                    9195694.68892519, 2070938.3484273
);
var options = {
    controls: [],
    maxExtent: bounds,
    maxResolution: "auto",
    projection: "EPSG:900913",
    units: 'degrees',
    minZoomLevel: 0, numZoomLevels:15,maxZoomLevel:15   
};
var  click_ap ;
var values = new Array();
map = new OpenLayers.Map('map', options);
    $('.selectionTable tr').each(function() {
        fromNo  =  $(this).closest("tr").find(".fromNo").val();
        toNo    = $(this).closest("tr").find(".toNo").val();
        colorId = $(this).closest("tr").find(".colorSelect option:selected").val();
        apZoomLevel1 = new OpenLayers.Layer.WMS(
                   "ap",
                "http://localhost:8090/geoserver/scmapWithSelectColor/wms??request=getCapabilities ",
                    {'layers': 'scMapQuery', 
                    viewparams: 'fromNo:'+fromNo+';toNo:'+toNo+';colorId:'+colorId+';',
                    'transparent': true,isbaselayer:false},{
                       singleTile: true,opacity: .5
                    }
              );
              values.push(apZoomLevel1);
});
click_ap = new OpenLayers.Control.WMSGetFeatureInfo({
 url:'http://localhost:8090/geoserver/scmapWithSelectColor/wms', 
 title: 'Identify features by clicking',
 layers: [apZoomLevel1],
 queryVisible: true,
 eventListeners: {
 getfeatureinfo: function(event) {

if (ContentFilterAp(event.text,'hos').length>0){
//alert("localitieslevel3");
    map.addPopup(new OpenLayers.Popup.FramedCloud(
        "apZoomLevel1", 
        map.getLonLatFromPixel(event.xy),
        null,
        ContentFilterAp(event.text,'hos'),
        null,
        true
            ));}
        }
        }
    });
gmap = new OpenLayers.Layer.Google
(
    "Google Streets"  
);
ghyb = new OpenLayers.Layer.Google
(
   "Google Hybrid",
   {type: google.maps.MapTypeId.HYBRID}
);
    map.addControl(new OpenLayers.Control.Navigation());

    map.addControl(new OpenLayers.Control.LayerSwitcher({'div':OpenLayers.Util.getElement('layerswitcher')}));
    map.addLayers([gmap,ghyb]);
    var length = values.length;
    if(length > 0)
    {
        for(i=0;i<length;i++)
        {
            //debugger;
            map.addLayers([values[i]]);
        }
    }
    map.addControl(new OpenLayers.Control.PanZoomBar({
    }));
    map.addControl(new OpenLayers.Control.Navigation());
    map.addControl(new OpenLayers.Control.Scale($('scale')));
    map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));
    map.zoomToExtent(bounds);
    map.addControl(click_ap);
    click_ap.activate();
    map.events.register('zoomend', this, function (event) {
}); }

Actually above bounds are hard coded but actually apzoomlevel1 have native bounds how to get those one? actually those bounds useful in map.zoomToExtent(bounds); based on that map zoom will change

Best Answer

Here's an example how you can parse WMS Capabilites with jQuery to get the bounding boxes and some other information about layers contained in the WMS:

var layers = [];    
$.ajax({
    type: "GET",
    url: yourWMSUrlHere,
    dataType: "xml",
    async: true
}).done(function (data) {        
    $(data).find('Layer').each(function (i, layer) {
        layer = $(layer);
        var name = layer.children("Name").text();
        if (name) {
            layers.push({
                name: name,
                title: layer.children("Title").text(),
                queryable: layer.attr('queryable'),
                minScale: layer.children("MaxScaleDenominator").text(),
                maxScale: layer.children("MinScaleDenominator").text(),
                xmin: layer.children('EX_GeographicBoundingBox').children('westBoundLongitude').text(),
                ymin: layer.children('EX_GeographicBoundingBox').children('southBoundLatitude').text(),
                xmax: layer.children('EX_GeographicBoundingBox').children('eastBoundLongitude').text(),
                ymax: layer.children('EX_GeographicBoundingBox').children('northBoundLatitude').text()
            });
        }
    });
});

Then you have to construct the bounds from xmin, ymin, xmax and ymax.