Click or drag to resize
Code Example - FeatureLayerViaGeodatabaseWebDownload

Demonstrates downloading a Geodatabase on ArcGIS Server to the local disk and then creating a FeatureLayer from a GeodatabaseFeatureTable in the downloaded Geodatabase.

Code Example
Feature Layer Via Geodatabase Web Download

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="FeatureLayerViaGeodatabaseWebDownload.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="600" Width="800" 
    xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013">

    <Grid x:Name="LayoutRoot">

        <!-- Create a few SimpleRenderer symbology objects for FeatureLayer's that will be added via code-behind. -->
        <Grid.Resources>
            <esri:SimpleRenderer x:Key="mySimpleMarkerSymbolRenderer_Red">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleMarkerSymbol Color="Red"  Size="12" Style="Circle"/>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
            <esri:SimpleRenderer x:Key="mySimpleLineSymbolRender_Blue">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleLineSymbol Color="Blue" Width="2" Style="Solid"/>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
            <esri:SimpleRenderer x:Key="mySimpleFillSymbolRender_Yellow">
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleFillSymbol Color="Yellow" Style="Solid"/>
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
        </Grid.Resources>

        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Vertical">

                <!-- TextBlock to provide the instructions on how to use the sample code. It will be 
                populated with instructions in the code-behind when the application loads. -->
                <TextBlock Height="58" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" 
                           Width="770" TextWrapping="Wrap"  Margin="10,2,0,0" />

            </StackPanel>
            <StackPanel Orientation="Horizontal">

                <!-- Button to download a geodatabase from ArcGIS Server. -->
                <Button Content="Download GeodatabaseFeatureTable From Web" Margin="2,2,2,2"
                        x:Name="Button1" Click="Button1_Click" Width="507" />

                <!-- Button to add an FeatureLayer via code-behind. -->
                <Button Content="Add FeatureLayer to Map" Margin="2,2,2,2" 
                  x:Name="Button2" Click="Button2_Click" Width="266" IsEnabled="False"/>

            </StackPanel>
            <StackPanel Orientation="Horizontal">

                <!-- A MapView Control to display various GIS layers. -->
                <esri:MapView x:Name="MapView1" Width="507" Height="480" VerticalAlignment="Top" Margin="2,2,2,2" Background="AliceBlue">

                    <!-- A Map. -->
                    <esri:Map  x:Name="Map1" />

                </esri:MapView>

                <StackPanel Orientation="Vertical">

                    <!-- Listbox will hold the names of FeatureLayers to add to the Map based upon GeodatabaseFeatureTables that were obtained
                    by downloading a geodatabase from ArcGIS Server. -->
                    <ListBox Height="139" Width="265" Name="ListBox1" Margin="2,2,2,2"/>

                    <!-- Controls to display the progress of creating a geodatabase on ArcGIS Server and downloading it to the local  device. -->
                    <StackPanel x:Name="panelStatus" Margin="0,12,0,0"  Visibility="Collapsed">
                        <TextBlock x:Name="txtStatus" HorizontalAlignment="Center" />
                        <ProgressBar x:Name="progress" IsIndeterminate="True" Margin="24,12"/>
                    </StackPanel>

                </StackPanel>

            </StackPanel>
        </StackPanel>
    </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 "FeatureLayerViaGeodatabaseWebDownload.MainWindow" to be just "MainWindow".

