View on GitHub Sample viewer app

Perform an exploratory viewshed analysis from a defined vantage point.

Image of exploratory viewshed location

Use case

An exploratory viewshed analysis is a type of visual analysis you can perform at the current rendered resolution of a scene. The exploratory viewshed shows what can be seen from a given location. The output is an overlay with two different colors - one representing the visible areas (green) and the other representing the obstructed areas (red).

Note: This analysis is a form of “exploratory analysis”, which means the results are calculated on the current scale of the data, and the results are generated very quickly but not persisted. If persisted analysis performed at the full resolution of the data is required, consider using a ViewshedFunction to perform a viewshed calculation instead.

How to use the sample

  1. Use the supporting pane sliders to change heading, pitch, horizontal and vertical angles, and minimum/maximum distances.
  2. Open the scene options floating pane to toggle frustum outline and analysis overlay visibility.
  3. Use scene option actions to align the camera with the viewshed or reset all viewshed options.

How it works

  1. Create an ExploratoryLocationViewshed passing in the observer location, heading, pitch, horizontal/vertical angles, and min/max distances.
  2. Set the property values on the exploratory viewshed instance for location, direction, range, and visibility properties.

Relevant API

  • AnalysisOverlay
  • ArcGISSceneLayer
  • ArcGISTiledElevationSource
  • ExploratoryLocationViewshed
  • ExploratoryViewshed

About the data

The scene shows a buildings layer in Brest, France hosted on ArcGIS Online.

Additional information

This sample uses the GeoView-Compose Toolkit module to be able to implement a composable SceneView.

Tags

3D, exploratory viewshed, frustum, geoview-compose, scene, visibility analysis

Sample code

