Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
44 changes: 44 additions & 0 deletions samples/style-features-with-custom-dictionary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Style features with custom dictionary

Use a custom dictionary style created from a web style or local style file (.stylx) to symbolize features using a variety of attribute values.

![Image of style features with custom dictionary](style-features-with-custom-dictionary.png)

## Use case

When symbolizing geoelements in your map, you may need to convey several pieces of information with a single symbol. You could try to symbolize such data using a unique value renderer, but as the number of fields and values increases, that approach becomes impractical. With a dictionary renderer you can build each symbol on-the-fly, driven by one or more attribute values, and handle a nearly infinite number of unique combinations.

## How to use the sample

Use the radio buttons to toggle between the dictionary symbols from the web style and style file. Pan and zoom around the map to see the symbology from the chosen dictionary symbol style. The web style and style file are slightly different to each other to give a visual indication of the switch between the two.

## How it works

1. Create a `PortalItem`, referring to a `Portal` and the item ID of the web style.
2. Based on the style selected:
* If the web style toggle has been selected, create a new `DictionarySymbolStyle` from the portal item using `DictionarySymbolStyle(portalItem)`, and load it
* If the file style toggle has been selected, create a new `DictionarySymbolStyle` using `DictionarySymbolStyle.createFromFile(stylxFile.getAbsolutePath())`
3. Create a new `DictionaryRenderer`, providing the dictionary symbol style.
4. Apply the dictionary renderer to a feature layer using `featureLayer.renderer = dictionaryRenderer`.
5. Add the feature layer to the map's operational layers using `getOperationalLayers().add(featureLayer)`.

## Relevant API

* DictionaryRenderer
* DictionarySymbolStyle
* Portal
* PortalItem

## About the data

The data used in this sample is from a feature layer showing a subset of [restaurants in Redlands, CA](https://www.arcgis.com/home/item.html?id=3daf83e1ec0941428526a07f2d2ae414) hosted as a feature service with attributes for rating, style, health score, and open hours.

The feature layer is symbolized using a dictionary renderer that displays a single symbol for all of these variables. The renderer uses symbols from a custom restaurant dictionary style created from a [stylx file](https://arcgis.com/home/item.html?id=751138a2e0844e06853522d54103222a) and a [web style](https://arcgis.com/home/item.html?id=adee951477014ec68d7cf0ea0579c800), available as items from ArcGIS Online, to show unique symbols based on several feature attributes. The symbols it contains were created using ArcGIS Pro. The logic used to apply the symbols comes from an Arcade script embedded in the stylx file (which is a SQLite database), along with a JSON string that defines expected attribute names and configuration properties.

## Additional information

For information about creating your own custom dictionary style, see the open source [dictionary renderer toolkit](https://esriurl.com/DictionaryToolkit) on *GitHub*.

## Tags

dictionary, military, portal, portal item, renderer, style, stylx, unique value, visualization
39 changes: 39 additions & 0 deletions samples/style-features-with-custom-dictionary/README.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"category": "Visualization",
"description": "Use a custom dictionary style created from a web style or local style file (.stylx) to symbolize features using a variety of attribute values.",
"formal_name": "StyleFeaturesWithCustomDictionary",
"ignore": false,
"images": [
"style-features-with-custom-dictionary.png"
],
"keywords": [
"dictionary",
"military",
"portal",
"portal item",
"renderer",
"style",
"stylx",
"unique value",
"visualization",
"DictionaryRenderer",
"DictionarySymbolStyle",
"Portal",
"PortalItem"
],
"language": "kotlin",
"redirect_from": "",
"relevant_apis": [
"DictionaryRenderer",
"DictionarySymbolStyle",
"Portal",
"PortalItem"
],
"snippets": [
"src/main/java/com/esri/arcgismaps/sample/stylefeatureswithcustomdictionary/components/StyleFeaturesWithCustomDictionaryViewModel.kt",
"src/main/java/com/esri/arcgismaps/sample/stylefeatureswithcustomdictionary/MainActivity.kt",
"src/main/java/com/esri/arcgismaps/sample/stylefeatureswithcustomdictionary/DownloadActivity.kt",
"src/main/java/com/esri/arcgismaps/sample/stylefeatureswithcustomdictionary/screens/StyleFeaturesWithCustomDictionaryScreen.kt"
],
"title": "Style features with custom dictionary"
}
22 changes: 22 additions & 0 deletions samples/style-features-with-custom-dictionary/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.stylefeatureswithcustomdictionary"
buildFeatures {
buildConfig = true
}
}

dependencies {
// Only module specific dependencies needed here
}
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:exported="true"
android:name=".MainActivity"
android:label="@string/style_features_with_custom_dictionary_app_name">

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

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

import android.content.Intent
import android.os.Bundle
import com.esri.arcgismaps.sample.sampleslib.DownloaderActivity

class DownloadActivity : DownloaderActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
downloadAndStartSample(
Intent(this, MainActivity::class.java),
getString(R.string.style_features_with_custom_dictionary_app_name),
listOf(
"https://www.arcgis.com/home/item.html?id=751138a2e0844e06853522d54103222a",
)
)
}
}
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.stylefeatureswithcustomdictionary

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.stylefeatureswithcustomdictionary.screens.StyleFeaturesWithCustomDictionaryScreen

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

