The OGC WMS and WMTS standards define a GetFeatureInfo operation that lets you query the server for properties of features at a particular location. This is useful for identifying what’s displayed at a given pixel, for example when the user clicks on the map.
What is GetFeatureInfo?
A WMS or WMTS server renders map imagery on the server side. Because the client receives only raster tiles, it doesn’t know which features are present in the image. The GetFeatureInfo operation solves this: you send a location to the server, and the server returns the properties of the features at that location.
Typical responses are encoded as Geography Markup Language(GML) or GeoJSON, which LuciadCPillar can decode into an IFeatureModelIFeatureModelIFeatureModel.
|
Not every WMS or WMTS server advertises GetFeatureInfo support.
You can check whether the server supports it by inspecting the capabilities document.
For WMS, use |
How to use GetFeatureInfo
Using GetFeatureInfo involves three steps:
-
Obtain a context from the layer for the location of interest.
-
Send the request to the server via the model.
-
Decode the response to extract features.
Using GetFeatureInfo with a WMS server
The entry points are:
-
WmsRasterLayer::getFeatureInfoContextWmsRasterLayer::getFeatureInfoContextWmsRasterLayer::getFeatureInfoContext— creates the context from a map point. -
IWmsModel::getFeatureInfoIWmsModel::getFeatureInfoIWmsModel::getFeatureInfo— executes the request. -
WmsGetFeatureInfoOptionsWmsGetFeatureInfoOptionsWmsGetFeatureInfoOptions— optionally configures the request (response format, maximum feature count, layer filter).
To create a layer that supports GetFeatureInfo, use WmsRasterLayer::newWmsBuilderWmsRasterLayer::newWmsBuilderWmsRasterLayer::newWmsBuilder instead of RasterLayer::newBuilder.
Only layers created with this builder expose the getFeatureInfoContext method.
// Obtain a context for the point of interest. This must happen on the UI thread.
auto point = Point{CoordinateReferenceUtil::geodetic(), Coordinate{-113, 42}};
auto infoContext = wmsLayer->getFeatureInfoContext(point);
if (infoContext.has_value()) {
// Perform the GetFeatureInfo request. This is a blocking network call and
// should run on a background thread in a real application.
auto featureInfoResult = wmsModel->getFeatureInfo(*infoContext, WmsGetFeatureInfoOptions::newBuilder().format("application/json").build());
if (featureInfoResult.has_value()) {
// Decode the GeoJson response into a feature model.
auto inMemoryDataLoader = std::make_shared<InMemoryDataLoader>(featureInfoResult->getData());
auto options = GeoJsonModelDecoderOptions::newBuilder().dataLoader(inMemoryDataLoader).build();
auto featureModel = GeoJsonModelDecoder::decode("wms-getFeatureInfo-reponse", options);
if (featureModel.has_value()) {
// Use the feature model, e.g. display the features on the map.
}
}
}
// Obtain a context for the point of interest. This must happen on the UI thread.
var point = new Point(reference, new Coordinate(-113, 42));
var infoContext = rasterLayer.GetFeatureInfoContext(point);
if (infoContext != null)
{
// Perform the GetFeatureInfo request. This is a blocking network call and
// should run on a background thread in a real application.
var dataEntity = model.GetFeatureInfo(infoContext);
// Decode the GeoJson response into a feature model.
var loader = new InMemoryDataLoader(dataEntity.Data, dataEntity.MimeType);
var options = GeoJsonModelDecoderOptions.NewBuilder().DataLoader(loader).Build();
var featureModel = GeoJsonModelDecoder.Decode("get-feature-info-response", options);
// Use the feature model, e.g. display the features on the map.
}
// Obtain a context for the point of interest. This must happen on the UI thread.
var point = new Point(reference, new Coordinate(-113, 42));
var infoContext = rasterLayer.getFeatureInfoContext(point);
if (infoContext != null) {
try {
// Perform the GetFeatureInfo request. This is a blocking network call and
// should run on a background thread in a real application.
DataEntity dataEntity = model.getFeatureInfo(infoContext);
// Decode the GeoJson response into a feature model.
InMemoryDataLoader loader = new InMemoryDataLoader(dataEntity.getData(), dataEntity.getMimeType());
GeoJsonModelDecoderOptions options = GeoJsonModelDecoderOptions.newBuilder().dataLoader(loader).build();
IFeatureModel featureModel = GeoJsonModelDecoder.decode("get-feature-info-response", options);
// Use the feature model, e.g. display the features on the map.
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Using GetFeatureInfo with a WMTS server
The WMTS workflow is nearly identical to the WMS workflow. The entry points are:
-
WmtsRasterLayer::getFeatureInfoContextWmtsRasterLayer::getFeatureInfoContextWmtsRasterLayer::getFeatureInfoContext— creates the context from a map point. -
IWmtsModel::getFeatureInfoIWmtsModel::getFeatureInfoIWmtsModel::getFeatureInfo— executes the request. -
WmtsGetFeatureInfoOptionsWmtsGetFeatureInfoOptionsWmtsGetFeatureInfoOptions— optionally configures the response format.
When you create the WMTS layer, use WmtsRasterLayer::newWmtsBuilderWmtsRasterLayer::newWmtsBuilderWmtsRasterLayer::newWmtsBuilder instead of RasterLayer::newBuilder.
// Obtain a context for the point of interest. This must happen on the UI thread.
auto point = Point{CoordinateReferenceUtil::geodetic(), Coordinate{-152, 64}};
auto infoContext = wmtsLayer->getFeatureInfoContext(point);
if (infoContext.has_value()) {
// Perform the GetFeatureInfo request. This is a blocking network call and
// should run on a background thread in a real application.
auto featureInfoResult = wmtsModel->getFeatureInfo(*infoContext);
if (featureInfoResult.has_value()) {
// Decode the GeoJson response into a feature model.
auto inMemoryDataLoader = std::make_shared<InMemoryDataLoader>(featureInfoResult->getData());
auto options = GeoJsonModelDecoderOptions::newBuilder().dataLoader(inMemoryDataLoader).build();
auto featureModel = GeoJsonModelDecoder::decode("wmts_getfeatureinfo_response.json", options);
if (featureModel.has_value()) {
// Use the feature model, e.g. display the features on the map.
}
}
}
// Obtain a context for the point of interest. This must happen on the UI thread.
var point = new Point(reference, new Coordinate(-152, 64));
var infoContext = rasterLayer.GetFeatureInfoContext(point);
if (infoContext != null)
{
// Perform the GetFeatureInfo request. This is a blocking network call and
// should run on a background thread in a real application.
var dataEntity = model.GetFeatureInfo(infoContext);
// Decode the GeoJson response into a feature model.
var loader = new InMemoryDataLoader(dataEntity.Data, dataEntity.MimeType);
var options = GeoJsonModelDecoderOptions.NewBuilder().DataLoader(loader).Build();
var featureModel = GeoJsonModelDecoder.Decode("get-feature-info-response", options);
// Use the feature model, e.g. display the features on the map.
}
// Obtain a context for the point of interest. This must happen on the UI thread.
var point = new Point(reference, new Coordinate(-152, 64));
var infoContext = rasterLayer.getFeatureInfoContext(point);
if (infoContext != null) {
try {
// Perform the GetFeatureInfo request. This is a blocking network call and
// should run on a background thread in a real application.
DataEntity dataEntity = model.getFeatureInfo(infoContext);
// Decode the GeoJson response into a feature model.
InMemoryDataLoader loader = new InMemoryDataLoader(dataEntity.getData(), dataEntity.getMimeType());
GeoJsonModelDecoderOptions options = GeoJsonModelDecoderOptions.newBuilder().dataLoader(loader).build();
IFeatureModel featureModel = GeoJsonModelDecoder.decode("get-feature-info-response", options);
// Use the feature model, e.g. display the features on the map.
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Configuring the request
Setting WMS options
You can customize a WMS GetFeatureInfo request through WmsGetFeatureInfoOptionsWmsGetFeatureInfoOptionsWmsGetFeatureInfoOptions:
-
Response format — set the MIME type of the response. The default MIME type is
text/xml. The available formats can be discovered throughWmsCapabilities::getInfoFormatsWmsCapabilities::getInfoFormatsWmsCapabilities::getInfoFormats. -
Maximum feature count — limit the number of features returned. The default number is 1.
-
Layer filter — restrict the query to specific WMS layer names. If you don’t set this filter, the layers from the model are used.
Setting WMTS options
WmtsGetFeatureInfoOptionsWmtsGetFeatureInfoOptionsWmtsGetFeatureInfoOptions allows setting the response format only.
The WMTS standard doesn’t define a parameter for maximum feature count.
Threading considerations
GetFeatureInfo involves a network round-trip. It’s recommended to call it on a background thread, and not on the UI thread.
However, getFeatureInfoContext reads data from the layer’s render state and must be called on the UI thread.
A typical solution is:
-
On the UI thread: call
getFeatureInfoContextto create the context. -
On a background thread: call
getFeatureInfoand decode the response. -
On the UI thread: display the resulting features on the map.
The sample code base includes a GetFeatureInfoEventHandler that implements this pattern using proper multi-threading.
In the samples, GetFeatureInfo is triggered by a click on the map and the result features are automatically selected, providing
visual feedback.
Decoding the response
The default response format is text/xml, which typically returns a GML document.
You can decode this into an IFeatureModelIFeatureModelIFeatureModel using GmlModelDecoder::decodeGmlModelDecoder::decodeGmlModelDecoder::decode with a GmlDataSourceGmlDataSourceGmlDataSource and GmlModelDecoder::OptionsGmlModelDecoder::OptionsGmlModelDecoder::Options. The decoder options contain an InMemoryDataLoaderInMemoryDataLoaderInMemoryDataLoader
that wraps the raw response bytes. See Program: performing a WMS GetFeatureInfo request and Program: performing a WMTS GetFeatureInfo request for an illustration.
See the GML data format tutorial for more information about GML decoding.
Sample code
The C++, C# and Android sample applications demonstrate a complete GetFeatureInfo integration.
The GetFeatureInfoEventHandler class:
-
Tries all visible WMS and WMTS layers from top to bottom.
-
Runs the network request and GML decoding on a background thread.
-
Adds the result as a
FeatureLayerFeatureLayerFeatureLayerto the controller’sLayerListLayerListLayerList. -
Automatically selects the returned features for visual feedback.
-
Removes the result layer when the user selects a different feature or the source layer becomes invisible.
Overall considerations
-
Server support required — GetFeatureInfo is an optional operation. Not all WMS and WMTS servers support it. If the server doesn’t advertise GetFeatureInfo in its capabilities, the
getFeatureInfocall returns an error. -
getFeatureInfoContextrequires a rendered layer — the context is derived from the layer’s last render pass. If the layer hasn’t been rendered yet, or the point falls outside the rendered area,getFeatureInfoContextreturnsnull. -
Response format depends on the server — while
text/xml(GML) is the default, not every server returns GML. Servers may return plain text, HTML, JSON, or other formats. You can decode only GML responses withGmlModelDecoderGmlModelDecoderGmlModelDecoder. Check the available formats in the capabilities before making a request. -
GML compatibility — some servers return GML that isn’t directly decodable through
GmlModelDecoderGmlModelDecoderGmlModelDecoder, for example due to missing namespace declarations. In such cases, custom preprocessing of the XML response may be needed. -
Use typed layer builders — only
WmsRasterLayerWmsRasterLayerWmsRasterLayer, created throughWmsRasterLayer::newWmsBuilder, andWmtsRasterLayerWmtsRasterLayerWmtsRasterLayer, created throughWmtsRasterLayer::newWmtsBuilder,` support GetFeatureInfo. Layers created with the genericRasterLayer::newBuilderdon’t expose thegetFeatureInfoContextmethod.