[GIS] How to insert a list of parameters from a parametric view in OpenLayers

geoserveropenlayers-2

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!

Best Answer

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.

Related Question