Apply mosaic rule to rasters

View on GitHub

Apply mosaic rule to a mosaic dataset of rasters.

Apply mosaic rule to 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

  1. Create an ImageServiceRaster using the service's URL.
  2. Create a MosaicRule object and set it to the mosaicRule property of the image service raster, if it does not specify a mosaic rule.
  3. Create a RasterLayer from the image service raster and add it to the map.
  4. 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

apply_mosaic_rule_to_rasters.dart
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
207
208
209
210
211
212
213
214
215
216
217
// 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(),
                ),
            ],
          ),
        );
      },
    );
  }
}

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