Skip to content

Commit

Permalink
Sample: Get elevation at point on surface (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
TADraeseke authored Jan 22, 2025
2 parents 1e299cd + f9b7fa3 commit 7645920
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 0 deletions.
37 changes: 37 additions & 0 deletions samples/get-elevation-at-point-on-surface/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Get elevation at point on surface

Get the elevation for a given point on a surface in a scene.

![Image of get elevation at point](get-elevation-at-point-on-surface.png)

## Use case

Knowing the elevation at a given point in a landscape can aid in navigation, planning and survey in the field.

## How to use the sample

Tap anywhere on the surface to get the elevation at that point. Elevation is reported in meters since the scene view is in WGS84.

## How it works

1. Create a composable `SceneView` and `ArcGISScene` with an imagery base map.
2. Set an `ArcGISTiledElevationSource` as the elevation source of the scene's `baseSurface`.
3. Use the `screenToLocation(screenCoordinate)` function on the scene view to convert the tapped screen coordinate into a point on surface.
4. Use the `getElevation(scenePoint)` method on the base surface to asynchronously get the elevation.

## Relevant API

* ArcGISTiledElevationSource
* BaseSurface
* ElevationSource
* SceneView

## Additional information

`getElevation(scenePoint)` retrieves the most accurate available elevation value at a given point. To do this, the method must go to the server or local raster file and load the highest level of detail of data for the target location and return the elevation value.

If multiple elevation sources are present in the surface, the top most visible elevation source with a valid elevation in the given location is used to determine the result.

## Tags

elevation, surface
31 changes: 31 additions & 0 deletions samples/get-elevation-at-point-on-surface/README.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"category": "Scenes",
"description": "Get the elevation for a given point on a surface in a scene.",
"formal_name": "GetElevationAtPointOnSurface",
"ignore": false,
"images": [
"get-elevation-at-point-on-surface.png"
],
"keywords": [
"elevation",
"surface",
"ArcGISTiledElevationSource",
"BaseSurface",
"ElevationSource",
"SceneView"
],
"language": "kotlin",
"redirect_from": "",
"relevant_apis": [
"ArcGISTiledElevationSource",
"BaseSurface",
"ElevationSource",
"SceneView"
],
"snippets": [
"src/main/java/com/esri/arcgismaps/sample/getelevationatpointonsurface/components/GetElevationAtPointOnSurfaceViewModel.kt",
"src/main/java/com/esri/arcgismaps/sample/getelevationatpointonsurface/MainActivity.kt",
"src/main/java/com/esri/arcgismaps/sample/getelevationatpointonsurface/screens/GetElevationAtPointOnSurfaceScreen.kt"
],
"title": "Get elevation at point on surface"
}
22 changes: 22 additions & 0 deletions samples/get-elevation-at-point-on-surface/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
alias(libs.plugins.arcgismaps.android.library)
alias(libs.plugins.arcgismaps.android.library.compose)
alias(libs.plugins.arcgismaps.kotlin.sample)
alias(libs.plugins.gradle.secrets)
}

secrets {
// this file doesn't contain secrets, it just provides defaults which can be committed into git.
defaultPropertiesFileName = "secrets.defaults.properties"
}

android {
namespace = "com.esri.arcgismaps.sample.getelevationatpointonsurface"
buildFeatures {
buildConfig = true
}
}

dependencies {
// Only module specific dependencies needed here
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />

<application><activity
android:exported="true"
android:name=".MainActivity"
android:label="@string/get_elevation_at_point_on_surface_app_name">

</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* 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.getelevationatpointonsurface

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
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.getelevationatpointonsurface.screens.GetElevationAtPointOnSurfaceScreen

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 {
GetElevationAtPointOnSurfaceApp()
}
}
}

