The default GetFeatureInfo response in GeoServer churns out all the attributes of the queried feature. We’ll see how we can play around with GetFeatureInfo to craft a more elegantly styled response.
To do this, we need to play with the GetFeatureInfo template. In a nutshell, the GetFeatureInfo template is a group of files that work together in telling GeoServer what to return on receiving a GetFeatureInfo request, and in what format to return it.
The GetFeatureInfo template consists of three different files- header.ftl, content.ftl and footer.ftl. These files contain HTML markup that define the style and structure of the response. This means that you can apply all of your normal HTML/CSS styling tricks on the template to get an awesome-looking GetFeatureInfo response.
Let’s see how we can create our own, custom GetFeatureInfo response step by step. I assume that you already know how publish your data using Geoserver. We will use Twitter Boostrap for the styling.
- Go to the folder where Geoserver is installed. Navigate to data>workspaces>[WORKSPACE]>[STORE]>[LAYER]. You should see two files listed: featuretype.xml and layer.xml.
- Create three new files and name them header.ftl, content.ftl and footer.ftl in this folder.
- Open header.ftl. This file should contain the , ,<!–
and the opening tags. Basically, all the tags that lie semantically above the tag in an HTML file would be contained in this file. You can import stylesheets for your template here.
- We import our stylesheets and any external references. In this case, we will import the Bootstrap css for styling (the link should point to the location where you have installed Bootstrap on your server):
<span class="hiddenSpellError" pre="">Geoserver</span> GetFeatureInfo output
<link href="http://localhost/bootstrap/css/bootstrap.min.css" rel="stylesheet">
- Now open content.ftl. This file is where we will code the markup for the response body. By default, GetFeatureInfo detches all of the attributes of a particular feature. We will format the response so that only the attributes
scalerankare returned. We will format this response as a description list rather than a table with the tags
<dd>. To do this, we will modify content.ftl with this markup:
<#list features as feature>
#list features as featurelists all of the features by each feature and returns only the specified attributes.
<dd>are used to construct the description list. the
text-infoclass is a Boostrap class which styles the description.
- Finally, open footer.ftl. This file only contains closing tags at the end of the markup. The footer.ftl tag will contain this code:
Your GetFeatureOutput should look something like this:
Congratulations! you have successfully crafted your custom Geoserver response. You can see that this response is much more readable and humanized than the default response.
You can read more about GeoServer templates here: http://docs.geoserver.org/latest/en/user/tutorials/GetFeatureInfo/index.html