Accessing Map informationΒΆ

This workshop section describes how to use the GeoServer template system to create custom HTML GetFeatureInfo responses. GetFeatureInfo is a WMS standard call that allows one to retrieve information about features and coverages displayed in a map.

The map can be composed of various layers, and GetFeatureInfo can be instructed to return multiple feature descriptions, which may be of different types. GetFeatureInfo can generate output in various formats: GML2, plain text and HTML.

Templating is concerned with the HTML one.

  1. Go to the Layer preview to show geosolutions:bplandmarks layer.

  2. Click for example on the Rocky Mountain Natl Park region in the OpenLayers map to show the FeatureInfo.

    ../_images/info1.png

    Default GetFeatureInfo request

  3. In order to configure a custom template of the GetFeatureInfo results create three files .FTL in $GEOSERVER_DATA_DIR/workspaces/geosolutions directory named:

    - header.ftl
    - content.ftl
    - footer.ftl
    

    Note

    The Template is managed using Freemarker. This is a simple yet powerful template engine that GeoServer uses whenever developers allowed user customization of textual outputs. In particular, at the time of writing it’s used to allow customization of GetFeatureInfo, GeoRSS and KML outputs.

    Note

    Splitting the template in three files allows the administrator to keep a consistent styling for the GetFeatureInfo result, but use different templates for different workspaces or different layers: this is done by providing a master header.ftl and footer.ftl file, but specify a different content.ftl for each layer.

  4. In header.ftl file enter the following HTML:

    <#--
    Header section of the GetFeatureInfo HTML output. Should have the <head> section, and
    a starter of the <body>. It is advised that eventual CSS uses a special class for featureInfo,
    since the generated HTML may blend with another page changing its aspect when using generic classes
    like td, tr, and so on.
    -->
    <html>
            <head>
                    <title>Geoserver GetFeatureInfo output</title>
            </head>
            <style type="text/css">
                    table.featureInfo, table.featureInfo td, table.featureInfo th {
                            border:1px solid #ddd;
                            border-collapse:collapse;
                            margin:0;
                            padding:0;
                            font-size: 90%;
                            padding:.2em .1em;
                    }
    
                    table.featureInfo th{
                            padding:.2em .2em;
                            text-transform:uppercase;
                            font-weight:bold;
                            background:#eee;
                    }
    
                    table.featureInfo td{
                            background:#fff;
                    }
    
                    table.featureInfo tr.odd td{
                            background:#eee;
                    }
    
                    table.featureInfo caption{
                            text-align:left;
                            font-size:100%;
                            font-weight:bold;
                            text-transform:uppercase;
                            padding:.2em .2em;
                    }
            </style>
            <body>
    
  5. In content.ftl file enter the following HMTL:

    <ul>
    <#list features as feature>
            <li><b>Type: ${type.name}</b> (id: <em>${feature.fid}</em>):
            <ul>
            <#list feature.attributes as attribute>
                    <#if !attribute.isGeometry>
                            <li>${attribute.name}: ${attribute.value}</li>
                    </#if>
            </#list>
            </ul>
            </li>
    </#list>
    </ul>
    
  6. In footer.ftl file enter the following HMTL:

    <#--
    Footer section of the GetFeatureInfo HTML output. Should close the body and the html tag.
    -->
            </body>
    </html>
    
  7. Go to the Map Preview to show geosolutions:bplandmarks layer.

  8. Click on the Rocky Mountain Natl Park region in the OpenLayers map to show the new FeatureInfo representation.

    ../_images/info2.png

    Custom GetFeatureInfo template