Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colinanderson/add raster from file #288

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions samples/add-raster-from-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Add raster from file

Create and use a raster layer made from a local raster file.

![Image of add raster from file](add-raster-from-file.png)

## Use case

Rasters can be digital aerial photographs, imagery from satellites, digital pictures, or even scanned maps. An end-user will frequently need to import raster files acquired through various data-collection methods into their map to view and analyze the data.

## How to use the sample

When the sample starts, a raster will be loaded from a file and displayed in the map view.

## How it works

1. Create a `Raster` from a raster file.
2. Create a `RasterLayer` from the raster.
3. Add it as an operational layer with `map.getOperationalLayers().add(rasterLayer)`.

## Relevant API

* Raster
* RasterLayer

## Additional information

See the topic [What is raster data?](http://desktop.arcgis.com/en/arcmap/10.3/manage-data/raster-and-images/what-is-raster-data.htm) in the *ArcMap* documentation for more information about raster images.

This sample uses the GeoViewCompose Toolkit module to be able to implement a Composable MapView.

## Tags

data, geoviewcompose, image, import, layer, raster, toolkit, visualization
34 changes: 34 additions & 0 deletions samples/add-raster-from-file/README.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"category": "Layers",
"description": "Create and use a raster layer made from a local raster file.",
"formal_name": "AddRasterFromFile",
"ignore": false,
"images": [
"add-raster-from-file.png"
],
"keywords": [
"data",
"geoviewcompose",
"image",
"import",
"layer",
"raster",
"toolkit",
"visualization",
"Raster",
"RasterLayer"
],
"language": "kotlin",
"redirect_from": "",
"relevant_apis": [
"Raster",
"RasterLayer"
],
"snippets": [
"src/main/java/com/esri/arcgismaps/sample/addrasterfromfile/components/AddRasterFromFileViewModel.kt",
"src/main/java/com/esri/arcgismaps/sample/addrasterfromfile/DownloadActivity.kt",
"src/main/java/com/esri/arcgismaps/sample/addrasterfromfile/MainActivity.kt",
"src/main/java/com/esri/arcgismaps/sample/addrasterfromfile/screens/AddRasterFromFileScreen.kt"
],
"title": "Add raster from file"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions samples/add-raster-from-file/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.addrasterfromfile"
buildFeatures {
buildConfig = true
}
}

dependencies {
// Only module specific dependencies needed here
}
18 changes: 18 additions & 0 deletions samples/add-raster-from-file/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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:name=".DownloadActivity"
android:exported="true"
android:label="@string/add_raster_from_file_app_name">
</activity>
<activity
android:exported="true"
android:name=".MainActivity">

</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright 2024 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.addrasterfromfile

import android.content.Intent
import android.os.Bundle
import com.esri.arcgismaps.sample.sampleslib.DownloaderActivity
import kotlin.collections.listOf
import kotlin.jvm.java

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.add_raster_from_file_app_name),
listOf(
"https://arcgisruntime.maps.arcgis.com/home/item.html?id=7c4c679ab06a4df19dc497f577f111bd"
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright 2024 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.addrasterfromfile

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.addrasterfromfile.screens.AddRasterFromFileScreen

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

@Composable
private fun AddRasterFromFileApp() {
Surface(color = MaterialTheme.colorScheme.background) {
AddRasterFromFileScreen(
sampleName = getString(R.string.add_raster_from_file_app_name)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Copyright 2024 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.addrasterfromfile.components

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.layers.RasterLayer
import com.arcgismaps.raster.Raster
import com.arcgismaps.toolkit.geoviewcompose.MapViewProxy
import com.esri.arcgismaps.sample.addrasterfromfile.R
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import kotlinx.coroutines.launch
import java.io.File

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

private val provisionPath: String by lazy { application.getExternalFilesDir(null)?.path.toString() +
File.separator +
application.getString(R.string.add_raster_from_file_app_name)
}

val mapViewProxy = MapViewProxy()

// create a raster
val raster = Raster.createWithPath(provisionPath +
File.separator + "raster-file" + File.separator + "Shasta.tif")

// create a raster layer
val rasterLayer = RasterLayer(raster)

val arcGISMap = ArcGISMap(BasemapStyle.ArcGISImagery).apply {
operationalLayers.add(rasterLayer)
}

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

init {
viewModelScope.launch {
arcGISMap.load().onFailure { error ->
messageDialogVM.showMessageDialog(
"Failed to load map",
error.message.toString()
)
}
rasterLayer.load().onSuccess {
// Set the viewpoint to the raster layer's extent
val extent = rasterLayer.fullExtent
if (extent != null) {
mapViewProxy.setViewpointGeometry(extent, paddingInDips = 20.0)
}
}.onFailure { error ->
messageDialogVM.showMessageDialog(
"Failed to load raster layer",
error.message.toString()
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright 2024 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.addrasterfromfile.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.MapView
import com.esri.arcgismaps.sample.addrasterfromfile.components.AddRasterFromFileViewModel
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 AddRasterFromFileScreen(sampleName: String) {
val mapViewModel: AddRasterFromFileViewModel = viewModel()
Scaffold(
topBar = { SampleTopAppBar(title = sampleName) },
content = {
Column(
modifier = Modifier
.fillMaxSize()
.padding(it),
) {
MapView(
modifier = Modifier
.fillMaxSize()
.weight(1f),
arcGISMap = mapViewModel.arcGISMap,
mapViewProxy = mapViewModel.mapViewProxy
)
}

mapViewModel.messageDialogVM.apply {
if (dialogStatus) {
MessageDialog(
title = messageTitle,
description = messageDescription,
onDismissRequest = ::dismissDialog
)
}
}
}
)
}
3 changes: 3 additions & 0 deletions samples/add-raster-from-file/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="add_raster_from_file_app_name">Add raster from file</string>
</resources>
Loading