Click or drag to resize

ShapefileFeatureTable Class

Represents a feature table based on a local Shapefile data source.
Inheritance Hierarchy
SystemObject
  Esri.ArcGISRuntime.DataFeatureTable
    Esri.ArcGISRuntime.DataShapefileFeatureTable

Namespace:  Esri.ArcGISRuntime.Data
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.5.0.0
Syntax
public sealed class ShapefileFeatureTable : FeatureTable

The ShapefileFeatureTable type exposes the following members.

Constructors
  NameDescription
Public methodShapefileFeatureTable
Initializes a new instance of the ShapefileFeatureTable class.
Public methodCode exampleShapefileFeatureTable(String)
Initializes a new instance of the ShapefileFeatureTable class.
Top
Properties
  NameDescription
Public propertyDisplayName
Gets or sets the table's display name.
(Inherited from FeatureTable.)
Public propertyCode exampleExtent
Gets the geographic extent of features within the table.
(Inherited from FeatureTable.)
Public propertyFeatureLayer
Gets the FeatureLayer that this table is attached to
(Inherited from FeatureTable.)
Public propertyCode exampleFields
Gets the fields of the feature table.
(Inherited from FeatureTable.)
Public propertyCode exampleGeometryType
Gets the GeometryType of features in this table.
(Inherited from FeatureTable.)
Public propertyHasGeometry
Gets a value indicating whether this table has geometry data.
(Inherited from FeatureTable.)
Public propertyHasM
Gets a value indicating whether the shapefile geometries have M values.
(Inherited from FeatureTable.)
Public propertyHasZ
Gets a value indicating whether the shapefile geometries have Z values.
(Inherited from FeatureTable.)
Public propertyCode exampleInfo
Gets the shapefile info.
Public propertyIsEditable
Gets a value indicating whether this table is editable.
(Inherited from FeatureTable.)
Public propertyIsPopupEnabled
Gets or sets a value indicating whether the PopupDefinition defined is enabled.
(Inherited from FeatureTable.)
Public propertyLoadError
Gets the exception associated with a failed load. This property remains null if load is successful.
(Inherited from FeatureTable.)
Public propertyCode exampleLoadStatus
Gets the current load status.
(Inherited from FeatureTable.)
Public propertyNumberOfFeatures
Gets the number of features in this table.
(Inherited from FeatureTable.)
Public propertyPath
Gets or sets the path to the shapefile
Public propertyPopupDefinition
Gets or sets the pop-up definition for the table.
(Inherited from FeatureTable.)
Public propertySpatialIndexEnabled
Gets a value indicating whether the shapefile uses spatial indexes.
Public propertySpatialReference
Gets the SpatialReference of this table.
(Inherited from FeatureTable.)
Public propertyCode exampleTableName
Gets the name of the table.
(Inherited from FeatureTable.)
Top
Methods
  NameDescription
Public methodCode exampleAddFeatureAsync
Asynchronously adds new Feature to the table.
(Inherited from FeatureTable.)
Public methodAddFeaturesAsync
Asynchronously adds the set of Features to the table.
(Inherited from FeatureTable.)
Public methodCanAdd
Determines if a feature can be added to the table.
(Inherited from FeatureTable.)
Public methodCancelLoad
Cancel the previous load operation,
(Inherited from FeatureTable.)
Public methodCanDelete
Determines if the specified Feature can be deleted from the table.
(Inherited from FeatureTable.)
Public methodCanEditGeometry
Determines if the table allows for the geometry field to be modified.
(Inherited from FeatureTable.)
Public methodCanUpdate
Determines if the specified Feature can be updated on the table.
(Inherited from FeatureTable.)
Public methodClose
Close a shapefile feature table.
Public methodCode exampleCreateFeature
Creates a new feature.
(Inherited from FeatureTable.)
Public methodCreateFeature(IEnumerableKeyValuePairString, Object, Geometry)
Creates a new feature with specified attributes and geometry.
(Inherited from FeatureTable.)
Public methodCode exampleDeleteFeatureAsync
Asynchronously deletes new Feature from the table.
(Inherited from FeatureTable.)
Public methodDeleteFeaturesAsync
Asynchronously deletes the set of Features from the table.
(Inherited from FeatureTable.)
Public methodGetField
Gets the associated Field with specified field name.
(Inherited from FeatureTable.)
Public methodCode exampleLoadAsync
Asynchronously initiates loading of this table.
(Inherited from FeatureTable.)
Public methodStatic memberCode exampleOpenAsync
Creates and initializes a ShapefileFeatureTable from a file path.
Public methodCode exampleQueryExtentAsync
Asynchronously queries the extent of all features that satisfy the provided parameters.
(Inherited from FeatureTable.)
Public methodCode exampleQueryFeatureCountAsync
Asynchronously queries the count of all features that satisfy the provided parameters.
(Inherited from FeatureTable.)
Public methodCode exampleQueryFeaturesAsync(QueryParameters)
Asynchronously performs query against the table.
(Inherited from FeatureTable.)
Public methodQueryFeaturesAsync(QueryParameters, CancellationToken)
Asynchronously performs query against the table.
(Inherited from FeatureTable.)
Public methodCode exampleQueryStatisticsAsync
Calculates values for the requested statistics.
(Inherited from FeatureTable.)
Public methodRetryLoadAsync
Asynchronously retries loading of this table table.
(Inherited from FeatureTable.)
Public methodCode exampleUpdateFeatureAsync
Asynchronously updates new Feature on the table.
(Inherited from FeatureTable.)
Public methodUpdateFeaturesAsync
Asynchronously updates the set of Features on the table.
(Inherited from FeatureTable.)
Top
Events
  NameDescription
