Click or drag to resize
Code Example - FeatureLayerViaShapefile

Demonstrates creating a FeatureLayer from a ShapefileTable that is stored locally on disk.

Code Example
Feature Layer Via Shapefile

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="FeatureLayerViaShapefile.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>

        <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="Vertical">

                <!-- Button to add an FeatureLayer via code-behind. -->
                <Button Content="Add FeatureLayer to Map" 
                        x:Name="Button1" Click="Button1_Click" Width="780" HorizontalAlignment="Left"/>

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

                <!-- A MapView Control to display various GIS layers. -->
                <esri:MapView x:Name="MapView1" Width="780" Height="480" VerticalAlignment="Top" Margin="2,2,2,2">
                    <!-- Add a Map. -->
                    <esri:Map x:Name="Map1"/>
                </esri:MapView>

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

// NOTE: The Shapefile that was used in this sample was obtained from the US Census at the following Url:
// http://www.census.gov/geo/maps-data/data/tiger-data.html <-- expand the '2010 Census' | 'Demographic Profile1 - Shapefile Format' section and click on the 'States' link   
// The downloaded file (State_2010Census_DP1.zip) was then unzipped to reveal the 'C:\TestData\State_2010Census_DP1' directory structure that contains 
// the Shapefile (.shp, .dbf, .shx, and .prj) and a metadata file (DP_TableDescriptions.xls) that describes the attributes fields in the Shapefile.

namespace FeatureLayerViaShapefile
{
    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 = "After downloading the 2010 Census Demographic Profile 1 Shapefile for States from the web site: http://www.census.gov/geo/maps-data/data/tiger-data.html " + 
                "and unzipping the contents on disk, click the button to load the FeatureLayer based upon a ShapefileTable. A ClassBreaksRenderer will be used to colorize the " + 
                "states according to the population field: DP0080001.";

            // Set the initial extent to that of the continental US.
            Esri.ArcGISRuntime.Geometry.Envelope myEnvelope = new Esri.ArcGISRuntime.Geometry.Envelope(-125.72, 18.73, -66.3, 55.29);
            MapView1.Map.InitialViewpoint = new Esri.ArcGISRuntime.Controls.Viewpoint(myEnvelope);