@Composable
private fun StyleFeaturesWithCustomDictionaryApp() {
Surface(color = MaterialTheme.colorScheme.background) {
StyleFeaturesWithCustomDictionaryScreen(
sampleName = getString(R.string.style_features_with_custom_dictionary_app_name)
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* 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.stylefeatureswithcustomdictionary.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.Viewpoint
import com.arcgismaps.mapping.layers.FeatureLayer
import com.arcgismaps.mapping.symbology.DictionaryRenderer
import com.arcgismaps.mapping.symbology.DictionarySymbolStyle
import com.arcgismaps.portal.Portal
import com.arcgismaps.mapping.PortalItem
import com.arcgismaps.data.ServiceFeatureTable
import com.esri.arcgismaps.sample.sampleslib.components.MessageDialogViewModel
import com.esri.arcgismaps.sample.stylefeatureswithcustomdictionary.R
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import java.io.File

private const val RESTAURANTS_SERVICE_URL =
"https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/Redlands_Restaurants/FeatureServer/0"
private const val WEB_STYLE_ITEM_ID = "adee951477014ec68d7cf0ea0579c800"

class StyleFeaturesWithCustomDictionaryViewModel(app: Application) : AndroidViewModel(app) {

// Feature layer showing restaurant data in Redlands, CA
private val restaurantFeatureLayer = FeatureLayer.createWithFeatureTable(
featureTable = ServiceFeatureTable(uri = RESTAURANTS_SERVICE_URL)
)
var arcGISMap = ArcGISMap(BasemapStyle.ArcGISTopographic).apply {
initialViewpoint = Viewpoint(
latitude = 34.0543,
longitude = -117.1963,
scale = 1e4
)
}
private var dictionaryRendererFromStyleFile: DictionaryRenderer? = null
private var dictionaryRendererFromWebStyle: DictionaryRenderer? = null

private val _selectedStyle = MutableStateFlow(CustomDictionaryStyle.StyleFile)

val messageDialogVM = MessageDialogViewModel()
private val provisionPath: String by lazy {
app.getExternalFilesDir(null)?.path.toString() + File.separator + app.getString(R.string.style_features_with_custom_dictionary_app_name)
}

init {
viewModelScope.launch {
// Prepare the dictionary renderer from the style file
dictionaryRendererFromStyleFile = createDictionaryRendererFromStyleFile()
// Prepare the dictionary renderer from the web style
dictionaryRendererFromWebStyle = createDictionaryRendererFromWebStyle().getOrElse {
messageDialogVM.showMessageDialog(it)
return@launch
}

// Apply the renderer for the initially selected style
applyRendererForSelectedStyle()

// Add the restaurant layer to the map and load the map
arcGISMap.apply {
operationalLayers.add(restaurantFeatureLayer)
}.load().onFailure {
messageDialogVM.showMessageDialog(it)
}
}
}

/**
* Update the current dictionary style selection and apply the renderer to the feature layer.
*/
fun updateSelectedStyle(style: CustomDictionaryStyle) {
_selectedStyle.value = style
applyRendererForSelectedStyle()
}

/**
* Apply the dictionary renderer to the restaurants layer.
*/
private fun applyRendererForSelectedStyle() {
restaurantFeatureLayer.renderer = when (_selectedStyle.value) {
CustomDictionaryStyle.StyleFile -> dictionaryRendererFromStyleFile
CustomDictionaryStyle.WebStyle -> dictionaryRendererFromWebStyle
}
}

/**
* Create a dictionary renderer from the style file included in the app's assets.
*/
private fun createDictionaryRendererFromStyleFile(): DictionaryRenderer {
val restaurantsStyleFile = File(provisionPath, "Restaurant.stylx")
check(restaurantsStyleFile.exists()) {
"Style file not found. Expected at: ${restaurantsStyleFile.canonicalPath}"
}
val restaurantStyle = DictionarySymbolStyle.createFromFile(restaurantsStyleFile.path)

return DictionaryRenderer(dictionarySymbolStyle = restaurantStyle)
}

/**
* Create a dictionary renderer from the web style hosted as a Portal item.
* Maps the feature layer's field "healthgrade" to the dictionary style's expected field "Inspection".
*/
private suspend fun createDictionaryRendererFromWebStyle(): Result<DictionaryRenderer> {
val portal = Portal(url = "https://www.arcgis.com")
val portalItem = PortalItem(portal = portal, itemId = WEB_STYLE_ITEM_ID)
val restaurantSymbolStyle = DictionarySymbolStyle(portalItem).apply {
load().getOrElse {
return Result.failure(it)
}
}

return Result.success(
DictionaryRenderer(
dictionarySymbolStyle = restaurantSymbolStyle,
symbologyFieldOverrides = mapOf("healthgrade" to "Inspection")
)
)
}
}

/**
* Enum representing the two custom dictionary styles available in this sample.
*/
enum class CustomDictionaryStyle {
StyleFile,
WebStyle
}
Loading
Loading