Public eventCode exampleLoaded
Occurs when the table is loaded.
(Inherited from FeatureTable.)
Public eventCode exampleLoadStatusChanged
Occurs when the table LoadStatus is changed.
(Inherited from FeatureTable.)
Public eventPropertyChanged
Occurs when a property value changes.
(Inherited from FeatureTable.)
Top
Remarks

A shapefile dataset consists of at least three files: *.shp, *.shx, and *.dbf (and may include various others). Open a shapefile using the path to the *.shp file (specified in Path). The associated *.shx and *.dbf files must be present at the same location and each of the component files of a shapefile must be smaller than 2 GB. If the minimum required files are not present(or larger than 2 GB), the table will fail to load. If the file permissions are read-only, features cannot be edited. On-the-fly projection of a shapefile is only supported if an associated *.prj file is present. Otherwise, the features are assumed to have the same spatial reference as the map.

Examples

WPF

Example Name: FeatureLayerShapefile

This sample demonstrates how to open a shapefile stored on the device and display it as a feature layer with default symbology.

Code example screen shot.

C#
// 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 System;
using System.Windows;
using ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Data;

namespace ArcGISRuntime.WPF.Samples.FeatureLayerShapefile
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Feature layer (shapefile)",
        "Data",
        "This sample demonstrates how to open a shapefile stored on the device and display it as a feature layer with default symbology.",
        "The shapefile will be downloaded from an ArcGIS Online portal automatically.",
        "Featured")]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("d98b3e5293834c5f852f13c569930caa")]
    public partial class FeatureLayerShapefile
    {
        public FeatureLayerShapefile()
        {
            InitializeComponent();

            // Open a shapefile stored locally and add it to the map as a feature layer
            Initialize();
        }

        private async void Initialize()
        {
            // Create a new map to display in the map view with a streets basemap
            MyMapView.Map = new Map(Basemap.CreateStreetsVector());

            // Get the path to the downloaded shapefile
            string filepath = GetShapefilePath();

            try
            {
                // Open the shapefile
                ShapefileFeatureTable myShapefile = await ShapefileFeatureTable.OpenAsync(filepath);

                // Create a feature layer to display the shapefile
                FeatureLayer newFeatureLayer = new FeatureLayer(myShapefile);

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

                // Zoom the map to the extent of the shapefile
                await MyMapView.SetViewpointGeometryAsync(newFeatureLayer.FullExtent, 50);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Error");
            }
        }

        private static string GetShapefilePath()
        {
            return DataManager.GetDataFolder("d98b3e5293834c5f852f13c569930caa", "Public_Art.shp");
        }
    }
}
C#
// Copyright 2018 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.
#if !_SSG_TOOLING_
using ArcGISRuntime.Samples.Shared.Models;
using Esri.ArcGISRuntime.Portal;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;

namespace ArcGISRuntime.Samples.Managers
{
    public static class DataManager
    {
        /// <summary>
        /// Downloads a portal item and leaves a marker to track download date.
        /// </summary>
        /// <param name="item">Portal item to download.</param>
        private static async Task DownloadItem(PortalItem item)
        {
            // Get sample data directory.
            string dataDir = Path.Combine(GetDataFolder(), item.ItemId);

            // Create directory matching item id.
            if (!Directory.Exists(dataDir))
            {
                Directory.CreateDirectory(dataDir);
            }

            // Get the download task.
            Task<Stream> downloadTask = item.GetDataAsync();

            // Get the path to the destination file.
            string tempFile = Path.Combine(dataDir, item.Name);

            // Download the file.
            using (var s = await downloadTask.ConfigureAwait(false))
            {
                using (var f = File.Create(tempFile))
                {
                    await s.CopyToAsync(f).ConfigureAwait(false);
                }
            }

            // Unzip the file if it is a zip archive.
            if (tempFile.EndsWith(".zip"))
            {
                UnpackData(tempFile, dataDir);
            }

            // Write the __sample.config file. This is used to ensure that cached data did not go out-of-date.
            string configFilePath = Path.Combine(dataDir, "__sample.config");
            File.WriteAllText(configFilePath, @"Data downloaded: " + DateTime.Now);
        }

