1

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

4
  • 3
    I usually write them on a piece of paper and type them in again :-)
    – Ian Turton
    Commented Aug 1, 2013 at 16:11
  • @AnilHoney If possible please share your code on JSfiddle
    – Sunil
    Commented Aug 2, 2013 at 5:01
  • You will need to do a GetCapabilities request on the WMS service to get the Bounding box. Commented Aug 2, 2013 at 6:16
  • check once my code
    – AnilHoney
    Commented Aug 2, 2013 at 12:28

3 Answers 3

1

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.

0

According to your question I think you want to apply bounding box control in map.

For that you need to :

1.add any layer and

2.use map.setCenter() property.

Please check this example for the reference & right click on that page and view source so that you insert your link in similar fashion.

hope this will help you..

3
  • map.setCenter(new OpenLayers.LonLat(146.7, -41.8), 6); i want to get that 146.7,-41.8 from geo server how to that
    – AnilHoney
    Commented Aug 1, 2013 at 12:31
  • @AnilHoney You can use your own WMS link from geoserver. Go though the example openlayers.org/dev/examples/wms.html
    – Sunil
    Commented Aug 1, 2013 at 12:43
  • vmap0.tiles.osgeo.org/wms/vmap0 it is geo server layer by default it have native bound box and LonLat how to acess those things from openlayers
    – AnilHoney
    Commented Aug 1, 2013 at 12:53
0

It's not clear what you are trying to achieve. Normally, the bounds of a projection are defined as global, unless the projection is specialised, like UK's BNG where the coordinate system only covers the UK.

If you want to prevent the user panning outside a certain zone, then the thing to set is restrictedExtent.

However, there's no easy way to get this bounding box from the server. All I could think to do would be to have a hidden Vector layer, populate it from the server, and use the getDataExtent() method to extract a bounding box. However, it seems a bit messy and expensive.

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.