Skip to content

Display feature layers

View inMAUIWPFWinUIView on GitHub

Display feature layers from various data sources.

Display feature layers

Use case

Feature layers, like all layers, are visual representations of data and are used on a map or scene. In the case of feature layers, the underlying data is held in a feature table or feature service.

Feature services are useful for sharing vector GIS data with clients so that individual features can be queried, displayed, and edited. There are various online and offline methods to load feature services.

How to use the sample

Tap the button on the toolbar to add feature layers, from different sources, to the map. Pan and zoom the map to view the feature layers.

How it works

  1. Set the basemap with a BasemapStyle.
  2. Load a feature layer with a URL.
    i. Create a ServiceFeatureTable from a URL.
    ii. Create a FeatureLayer with the feature table.
  3. Load a feature layer with a portal item.
    i. Create a PortalItem with the portal and item ID.
    ii. Create a FeatureLayer with the portal item and layer ID.
  4. Load a feature layer with a geodatabase.
    i. Instantiate and load a Geodatabase using the file name.
    ii. Get the feature table from the geodatabase with the feature table's name.
    iii. Create a FeatureLayer from the feature table.
  5. Load a feature layer with a geopackage.
    i. Instantiate and load a geopackage using its file name.
    ii. Get the first GeoPackageFeatureTable from the geoPackageFeatureTables array.
    iii. Create an FeatureLayer from the feature table.
  6. Load a feature layer with a shapefile.
    i. Create a ShapefileFeatureTable using the shapefile name.
    ii. Create a FeatureLayer from the feature table and load it.
  7. Add the feature layer to the map's OperationalLayers.

Relevant API

  • FeatureLayer
  • Geodatabase
  • GeoPackageFeatureTable
  • PortalItem
  • ServiceFeatureTable
  • ShapefileFeatureTable

About the data

This sample uses the Naperville damage assessment service, Trees of Portland portal item, Los Angeles Trailheads geodatabase, Aurora, Colorado GeoPackage, and Scottish Wildlife Trust Reserves Shapefile.

The Scottish Wildlife Trust shapefile data is provided from Scottish Wildlife Trust under CC-BY licence. Data Copyright Scottish Wildlife Trust (2022).

Tags

feature, geodatabase, geopackage, layers, service, shapefile, table

Sample Code

