Styling a WMS GetFeatureInfo response with ArcGIS Server 10

The GetFeatureInfo operation on a WMS is designed to return attributes of features in the map. The WMS spec doesn’t really define the schema for the GetFeatureInfo response, so you’ll see it handled differently as you look at various WMS implementations. With an ArcGIS Server WMS, you can request the feature info in various formats, including HTML, XML, and plain text.

For example, here’s a GetFeatureInfo request along with its response in the default HTML format:

Request

http://localhost:8399/arcgis/services/ihs_petroleum/MapServer/WMSServer?&service=WMS&version=1.1.0&request=GetFeatureInfo&layers=fields&query_layers=fields&styles=&bbox=47.130647,8.931116,48.604188,29.54223&srs=EPSG:4326&feature_count=10&x=562&y=193&height=445&width=1073&info_format=text/html&

Response

Default GetFeatureInfo response

In many cases, the default response in HTML, XML, or plain text may be good enough for your own purposes, but there may be times when you want to customize the response format or schema. Perhaps you want your own look and feel to the response, or for interoperability reasons maybe you want to get the feature information back in a standard schema like GML or GeoJSON.

In ArcGIS Server 10, both server administrators and WMS clients can customize the GetFeatureInfo response using XSLT templates. This blog post explains the basic steps.

Background

When you work with a WMS created by ArcGIS Server, the GetFeatureInfo response is generated by applying an XSLT template on a raw internal XML format. This internal XML just contains a list of fields and values of identified features. A default XSLT template for the particular format (HTML, text, etc.) is applied to make the final response look readable.

Knowing this workflow, you can customize both the format and schema of the GetFeatureInfo response. You need to understand:

  • The raw internal XML format of the ArcGIS Server WMS GetFeatureInfo response
  • The default XSLT templates that come with ArcGIS Server for the different types of supported formats

Let’s tackle these one at a time.

The raw internal XML format

Here’s an example of the raw internal XML GetFeatureInfo response from an ArcGIS Server 10.0-produced WMS:

<?xml version="1.0" encoding="UTF-8"?>
<esri_wms:FeatureInfoResponse version="1.3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:esri_wms="http://www.esri.com/wms" xmlns="http://www.esri.com/wms">
  <esri_wms:FeatureInfoCollection layername="fields">
    <esri_wms:FeatureInfo>
      <esri_wms:Field>
        <esri_wms:FieldName><![CDATA[OBJECTID]]></esri_wms:FieldName>
        <esri_wms:FieldValue><![CDATA[1]]></esri_wms:FieldValue>
      </esri_wms:Field>
      <esri_wms:Field>
        <esri_wms:FieldName><![CDATA[Shape]]></esri_wms:FieldName>
        <esri_wms:FieldValue><![CDATA[Polygon]]></esri_wms:FieldValue>
      </esri_wms:Field>
      <esri_wms:Field>
        <esri_wms:FieldName><![CDATA[Shape_Area]]></esri_wms:FieldName>
        <esri_wms:FieldValue><![CDATA[0.009079]]></esri_wms:FieldValue>
      </esri_wms:Field>
        ...
        <!-- there could be more <esri_wms:Field> -->
        ...
    </esri_wms:FeatureInfo>
    ...
    <!-- there could be more <esri_wms:FeatureInfo> -->
    ...
  </esri_wms:FeatureInfoCollection>
  ...
  <!-- there could be more <esri_wms:FeatureInfoCollection> -->
  ...
</esri_wms:FeatureInfoResponse>

Notice that:

  • The root tag <FeatureInfoResponse> can contain multiple <FeatureInfoCollection> elements.
  • Each <FeatureInfoCollection> element contains the attribute fields and values of all identified features from a single WMS layer.
  • The information for a single identified feature is contained in a <FeatureInfo> tag. Notice the name-value pair for each field.

Since the whole idea of XSLT transformation involves converting one format of information to another, it’s really important to take some time to understand the above format before you continue.

Default XSLT templates in ArcGIS Server

The ArcGIS Server 10.0 installation comes with XSLT templates for the supported formats listed in the WMS’s capabilities files. You can see these default templates if you open <ArcGIS Server install location>StylesWMS. As indicated by their names, each template is used to produce the default GetFeatureInfo response in a given format. In example at the beginning of this post, the HTML table with the blue caption was produced thanks to the default XSLT for the HTML format.

XSLT templates in ArcGIS Server 10

If you ever need to get the raw XML as the response, you can set the GetFeatureInfo request parameter INFO_FORMAT to application/vnd.esri.wms_raw_xml. You might do this if you wanted to create your own template and you needed some understanding of the raw format.