        /// <summary>
        /// Determines if a portal item has been downloaded and is up-to-date. 
        /// </summary>
        /// <param name="item">The portal item to check.</param>
        /// <returns><c>true</c> if data is available and up-to-date, false otherwise.</returns>
        private static bool IsDataPresent(PortalItem item)
        {
            // Look for __sample.config file. Return false if not present.
            string configPath = Path.Combine(GetDataFolder(item.ItemId), "__sample.config");
            if (!File.Exists(configPath)) { return false; }

            // Get the last write date from the __sample.config file metadata.
            DateTime downloadDate = File.GetLastWriteTime(configPath);

            // Return true if the item was downloaded after it was last modified.
            return downloadDate >= item.Modified;
        }

        /// <summary>
        /// Ensures that data needed for a sample has been downloaded and is up-to-date.
        /// </summary>
        /// <param name="sample">The sample to ensure data is present for.</param>
        public static async Task EnsureSampleDataPresent(SampleInfo sample)
        {
            // Return if there's nothing to do.
            if (sample.OfflineDataItems == null || !sample.OfflineDataItems.Any()) { return; }

            // Hold a list of download tasks (to enable parallel download).
            List<Task> downloads = new List<Task>();

            foreach (string itemId in sample.OfflineDataItems)
            {
                // Create ArcGIS portal item
                var portal = await ArcGISPortal.CreateAsync().ConfigureAwait(false);
                var item = await PortalItem.CreateAsync(portal, itemId).ConfigureAwait(false);
                // Download item if not already present
                if (!IsDataPresent(item))
                {
                    Task downloadTask = DownloadItem(item);
                    downloads.Add(downloadTask);
                }
            }
            // Wait for all downloads to complete
            await Task.WhenAll(downloads);
        }

        /// <summary>
        /// Unzips the file at path defined by <paramref name="zipFile"/>
        ///  into <paramref name="folder"/>.
        /// </summary>
        /// <param name="zipFile">Path to the zip archive to extract.</param>
        /// <param name="folder">Destination folder.</param>
        private static void UnpackData(string zipFile, string folder)
        {
            using (var archive = ZipFile.OpenRead(zipFile))
            {
                foreach (var entry in archive.Entries.Where(m => !String.IsNullOrWhiteSpace(m.Name)))
                {
                    string path = Path.Combine(folder, entry.FullName);
                    Directory.CreateDirectory(Path.GetDirectoryName(path));
                    entry.ExtractToFile(path, true);
                }
            }
        }

        /// <summary>
        /// Gets the data folder where locally provisioned data is stored.
        /// </summary>
        internal static string GetDataFolder()
        {
#if NETFX_CORE
            string appDataFolder  = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#elif XAMARIN
            string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
#else
            string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
#endif
            string sampleDataFolder = Path.Combine(appDataFolder, "ArcGISRuntimeSampleData");

            if (!Directory.Exists(sampleDataFolder)) { Directory.CreateDirectory(sampleDataFolder); }

            return sampleDataFolder;
        }

        /// <summary>
        /// Gets the path to an item on disk. 
        /// The item must have already been downloaded for the path to be valid.
        /// </summary>
        /// <param name="itemId">ID of the portal item.</param>
        internal static string GetDataFolder(string itemId)
        {
            return Path.Combine(GetDataFolder(), itemId);
        }

        /// <summary>
        /// Gets the path to an item on disk. 
        /// The item must have already been downloaded for the path to be valid.
        /// </summary>
        /// <param name="itemId">ID of the portal item.</param>
        /// <param name="pathParts">Components of the path.</param>
        internal static string GetDataFolder(string itemId, params string[] pathParts)
        {
            return Path.Combine(GetDataFolder(itemId), Path.Combine(pathParts));
        }
    }
}
#endif
XAML
<UserControl 
    x:Class="ArcGISRuntime.WPF.Samples.FeatureLayerShapefile.FeatureLayerShapefile"
    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">
    <esri:MapView x:Name="MyMapView" />
</UserControl>
See Also
Additional Examples
Hyperlink to ExampleDescription
FeatureLayerShapefileThis sample demonstrates how to open a shapefile stored on the device and display it as a feature layer with default symbology.
ReadShapefileMetadataThis sample demonstrates how to open a shapefile stored on the device, read metadata that describes the dataset, and display it as a feature layer with default symbology.
SymbolizeShapefileThis sample demonstrates how to apply a custom renderer to a shapefile displayed by a feature layer.