Click or drag to resize
Code Example - LayerDefinitions

Demonstrates setting numerous LayerDefinitions values on an ArcGISDynamicMapServiceLayer and visualizing the impact of the features being returned in the Map.

Code Example
Layer Definitions

The following screenshot provides a collage of all the various LayerDefinitions options for the ArcGISDynamicMapServiceLayer showing earthquakes since 1970.

Layer Definitions 2

This section contains selected code files from a Visual Studio project that emphasize specific ArcGIS Runtime SDK (Windows Desktop) features. For example: some code examples may accomplish the bulk of the work as a configuration property in the .xaml file and hence only the .xaml file will be shown in detail below. In other code examples, the .xaml is used to define the configuration of graphical elements for the application but the application logic is performed in the code behind, hence you may see both the .xaml and .cs/.vb files shown in detail below.

XAML
<Window x:Class="LayerDefinitions.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
    Title="MainWindow" Height="600" Width="800">
    <Grid x:Name="LayoutRoot">

        <!-- Add a MapView Control. -->
        <esri:MapView x:Name="MapView1" Background="White" WrapAround="True" Margin="0,75,0,0" HorizontalAlignment="Left" 
                VerticalAlignment="Top" Height="338" Width="782">

            <!-- Add a Map. -->
            <esri:Map x:Name="Map1">

                <!-- Add a background ArcGISDynamicMapServiceLayer. -->
                <esri:ArcGISDynamicMapServiceLayer ID="ESRI_StreetMap_World_2D" ImageFormat="PNG24"
                    ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer" />

                <!-- Add another ArcGISDynamicMapServiceLayer. This will be the one that has various LayerDefinitions
                set. By default no LayerDefinitions are set unless explicitly set on the server. -->
                <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes" ImageFormat="PNG24"
                    ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer" />

                <!-- 
                ============================================================================================================
                The following are some XAML examples of various ArcGISDynamicLayer.LayerDefinitions that can be set via XAML
                NOTE: LayerID="0" is the Earthquakes1970 sub-layer: 
                ============================================================================================================
                -->

                <!-- The Definition only displays earthquakes that have a Magnitude greater than 6. The field Magnitude is of 
                type esriFieldTypeDouble. 750 records returned. -->
                <!--
                <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
                      ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
                    <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                        <esri:LayerDefinition LayerID="0" Definition="Magnitude > 6" />
                    </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                </esri:ArcGISDynamicMapServiceLayer>
                -->


                <!-- The Definition only displays earthquakes that have a Magnitude greater than 3 and less than 6. The field 
                Magnitude is of type esriFieldTypeDouble. 662 records returned. -->
                <!--
                <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
                     ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
                    <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                        <esri:LayerDefinition LayerID="0" Definition="Magnitude > 3 AND Magnitude &lt; 6" />
                    </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                </esri:ArcGISDynamicMapServiceLayer>
                -->


                <!-- The Definition only displays earthquake events where the Name of the earthquake contains the letters 'CHINA'. 
                Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString. 132 records returned. -->
                <!--
                <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
                    ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
                    <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                        <esri:LayerDefinition LayerID="0" Definition="Name LIKE '%CHINA%'" />
                    </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                </esri:ArcGISDynamicMapServiceLayer>
                -->


                <!-- The Definition only displays earthquakes where the Name of the earthquake exactly matches the letters 'VENEZUELA'. 
                Note: the Definition is case sensitive. The field Name is of type esriFieldTypeString. 3 records returned.-->
                <!--
                <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
                    ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
                    <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                        <esri:LayerDefinition LayerID="0" Definition="Name = 'VENEZUELA'" />
                    </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                </esri:ArcGISDynamicMapServiceLayer>
                -->


                <!-- The Definition only displays earthquakes if they occurred after January 15, 2000. The field Date_ is of type 
                esriFieldTypeDate. 540 records returned. -->
                <!--
                <esri:ArcGISDynamicMapServiceLayer ID="Earthquakes"
                    ServiceUri="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer">
                    <esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                        <esri:LayerDefinition LayerID="0" Definition="Date_ > DATE '1/15/2000'" />
                    </esri:ArcGISDynamicMapServiceLayer.LayerDefinitions>
                </esri:ArcGISDynamicMapServiceLayer>-->

            </esri:Map>
        </esri:MapView>
        <!-- Add controls for the user to interact with and see the effect of choosing different syntaxes for setting 
        the ArcGISDynamicMapServiceLayer.LayerDefinitions. -->
        <ListBox Height="152" HorizontalAlignment="Left" Margin="0,418,0,0" Name="ListBox_LayerDefinitions" 
           VerticalAlignment="Top" Width="381" />
        <Label Height="28" Margin="398,531,279,0" Name="Label1" VerticalAlignment="Top" Content="Records Returned:"/>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="518,533,0,0" Name="TextBlock_RecordsReturned" 
             VerticalAlignment="Top" Width="264" />
        <Button Content="Apply LayerDefinitions" Height="105" HorizontalAlignment="Left" Margin="386,418,0,0" 
          Name="Button_LayerDefinitions" VerticalAlignment="Top" Width="396" Click="Button_LayerDefinitions_Click"/>

        <!-- TextBlock to hold the instructions on how to use the sample code. -->
        <TextBlock Height="70" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="782" 
             TextWrapping="Wrap" />
    </Grid>