How to customize your WMS GetFeatureInfo response

Given all of the information above, let’s explore a couple of ways you can customize the WMS GetFeatureInfo response in ArcGIS Server 10.0.

Modify the default XSLT templates in ArcGIS Server

You’ve seen where the default XSLT templates live, and one option is to just open them up and start tweaking the styles to fit your liking. Remember, though, that if you change these templates, it will affect any WMS published through ArcGIS Server. For this reason, avoid trying to put any map-service-specific logic into the templates.

Here’s an example change to featureinfo_text_html.xsl, the HTML template. If you opened the file in a text editor and replaced the <Style> tag with the following, you would see the table caption color change to red.

  <style type="text/css">
          table, th, td {
            border:1px solid #e5e5e5;
            border-collapse:collapse;
            font-family: arial;
            font-size: 80%;
            color: #333333
          }
          th, td {
            valign: top;
            text-align: center;
          }
          th {
            background-color: #ffb7b7
          }
          caption {
            border:1px solid #e5e5e5;
            border-collapse:collapse;
            font-family: arial;
            font-weight: bold;
            font-size: 80%;
            text-align: left;
            color: #333333;
          }
        </style>

An example response using this template would look like this:

Default GetFeatureInfo response customized with red 

Override the behavior of the default XSLT template using the xslt_template parameter

xslt_template is an Esri-specific parameter that you can set to the URL of an XSLT template file. The ArcGIS Server-produced WMS will then pick up the template and override the default template.

When using the xslt_template parameter, your XSLT template doesn’t have to follow the same naming convention as the default templates, but it does need to be available through a URL; a local path or UNC path won’t work.

Here’s an example of a GetFeatureInfo request with the xslt_template parameter:

http://localhost:8399/arcgis/services/ihs_petroleum/MapServer/WMSServer?&service=WMS&version=1.1.1&request=GetFeatureInfo&layers=pipelines&query_layers=pipelines&styles=&bbox=47.119661,28.931116,48.593202,29.54223&srs=EPSG:4326&feature_count=10&x=389&y=120&height=445&width=1073&info_format=text/plain&xsl_template=http://server:8080/dev-summit-2010/resource/xsl/featureinfo_application_geojson.xsl

The above URL references an external template that overrides the default template. The customized template looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:esri_wms="http://www.esri.com/wms" xmlns="http://www.esri.com/wms">
  <xsl:output
    method="text"
    indent="yes"
    encoding="ISO-8859-1"/>

  <xsl:template match="/">{
  "type": "FeatureCollection",
  "features": [<xsl:for-each select="esri_wms:FeatureInfoResponse/esri_wms:FeatureInfoCollection/esri_wms:FeatureInfo">
    {
      "type": "Feature",
      "properties":
        {<xsl:for-each select="esri_wms:Field">
          "<xsl:value-of select="esri_wms:FieldName"/>":"<xsl:value-of select="esri_wms:FieldValue"/>",</xsl:for-each>
        },
      "layerName":"<xsl:value-of select="../@layername"/>"
    },</xsl:for-each>
  ]
}
  </xsl:template>
</xsl:stylesheet>

The above template has been created to make the WMS return its GetFeatureInfo responses in plain text instead of the default HTML format. In fact, here this plain text is styled in the popular GeoJSON format. GeoJSON can be parsed by many JavaScript libraries, allowing you to integrate your responses with your Web pages in an easy-to-read format.

Here’s an example of using a GetFeatureInfo GeoJSON response as a data source of an Ext.Grid in an OpenLayers Web Map application:

GeoJSON response in a grid 

Click here (or right-click the link and save) to see the code for an example template that could be used to embed a video object in the response.

Embedded video in the response 

Click here (or right-click the link and save) to see the code for a more comprehensive example that heavily customizes the GetFeatureInfo HTML response to embed a JavaScript code snippet. The code plays a tour in the Google Earth plug-in Web application.

Notice that in this XSLT template, there is logic specific to a particular WMS service. Therefore, this template should always be referenced through the xslt_template parameter; it should not be made the default template.

Contributed by Yingqi Tang of the ArcGIS Server development team

This entry was posted in Services and tagged , , . Bookmark the permalink.

Leave a Reply

2 Comments

  1. dsierra says:

    Is it possible to do something like this in ArcGIS Server 9.3.1. somehow? Thanks in advance

  2. anakintang says:

    For ArcGIS Server 9.3.1 WMS, no, because this is a new feature in 10.0. The closest feature in 9.3.1 is the html popup for a map service, which you can also customize using xslt template.