Read shapefile metadata

View inAndroidFormsUWPWPFWinUIiOSView on GitHub

Read a shapefile and display its metadata.

Image of read shapefile metadata

Use case

You can display information about the shapefile your user is viewing, like tags, credits, and summary.

How to use the sample

The shapefile's metadata will be displayed when you open the sample.

How it works

  1. Call ShapefileFeatureTable.OpenAsync("path_to_shapefile") to create the ShapefileFeatureTable.
  2. Get the ShapefileInfo from the feature table's Info property.
  3. Get the image from fileInfo.Thumbnail and display it.
  4. Display the Summary, Credits, and Tags properties from the shapefile info.

Relevant API

  • ShapefileFeatureTable
  • ShapefileFeatureTable.Info
  • ShapefileFeatureTable.OpenAsync
  • ShapefileInfo
  • ShapefileInfo.Credits
  • ShapefileInfo.Summary
  • ShapefileInfo.Tags
  • ShapefileInfo.Thumbnail

Offline data

Aurora Colorado Shapefiles is available as an item hosted on ArcGIS Online].

About the data

This sample uses a shapefile showing bike trails in Aurora, CO. The Aurora Colorado Shapefiles are available as an item on ArcGIS Online.

Tags

credits, description, metadata, package, shape file, shapefile, summary, symbology, tags, visualization

Sample Code

ReadShapefileMetadata.cs
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
// 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 Android.App;
using Android.Graphics;
using Android.OS;
using Android.Views;
using Android.Widget;
using ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using ContextThemeWrapper = AndroidX.AppCompat.View.ContextThemeWrapper;

namespace ArcGISRuntime.Samples.ReadShapefileMetadata
{
    [Activity(ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("d98b3e5293834c5f852f13c569930caa")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        name: "Read shapefile metadata",
        category: "Data",
        description: "Read a shapefile and display its metadata.",
        instructions: "The shapefile's metadata will be displayed when you open the sample.",
        tags: new[] { "credits", "description", "metadata", "package", "shape file", "shapefile", "summary", "symbology", "tags", "visualization" })]
    public class ReadShapefileMetadata : Activity
    {
        // Store the app's map view
        private MapView _myMapView;

        // Store the object that holds the shapefile metadata
        private ShapefileInfo _shapefileMetadata;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Title = "Read shapefile metadata";
            CreateLayout();

            // Download (if necessary) and add a local shapefile dataset to the map
            Initialize();
        }

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

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

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

                // Read metadata about the shapefile and display it in the UI
                _shapefileMetadata = myShapefile.Info;

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

                // Zoom the map to the extent of the shapefile
                _myMapView.SpatialReferenceChanged += async (s, e) =>
                {
                    await _myMapView.SetViewpointGeometryAsync(newFeatureLayer.FullExtent);
                };

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

                // Show the map in the MapView
                _myMapView.Map = streetMap;
            }
            catch (Exception e)
            {
                new AlertDialog.Builder(this).SetMessage(e.ToString()).SetTitle("Error").Show();
            }
        }

        private static string GetShapefilePath()
        {
            return DataManager.GetDataFolder("d98b3e5293834c5f852f13c569930caa", "TrailBikeNetwork.shp");
        }

        private void CreateLayout()
        {
            // Create a new vertical layout for the app
            LinearLayout layout = new LinearLayout(this) { Orientation = Orientation.Vertical };

            // Add a button to show the metadata
            Button showMetadataButton = new Button(this)
            {
                Text = "Show Metadata"
            };
            showMetadataButton.Click += ShowMetadataDialog;
            layout.AddView(showMetadataButton);

            // Add the map view to the layout
            _myMapView = new MapView(this);
            layout.AddView(_myMapView);

            // Show the layout in the app
            SetContentView(layout);
        }

        private void ShowMetadataDialog(object sender, System.EventArgs e)
        {
            MetadataDialogFragment metadataDialog = new MetadataDialogFragment(_shapefileMetadata);

            // Begin a transaction to show a UI fragment (the metadata dialog)
            FragmentTransaction trans = FragmentManager.BeginTransaction();
            metadataDialog.Show(trans, "metadata");
        }
    }

    // A custom DialogFragment class to show shapefile metadata
    public class MetadataDialogFragment : DialogFragment
    {
        private ShapefileInfo _metadata;
        private ImageView _thumbnailImageView;

        public MetadataDialogFragment(ShapefileInfo metadata)
        {
            _metadata = metadata;
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            base.OnCreateView(inflater, container, savedInstanceState);

            // Dialog to display
            LinearLayout dialogView = null;

            // Get the context for creating the dialog controls
            Android.Content.Context ctx = Activity.ApplicationContext;
            AndroidX.AppCompat.View.ContextThemeWrapper ctxWrapper = new ContextThemeWrapper(ctx, Android.Resource.Style.ThemeMaterialLight);

            // Set a dialog title
            Dialog.SetTitle(_metadata.Credits);

            // The container for the dialog is a vertical linear layout
            dialogView = new LinearLayout(ctxWrapper)
            {
                Orientation = Orientation.Vertical
            };
            dialogView.SetPadding(20, 20, 20, 20);

            // Add a text box for showing metadata summary
            TextView summaryTextView = new TextView(ctxWrapper)
            {
                Text = _metadata.Summary
            };
            dialogView.AddView(summaryTextView);

            // Add an image to show the thumbnail
            _thumbnailImageView = new ImageView(ctxWrapper);
            _thumbnailImageView.SetMinimumHeight(200);
            _thumbnailImageView.SetMinimumWidth(200);
            dialogView.AddView(_thumbnailImageView);

            // Call a function to load the thumbnail image from the metadata
            LoadThumbnail();

            // Add a text box for showing metadata tags
            TextView tagsTextView = new TextView(ctxWrapper)
            {
                Text = string.Join(",", _metadata.Tags)
            };
            dialogView.AddView(tagsTextView);

            // Add a button to close the dialog
            Button dismissButton = new Button(ctxWrapper)
            {
                Text = "OK"
            };
            dismissButton.Click += (s, e) => Dismiss();
            dialogView.AddView(dismissButton);

            // Return the new view for display
            return dialogView;
        }

        private async void LoadThumbnail()
        {
            try
            {
                Bitmap img = await _metadata.Thumbnail.ToImageSourceAsync();
                _thumbnailImageView.SetImageBitmap(img);
            }
            catch (Exception e)
            {
                new AlertDialog.Builder(Context).SetMessage(e.ToString()).SetTitle("Error").Show();
            }
        }
    }
}

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