I use Mapserver with the Leaflet JS library and I want to display some attributes informations of a raster file by clicking on the map.
Therefore I use the GetFeatureInfo WMS request in order to return these informations.
I managed to get the infos with the text/html format but I'd like to get a JSON response back in my browser, e.g :
{
"value": 14.5, // I must precise I use GetFeatureInfo on a raster and the value result corresponds to the value of the pixel
}
BELOW ARE THE UPDATES :
So I changed the wms_get_feature_info_formalist in the METADATA bloc of my mapfile :
"wms_getfeatureinfo_formatlist" "application/json"
Then, I edited my template as following to return the value of the pixel :
<!-- MapServer Template -->
{
"value": [value_0] // [value_0] allow the user to get the value of a pixel
}
I edited the mapfile with the OUTPUT format parameter in order to use the JSON format :
OUTPUTFORMAT
NAME "json"
MIMETYPE "application/json"
DRIVER "TEMPLATE"
FORMATOPTION "FILE=template.js"
END
And as it is written in the docs, I set :
TEMPLATE "EMPTY"
My GetFeatureInfo request is the one (see "application/json" specified in the INFO_FORMAT
parameter)
http://IP/cgi-bin/mapserv?map=/var/www/html/mapfiles/test1.map&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&BBOX=465348.6282001531,5431156.607774916,697564.3201305186,5503160.28841955&SRS=EPSG:3857&WIDTH=1519&HEIGHT=471&LAYERS=layer1&STYLES=&FORMAT=image/png&TRANSPARENT=true&QUERY_LAYERS=layer1&INFO_FORMAT=application/json&X=766&Y=261
But when I try the GetFeatureInfo request, my browser returns a JSON response but with the following error message :
SyntaxError: JSON.parse: unexpected character at line 2 column 11 of the JSON data
It seems the [value_0]
is not recognized in JSON whereas it works well with the text/html format..
How could I do to make it work ?
I'm really stuck and any help would be greatly appreciated !
"value":
? I did not mean that you should use the GeoJSON template as is, but to read it as an example of a working json template. What a browser does for downloaded data depends on how it is configured. If you change the mimetype into application/json it is possible that your browser desides to show data as text.