[GIS] OpenLayers get single layer from WMS multiple layers

layersopenlayers-2wms

Openlayers enables to declare multiple layers in one WMS layer class.

 var nasa_wms = new OpenLayers.Layer.WMS(
        "NASA Global Mosaic",
        "http://wms.jpl.nasa.gov/wms.cgi", 
        {
           layers: "modis, global_mosaic"
        });

When I add this layer to map, the items are coming together on the map. But sometimes I want to use a single layer to apply CQRS filter to "modis" layer or "global_mosaic".
So I should use a single wms layer and add CQRS filter.

Is this possible, or should I separate layers as two layers.

Best Answer

In OpenLayers 2 when you define a new OpenLayers.Layer object, it will be handled as one layer independently from how many layers you requested from the WMS server.

If you would like to get two separate layers to make different operations on them, you must separate the two layers into two OpenLayers.Layer objects.

var nasa_glob = new OpenLayers.Layer.WMS(
    "NASA Global Mosaic",
    "http://wms.jpl.nasa.gov/wms.cgi", 
    {
       layers: "global_mosaic"
    });

var nasa_modis = new OpenLayers.Layer.WMS(
    "NASA Modis",
    "http://wms.jpl.nasa.gov/wms.cgi", 
    {
       layers: "modis"
    });

map.addLayers([nasa_glob, nasa_modis]);

For further references on OpenLayers.Layer object properties and functions, please consult the OpenLayers 2 API documentation.