1

I just managed to create a parametric view that is working OK when i define the parameter value as, for instance, viewparams:'value:31' However, when i try to pass a value that comes from a variable, for instance, viewparams:'value:variable', the wms layer just wont load. The variable is defined as var variable= 31; Any ideas on why this happens? I'm using OpenLayers + Geoserver + PostGIS

EDIT 1 -

Just to explain a little bit better what I'm trying to achieve. I have a web map application developed with Openlayers where i have a set of wms layers that are being created by Geoserver. Despite the fact that i already have some functionalities implemented, such as feature info in pop ups, images on those pop ups, etc, i thing that it would be really important to allow users to query the layers, to allow the map to show only what they wanted to see. The query would be done through a php form, where the values selected by users would act as variables on the Parametric views created on Geoserver, and by doing so, the features showing up on the map would be only the ones selected by the user. The parametric view i created for testing purposes works, if i "hardcode" the value for the parameter:

var rov_dives = new OpenLayers.Layer.WMS(
           "ROV Dives",
                   "http://host:8080/geoserver/wms",
                   {'layers': 'parametric_view', 'format':'image/png', 'transparent':'true', viewparams:'test_parameter:50'},
                   {'opacity': 1, 'isBaseLayer': false, 'visibility': false}
                );

In this example the map only shows the features that comply with the condition stated on the SQL script that defines the parametric view, in this case, all features that have a number of measurements higher than 50. If I could pass that 50 as a variable coming from the aforementioned PHP form, I could implement the solution I thought of... The definition of the variable and WMS layer I'm trying to use right now is:

var variable = 50;

var rov_dives = new OpenLayers.Layer.WMS(
           "ROV Dives",
                   "http://host:8080/geoserver/wms",
                   {'layers': 'parametric_view', 'format':'image/png', 'transparent':'true', viewparams:'test_parameter:variable'},
                   {'opacity': 1, 'isBaseLayer': false, 'visibility': false}
                );

Despite all my efforts I'm yet to find a solution for this.

Hope this explanation helps to understand the problem!

1 Answer 1

1

That's because you're not allowed to substitute variables in JavaScript like this. I'm also assuming this it's part of a longer url string.

Try splitting that string around the variable you need to substitute

You can use the following if you're sure your variable is always going to be a number:

var variable = 50;

var rov_dives = new OpenLayers.Layer.WMS(
    "ROV Dives",
    "http://host:8080/geoserver/wms",
    {'layers': 'parametric_view', 'format':'image/png', 'transparent':'true', viewparams:'test_parameter:' + variable },
    {'opacity': 1, 'isBaseLayer': false, 'visibility': false}
);
6
  • Hi Guy, thanks for the reply. As a matter of fact, the viewparams, as far as i know is to be called as i wrote on the post. It is even stranger that when i debug the code the variable value is passed correctly, just not interpreted as so. Any further thoughts on this? Thanks!
    – fgbrandao
    Commented Dec 2, 2013 at 18:58
  • I'm a little confused as to what you're trying to achieve. Could you post a small snipped of your Javascript code?
    – Guy
    Commented Dec 3, 2013 at 10:18
  • Hi again Guy, thank you for your time. I've just edited the post. Hope it explains better what i'm trying. Thanks!
    – fgbrandao
    Commented Dec 3, 2013 at 11:06
  • Thanks once again Guy, can't wait to try it. I'll let you know how it went! Not wanting to abuse your good will, is there a way to apply something like this on textual variables? They are to be included also (but on different views!)... Thanks!
    – fgbrandao
    Commented Dec 3, 2013 at 18:37
  • Yes, you just have to make sure you get the quotes in the right places. You can use your browser's developer tools (devtoolsecrets.com) to check that openlayers is requesting the url you expect.
    – Guy
    Commented Dec 3, 2013 at 18:55

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.