            // CAUTION: Local disk based FeatureLayers DO NOT re-project to match the Map's SpatialReference!
            // It is recommended to always set the SpatialReference for the Map when using a FeatureLayer based on a local disk based FeatureTable such as
            // a ShapefileTable or a GeodatabaseFeatureTable. The reason is that the visual elements of the FeatureLayer may not display if the Shapefile or
            // GeodatabaseFeatureTable are not based on the SpatialReference WKID of 3787 (aka. Web Mercator). By specifying the Map's SpatialReference to 
            // be exactly the same at the ShapefileTable or GeodatabaseFeatureTable, you will be assured that it displays visually in the Map. 
            // NOTE: If the FeatureLayer is based on an ArcGIS Server web ServiceFeatureTable, then re-projection of the data automatically occurs 
            // to match the SpatialReference of the Map.
            MapView1.Map.SpatialReference = Esri.ArcGISRuntime.Geometry.SpatialReference.Create(4269);
        }

        private async void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // Create a ShapefileTable from the .shp file. 
            Esri.ArcGISRuntime.Data.ShapefileTable myShapefileTable = await Esri.ArcGISRuntime.Data.ShapefileTable.OpenAsync("C:\\TestData\\State_2010Census_DP1\\State_2010Census_DP1.shp");

            // While specifying a QueryFilter is not necessary to restrict the number of records from the ShapefileTable, it is a good coding practice 
            // if there are thousands of records returned. The more records displayed the less performant the application will be.
            Esri.ArcGISRuntime.Data.QueryFilter myQueryFilter = new Esri.ArcGISRuntime.Data.QueryFilter();
            myQueryFilter.WhereClause = "1=1"; // Get all of the records. There are only 50 states.
            var _x = await myShapefileTable.QueryAsync(myQueryFilter);

            // Create a new FeatureLayer based upon the ShapefileTable. Set its .ID and .DisplayName properties.
            Esri.ArcGISRuntime.Layers.FeatureLayer myFeatureLayer = new Esri.ArcGISRuntime.Layers.FeatureLayer(myShapefileTable);
            myFeatureLayer.ID = "States";
            myFeatureLayer.DisplayName = "States";

            // To make the display of the states polygons more interesting visually, call the custom MakeClassBreaksRenderer_Fill function which will 
            // render the states categorically based upon population values in the field 'DP0080001'. Read the metadata file 'DP_TableDescriptions.xls'
            // that comes with the downloaded Census Shapefile to play with other attribute fields.
            myFeatureLayer.Renderer = MakeClassBreaksRenderer_Fill("DP0080001");

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

        public Esri.ArcGISRuntime.Symbology.ClassBreaksRenderer MakeClassBreaksRenderer_Fill(string theFieldName)
        {
            // Create a ClassBreaksRenderer using what is specified for theFieldName as the Field to  perform the Rendering. In our case the 
            // various ClassBreakInfo objects have hard-coded  .MinimumValue and .MaximumValue settings that are appropriate for the .Field. 
            // If you use a attribute field you will need to adjust these values accordingly.
            Esri.ArcGISRuntime.Symbology.ClassBreaksRenderer myClassBreaksRenderer = new Esri.ArcGISRuntime.Symbology.ClassBreaksRenderer();
            myClassBreaksRenderer.Field = theFieldName; // Requires a numeric Field

            // Define the outline Symbol for the Fills
            Esri.ArcGISRuntime.Symbology.SimpleLineSymbol myBlackSolidOutline = new Esri.ArcGISRuntime.Symbology.SimpleLineSymbol();
            myBlackSolidOutline.Color = System.Windows.Media.Colors.Black;
            myBlackSolidOutline.Style = Esri.ArcGISRuntime.Symbology.SimpleLineStyle.Solid;
            myBlackSolidOutline.Width = 1;

            // Group #1
            Esri.ArcGISRuntime.Symbology.ClassBreakInfo myClassBreakInfo1 = new Esri.ArcGISRuntime.Symbology.ClassBreakInfo();
            myClassBreakInfo1.Minimum = 0;
            myClassBreakInfo1.Maximum = 1000000;
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol1 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol1.Color = System.Windows.Media.Colors.Yellow;
            mySimpleFillSymbol1.Outline = myBlackSolidOutline;
            mySimpleFillSymbol1.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myClassBreakInfo1.Symbol = mySimpleFillSymbol1;
            myClassBreaksRenderer.Infos.Add(myClassBreakInfo1);

            // Group #2
            Esri.ArcGISRuntime.Symbology.ClassBreakInfo myClassBreakInfo2 = new Esri.ArcGISRuntime.Symbology.ClassBreakInfo();
            myClassBreakInfo2.Minimum = 1000001;
            myClassBreakInfo2.Maximum = 10000000;
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol2 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol2.Color = System.Windows.Media.Colors.Green;
            mySimpleFillSymbol2.Outline = myBlackSolidOutline;
            mySimpleFillSymbol2.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myClassBreakInfo2.Symbol = mySimpleFillSymbol2;
            myClassBreaksRenderer.Infos.Add(myClassBreakInfo2);

            // Group #3
            Esri.ArcGISRuntime.Symbology.ClassBreakInfo myClassBreakInfo3 = new Esri.ArcGISRuntime.Symbology.ClassBreakInfo();
            myClassBreakInfo3.Minimum = 10000001;
            myClassBreakInfo3.Maximum = 100000000;
            Esri.ArcGISRuntime.Symbology.SimpleFillSymbol mySimpleFillSymbol3 = new Esri.ArcGISRuntime.Symbology.SimpleFillSymbol();
            mySimpleFillSymbol3.Color = System.Windows.Media.Colors.Red;
            mySimpleFillSymbol3.Outline = myBlackSolidOutline;
            mySimpleFillSymbol3.Style = Esri.ArcGISRuntime.Symbology.SimpleFillStyle.Solid;
            myClassBreakInfo3.Symbol = mySimpleFillSymbol3;
            myClassBreaksRenderer.Infos.Add(myClassBreakInfo3);

            return myClassBreaksRenderer;
        }
    }
}