namespace FeatureLayerViaGeodatabaseWebDownload
{
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // Add the instructions on how to use this example code to the TextBlock.
            TextBlock1.Text = "When the application loads, click the 'Download GeodatabaseFeatureTable From Web' button. A progress bar will display the generation of " +
                "the geodatabase on ArcGISServer and then its download across the web to the local device. When complete, click on one of the names of the " +
                "GeodatabaseFeatureTables in the Listbox and then click the 'Add FeatureLayer to Map' button to add the FeaturLayer to the Map.";
        }

        // Member (i.e. Global variables) used in the example code.
        public string MyUrlString = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Sync/WildfireSync/FeatureServer"; // Url of ArcGIS Server with a sync enabled geodatabase that can be downloaded.
        public string MyDownloadPath = "C:\\TestData"; // Location where the geodatabase will be downloaded.
        public string MyGeodatabaseFileName = "WildfireLocal.geodatabase"; // Name of the geodatabase that will be downloaded.

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Don't let the user re-run the download of the geodatabase from ArcGIS Server. 
            Button1.IsEnabled = false;

            // Enable the visibility of the StackPanel that shows the progress of the geodatabase downloads.  
            panelStatus.Visibility = System.Windows.Visibility.Visible;

            try
            {
                // Create a Uri from the Url to a an ArcGIS Server sync enabled geodatabase that can be downloaded.
                System.Uri myUri = new System.Uri(MyUrlString);

                // Create a new GeodatabaseSyncTask with the Uri of the feature server to pull from.
                Esri.ArcGISRuntime.Tasks.Offline.GeodatabaseSyncTask myGeodatabaseSyncTask = new Esri.ArcGISRuntime.Tasks.Offline.GeodatabaseSyncTask(myUri);

                // Create a SpatialReference that matches the feature service.
                Esri.ArcGISRuntime.Geometry.SpatialReference mySpatialReference = new Esri.ArcGISRuntime.Geometry.SpatialReference(4326);

                // Create an Envelope that is the spatial extent of the feature service.
                Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-179.99, -218.28, 179.99, 213.71, mySpatialReference);

                // Define the list of layers (by their index number) that will be downloaded and included in the geodatabase.
                System.Collections.Generic.List<int> layersList = new System.Collections.Generic.List<int>(new int[3] { 0, 1, 2 });

                // Create the parameters for the GeodatabaseSyncTask using the List of layer's index numbers and the extent of the layers. The add the OutSpatialReference and SyncModel parameters. 
                Esri.ArcGISRuntime.Tasks.Offline.GenerateGeodatabaseParameters gdbParams = new Esri.ArcGISRuntime.Tasks.Offline.GenerateGeodatabaseParameters(layersList, myEnvelope);
                gdbParams.OutSpatialReference = mySpatialReference;
                gdbParams.SyncModel = Esri.ArcGISRuntime.Tasks.Offline.SyncModel.PerLayer;

                // Create a System.Progress<T> object to report status as the task executes
                System.Progress<Esri.ArcGISRuntime.Tasks.Offline.GeodatabaseStatusInfo> progress = new System.Progress<Esri.ArcGISRuntime.Tasks.Offline.GeodatabaseStatusInfo>();

                // Display a message to the user that the geodatabase is being generated on ArcGIS Server. 
                ReportStatus("Generating the geodatabase on ArcGIS Server...");

                // Call GenerateGeodatabaseAsync, pass in the parameters and the callback to execute when it is complete.
                var gdbResult = await myGeodatabaseSyncTask.GenerateGeodatabaseAsync(gdbParams, GdbCompleteCallback, new System.TimeSpan(0, 1, 0), progress, System.Threading.CancellationToken.None);
            }
            catch (System.Exception ex)
            {
                System.Windows.MessageBox.Show("Unable to create offline database: " + ex.Message);
            }
        }

        private void ReportStatus(string status)
        {
            // Helper subroutine that reports the status of the geodatabase download.

            txtStatus.Text = status;
        }

        private async void GdbCompleteCallback(Esri.ArcGISRuntime.Tasks.Offline.GeodatabaseStatusInfo statusInfo, System.Exception ex)
        {
            // Callback to execute when the GeodatabaseSyncTask completes (successfully or with an exception).

            // Display a message to the user that the geodatabase that was generated on ArcGIS Server is now being downloaded to the local device. 
            ReportStatus("Downloading geodatabase to the local device...");

            // If the generation of the geodatabase on ArcGIS Server was unsuccessful, report the exception and return.
            if (ex != null)
            {
                return;
            }

            // Create a new ArcGISHttpClient to talk to ArcGIS Server.
            Esri.ArcGISRuntime.Http.ArcGISHttpClient myArcGISHttpClient = new Esri.ArcGISRuntime.Http.ArcGISHttpClient();

            // Create a new Task to talk to to ArcGIS Server.
            System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> myHttpResponseMessage = myArcGISHttpClient.GetOrPostAsync(statusInfo.ResultUri, null);

            // Create a string that will be the path/filename of the geodatabase to be downloaded.
            string myGeodatabasePathFileName = System.IO.Path.Combine(MyDownloadPath, MyGeodatabaseFileName);

            // Create a local path for the geodatabase, if it does not already exist.
            if (!(System.IO.Directory.Exists(MyDownloadPath)))
            {
                System.IO.Directory.CreateDirectory(MyDownloadPath);
            }

            // Write the geodatabase to local location using the ArcGISHttpClient Task.
            await System.Threading.Tasks.Task.Factory.StartNew(async () =>
            {
                using (var stream = System.IO.File.Create(myGeodatabasePathFileName))
                {
                    await myHttpResponseMessage.Result.Content.CopyToAsync(stream);
                }
            });


            // Display a message to the user that the geodatabase GeodatabaseFeatureTables are being generated.
            ReportStatus("Create local GeodatabaseFeatureTables...");

            // Create a geodatabase using the Geodatabase.OpenAsync static/shared Method from a path/filename on the local device.
            Esri.ArcGISRuntime.Data.Geodatabase myGeodatabase = await Esri.ArcGISRuntime.Data.Geodatabase.OpenAsync(myGeodatabasePathFileName);

            // Get all of the GeodatabaseFeatureTables from the geodatabase. They will be used to construct a FeatureLayer later.
            System.Collections.Generic.IEnumerable<Esri.ArcGISRuntime.Data.GeodatabaseFeatureTable> myGeodatabaseFeatureTables = myGeodatabase.FeatureTables;

            // Loop through each of the GeodatabaseFeatureTables and get its name and display that in a ListBox that user can then select and add to the Map as a FeatureLayer. 
            foreach (var oneFeatureTable in myGeodatabaseFeatureTables)
            {
                Esri.ArcGISRuntime.Data.TableSchema myTableSchema = oneFeatureTable.Schema;
                string myTableName = myTableSchema.Name;
                ListBox1.Items.Add(myTableName);
                DictFeatureTableIDs.Add(myTableName, oneFeatureTable); // Use a custom Dictionary to hold the GeodatabaseFeatureTable and its name.
            }

            // Hide the StackPanel that holds the ProgressBar and enable the ability to add a FeatureLayer based on the GeodatabaseFeatureTable's listed in the ListBox.
            panelStatus.Visibility = System.Windows.Visibility.Collapsed;
            Button2.IsEnabled = true;
        }

        // Custom Dictionary to hold GeodatabaseFeatureTables and their names.
        public System.Collections.Generic.Dictionary<string, Esri.ArcGISRuntime.Data.GeodatabaseFeatureTable> DictFeatureTableIDs = new System.Collections.Generic.Dictionary<string, Esri.ArcGISRuntime.Data.GeodatabaseFeatureTable>();

        private void Button2_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Get the name of the GeodatabaseFeatureTable from what the user selected in the ListBox.
            string myGeodatabaseFeatureTableName = (string)ListBox1.SelectedValue;

            // Get the GetdatabaseFeatureTable object from the custom dictionary based upon its name.
            Esri.ArcGISRuntime.Data.GeodatabaseFeatureTable myGeodatabaseFeatureTable = DictFeatureTableIDs[myGeodatabaseFeatureTableName];

            // Create a new FeatureLayer based upon a GeodatabaseFeatureTable and also set its Name and ID values.
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = new Esri.ArcGISRuntime.Layers.FeatureLayer();
            myFeatureLayer.FeatureTable = myGeodatabaseFeatureTable;
            myFeatureLayer.ID = myGeodatabaseFeatureTable.Name;
            myFeatureLayer.DisplayName = myGeodatabaseFeatureTable.Name;

            // Apply some simple rendering (defined in the XAML) to the FeatureLayer based upon its geometry type.
            if (myGeodatabaseFeatureTable.ServiceInfo.GeometryType == Esri.ArcGISRuntime.Geometry.GeometryType.Point)
            {
                myFeatureLayer.Renderer = (Esri.ArcGISRuntime.Symbology.Renderer)LayoutRoot.Resources["mySimpleMarkerSymbolRenderer_Red"];
            }
            else if (myGeodatabaseFeatureTable.ServiceInfo.GeometryType == Esri.ArcGISRuntime.Geometry.GeometryType.Polyline)
            {
                myFeatureLayer.Renderer = (Esri.ArcGISRuntime.Symbology.Renderer)LayoutRoot.Resources["mySimpleLineSymbolRender_Blue"];
            }
            else if (myGeodatabaseFeatureTable.ServiceInfo.GeometryType == Esri.ArcGISRuntime.Geometry.GeometryType.Polygon)
            {
                myFeatureLayer.Renderer = (Esri.ArcGISRuntime.Symbology.Renderer)LayoutRoot.Resources["mySimpleFillSymbolRender_Yellow"];
            }

            // Add the FeatureLayer to the Map.
            MapView1.Map.Layers.Add(myFeatureLayer);
        }
    }
}