Apply mosaic rule to a mosaic dataset of rasters.
Use case
An image service can use a mosaic rule to mosaic multiple rasters on-the-fly. A mosaic rule can specify which rasters are selected, and how the selected rasters are z-ordered. It can also specify how overlapping pixels from different rasters at the same location are resolved.
For example, when using the "attribute" mosaic method, the values in an attribute field are used to sort the images, and when using the "center" method, the image closest to the center of the display is positioned as the top image in the mosaic. Additionally, the mosaic operator allows you to define how to resolve the overlapping cells, such as choosing a blending operation.
Specifying mosaic rules is useful for viewing overlapping rasters. For example, using the "attribute" mosaic method to sort the rasters based on their acquisition date allows the newest image to be on top. Using "mean" mosaic operation makes the overlapping areas contain the mean cell values from all the overlapping rasters.
How to use the sample
When the rasters are loaded, choose from a list of preset mosaic rules to apply to the rasters.
How it works
- Create an
ImageServiceRaster
using the service's URL. - Create a
MosaicRule
object and set it to themosaicRule
property of the image service raster, if it does not specify a mosaic rule. - Create a
RasterLayer
from the image service raster and add it to the map. - Set the
mosaicMethod
and other properties of the mosaic rule object accordingly to specify the rule on the raster dataset.
Relevant API
- ImageServiceRaster
- MosaicMethod
- MosaicRule
About the data
This sample uses a raster image service that shows aerial images of Amberg, Germany.
Additional information
For more information, see Understanding the mosaicking rules from ArcGIS Desktop documentation. To learn more about how to define certain mosaic rules, see Mosaic rule objects from ArcGIS for Developers.
Tags
image service, mosaic method, mosaic rule, raster
Sample Code
// 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 'dart:async';
import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:arcgis_maps_sdk_flutter_samples/common/common.dart';
import 'package:flutter/material.dart';
enum MosaicMethodEnum {
objectID('Object ID', 'Orders rasters based on the order (ObjectID).'),
northwest(
'North West',
'Orders rasters based on the distance between each raster center and the northwest point.',
),
center(
'Center',
'Orders rasters based on the distance between each raster center and the view center.',
),
attribute(
'By Attribute',
'Orders rasters based on the absolute distance between their values of an attribute and a base value.',
),
lockRaster(
'Lock Raster',
'Displays only the selected rasters specified in [MosaicRule.lockRasterIds].',
);
const MosaicMethodEnum(this.value, this.description);
final String value;
final String description;
}
class ApplyMosaicRuleToRasters extends StatefulWidget {
const ApplyMosaicRuleToRasters({super.key});
@override
State<ApplyMosaicRuleToRasters> createState() =>
_ApplyMosaicRuleToRastersState();
}
class _ApplyMosaicRuleToRastersState extends State<ApplyMosaicRuleToRasters>
with SampleStateSupport {
// Create a controller for the map view.
final _mapViewController = ArcGISMapView.createController();
// A flag for when the map view is ready and controls can be used.
var _ready = false;
// If the mosaic options should be shown.
var _showMosaicOptions = false;
// Current selected mosaic method.
var _selectedMosaicMethod = MosaicMethodEnum.objectID;
// Raster to apply the mosaic rule.
late ImageServiceRaster _raster;
late StreamSubscription<DrawStatus> _drawStatusSubscription;
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
top: false,
left: false,
right: false,
child: Stack(
children: [
Column(
children: [
Expanded(
// Add a map view to the widget tree and set a controller.
child: ArcGISMapView(
controllerProvider: () => _mapViewController,
onMapViewReady: onMapViewReady,
),
),
],
),
// Display a progress indicator and prevent interaction until state is ready.
LoadingIndicator(visible: !_ready),
],
),
),
bottomSheet: _buildBottomSheet(),
);
}
Future<void> onMapViewReady() async {
// Create a map with a topographic basemap style.
final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic);
_mapViewController.arcGISMap = map;
// Create a Raster with the provided URL
_raster = ImageServiceRaster(
uri: Uri.parse(
'https://sampleserver7.arcgisonline.com/server/rest/services/amberg_germany/ImageServer',
),
);
// Create a RasterLayer with the Raster
final rasterLayer = RasterLayer.withRaster(_raster);
await rasterLayer.load();
// Set a default MosaicRule to the RasterLayer
_raster.mosaicRule = MosaicRule()..mosaicMethod = MosaicMethod.none;
// Add the RasterLayer to the operational layers of the map
map.operationalLayers.add(rasterLayer);
await _mapViewController.setViewpointCenter(
rasterLayer.fullExtent!.center,
scale: 25000,
);
_drawStatusSubscription = _mapViewController.onDrawStatusChanged.listen((
status,
) {
if (status == DrawStatus.completed) {
setState(() => _ready = true);
}
});
// Set the ready state variable to true to enable the sample UI.
setState(() => _ready = true);
}
@override
void dispose() {
_drawStatusSubscription.cancel();
super.dispose();
}
// Update the mosaic method of the raster layer.
void _updateMosaicMethod() {
final mosaicRule = MosaicRule();
switch (_selectedMosaicMethod) {
case MosaicMethodEnum.objectID:
mosaicRule.mosaicMethod = MosaicMethod.none;
case MosaicMethodEnum.northwest:
mosaicRule.mosaicMethod = MosaicMethod.northwest;
mosaicRule.mosaicOperation = MosaicOperation.first;
case MosaicMethodEnum.center:
mosaicRule.mosaicMethod = MosaicMethod.center;
mosaicRule.mosaicOperation = MosaicOperation.blend;
case MosaicMethodEnum.attribute:
mosaicRule.mosaicMethod = MosaicMethod.attribute;
mosaicRule.sortField = 'OBJECTID';
case MosaicMethodEnum.lockRaster:
mosaicRule.mosaicMethod = MosaicMethod.lockRaster;
mosaicRule.lockRasterIds.addAll([1, 7, 12]);
}
_raster.mosaicRule = mosaicRule;
}
// Build a bottom sheet to display mosaic method options.
Widget _buildBottomSheet() {
return BottomSheet(
onClosing: () {},
builder: (context) {
return Container(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Choose a mosaic rule for image service.'),
const Divider(),
if (!_showMosaicOptions)
ElevatedButton(
onPressed: () {
setState(() {
_showMosaicOptions = !_showMosaicOptions;
});
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.check, color: Colors.blue),
const SizedBox(width: 8),
Text(_selectedMosaicMethod.value),
],
),
),
if (_showMosaicOptions)
ListView(
shrinkWrap: true,
children:
MosaicMethodEnum.values.map((method) {
return ListTile(
leading:
(method == _selectedMosaicMethod)
? const Icon(Icons.check, color: Colors.blue)
: const SizedBox(width: 16),
title: Text(method.value),
subtitle: Text(method.description),
onTap: () {
setState(() {
_selectedMosaicMethod = method;
_showMosaicOptions = false;
_ready = false;
});
_updateMosaicMethod();
},
);
}).toList(),
),
],
),
);
},
);
}
}