Visualize changes in rasters over time by filtering a mosaic dataset raster for a selected timestamp.

Use case
A mosaic dataset can contain rasters collected over the same area at different times. Viewing the imagery one timestamp at a time makes it easier to compare how a place has changed, such as when tracking urban development, changes in vegetation, or the effects of a natural event.
How to use the sample
When the sample starts, the mosaic dataset raster is displayed in a map view and the available acquisition timestamps are loaded. Use the previous and next controls to step through the distinct timestamps in chronological order. The currently selected timestamp is shown, and the map view updates to display imagery for that time. Use the slider to adjust the opacity of the raster layer to compare it to the imagery basemap.
How it works
- Create a
Mapand display it in aMapView. - Create and load a
MosaicDatasetRasterfrom the sample mobile mosaic dataset and use it to create a raster layer. - Add the raster layer to the map.
- Get the raster’s
distinctTimestamps. - Create a time extent with a time instant of the initial timestamp and set it on the map view.
- Update the time extent on the map view when the selected timestamp changes.
Relevant API
- Map
- MapView
- MosaicDatasetRaster
- RasterLayer
- TimeAware
About the data
The sample uses a mobile mosaic dataset ElToroMosaicDataset containing imagery rasters for one local area across multiple acquisition timestamps.
Tags
aerial imagery, date, mosaic dataset, raster, time, time extent, time-aware, time-enabled, timestamp
Sample code
// Copyright 2026 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 ArcGISimport SwiftUI
struct FilterMosaicDatasetRasterByTimeExtentView: View { /// The map displayed in the view. @State private var map = Map(basemapStyle: .arcGISImageryStandard) /// The time extent used to filter the mosaic dataset raster. @State private var timeExtent: TimeExtent? /// The timestamps from the mosaic dataset raster. @State private var timestamps: [Date] = [] /// The index of the currently selected timestamp. @State private var selectedIndex = 0 /// The raster layer added to the map. @State private var rasterLayer: RasterLayer? /// The opacity of the raster layer. @State private var rasterOpacity: Double = 1 /// A Boolean value indicating whether the opacity controls are shown. @State private var isShowingOpacityControls = false /// A Boolean value indicating whether the raster is loading. @State private var isLoading = true /// The error shown in the error alert. @State private var loadError: (any Error)? /// The height of the attribution bar at the bottom of the map view. @State private var attributionBarHeight: CGFloat?
var body: some View { MapViewReader { mapView in MapView(map: map, timeExtent: $timeExtent) .onAttributionBarHeightChanged { height in withAnimation { attributionBarHeight = height } } .overlay(alignment: .bottom) { VStack { timestampStepper .fixedSize(horizontal: false, vertical: true)
if isShowingOpacityControls { VStack { LabeledContent("Opacity", value: rasterOpacity, format: .percent) Slider(value: $rasterOpacity, in: 0...1, step: 0.01) .onChange(of: rasterOpacity) { rasterLayer?.opacity = Float(rasterOpacity) } } .frame(idealWidth: 320, maxWidth: 440) } } .padding() .background(.regularMaterial, in: .rect(cornerRadius: 16)) .padding([.horizontal, .bottom]) .padding(.bottom, attributionBarHeight) } .task { do { isLoading = true defer { isLoading = false } try await loadRaster(mapView: mapView) } catch { loadError = error } } .errorAlert(presentingError: $loadError) } }
/// A view for stepping through the timestamps of the mosaic dataset raster. private var timestampStepper: some View { HStack { Button("First timestamp", systemImage: "chevron.backward.to.line") { selectFirstTimestamp() } .disabled(timestamps.isEmpty || selectedIndex <= 0)
Divider()
Button("Previous timestamp", systemImage: "chevron.left") { selectTimestamp(at: selectedIndex - 1) } .disabled(timestamps.isEmpty || selectedIndex <= 0)
Divider()
if isLoading { HStack { ProgressView() Text("Loading") } } else if timestamps.indices.contains(selectedIndex) { Text( timestamps[selectedIndex], format: .dateTime.month(.twoDigits).day(.twoDigits).year() ) .monospacedDigit() } else { Text("No timestamps available") }
Divider()
Button("Next timestamp", systemImage: "chevron.right") { selectTimestamp(at: selectedIndex + 1) } .disabled(timestamps.isEmpty || selectedIndex >= timestamps.count - 1)
Divider()
Button("Last timestamp", systemImage: "chevron.forward.to.line") { selectLastTimestamp() } .disabled(timestamps.isEmpty || selectedIndex >= timestamps.count - 1)
Divider()
Button("Adjust opacity", systemImage: "slider.horizontal.below.rectangle") { withAnimation { isShowingOpacityControls.toggle() } } .disabled(rasterLayer == nil) } .labelStyle(.iconOnly) }
/// Sets the time extent to the timestamp at the specified index. /// - Parameter index: The index of the timestamp to select. private func selectTimestamp(at index: Int) { guard timestamps.indices.contains(index) else { fatalError("Index \(index) is out of bounds for timestamps array.") } selectedIndex = index timeExtent = TimeExtent(date: timestamps[index]) }
/// Sets the time extent to the first timestamp. private func selectFirstTimestamp() { guard let firstIndex = timestamps.indices.first else { return } selectTimestamp(at: firstIndex) }
/// Sets the time extent to the last timestamp. private func selectLastTimestamp() { guard let lastIndex = timestamps.indices.last else { return } selectTimestamp(at: lastIndex) }
/// Loads the mosaic dataset raster and adds it to the map. /// - Parameter mapView: The map view. private func loadRaster(mapView: MapViewProxy) async throws { guard let databaseURL = Bundle.main.url( forResource: "el_toro", withExtension: "geodatabase", subdirectory: "ElToroMosaicDataset" ) else { throw CocoaError(.fileNoSuchFile) } let rasterName = MosaicDatasetRaster.names(fromDatabaseAt: databaseURL).first ?? databaseURL.deletingPathExtension().lastPathComponent let raster = MosaicDatasetRaster(databaseURL: databaseURL, name: rasterName) let layer = RasterLayer(raster: raster) try await layer.load() rasterLayer = layer layer.opacity = Float(rasterOpacity)
timestamps = try await raster.distinctTimestamps map.addOperationalLayer(layer)
if !timestamps.isEmpty { selectTimestamp(at: 0) }
if let fullExtent = layer.fullExtent { await mapView.setViewpointGeometry(fullExtent, padding: 100) } }}