Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Identify WMS features

This code sample is available for these platforms:
View Sample on GitHub

Identify features in a WMS layer and display the associated popup content.

screenshot

How to use the sample

Tap a feature to identify it. The HTML content associated with the feature will be displayed in a web view.

How it works

  1. A WMS layer is added via URL and a layer name.
  2. When the map is tapped, MapView.IdentifyLayerAsync is used to find matching results within the WMS layer.
  3. If there is a matching feature, the HTML property is taken via a lookup in the feature's attribute dictionary.
  4. This particular server will produce an identify result with an empty table when there is no identified feature. In all other cases, a table with an OBJECTID column is added. This sample checks for the presence of OBJECTID in the HTML, and doesn't display the result if it is missing.
  5. The HTML is displayed in the web view.

Relevant API

  • IdentifyLayerResult
  • IdentifyLayerResult.GeoElements
  • MapView.IdentifyLayerAsync
  • WmsFeature
  • WmsFeature.Attributes

About the data

This sample shows a map of surface water sources in each U.S. state. States with more surface and ground water sources appear darker blue. The attribute table includes counts of surface and ground water sources. This map service is provided by the U.S. EPA.

Tags

IdentifyLayerAsync, OGC, ShowCalloutAt, WMS, WmsLayer, callout, web map service

Sample Code

<UserControl
    x:Class="ArcGISRuntime.UWP.Samples.WmsIdentify.WmsIdentify"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esriUI="using:Esri.ArcGISRuntime.UI.Controls">
    <Grid>
        <esriUI:MapView x:Name="MyMapView" />
        <Border Style="{StaticResource BorderStyle}">
            <StackPanel>
                <TextBlock Text="Tap to identify features."
                           TextAlignment="Center"
                           FontWeight="SemiBold" />
                <WebView x:Name="ResultWebView"
                         Height="100"
                         Visibility="Collapsed" />
            </StackPanel>

        </Border>
    </Grid>
</UserControl>
// Copyright 2017 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Ogc;
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Popups;
using Windows.UI.Xaml;

namespace ArcGISRuntime.UWP.Samples.WmsIdentify
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Identify WMS features",
        "Layers",
        "This sample demonstrates how to identify WMS features and display the associated content for an identified WMS feature.",
        "Tap a feature to identify. Note: the service returns HTML regardless of whether there was an identify result. The sample uses a heuristic to hide empty results.")]
    public partial class WmsIdentify
    {
        // Create and hold the URL to the WMS service showing EPA water info.
        private readonly Uri _wmsUrl = new Uri(
            "https://watersgeo.epa.gov/arcgis/services/OWPROGRAM/SDWIS_WMERC/MapServer/WMSServer?request=GetCapabilities&service=WMS");

        // Create and hold a list of uniquely-identifying WMS layer names to display.
        private readonly List<string> _wmsLayerNames = new List<string> {"4"};

        // Hold the WMS layer.
        private WmsLayer _wmsLayer;

        public WmsIdentify()
        {
            InitializeComponent();

            // Initialize the sample.
            Initialize();
        }

        private async void Initialize()
        {
            // Apply an imagery basemap to the map.
            MyMapView.Map = new Map(Basemap.CreateImagery());

            // Create a new WMS layer displaying the specified layers from the service.
            _wmsLayer = new WmsLayer(_wmsUrl, _wmsLayerNames);

            try
            {
                // Load the layer.
                await _wmsLayer.LoadAsync();

                // Add the layer to the map.
                MyMapView.Map.OperationalLayers.Add(_wmsLayer);

                // Zoom to the layer's extent.
                MyMapView.SetViewpoint(new Viewpoint(_wmsLayer.FullExtent));

                // Subscribe to tap events - starting point for feature identification.
                MyMapView.GeoViewTapped += MyMapView_GeoViewTapped;
            }
            catch (Exception e)
            {
                await new MessageDialog(e.ToString(), "Error").ShowAsync();
            }
        }

        private async void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.UI.Controls.GeoViewInputEventArgs e)
        {
            // Clear any existing result.
            ResultWebView.Visibility = Visibility.Collapsed;

            try
            {
                // Perform the identify operation.
                IdentifyLayerResult myIdentifyResult = await MyMapView.IdentifyLayerAsync(_wmsLayer, e.Position, 20, false);

                // Return if there's nothing to show.
                if (!myIdentifyResult.GeoElements.Any())
                {
                    return;
                }

                // Retrieve the identified feature, which is always a WmsFeature for WMS layers.
                WmsFeature identifiedFeature = (WmsFeature) myIdentifyResult.GeoElements[0];

                // Retrieve the WmsFeature's HTML content.
                string htmlContent = identifiedFeature.Attributes["HTML"].ToString();

                // Note that the service returns a boilerplate HTML result if there is no feature found.
                // This test should work for most arcGIS-based WMS services, but results may vary.
                if (!htmlContent.Contains("OBJECTID"))
                {
                    // Return without showing the callout.
                    return;
                }

                // Show the result.
                ResultWebView.NavigateToString(htmlContent);
                ResultWebView.Visibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                await new MessageDialog(ex.ToString(), "Error").ShowAsync();
            }
        }
    }
}