View on GitHub Sample viewer app

Apply mosaic rules to a mosaic dataset of rasters.

Image of Apply mosaic rule to rasters sample

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, how the selected rasters are z-ordered, and how overlapping pixels from different rasters at the same location are resolved.

For example, when using the “byAttribute” 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 “By Attribute” mosaic method to sort the rasters based on their acquisition date allows the newest image to be on top. Using the “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 using the dropdown menu at the bottom of the screen. The map will update to display the rasters according to the selected rule.

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.
  3. Create a RasterLayer from the image service raster and add it to the map.
  4. Set the mosaicMethod, mosaicOperation, and other properties of the mosaic rule object accordingly to specify the rule on the raster dataset.

Relevant API

  • ImageServiceRaster
  • MosaicMethod
  • MosaicOperation
  • MosaicRule
  • RasterLayer

About the data

This sample uses a raster image service that shows aerial images of Amberg, Germany.

Additional information

The sample applies a hillshade function to a raster produced from the National Land Cover Database, NLCDLandCover2001. You can learn more about the hillshade function in the ArcGIS Pro documentation.

Tags

image service, mosaic method, mosaic rule, raster

Sample Code

MainActivity.kt MainActivity.kt ApplyMosaicRuleToRastersViewModel.kt ApplyMosaicRuleToRastersScreen.kt
/* 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
*
* 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.
*
*/
package com.esri.arcgismaps.sample.applymosaicruletorasters
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
import com.esri.arcgismaps.sample.applymosaicruletorasters.screens.ApplyMosaicRuleToRastersScreen
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// authentication with an API key or named user is
// required to access basemaps and other location services
ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.ACCESS_TOKEN)
enableEdgeToEdge()
setContent {
SampleAppTheme {
ApplyMosaicRuleToRastersApp()
}
}
}
@Composable
private fun ApplyMosaicRuleToRastersApp() {
Surface(color = MaterialTheme.colorScheme.background) {
ApplyMosaicRuleToRastersScreen(
sampleName = getString(R.string.apply_mosaic_rule_to_rasters_app_name)
)
}
}
}