Apply a colormap renderer to a raster.

Use case
A colormap renderer transforms pixel values in a raster to display raster data based on specific colors, aiding in visual analysis of the data. For example, a forestry commission may want to quickly visualize areas above and below the tree-line line occurring at a known elevation on a raster containing elevation values. They could overlay a transparent colormap set to color those areas below the tree-line elevation green, and those above white.
How to use the sample
Pan and zoom to explore the effect of the colormap applied to the raster.
How it works
- Create a raster from a raster file using
Raster.createWithPath(path: String). - Create a raster layer with the raster using
RasterLayer(raster: Raster). - Create an array of colors. Colors at the beginning of the array replace the darkest values in the raster and colors at the end of the array replace the brightest values of the raster.
- Create a colormap renderer with the color array using
ColormapRenderer(colors: Iterable<Color>), and assign it to the raster layer usingRasterLayer.renderer.
Relevant API
- ColormapRenderer
- Raster
- RasterLayer
Offline data
This sample uses the ShastaBW raster. It is downloaded from ArcGIS Online automatically.
About the data
The raster used in this sample shows an area in the south of the Shasta-Trinity National Forest, California.
Tags
colormap, data, raster, renderer, visualization
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 * * 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.applycolormaprenderertoraster
import android.content.Intentimport android.os.Bundleimport com.esri.arcgismaps.sample.sampleslib.DownloaderActivity
class DownloadActivity : DownloaderActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) downloadAndStartSample( Intent(this, MainActivity::class.java), getString(R.string.apply_colormap_renderer_to_raster_app_name), listOf( "https://www.arcgis.com/home/item.html?id=cc68728b5904403ba637e1f1cd2995ae" ) ) }}/* 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.applycolormaprenderertoraster
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport androidx.compose.runtime.Composableimport com.arcgismaps.ApiKeyimport com.arcgismaps.ArcGISEnvironmentimport com.esri.arcgismaps.sample.applycolormaprenderertoraster.screens.ApplyColormapRendererToRasterScreenimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
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)
setContent { SampleAppTheme { ApplyColormapRendererToRasterApp() } } }
@Composable private fun ApplyColormapRendererToRasterApp() { Surface(color = MaterialTheme.colorScheme.background) { ApplyColormapRendererToRasterScreen( sampleName = getString(R.string.apply_colormap_renderer_to_raster_app_name) ) } }}/* 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.applycolormaprenderertoraster.components
import android.app.Applicationimport androidx.lifecycle.AndroidViewModelimport androidx.lifecycle.viewModelScopeimport com.arcgismaps.Colorimport com.arcgismaps.mapping.ArcGISMapimport com.arcgismaps.mapping.BasemapStyleimport com.arcgismaps.mapping.Viewpointimport com.arcgismaps.mapping.layers.RasterLayerimport com.arcgismaps.mapping.symbology.raster.ColormapRendererimport com.arcgismaps.raster.Rasterimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModelimport kotlinx.coroutines.launchimport java.io.File
/** * ViewModel that creates a map with a raster layer and applies a colormap renderer to the raster. * * The sample expects the raster file to be placed in the application's external files directory * under a folder matching the sample name and a subfolder "raster-file" containing "Shasta.tif". */class ApplyColormapRendererToRasterViewModel(private val app: Application) : AndroidViewModel(app) {
// Provision path where sample data is expected if provided by the downloader activity private val provisionPath: String by lazy { app.getExternalFilesDir(null)?.path.toString() + File.separator + app.getString(com.esri.arcgismaps.sample.applycolormaprenderertoraster.R.string.apply_colormap_renderer_to_raster_app_name) }
// Raster created from a local file. private val raster: Raster by lazy { Raster.createWithPath(provisionPath + File.separator + "ShastaBW.tif") }
// Raster layer that will display the raster. private val rasterLayer: RasterLayer by lazy { // Create a simple colormap: 150 red entries followed by 151 yellow entries val colors = mutableListOf<Color>().apply { repeat(150) { add(Color.red) } repeat(151) { add(Color.yellow) } }
// Create and assign the ColormapRenderer val colormapRenderer = ColormapRenderer(colors = colors) RasterLayer(raster).apply { renderer = colormapRenderer } }
// The ArcGISMap used by the sample. val arcGISMap = ArcGISMap(BasemapStyle.ArcGISImageryStandard).apply { operationalLayers += rasterLayer }
// Message dialog view model for error handling val messageDialogVM = MessageDialogViewModel()
init { viewModelScope.launch { rasterLayer.load().onSuccess { // When the raster layer is loaded, center the map on the raster's full extent rasterLayer.fullExtent?.center?.let { centerPoint -> arcGISMap.initialViewpoint = Viewpoint(center = centerPoint, scale = 80_000.0) } }.onFailure { messageDialogVM.showMessageDialog(it) } } }}/* 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.applycolormaprenderertoraster.screens
import androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.paddingimport androidx.compose.material3.Scaffoldimport androidx.compose.runtime.Composableimport androidx.compose.ui.Modifierimport androidx.lifecycle.viewmodel.compose.viewModelimport com.arcgismaps.toolkit.geoviewcompose.MapViewimport com.esri.arcgismaps.sample.applycolormaprenderertoraster.components.ApplyColormapRendererToRasterViewModelimport com.esri.arcgismaps.sample.sampleslib.components.MessageDialogimport com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar
/** * Main screen for the Apply colormap renderer to raster sample. * Displays the MapView using the ArcGISMap prepared by the ViewModel. */@Composablefun ApplyColormapRendererToRasterScreen(sampleName: String) { val mapViewModel: ApplyColormapRendererToRasterViewModel = viewModel()
Scaffold( topBar = { SampleTopAppBar(title = sampleName) } ) { padding -> Column( modifier = Modifier .fillMaxSize() .padding(padding) ) { // The MapView composable is provided the ArcGISMap from the ViewModel. MapView( modifier = Modifier .fillMaxSize(), arcGISMap = mapViewModel.arcGISMap ) }
// Show any errors produced by the ViewModel mapViewModel.messageDialogVM.apply { if (dialogStatus) { MessageDialog( title = messageTitle, description = messageDescription, onDismissRequest = ::dismissDialog ) } } }}