</Window>

SPECIAL NOTE: The XAML displayed above comes from a C# project. If you are a VB.NET developer, you will need to modify the text for the x:Class namespace from "LayerDefinitions.MainWindow" to be just "MainWindow".

namespace LayerDefinitions
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add any initialization after the InitializeComponent() call.

            // Add instructions on how to use the sample.  
            string instructionsText = "When the application loads, choose a specific LayerDefinition syntax in " + 
                "the ListBox and then click the Button. The Map will redraw with the LayerDefinitions applied to the " +
                "ArcGISDynamicMapServiceLayer. A count of the features (via a QueryTask) returned will also be displayed.";

            TextBlock1.Text = instructionsText;

            // When the application starts add several strings that will be used for the LayerDefinition syntax  into 
            // the ListBox.
            ListBox_LayerDefinitions.Items.Add("[NO DEFINITION SET]");
            ListBox_LayerDefinitions.Items.Add("Magnitude > 6");
            ListBox_LayerDefinitions.Items.Add("Magnitude > 3 AND Magnitude < 6");
            ListBox_LayerDefinitions.Items.Add("Name LIKE '%CHINA%'");
            ListBox_LayerDefinitions.Items.Add("Name = 'VENEZUELA'");
            ListBox_LayerDefinitions.Items.Add("Date_ > DATE '1/15/2000'");

            // Choose the first string in the ListBox.
            ListBox_LayerDefinitions.SelectedIndex = 0;
        }

        private void Button_LayerDefinitions_Click(object sender, System.Windows.RoutedEventArgs e)
        {

            // Get the second layer in the LayerInfo collection. Cast it to an ArcGISDynamicMapServiceLayer. 
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
            myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(MapView1.Map.Layers[1]);

            // Set the .LayerDefinition for the .LayerID = 0 (the Earthquakes1970 sub-layer). By default no LayerDefinition 
            // is set unless explicitly set on the server.
            Esri.ArcGISRuntime.Layers.LayerDefinition myDefinition = new Esri.ArcGISRuntime.Layers.LayerDefinition();
            myDefinition.LayerID = 0;

            // Get the string that will be used for the ArcGISDynamicMapServiceLayer.LayerDefinitions Property from the ListBox.
            string myUserChoice = ListBox_LayerDefinitions.SelectedItem.ToString();

            // Set theUserChoice string to "1=1" if they choose the first option in the ListBox. NOTE: This will cause 
            // ArcGIS Server to return all features for the ArcGISDynamicMapServiceLayer.
            if (myUserChoice == "[NO DEFINITION SET]")
            {
                // Not setting a LayerDefinition is the same as returning all records!
                myUserChoice = "1=1";
            }

            // Set the ArcGISDynamicMapServiceLayer.Definition to the string the user chose in the ListBox.
            myDefinition.Definition = myUserChoice;

            // Create an ObservableCollection and add the .Definition to it.
            System.Collections.ObjectModel.ObservableCollection<Esri.ArcGISRuntime.Layers.LayerDefinition> myObservableCollection = new System.Collections.ObjectModel.ObservableCollection<Esri.ArcGISRuntime.Layers.LayerDefinition>();
            myObservableCollection.Add(myDefinition);

            // Apply the custom LayerDefinition to the LayerDefinitions ObservableCollection. This will cause the Map to 
            // refresh the display the visual representation of the ArcGISDynamicMapServiceLayer.
            myArcGISDynamicMapServiceLayer.LayerDefinitions = myObservableCollection;

            // Call the function that will display the number of features returned (via a QueryTask). This does nothing to
            // what is shown in the Map. It just gives a count of the features returned based upon the same string that
            // was used in the LayerDefinition.
            GetFeatureCount(myUserChoice);

        }

        private async void GetFeatureCount(string sqlQuery)
        {

            // This function approximates the same SQL syntax that is used by  the ArcGISDynamicMapServiceLayer's 
            // LayerDefintions Property and displays the feature count back to the user.

            // Get the ArcGISDynamicMapServicelayer.
            Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer myArcGISDynamicMapServiceLayer = null;
            myArcGISDynamicMapServiceLayer = (Esri.ArcGISRuntime.Layers.ArcGISDynamicMapServiceLayer)(MapView1.Map.Layers["Earthquakes"]);

            // Get the Url of the ArcGISDynamicMapServiceLayer. 
            string myUrl = myArcGISDynamicMapServiceLayer.ServiceUri;

            // Get the ID of the Earthquakes1970 sub-layer.
            Esri.ArcGISRuntime.ArcGISServices.AllLayersServiceInfo myAllLayersServiceInfo = await myArcGISDynamicMapServiceLayer.GetAllDetailsAsync();
            int mySubLayerID = myAllLayersServiceInfo.Layers[0].ID;

            // Create a Query. Use the MapView's Extent and SpatialReference. Return all the fields. 
            Esri.ArcGISRuntime.Tasks.Query.Query myQuery = new Esri.ArcGISRuntime.Tasks.Query.Query(sqlQuery);
            myQuery.Geometry = MapView1.Extent;
            myQuery.OutSpatialReference = MapView1.SpatialReference;
            myQuery.OutFields.Add("*");

            // Create a QueryTask using the correct ServiceUri string.
            string myQueryTaskServiceUri = myUrl + "/" + mySubLayerID.ToString();
            Esri.ArcGISRuntime.Tasks.Query.QueryTask myQueryTask = new Esri.ArcGISRuntime.Tasks.Query.QueryTask(new System.Uri(myQueryTaskServiceUri));

            // Call the asynchronous QueryTask and wait for the results to be returned. 
            Esri.ArcGISRuntime.Tasks.Query.QueryResult myQueryResult = await myQueryTask.ExecuteAsync(myQuery);

            // Obtain a FeatureSet from the queryArgs returned from the web service.
            Esri.ArcGISRuntime.Data.FeatureSet myFeatureSet = myQueryResult.FeatureSet;

            // Get the Features from the FeatureSet.
            System.Collections.Generic.IReadOnlyList<Esri.ArcGISRuntime.Data.Feature> myFeatures = myFeatureSet.Features;

            // Display the number of features returned from the QueryTask.
            int myCount = myFeatures.Count;
            TextBlock_RecordsReturned.Text = myCount.ToString();

        }

    }
}