Skip To Content ArcGIS for Developers Sign In Dashboard

ArcGIS Runtime SDK for .NET

Feature layer (GeoPackage)

This code sample is available for these platforms:
View Sample on GitHub

Display features from a local GeoPackage.

Image of feature layer geopackage

Use case

A GeoPackage is an OGC standard, making it useful when your project requires an open source data format or when other, non-ArcGIS systems may be creating the data. Accessing data from a local GeoPackage is useful when working in an environment that has an inconsistent internet connection or that does not have an internet connection at all. For example, a department of transportation field worker might source map data from a GeoPackage when conducting signage inspections in rural areas with poor network coverage.

How to use the sample

Pan and zoom around the map. View the data loaded from the geopackage.

How it works

  1. Create a GeoPackage passing the URI string into the constructor.
  2. Load the GeoPackage with GeoPackage.loadAsync
  3. When it's done loading, get the GeoPackageFeatureTable objects from the geopackage with geoPackage.getGeoPackageFeatureTables()
  4. Create a FeatureLayer(featureTable) for each feature table and add it to the map as an operational layer. Add each to the map as an operational layer with map.OperationalLayers.Add(featureLayer).

Relevant API

  • Map
  • FeatureLayer
  • GeoPackage
  • GeoPackageFeatureTable

Offline data

This sample downloads the following items from ArcGIS Online automatically:

  • Aurora, Colorado GeoPackage - GeoPackage with datasets that cover Aurora Colorado: Public art (points), Bike trails (lines), and Subdivisions (polygons), Airport noise (raster), Buildings (raster).

Tags

feature table, geopackage, gpkg, OGC, package, standards

Sample Code

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"             
             xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
             xmlns:mapping="clr-namespace:Esri.ArcGISRuntime.Mapping;assembly=Esri.ArcGISRuntime" 
             x:Class="ArcGISRuntime.Samples.FeatureLayerGeoPackage.FeatureLayerGeoPackage">
    <esriUI:MapView x:Name="MyMapView"/>
</ContentPage>
// 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 ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using System.Linq;
using Xamarin.Forms;

namespace ArcGISRuntime.Samples.FeatureLayerGeoPackage
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Feature layer (GeoPackage)",
        category: "Data",
        description: "Display features from a local GeoPackage.",
        instructions: "Pan and zoom around the map. View the data loaded from the geopackage.",
        tags: new[] { "OGC", "feature table", "geopackage", "gpkg", "package", "standards" })]
	[ArcGISRuntime.Samples.Shared.Attributes.OfflineData("68ec42517cdd439e81b036210483e8e7")]
    public partial class FeatureLayerGeoPackage : ContentPage
    {
        public FeatureLayerGeoPackage()
        {
            InitializeComponent();

            // Read data from the GeoPackage
            Initialize();
        }

        private async void Initialize()
        {
            // Create a new map centered on Aurora Colorado
            MyMapView.Map = new Map(BasemapType.LightGrayCanvas, 39.7294, -104.8319, 9);

            // Get the full path
            string geoPackagePath = GetGeoPackagePath();

            try
            {
                // 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 layer to show the feature table
                FeatureLayer newLayer = new FeatureLayer(geoPackageTable);
                await newLayer.LoadAsync();

                // Add the feature table as a layer to the map (with default symbology)
                MyMapView.Map.OperationalLayers.Add(newLayer);
            }
            catch (Exception e)
            {
                await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");
            }
        }

        private static string GetGeoPackagePath()
        {
            return DataManager.GetDataFolder("68ec42517cdd439e81b036210483e8e7", "AuroraCO.gpkg");
        }
    }
}