1

After successfully (with the precious help of members from stackexchange) creating a Geoserver parametric view that works as it should, now i'm facing another problem: How to pass a list of parameters from a given view in a new OpenLayers.Layer.WMS call. From the geoserver user manual it is stated that The viewparams argument is a list of key:value pairs, separated by semicolons:

viewparams=p1:v1;p2:v2;...

But since i'm calling the viewparams in an openlayers new wms layer, as soon as i put the semicolon on the script:

 var x = new OpenLayers.Layer.WMS(
        "y",
            "http://10.194.32.103:8080/geoserver/wms",
            {'layers': 'vista', 'format':'image/png', 'transparent':'true', viewparams:'parameter:data';'parameter1:data1'},
            {'opacity': 1, 'isBaseLayer': false, 'visibility': false}
            );

i get an error:

enter image description here

Any ideas on how to overcome this situation?

Thanks!

  • Does using encodeURI() for the parameters work? – Devdatta Tengshe Dec 5 '13 at 15:53
  • Sorry, still newbie on this... Do you mean passing the parameters list as a variable? – fgbrandao Dec 5 '13 at 15:56
  • Been trying with encodeURI() and it wasn't successfull. The double quotes mess it up, since the parameters are passed as a string and none of the values from the parameters are interpreted as so... – fgbrandao Dec 5 '13 at 16:33
  • What happens if you remove the single quotes either side on the semi-colon? i.e. viewparams:'parameter:data;parameter1:data1' – Guy Dec 5 '13 at 17:26
  • The WMS layer wont load... But i've managed to figure out a solution with the encodeURI() as mentioned by Devdatta Tengshe. The code now looks like this: variable = 30000000; var variable1 = 35000000; var uri = 'number:'+variable; var uri1 = 'number1:'+variable1; var res = encodeURI(uri+';'+uri1); and the finally: viewparams:res. Remember, i'm working with variables as inputs for the parameters values... – fgbrandao Dec 5 '13 at 17:32
2

If your parameters are numbers, then its quite simple. You can just use the following code:

{'layers': 'vista', viewparams:"parameter:data;parameter1:data1", 'format':'image/png', 'transparent':'true'}

If your parameters are strings, then it's slightly complicated. if you are using a string in a SQL query's where clause, you need to pass the string with inverted commas around it.

Assuming you have PostGIS as the Backend, the pure query will be something like this:

Select gid, name, st_name, the_geom from data where st_name ='NY';

When making that as a parameterised view in Geoserver, you'll provide something like

Select gid, name, st_name, the_geom from data where st_name =%st%

with the default value of st= 'NY'

You need to pay attention to the regular expression which validates the input. The default is: ^[\w\d\s]+$ This regular expression will not match the input when you have the string with inverted commas.

To solve this, you will have to change the SQL command for the view to include the inverted commas like this:

Select gid, name, st_name, the_geom from data where st_name ='%st%'

Once you have edited the SQL, then you can use the viewparameters like given above, even when the values are string.

  • Thanks! I've continued to investigate the encodeURI option and have successfully made it! Just to add a little bit more information, i'm dealing with numbers and strings, but they all come from variables. The goal is to let the user see only what he wants... Thanks again. I'll keep in mind all your advices! – fgbrandao Dec 5 '13 at 17:07
  • I was wondering if you could give another insight on how to specify default values on parametric views in order to include all records from a textual parameter. The purpose is: if the user doesn't define any value for that parameter (as a variable), all records are shown. Thanks for your time! – fgbrandao Dec 5 '13 at 17:21
  • 1
    If you will have that kind of situation, then it might be better to us a cql_filter on a wms layer with all the data instead of using a patameterised view in GeoServer – Devdatta Tengshe Dec 5 '13 at 17:41
  • I'm completely open to suggestions on the best way to do this. My main goal is to implement a php form that graphically builds a sql query, to give the users the hability to see only what they want, from the available wms layers. Is this possible to achive with the cql_filter? Thanks once again! – fgbrandao Dec 5 '13 at 17:47
  • It's very much possible. Have a look here: jsfiddle.net/devdatta/HYcLt You as a developer can either ask the user for the entire Where clause, or can even ask only for specific values of specific fields, and then build a where clause programatically. – Devdatta Tengshe Dec 6 '13 at 3:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.