DisplayFeatureLayers.xaml.csDisplayFeatureLayers.xaml.csDisplayFeatureLayers.xaml
Use dark colors for code blocksCopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 // Copyright 2022 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 ArcGIS.Samples.Managers; using Esri.ArcGISRuntime.Data; using Esri.ArcGISRuntime.Mapping; using Esri.ArcGISRuntime.Portal; using Esri.ArcGISRuntime.Security; using System.Diagnostics;  namespace ArcGIS.Samples.DisplayFeatureLayers {  [ArcGIS.Samples.Shared.Attributes.Sample(  name: "Display feature layers",  category: "Layers",  description: "Display feature layers from various data sources.",  instructions: "Tap the button on the toolbar to add feature layers, from different sources, to the map. Pan and zoom the map to view the feature layers.",  tags: new[] { "feature", "geodatabase", "geopackage", "layers", "service", "shapefile", "table" })]  [ArcGIS.Samples.Shared.Attributes.OfflineData("1759fd3e8a324358a0c58d9a687a8578", "cb1b20748a9f4d128dad8a87244e3e37", "68ec42517cdd439e81b036210483e8e7", "15a7cbd3af1e47cfa5d2c6b93dc44fc2")]  public partial class DisplayFeatureLayers : ContentPage  {  public enum FeatureLayerSource  {  ServiceFeatureTable,  PortalItem,  Geodatabase,  Geopackage,  Shapefile  }   public DisplayFeatureLayers()  {  InitializeComponent();  Initialize();  }   private void Initialize()  {  // Create a new map.  MyMapView.Map = new Map(BasemapStyle.ArcGISTopographic);   // Configure the feature layer selection box.  FeatureLayerPicker.ItemsSource = Enum.GetValues(typeof(FeatureLayerSource));  FeatureLayerPicker.SelectedItem = FeatureLayerSource.ServiceFeatureTable;  }   private void FeatureLayerPicker_SelectionChanged(object sender, EventArgs e)  {  _ = SetFeatureLayerSource();  }   private async Task SetFeatureLayerSource()  {  try  {  // Clear the existing FeatureLayer when a new FeatureLayer is selected.  MyMapView.Map.OperationalLayers.Clear();   switch (FeatureLayerPicker.SelectedItem)  {  case FeatureLayerSource.ServiceFeatureTable:  await SetServiceFeatureTableFeatureLayer();  break;   case FeatureLayerSource.PortalItem:  await SetPortalItemFeatureLayer();  break;   case FeatureLayerSource.Geodatabase:  await SetGeodatabaseFeatureLayerSource();  break;   case FeatureLayerSource.Geopackage:  await SetGeopackagingFeatureLayer();  break;   case FeatureLayerSource.Shapefile:  await SetShapefileFeatureLayer();  break;  }  }  catch (Exception e)  {  await Application.Current.Windows[0].Page.DisplayAlert("Error", e.Message, "OK");  }  }   #region ServiceFeatureTable   private async Task SetServiceFeatureTableFeatureLayer()  {  // Handle the login to the feature service.  AuthenticationManager.Current.ChallengeHandler = new ChallengeHandler(async (info) =>  {  try  {  // WARNING: Never hardcode login information in a production application. This is done solely for the sake of the sample.  string sampleServer7User = "viewer01";  string sampleServer7Pass = "I68VGU^nMurF";  return await AccessTokenCredential.CreateAsync(info.ServiceUri, sampleServer7User, sampleServer7Pass);  }  catch (Exception ex)  {  Debug.WriteLine(ex.Message);  return null;  }  });   // Set the viewpoint.  await MyMapView.SetViewpointAsync(new Viewpoint(41.773519, -88.143104, 4e3));   // Create uri for a given feature service.  Uri serviceUri = new Uri(  "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer/0");   // Create a new FeatureTable from the service uri.  FeatureTable featureTable = new ServiceFeatureTable(serviceUri);   // Create a FeatureLayer with the FeatureTable.  FeatureLayer featureLayer = new FeatureLayer(featureTable);   // Add the FeatureLayer to the operations layers collection of the map.  MyMapView.Map.OperationalLayers.Add(featureLayer);   // Wait for the FeatureLayer to load.  await featureLayer.LoadAsync();  }   #endregion ServiceFeatureTable   #region Geodatabase   private async Task SetGeodatabaseFeatureLayerSource()  {  // Get the path to the downloaded mobile geodatabase (.geodatabase file).  string mobileGeodatabaseFilePath = DataManager.GetDataFolder("cb1b20748a9f4d128dad8a87244e3e37", "LA_Trails.geodatabase");   // Open the mobile geodatabase.  Geodatabase mobileGeodatabase = await Geodatabase.OpenAsync(mobileGeodatabaseFilePath);   // Get the 'Trailheads' geodatabase feature table from the mobile geodatabase.  GeodatabaseFeatureTable trailheadsGeodatabaseFeatureTable = mobileGeodatabase.GetGeodatabaseFeatureTable("Trailheads");   // Asynchronously load the 'Trailheads' geodatabase feature table.  await trailheadsGeodatabaseFeatureTable.LoadAsync();   // Create a FeatureLayer based on the geodatabase feature table.  FeatureLayer trailheadsFeatureLayer = new FeatureLayer(trailheadsGeodatabaseFeatureTable);   // Add the FeatureLayer to the operations layers collection of the map.  MyMapView.Map.OperationalLayers.Add(trailheadsFeatureLayer);   // Zoom the map to the extent of the FeatureLayer.  await MyMapView.SetViewpointGeometryAsync(trailheadsFeatureLayer.FullExtent, 50);  }   #endregion Geodatabase   #region Geopackage   private async Task SetGeopackagingFeatureLayer()  {  // Set the viewpoint.  await MyMapView.SetViewpointAsync(new Viewpoint(39.7294, -104.8319, 5e5));   // Get the full path.  string geoPackagePath = DataManager.GetDataFolder("68ec42517cdd439e81b036210483e8e7", "AuroraCO.gpkg");   // Open the GeoPackage.  GeoPackage myGeoPackage = await GeoPackage.OpenAsync(geoPackagePath);   // Read the feature tables and get the first one.  FeatureTable geoPackageTable = myGeoPackage.GeoPackageFeatureTables.FirstOrDefault();   // Make sure a feature table was found in the package.  if (geoPackageTable == null) { return; }   // Create a FeatureLayer to show the FeatureTable.  FeatureLayer featureLayer = new FeatureLayer(geoPackageTable);  await featureLayer.LoadAsync();   // Add the FeatureLayer to the operations layers collection of the map.  MyMapView.Map.OperationalLayers.Add(featureLayer);  }   #endregion Geopackage   #region PortalItem   private async Task SetPortalItemFeatureLayer()  {  // Set the viewpoint.  await MyMapView.SetViewpointAsync(new Viewpoint(45.5266, -122.6219, 6000));   // Create a portal instance.  ArcGISPortal portal = await ArcGISPortal.CreateAsync();   // Instantiate a PortalItem for a given portal item ID.  PortalItem portalItem = await PortalItem.CreateAsync(portal, "1759fd3e8a324358a0c58d9a687a8578");   // Create a FeatureLayer using the PortalItem.  FeatureLayer featureLayer = new FeatureLayer(portalItem, 0);   // Add the FeatureLayer to the operations layers collection of the map.  MyMapView.Map.OperationalLayers.Add(featureLayer);  }   #endregion PortalItem   #region Shapefile   private async Task SetShapefileFeatureLayer()  {  // Get the path to the downloaded shapefile.  string filepath = DataManager.GetDataFolder("15a7cbd3af1e47cfa5d2c6b93dc44fc2", "ScottishWildlifeTrust_ReserveBoundaries_20201102.shp");   // Open the shapefile.  ShapefileFeatureTable myShapefile = await ShapefileFeatureTable.OpenAsync(filepath);   // Create a FeatureLayer to display the shapefile.  FeatureLayer newFeatureLayer = new FeatureLayer(myShapefile);   // Add the FeatureLayer to the operations layers collection of the map.  MyMapView.Map.OperationalLayers.Add(newFeatureLayer);   // Set the viewpoint.  await MyMapView.SetViewpointAsync(new Viewpoint(56.641344, -3.889066, 6e6));  }   #endregion Shapefile  } }

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.