Apply a hillshade renderer to a raster.

Use case
To monitor coastal erosion, an environmental agency could analyze images of a specific area captured over a longer period of time while applying hillshade renderers for comparison.
How to use the sample
Choose and adjust the settings to update the hillshade renderer on the raster layer. The sample allows you to change the Altitude, Azimuth, and Slope Type.
How it works
- Create a
Rasterfrom a grayscale raster file. - Create a
RasterLayerfrom the raster. - Create a
Basemapfrom the raster layer and set it to the map. - Create a
HillshadeRenderer, specifying the slope type and other properties,HillshadeRenderer.create(...). - Set the hillshade renderer to be used on the raster layer with
RasterLayer.renderer.
Relevant API
- Basemap
- HillshadeRenderer
- Raster
- RasterLayer
Tags
altitude, angle, azimuth, raster, slope, 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.applyhillshaderenderertoraster
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), // get the app name of the sample getString(R.string.apply_hillshade_renderer_to_raster_app_name), listOf( // Portal item which contains a zip of the SRTM raster image files "https://www.arcgis.com/home/item.html?id=134d60f50e184e8fa56365f44e5ce3fb" ) ) }}/* 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.applyhillshaderenderertoraster
import android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.activity.enableEdgeToEdgeimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.Surfaceimport androidx.compose.runtime.Composableimport com.arcgismaps.ApiKeyimport com.arcgismaps.ArcGISEnvironmentimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppThemeimport com.esri.arcgismaps.sample.applyhillshaderenderertoraster.screens.ApplyHillshadeRendererToRasterScreen
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 { ApplyHillshadeRendererToRasterApp() } } }
@Composable private fun ApplyHillshadeRendererToRasterApp() { Surface(color = MaterialTheme.colorScheme.background) { ApplyHillshadeRendererToRasterScreen( sampleName = getString(R.string.apply_hillshade_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.applyhillshaderenderertoraster.components
import android.app.Applicationimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableDoubleStateOfimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.lifecycle.AndroidViewModelimport com.arcgismaps.mapping.ArcGISMapimport com.arcgismaps.mapping.Basemapimport com.arcgismaps.mapping.layers.RasterLayerimport com.arcgismaps.mapping.symbology.raster.HillshadeRendererimport com.arcgismaps.raster.Rasterimport com.arcgismaps.raster.SlopeTypeimport com.esri.arcgismaps.sample.applyhillshaderenderertoraster.Rimport java.io.File
class ApplyHillshadeRendererToRasterViewModel(application: Application) : AndroidViewModel(application) {
private val provisionPath: String by lazy { application.getExternalFilesDir(null)?.path.toString() + File.separator + application.getString( R.string.apply_hillshade_renderer_to_raster_app_name ) }
// Create raster private val raster = Raster.createWithPath(path = "$provisionPath/srtm-hillshade/srtm.tiff")
// Create raster layer private val rasterLayer = RasterLayer(raster)
// Blank map to display raster layer val arcGISMap = ArcGISMap(Basemap(rasterLayer))
// Track UI values to be applied by the update renderer var currentAltitude by mutableDoubleStateOf(45.0) private set var currentAzimuth by mutableDoubleStateOf(0.0) private set var currentSlopeType by mutableStateOf<SlopeType>(SlopeType.None) private set
init { // Apply the renderer values updateRenderer() }
/** * Updates the current raster layer with a new [HillshadeRenderer] * using the current viewmodel parameters. */ private fun updateRenderer() { // Create blend renderer val hillshadeRenderer = HillshadeRenderer.create( altitude = currentAltitude, azimuth = currentAzimuth, slopeType = currentSlopeType, pixelSizeFactor = PIXEL_SIZE_FACTOR, pixelSizePower = PIXEL_SIZE_POWER, outputBitDepth = OUTPUT_BIT_DEPTH, zFactor = Z_FACTOR, ) // Set the renderer used by the layer rasterLayer.renderer = hillshadeRenderer }
fun updateAltitude(altitude: Double) { currentAltitude = altitude updateRenderer() }
fun updateAzimuth(azimuth: Double) { currentAzimuth = azimuth updateRenderer() }
fun updateSlopeType(slopeType: SlopeType) { currentSlopeType = slopeType updateRenderer() }
companion object { // Adjusts the vertical scaling of the terrain to ensure accurate representation of elevation changes const val Z_FACTOR: Double = 0.000016 const val PIXEL_SIZE_FACTOR: Double = 1.0 const val PIXEL_SIZE_POWER: Double = 1.0 const val OUTPUT_BIT_DEPTH: Int = 8 }}/* 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. * */
@file:OptIn(ExperimentalMaterial3Api::class)
package com.esri.arcgismaps.sample.applyhillshaderenderertoraster.screens
import android.content.res.Configurationimport androidx.compose.foundation.layout.Arrangementimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.Rowimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.paddingimport androidx.compose.foundation.layout.wrapContentHeightimport androidx.compose.material.icons.Iconsimport androidx.compose.material.icons.filled.Settingsimport androidx.compose.material3.DropdownMenuItemimport androidx.compose.material3.ExperimentalMaterial3Apiimport androidx.compose.material3.ExposedDropdownMenuBoximport androidx.compose.material3.ExposedDropdownMenuDefaultsimport androidx.compose.material3.FloatingActionButtonimport androidx.compose.material3.HorizontalDividerimport androidx.compose.material3.Iconimport androidx.compose.material3.MaterialThemeimport androidx.compose.material3.MenuAnchorTypeimport androidx.compose.material3.ModalBottomSheetimport androidx.compose.material3.Scaffoldimport androidx.compose.material3.Sliderimport androidx.compose.material3.Surfaceimport androidx.compose.material3.Textimport androidx.compose.material3.TextFieldimport androidx.compose.material3.rememberModalBottomSheetStateimport androidx.compose.runtime.Composableimport androidx.compose.runtime.LaunchedEffectimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.Alignmentimport androidx.compose.ui.Modifierimport androidx.compose.ui.text.style.TextAlignimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.unit.dpimport androidx.lifecycle.viewmodel.compose.viewModelimport com.arcgismaps.raster.SlopeTypeimport com.arcgismaps.toolkit.geoviewcompose.MapViewimport com.esri.arcgismaps.sample.applyhillshaderenderertoraster.components.ApplyHillshadeRendererToRasterViewModelimport com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBarimport com.esri.arcgismaps.sample.sampleslib.theme.SampleAppThemeimport kotlin.math.roundToInt
val slopeTypes = listOf( SlopeType.None, SlopeType.Degree, SlopeType.PercentRise, SlopeType.Scaled)
/** * Main screen layout for the sample app */@OptIn(ExperimentalMaterial3Api::class)@Composablefun ApplyHillshadeRendererToRasterScreen(sampleName: String) { val mapViewModel: ApplyHillshadeRendererToRasterViewModel = viewModel()
// Set up the bottom sheet controls val controlsBottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true) var isBottomSheetVisible by remember { mutableStateOf(false) } LaunchedEffect(isBottomSheetVisible) { if (isBottomSheetVisible) controlsBottomSheetState.show() else controlsBottomSheetState.hide() }
// Collect latest UI states val altitude by remember { mapViewModel::currentAltitude } val azimuth by remember { mapViewModel::currentAzimuth } val slopeType by remember { mapViewModel::currentSlopeType }
Scaffold( topBar = { SampleTopAppBar(title = sampleName) }, content = { Column( modifier = Modifier .fillMaxSize() .padding(it), ) { MapView( arcGISMap = mapViewModel.arcGISMap, modifier = Modifier.fillMaxSize() ) if (isBottomSheetVisible) { ModalBottomSheet( modifier = Modifier.wrapContentHeight(), sheetState = controlsBottomSheetState, onDismissRequest = { isBottomSheetVisible = false } ) { HillshadeRendererOptions( currentAltitude = altitude, currentAzimuth = azimuth, currentSlopeType = slopeType, updateAltitude = mapViewModel::updateAltitude, updateAzimuth = mapViewModel::updateAzimuth, updateSlopeType = mapViewModel::updateSlopeType ) } } } }, floatingActionButton = { if (!isBottomSheetVisible) { FloatingActionButton( modifier = Modifier.padding(bottom = 36.dp, end = 12.dp), onClick = { isBottomSheetVisible = true } ) { Icon(Icons.Filled.Settings, contentDescription = "Hillshade renderer options") } } } )}
@Composablefun HillshadeRendererOptions( currentAltitude: Double, currentAzimuth: Double, currentSlopeType: SlopeType, updateAltitude: (Double) -> Unit, updateAzimuth: (Double) -> Unit, updateSlopeType: (SlopeType) -> Unit,) { Column( modifier = Modifier .fillMaxWidth() .padding(vertical = 12.dp, horizontal = 8.dp), verticalArrangement = Arrangement.spacedBy(12.dp), horizontalAlignment = Alignment.CenterHorizontally ) { Text( modifier = Modifier .fillMaxWidth(), text = "Hillshade Renderer Settings", style = MaterialTheme.typography.titleLarge, textAlign = TextAlign.Center ) HorizontalDivider() Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { Text(text = "Altitude:", style = MaterialTheme.typography.labelLarge) Text(text = currentAltitude.toString(), style = MaterialTheme.typography.labelLarge) } // Altitude Slider Slider( modifier = Modifier .fillMaxWidth() .padding(horizontal = 24.dp), value = currentAltitude.toFloat(), onValueChange = { updateAltitude(it.roundToInt().toDouble()) }, valueRange = 0f..90f ) Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween ) { Text(text = "Azimuth:", style = MaterialTheme.typography.labelLarge) Text(text = currentAzimuth.toString(), style = MaterialTheme.typography.labelLarge) } // Azimuth Slider Slider( modifier = Modifier .fillMaxWidth() .padding(horizontal = 24.dp), value = currentAzimuth.toFloat(), onValueChange = { updateAzimuth(it.roundToInt().toDouble()) }, valueRange = 0f..360f ) // SlopeType dropdown Row( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically ) { var expanded by remember { mutableStateOf(false) } ExposedDropdownMenuBox( modifier = Modifier .fillMaxWidth() .padding(horizontal = 24.dp), expanded = expanded, onExpandedChange = { expanded = !expanded } ) { TextField( label = { Text("SlopeType") }, value = currentSlopeType.javaClass.simpleName, onValueChange = {}, readOnly = true, trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) }, modifier = Modifier .fillMaxWidth() .menuAnchor(type = MenuAnchorType.PrimaryNotEditable) ) ExposedDropdownMenu( expanded = expanded, onDismissRequest = { expanded = false } ) { slopeTypes.forEachIndexed { index, slopeType -> DropdownMenuItem( text = { Text(slopeType.javaClass.simpleName) }, onClick = { updateSlopeType(slopeType) expanded = false }) // Show a divider between dropdown menu options if (index < slopeTypes.lastIndex) { HorizontalDivider() } } } } } }}
@Preview(showBackground = true)@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)@Composablefun HillshadeRendererOptionsPreview() { SampleAppTheme { Surface { HillshadeRendererOptions( currentAltitude = 45.0, currentAzimuth = 0.0, currentSlopeType = SlopeType.None, updateAltitude = { }, updateAzimuth = { }, updateSlopeType = { } ) } }}