Monday, 26 October 2015

geoserver - how to pass value like 100,102,103 to view params


I am trying to zoom plot numbers.Plot numbers like 1 10-12 13/P road 20(A) 100&102 and etc.I am able to zoom to all combination plot numbers except 1,2,3.when I pass such value its trowing error:


1 feature types requested, but found 2 view params specified


geoserver sql view is:


select * from plotboundary where nmindar='%nmindar%' and plno='%plno%'

default values and validation are


Name  | Default Value   | validation


nmidar| Hanagawadi I A | ^[\w\d\s]+$

plno | 100,102&103 | ^[ A-Za-z0-9_ *@.\/#&+-\,()-_]*$

ajax function:


 $.ajax({
url: 'http://XX.168.1.XX:8089/geoserver/XX/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=XX:vwplot&maxFeatures=5000&outputFormat=text/javascript',
contentType: 'application/json',
dataType: 'jsonp',
jsonpCallback: "parseResponse",

data: {
viewparams: "nmindar:" + nmindar + ";plno:" + plno
}

Can any one tell me how to pass value to viewparams in geoserver, value includes comma. For example as mentioned above "100,102&103" is a single value(plot number).Here comma is not a separator.



Answer



After looking at the Geoserver docs, I realized that we need to escape all the commas and semicolons.


The Geoserver docs for SQL Views says:



If the values contain semicolons or commas these must be escaped with a backslash (e.g. \, and \;).




We can do this using the following JavaScript code:


//Function to add replaceAll to Strings
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};

function EscapeCommasSemiColons(input){
var output=input.replaceAll(",", "\\,"); //replace all the commas

output=output.replaceAll(";", "\\;"); //replace all the SemiColons
return output;
}

//Now call the data from GeoServer

$.ajax({
url: 'http://XX.168.1.XX:8089/geoserver/XX/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=XX:vwplot&maxFeatures=5000&outputFormat=text/javascript',
contentType: 'application/json',
dataType: 'jsonp',

jsonpCallback: "parseResponse",
data: {
viewparams: "nmindar:" + nmindar + ";plno:" + EscapeCommasSemiColons(plno)
}});

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...