Skip to content

Show shapefile metadata

View on GitHub

Read a shapefile and display its metadata.

Image of show shapefile metadata sample

Use case

Display metadata for the shapefile currently being viewed—such as tags, credits, and a summary.

How to use the sample

Open the sample to automatically view the shapefile’s metadata.

How it works

  1. Load the shapefile using the ShapefileFeatureTable with the assets URL.
  2. Access the shapefile metadata through the info property of the feature table.
  3. Retrieve and display the thumbnail image from fileInfo.thumbnail.
  4. Display the shapefile's summary, credits, and tags from the metadata.

Relevant API

  • ShapefileFeatureTable
  • ShapefileFeatureTable.info
  • 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

ShowShapefileMetadataView.swift
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
// Copyright 2025 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
//
//   https://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.

import ArcGIS
import SwiftUI

struct ShowShapefileMetadataView: View {
    /// Model which contains the logic for loading and setting the data.
    @State private var model = Model()
    /// The error that occurred, if any, when trying to load the shapefile or display its metadata.
    @State private var error: Error?
    /// A Boolean value specifying whether the metadata view should be shown
    @State private var showMetadata: Bool = false

    var body: some View {
        MapViewReader { mapView in
            MapView(map: model.map)
                .onAppear {
                    Task {
                        do {
                            // Attempt to asynchronously load the
                            // feature layer from the model.
                            try await model.loadShapefile()
                            // If the feature layer has a full extent,
                            // use it to set the map's viewpoint.
                            if let fullExtent = model.featureLayer?.fullExtent {
                                await mapView.setViewpointGeometry(
                                    fullExtent,
                                    padding: 50
                                )
                            }
                        } catch {
                            self.error = error
                        }
                    }
                }
                .toolbar {
                    ToolbarItem(placement: .bottomBar) {
                        Button("Show Shapefile Metadata") {
                            showMetadata.toggle()
                        }
                        .popover(isPresented: $showMetadata) {
                            metadataPopover
                        }
                    }
                }
                .errorAlert(presentingError: $error)
        }
    }

    @ViewBuilder var metadataPopover: some View {
        NavigationStack {
            MetadataPanel(model: $model)
                .navigationTitle("Shapefile Metadata")
                .navigationBarTitleDisplayMode(.inline)
                .toolbar {
                    ToolbarItem(placement: .confirmationAction) {
                        Button("Done") {
                            showMetadata = false
                        }
                    }
                }
        }
        .presentationDetents([.fraction(0.55)])
        .frame(idealWidth: 320, idealHeight: 380)
    }
}

private extension ShowShapefileMetadataView {
    @MainActor
    @Observable
    class Model {
        /// Create a map with a topographic basemap.
        @ObservationIgnored var map = Map(basemapStyle: .arcGISTopographic)

        /// Declare a FeatureLayer to display the shapefile features on the map.
        @ObservationIgnored var featureLayer: FeatureLayer?

        /// Holds metadata information about the shapefile, such as name, description, etc.
        var shapefileInfo: ShapefileInfo?

        /// Holds the thumbnail image associated with the shapefile, if available.
        var thumbnailImage: UIImage?

        /// Asynchronous function to load the feature layer from the shapefile.
        func loadShapefile() async throws {
            let featureTable = ShapefileFeatureTable(fileURL: .auroraShapefile)
            let layer = FeatureLayer(featureTable: featureTable)

            try await layer.featureTable?.load()

            map.addOperationalLayer(layer)
            featureLayer = layer
            shapefileInfo = featureTable.info
            thumbnailImage = featureTable.info?.thumbnail
        }
    }

    struct MetadataPanel: View {
        /// Binding to the model to reflect changes in the UI.
        @Binding var model: ShowShapefileMetadataView.Model

        var body: some View {
            VStack(alignment: .center, spacing: 16) {
                if let info = model.shapefileInfo {
                    Text(info.credits)
                        .bold()
                    Text(info.summary)
                        .font(.caption)
                        .multilineTextAlignment(.leading)
                }
                if let image = model.thumbnailImage {
                    Image(uiImage: image)
                        .resizable()
                        .scaledToFit()
                        .frame(maxHeight: 150)
                }

                if let tags = model.shapefileInfo?.tags {
                    Text("Tags: \(tags.joined(separator: ", "))")
                        .font(.caption2)
                        .foregroundColor(.secondary)
                }
            }
            .padding()
            .cornerRadius(8)
            .padding(.horizontal)
        }
    }
}

private extension URL {
    static var auroraShapefile: URL {
        Bundle.main.url(
            forResource: "TrailBikeNetwork",
            withExtension: "shp",
            subdirectory: "Aurora_CO_shp"
        )!
    }
}

#Preview {
    ShowShapefileMetadataView()
}

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