@Composable
private fun GetElevationAtPointOnSurfaceApp() {
Surface(color = MaterialTheme.colorScheme.background) {
GetElevationAtPointOnSurfaceScreen(
sampleName = getString(R.string.get_elevation_at_point_on_surface_app_name)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* 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.getelevationatpointonsurface.components

import android.app.Application
import android.widget.Toast
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.mapping.ArcGISScene
import com.arcgismaps.mapping.ArcGISTiledElevationSource
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbol
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyle
import com.arcgismaps.mapping.view.Camera
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.arcgismaps.mapping.view.SingleTapConfirmedEvent
import com.arcgismaps.toolkit.geoviewcompose.SceneViewProxy
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch
import kotlin.math.roundToInt


class GetElevationAtPointOnSurfaceViewModel(application: Application) : AndroidViewModel(application) {

val sceneViewProxy = SceneViewProxy()

val arcGISScene = ArcGISScene(BasemapStyle.ArcGISImagery).apply {
// Add an elevation source to the scene's base surface.
baseSurface.elevationSources.add(
ArcGISTiledElevationSource(
"https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer"
)
)
}

// Graphics overlay used to add feature graphics to the map
val graphicsOverlay = GraphicsOverlay()

// Create a message dialog view model for handling error messages
val messageDialogVM = MessageDialogViewModel()

init {
viewModelScope.launch {
arcGISScene.load().onSuccess {
// Set the initial viewpoint of the scene view, once loaded
sceneViewProxy.setViewpointCamera(
Camera(
latitude = 28.42, longitude = 83.9, altitude = 10000.0, heading = 10.0, pitch = 80.0, roll = 0.0
)
)
}.onFailure { error ->
messageDialogVM.showMessageDialog(
"Failed to load map", error.message.toString()
)
}
}
}

/**
* Get the elevation at the point on the surface where the user taps.
*/
fun getElevation(singleTapConfirmedEvent: SingleTapConfirmedEvent) {
viewModelScope.launch {
sceneViewProxy.screenToLocation(singleTapConfirmedEvent.screenCoordinate).onSuccess { scenePoint ->
arcGISScene.baseSurface.getElevation(scenePoint).onSuccess { elevation ->
// Create a point symbol to mark where elevation is being measured
val circleSymbol = SimpleMarkerSymbol(
style = SimpleMarkerSymbolStyle.Circle, color = com.arcgismaps.Color.red, size = 10f
)
// Add the graphic to the graphics overlay
graphicsOverlay.graphics.add(Graphic(geometry = scenePoint, symbol = circleSymbol))
// Show a toast message with the elevation
Toast.makeText(
getApplication(), "Elevation at point is ${elevation.roundToInt()} meters", Toast.LENGTH_LONG
).show()
}.onFailure { error ->
messageDialogVM.showMessageDialog(
"Failed to get elevation", error.message.toString()
)
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* 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.getelevationatpointonsurface.screens

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import com.arcgismaps.toolkit.geoviewcompose.SceneView
import com.esri.arcgismaps.sample.getelevationatpointonsurface.components.GetElevationAtPointOnSurfaceViewModel
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialog
import com.esri.arcgismaps.sample.sampleslib.components.SampleTopAppBar

/**
* Main screen layout for the sample app
*/
@Composable
fun GetElevationAtPointOnSurfaceScreen(sampleName: String) {
val sceneViewModel: GetElevationAtPointOnSurfaceViewModel = viewModel()
Scaffold(topBar = { SampleTopAppBar(title = sampleName) }, content = {
Column(
modifier = Modifier
.fillMaxSize()
.padding(it),
) {
SceneView(
modifier = Modifier
.fillMaxSize()
.weight(1f),
arcGISScene = sceneViewModel.arcGISScene,
graphicsOverlays = listOf(sceneViewModel.graphicsOverlay),
// On single tap, call get elevation in the view model
onSingleTapConfirmed = sceneViewModel::getElevation,
sceneViewProxy = sceneViewModel.sceneViewProxy,
)
}

sceneViewModel.messageDialogVM.apply {
if (dialogStatus) {
MessageDialog(
title = messageTitle, description = messageDescription, onDismissRequest = ::dismissDialog
)
}
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="get_elevation_at_point_on_surface_app_name">Get elevation at point on surface</string>
</resources>

0 comments on commit 7645920

Please sign in to comment.