5

I am using OpenLayers, Geoserver 2.1 CQL language. The problem is that I do not know how to address the layer. I am trying to call a function like this:

   var createWmsLayer = function(name, url, params, options) {
        map.addLayer(new OpenLayers.Layer.WMS(name, url, params, options));         
    };

I am creating the layer like this:

var ikke_listet = new  createWmsLayer("<span class='lc'>Ikke rødlistet</span>", 
     "http://kart.naturkart.no/geoserver/wms",
                {layers: 'gbif_ns:ikke_listet',format: 'image/png', 
                transparent: 'true'}, {singleTile: true, ratio: 1, 
                  isBaseLayer:false, visibility: false}); 

Then I am calling a function on the layer to redraw it with CQL params to GeoServer.

function CQLfilter(param){
        filter_list = [] 
        filter_list.append("collector LIKE '%H%' ")
        ikke_listet.mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "})

But I get this error in Firebug:

ikke_listet is not defined

So my question is: What is the name of my layer?

I am using the "name" part of my function to write out some styles for the layerlist (I use ExtJS) and put a padding and different icons in the layerlist to show what icons represent which layer. Is this a wrong way to do this styling?

Any suggestions as to what I use in the function to add CQL call to the layer and redraw it?

Aragon, I was able to get the layername from your suggestion of the layer array:

function CQLfilter(param){
var mLayers = map.layers;
mLayers[9].mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "})
// This gave me the names of the layers
// for(var a = 0; a < mLayers.length; a++ ){
//    alert(mLayers[a].name + '#' + a + '#')
//}; 
}

So I wonder if there is another way to style my layer list because now I get the layernames like this: "<span class='lc'>Ikke rødlistet</span>". But of course I can still get to the layer by referencing the layer array position.

I managed to run the function now and it filters correctly. I only had to ignore the first base 4 layers and I have to find out how to collect the values from the search form. But manually this code now works and I have much progress :-)

function CQLfilter(param){
var mLayers = map.layers;
// have to get the search by document.getElementById or jQuery I guess. Testing manually now
var search = "kingdom LIKE '%Plantae%' ";
for(var a = 4; a < (mLayers.length); a++ ){
    alert(mLayers[a].name + '#' + a + '#')
    mLayers[a].mergeNewParams({'CQL_FILTER': search})
}; 
}

Hi again, I am able to get the layer now. Works fine with this function on the CQL filter and the reset filter function at the end.

function CQLfilter(sok,cat){
var mLayers = map.layers;
switch (cat)
{
case 'SN':
   param = "scientificname LIKE '%"+ sok + "%' ";
  break;
case 'NNG':
   param = "norsknavngruppe LIKE '%"+ sok + "%' ";
  break;
case 'NNA':
   param = "norsknavnart LIKE '%"+ sok + "%' ";
  break;
case 'PROJ':
   param2 = new Array()
   param2.push("prosjektnummer LIKE '%" + sok + "%'")
   param2.push("prosjektnavn LIKE '%" + sok + "%'")
   param = param2.join(" OR ");
  break;  
default:
   param2 = new Array()
   param2.push("scientificname LIKE '%" + sok + "%'")
   param2.push("norsknavngruppe LIKE '%" + sok + "%'")
   param2.push("norsknavnart LIKE '%" + sok + "%'")
   param2.push("collector LIKE '%" + sok + "%'")
   param = param2.join(" OR ");
  break;
}
for(var a = 4; a < (mLayers.length); a++ ){
    mLayers[a].mergeNewParams({'CQL_FILTER': param});
    mLayers[a].setVisibility(true);
}}
function resetCQLfilters() {
    var mLayers = map.layers;
    for(var a = 4; a < (mLayers.length); a++ ){
    delete mLayers[a].params.CQL_FILTER;
    mLayers[a].redraw();
}}

3 Answers 3

11

which layer do you want, i dont understand? but getting all layer:

map.layers

getting all layer name:

var mLayers = map.layers;

for(var a = 0; a < mLayers.length; a++ ){
    alert(mLayers[a].name)
};

getting last added layer name:

map.layers.getLast().name

and beside this you can use all this method too:

1.map.getLayer()
2.map.getLayerIndex()
3.map.getLayerPxFromLonLat()
4.map.getLayerPxFromViewPortPx()
5.map.getLayersBy(),
6.map.getLayersByClass()
7.map.getLayersByName()

i hope it helps you...

6
  • I am trying to access the layer "gbif_ns:ikke_listet" but when I alert the layername it says the layername is "<span class='lc'>Ikke rødlistet</span>" - that is of course since I use the layername to style it. You can see it here: babkart.no I am really stuck here on some basic js obiously.
    – asle
    Commented May 14, 2012 at 13:34
  • 1
    you can add new defination to your layer properties sth. like this: map.layers[1].orgName = "gbif_ns:ikke_listet" than you can easily reach it...
    – urcm
    Commented May 14, 2012 at 13:47
  • Would that be inside the init function (on body onload) just after the layer declarations? I will try this. Thanks!
    – asle
    Commented May 14, 2012 at 15:59
  • you will add this when creating a new layer. it will be properties of your newly created layer.
    – urcm
    Commented May 14, 2012 at 17:10
  • Thanks Aragon, look at my comments in the original question. I hope using map.layers array is an ok way to handle it!
    – asle
    Commented May 18, 2012 at 12:22
1

I think your scoping has gone haywire.

(if i understand your code corrctly) You have two functions: createWmsLayer and CQLfilter.

in the line var ikke_listet = new createWmsLayer...

you assign the returned value of the call to createWmsLayer to the variable ikke_listet. The problem is that createWmsLayer does not return anything.

So, you should rewrite it to :

var createWmsLayer = function(name, url, params, options) {
    var layer = new OpenLayers.Layer.WMS(name, url, params, options);
    map.addLayer(layer);         
    return layer;
};

as variables created inside functions are not available outside (scoping).

On another note: while you do reach map inside the createWmsLayer fucntion, this is only because you have defined the map variable before the you define the function, so you should pass that as an input parameter as well.

But, basically; remember to return any variables created inside a function in order to use them outside of the function

EDIT: and skip the "new" keyword when calling your createWmsLayer function

4
  • Hi, I changed the createWmsLayer code and removed the "new" keyword. Try to create it this way: var ikke_listet = createWmsLayer( "<span class='lc'>Ikke listet</span>", "localhost/geoserver/wms", {layers: 'gbif_ns:ikke_rodlistet',format: 'image/png', transparent: 'true'}, {singleTile: true, ratio: 1, isBaseLayer:false, visibility: false} ); I still get this error: ikke_listet is not defined ikke_listet.mergeNewParams({'CQL_FILTER': "collector LIKE '%H%' "})
    – asle
    Commented May 15, 2012 at 8:13
  • I tried to define the function CQLfilter() both inside the app and outside with no help. I must be doing something totaly newbie wrong here :-(
    – asle
    Commented May 15, 2012 at 8:13
  • if you could post a more detailed version of your code on pastebin or something i could take a look. But, it seems as though your main problem is Javascript itself, not OpenLayers as such
    – atlefren
    Commented May 15, 2012 at 11:23
  • Here is my js - app.js pastebin.com/zHYQP5qZ Here is my index file: index.html pastebin.com/TW4Aqz4Q Here is the site at the moment: babkart.no
    – asle
    Commented May 15, 2012 at 15:51
1

you can try by using Firebug (with firefox browser) and insert "console.dir(map.layers);" in your javascript code to show layer details.

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.