7

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.

19

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.

2

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

  • 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
0

I made this code for json ftl template, based in a post:

This is the code for header.ftl

[

This is the code for content.ftl:

{
"layer": "${type.name}",
"features": 
[
    <#list features as feature>
    {
        <#list feature.attributes as attribute>
            <#if !attribute.isGeometry>
                "${attribute.name?capitalize?js_string}": "${attribute.value?js_string}"<#if attribute_has_next>,</#if>
            </#if>
        </#list>
    }
    <#if feature_has_next>,</#if>
    </#list>
]
},

And this is for footer.ftl:

]

That is it! The parsers will ignore the last comma in every last json object.

It work's for me.

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.