SceneViewModel.kt SceneViewModel.kt MainActivity.kt MainScreen.kt ViewshedSupportingContent.kt ViewshedFloatingContent.kt
/* Copyright 2023 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.showexploratoryviewshedfrompointinscene.components
import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.analysis.interactive.ExploratoryLocationViewshed
import com.arcgismaps.geometry.Point
import com.arcgismaps.mapping.ArcGISScene
import com.arcgismaps.mapping.ArcGISTiledElevationSource
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Surface
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.layers.ArcGISSceneLayer
import com.arcgismaps.mapping.view.AnalysisOverlay
import com.arcgismaps.mapping.view.AnalysisViewStatus
import com.arcgismaps.mapping.view.Camera
import com.arcgismaps.mapping.view.GeoView
import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import com.esri.arcgismaps.sample.showexploratoryviewshedfrompointinscene.R
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.seconds
class SceneViewModel(private val application: Application) : AndroidViewModel(application) {
// initialize location viewshed parameters
private var viewshed: ExploratoryLocationViewshed
private val initHeading = 82.0
private val initPitch = 60.0
private val initHorizontalAngle = 75.0
private val initVerticalAngle = 90.0
private val initMinDistance = 0.0
private val initMaxDistance = 1500.0
private val initFrustumVisible = true
private val initAnalysisVisible = true
private val initViewshedUiState = ViewshedUiState(
heading = initHeading.toFloat(),
pitch = initPitch.toFloat(),
horizontalAngle = initHorizontalAngle.toFloat(),
verticalAngle = initVerticalAngle.toFloat(),
minDistance = initMinDistance.toFloat(),
maxDistance = initMaxDistance.toFloat(),
isFrustumVisible = initFrustumVisible,
isAnalysisVisible = initAnalysisVisible
)
val initLocation = Point(
x = -4.50,
y = 48.4,
z = 1000.0
)
private val initialCamera = Camera(
lookAtPoint = initLocation,
distance = 3500.0,
heading = 50.0,
pitch = 70.0,
roll = 0.0
)
var scene by mutableStateOf(ArcGISScene(BasemapStyle.ArcGISNavigationNight))
var analysisOverlay by mutableStateOf(AnalysisOverlay())
private val _viewshedUiState = MutableStateFlow(initViewshedUiState)
val viewshedUiState = _viewshedUiState.asStateFlow()
val sceneViewProxy = SceneViewProxy()
// Message dialog view model to display errors
val messageDialogVM = MessageDialogViewModel()
init {
// create a surface for elevation data
val surface = Surface().apply {
elevationSources.add(ArcGISTiledElevationSource(application.getString(R.string.elevation_service)))
}
// create a layer of buildings
val buildingsSceneLayer = ArcGISSceneLayer(application.getString(R.string.buildings_layer))
// create a scene and add imagery basemap, elevation surface, and buildings layer to it
val buildingsScene = ArcGISScene(BasemapStyle.ArcGISImagery).apply {
baseSurface = surface
operationalLayers.add(buildingsSceneLayer)
}
// create viewshed from the initial location
viewshed = ExploratoryLocationViewshed(
location = initLocation,
heading = initHeading,
pitch = initPitch,
horizontalAngle = initHorizontalAngle,
verticalAngle = initVerticalAngle,
minDistance = initMinDistance,
maxDistance = initMaxDistance
).apply {
frustumOutlineVisible = initFrustumVisible
}
// add the buildings scene to the sceneView
scene = buildingsScene.apply {
baseSurface = surface
initialViewpoint = Viewpoint(initLocation, initialCamera)
}
// add the viewshed to the analysisOverlay of the scene view
analysisOverlay.apply {
analyses.add(viewshed)
isVisible = initAnalysisVisible
}
}
fun setHeading(sliderHeading: Float) {
viewshed.heading = sliderHeading.toDouble()
_viewshedUiState.update { it.copy(heading = sliderHeading) }
}
fun setMaximumDistance(sliderValue: Float) {
viewshed.maxDistance = sliderValue.toDouble()
_viewshedUiState.update { it.copy(maxDistance = sliderValue) }
}
fun setMinimumDistance(sliderValue: Float) {
viewshed.minDistance = sliderValue.toDouble()
_viewshedUiState.update { it.copy(minDistance = sliderValue) }
}
fun setVerticalAngle(sliderValue: Float) {
viewshed.verticalAngle = sliderValue.toDouble()
_viewshedUiState.update { it.copy(verticalAngle = sliderValue) }
}
fun setHorizontalAngle(sliderValue: Float) {
viewshed.horizontalAngle = sliderValue.toDouble()
_viewshedUiState.update { it.copy(horizontalAngle = sliderValue) }
}
fun setPitch(sliderValue: Float) {
viewshed.pitch = sliderValue.toDouble()
_viewshedUiState.update { it.copy(pitch = sliderValue) }
}
fun setFrustumVisibility(checkedValue: Boolean) {
viewshed.frustumOutlineVisible = checkedValue
_viewshedUiState.update { it.copy(isFrustumVisible = checkedValue) }
}
fun setAnalysisVisibility(checkedValue: Boolean) {
viewshed.isVisible = checkedValue
_viewshedUiState.update { it.copy(isAnalysisVisible = checkedValue) }
}
fun resetViewshedOptions() {
viewshed.apply {
heading = initHeading
pitch = initPitch
horizontalAngle = initHorizontalAngle
verticalAngle = initVerticalAngle
minDistance = initMinDistance
maxDistance = initMaxDistance
frustumOutlineVisible = initFrustumVisible
isVisible = initAnalysisVisible
}
_viewshedUiState.value = initViewshedUiState
viewModelScope.launch {
sceneViewProxy.setViewpointCameraAnimated(
camera = initialCamera,
duration = 1.seconds
)
}
}
/**
* Animates the camera to a meaningful overview centered on the viewshed analysis location.
*/
fun setViewpointToAnalysisExtent() {
viewModelScope.launch {
sceneViewProxy.setViewpointCameraAnimated(
camera = Camera(
lookAtPoint = viewshed.location,
distance = viewshed.maxDistance ?: initMaxDistance,
heading = viewshed.heading,
pitch = viewshed.pitch,
roll = 0.0
),
duration = 2.seconds
)
}
}
/**
* Display dialog if there is an error with analysis.
*/
fun analysisViewStatusListener(event: GeoView.GeoViewAnalysisViewStatusChanged) {
if (event.analysisViewStatus is AnalysisViewStatus.Error) {
messageDialogVM.showMessageDialog(
throwable = (event.analysisViewStatus as AnalysisViewStatus.Error).details
)
}
}
}
data class ViewshedUiState(
val heading: Float,
val pitch: Float,
val horizontalAngle: Float,
val verticalAngle: Float,
val minDistance: Float,
val maxDistance: Float,
val isFrustumVisible: Boolean,
val isAnalysisVisible: Boolean
)