Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I wish to get the results of a WMS GetFeatureInfo request as a JSON instead of the table that comes up by default. Out of the box, the only options for the response from a WMS GetFeatureInfo are the table, XML and text.

share|improve this question

2 Answers 2

up vote 16 down vote accepted

Update The 'hack' given below is necessary only for Geoserver versions 2.2.x and below. With version 2.3.0 and upwards, Geoserver can return JSON for WMS GetFeatureInfo request natively.

End of Update


There is an open feature request for this on geoserver's bug tracker.

Using custom GetFeatureInfo Templates, it is possible to generate a Json response from Geoserver.

You can follow these steps:

1]Go to the following folder

GEOSERVER_DATA_DIR/workspaces/{workspace}/{datastore}/{featuretype}/

2] Create 3 empty text files with the following names: header.ftl, content.ftl & footer.ftl

3] Open the content.ftl file, and copy the following free-marker code:

[
<#list features as feature>
{ "Type": "${type.name}"
<#list feature.attributes as attribute>
    <#if !attribute.isGeometry>
    , "${attribute.name}": "${attribute.value}"
    </#if>
</#list>
}
</#list> ,{}
]

4] Save the file and now make the WMS getFeatureInfo request. You'll see that the response is in JSON.

Note: You need to remember that this will wrap the numeric values in inverted commas as a string. Hence you might have to take care of it in the client code.

share|improve this answer
    
Nice explanation & it works for me too : ) thanks –  Sunil Dec 26 '12 at 11:53

This capability now exists in GeoServer 2.2.4.

Here's how to request JSON output:

http://docs.geoserver.org/stable/en/user/services/wms/reference.html#getfeatureinfo

share|improve this answer
    
Thanks! It's good to know that this is finally available. Unfortunately I am still on an older version, and will be at that for the foreseeable future. –  Devdatta Tengshe Feb 1 '13 at 3:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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