|
| 1 | +/* Copyright 2024 Esri |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.arcgismaps.sample.addencexchangeset.components |
| 18 | + |
| 19 | +import android.app.Application |
| 20 | +import androidx.compose.runtime.getValue |
| 21 | +import androidx.compose.runtime.mutableStateOf |
| 22 | +import androidx.compose.runtime.setValue |
| 23 | +import androidx.lifecycle.AndroidViewModel |
| 24 | +import androidx.lifecycle.viewModelScope |
| 25 | +import com.arcgismaps.geometry.Envelope |
| 26 | +import com.arcgismaps.geometry.GeometryEngine |
| 27 | +import com.arcgismaps.hydrography.EncCell |
| 28 | +import com.arcgismaps.hydrography.EncEnvironmentSettings |
| 29 | +import com.arcgismaps.hydrography.EncExchangeSet |
| 30 | +import com.arcgismaps.mapping.ArcGISMap |
| 31 | +import com.arcgismaps.mapping.BasemapStyle |
| 32 | +import com.arcgismaps.mapping.Viewpoint |
| 33 | +import com.arcgismaps.mapping.layers.EncLayer |
| 34 | +import com.esri.arcgismaps.sample.addencexchangeset.R |
| 35 | +import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel |
| 36 | +import kotlinx.coroutines.launch |
| 37 | +import java.io.File |
| 38 | + |
| 39 | +class MapViewModel(application: Application) : AndroidViewModel(application) { |
| 40 | + private val provisionPath: String by lazy { |
| 41 | + application.getExternalFilesDir(null)?.path.toString() + |
| 42 | + File.separator + |
| 43 | + application.getString(R.string.add_enc_exchange_set_app_name) |
| 44 | + } |
| 45 | + |
| 46 | + // Paths to ENC data and hydrology resources |
| 47 | + private val encResourcesPath = provisionPath + application.getString(R.string.enc_res_dir) |
| 48 | + private val encDataPath = provisionPath + application.getString(R.string.enc_data_dir) |
| 49 | + |
| 50 | + // Create an ENC exchange set from the local ENC data |
| 51 | + private val encExchangeSet = EncExchangeSet(listOf(encDataPath)) |
| 52 | + private val encEnvironmentSettings: EncEnvironmentSettings = EncEnvironmentSettings |
| 53 | + |
| 54 | + // Create an empty map, to be updated once ENC data is loaded |
| 55 | + var arcGISMap by mutableStateOf(ArcGISMap()) |
| 56 | + |
| 57 | + // Create a message dialog view model for handling error messages |
| 58 | + val messageDialogVM = MessageDialogViewModel() |
| 59 | + |
| 60 | + init { |
| 61 | + // Provide ENC environment with location of ENC resources and configure SENC caching location |
| 62 | + encEnvironmentSettings.resourcePath = encResourcesPath |
| 63 | + encEnvironmentSettings.sencDataPath = application.externalCacheDir?.path |
| 64 | + |
| 65 | + viewModelScope.launch { |
| 66 | + encExchangeSet.load().onSuccess { |
| 67 | + |
| 68 | + // Calculate the combined extent of all datasets in the exchange set |
| 69 | + val exchangeSetExtent: Envelope? = encExchangeSet.extentOrNull() |
| 70 | + |
| 71 | + // Set the map to the oceans basemap style, and viewpoint to the exchange set extent |
| 72 | + arcGISMap = ArcGISMap(BasemapStyle.ArcGISOceans).apply { |
| 73 | + exchangeSetExtent?.let { |
| 74 | + initialViewpoint = Viewpoint(exchangeSetExtent) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + encExchangeSet.datasets.forEach { encDataset -> |
| 79 | + // Create a layer for each ENC dataset and add it to the map |
| 80 | + val encCell = EncCell(encDataset) |
| 81 | + val encLayer = EncLayer(encCell) |
| 82 | + arcGISMap.operationalLayers.add(encLayer) |
| 83 | + |
| 84 | + encLayer.load().onFailure { err -> |
| 85 | + messageDialogVM.showMessageDialog( |
| 86 | + "Error loading ENC layer", |
| 87 | + err.message.toString() |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + }.onFailure { err -> |
| 92 | + messageDialogVM.showMessageDialog( |
| 93 | + "Error loading ENC exchange set", |
| 94 | + err.message.toString() |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Get the combined extent of every dataset in the exchange set. |
| 103 | + */ |
| 104 | +private fun EncExchangeSet.extentOrNull(): Envelope? { |
| 105 | + var extent: Envelope? = null |
| 106 | + |
| 107 | + datasets.forEach { dataset -> |
| 108 | + if (extent == null) { |
| 109 | + extent = dataset.extent |
| 110 | + } |
| 111 | + |
| 112 | + if (extent != null && dataset.extent != null) { |
| 113 | + // Update the combined extent of the exchange set if geometry engine returns non-null |
| 114 | + extent = GeometryEngine.combineExtentsOrNull(extent!!, dataset.extent!!) ?: extent |
| 115 | + } |
| 116 | + } |
| 117 | + return extent |
| 118 | +} |
0 commit comments