Custom dictionary style

View on GitHubSample viewer app

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 custom dictionary style

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 DictionarySymbolStyle from a local .stylx file.
  2. Create a DictionaryRenderer from this dictionary symbol style.
  3. Create a PortalItem, referring to a Portal and the item ID of the web style.
  4. Create a DictionarySymbolStyle from this portal item.
  5. Create a DictionaryRenderer from this dictionary symbol style.
  6. Based on the style selected, use featureLayer.setRenderer(...) to the correct dictionary symbol style.

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 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 and a web style, 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 on GitHub.

Offline data

  1. Download the data from ArcGIS Online.
  2. Extract the contents of the downloaded zip file to disk.
  3. Open your command prompt and navigate to the folder where you extracted the contents of the data from step 1.
  4. Execute the following command:

adb push Restaurant.stylx /Android/data/com.esri.arcgisruntime.sample.customdictionarystyle/files/Restaurant.stylx

Link Local Location
Restaurant stylx /Android/data/com.esri.arcgisruntime.sample.customdictionarystyle/files/Restaurant.stylx

Tags

dictionary, military, portal, portal item, renderer, style, stylx, unique value, visualization

Sample Code

MainActivity.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.esri.arcgisruntime.sample.customdictionarystyle

import android.os.Bundle
import android.widget.RadioButton
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.data.ServiceFeatureTable
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.customdictionarystyle.databinding.ActivityMainBinding
import com.esri.arcgisruntime.portal.Portal
import com.esri.arcgisruntime.portal.PortalItem
import com.esri.arcgisruntime.symbology.DictionaryRenderer
import com.esri.arcgisruntime.symbology.DictionarySymbolStyle

class MainActivity : AppCompatActivity() {

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    private val styleFileRadioButton: RadioButton by lazy {
        activityMainBinding.styleControlsLayout.styleFileRadioButton
    }

    private val webStyleRadioButton: RadioButton by lazy {
        activityMainBinding.styleControlsLayout.webStyleRadioButton
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activityMainBinding.root)

        // authentication with an API key or named user is required to access basemaps and other
        // location services
        ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)

        // create a feature layer from a service feature table
        val featureTable =
            ServiceFeatureTable("https://services2.arcgis.com/ZQgQTuoyBrtmoGdP/arcgis/rest/services/Redlands_Restaurants/FeatureServer/0")
        val featureLayer = FeatureLayer(featureTable)

        // create a new map with a streets basemap and set it to the map view
        mapView.map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC).apply {
            // add the the feature layer to the map's operational layers
            operationalLayers.add(featureLayer)
            // set the initial viewpoint to the Esri Redlands campus
            initialViewpoint = Viewpoint(34.0574, -117.1963, 5000.0)
        }

        // create a dictionary symbol style from the stylx file
        val dictionarySymbolStyleFromFile =
            DictionarySymbolStyle.createFromFile(getExternalFilesDir(null)?.path + "/Restaurant.stylx")
        // create a new dictionary renderer from the dictionary symbol style
        val dictionaryRendererFromFile = DictionaryRenderer(dictionarySymbolStyleFromFile)

        // on style file click
        styleFileRadioButton.setOnClickListener {
            // set the feature layer renderer to the dictionary renderer from local stylx file
            featureLayer.renderer = dictionaryRendererFromFile
        }
        // set the initial state to use the dictionary renderer from local stylx file
        styleFileRadioButton.performClick()

        // create a portal item using the portal and the item id of the dictionary web style
        val portal = Portal("https://arcgisruntime.maps.arcgis.com")
        val portalItem = PortalItem(portal, "adee951477014ec68d7cf0ea0579c800")
        // map the input fields in the feature layer to the dictionary symbol style's expected fields for symbols and text
        val fieldMap: HashMap<String, String> = HashMap()
        fieldMap["healthgrade"] = "Inspection"
        // create a new dictionary symbol style from the web style in the portal item
        val dictionarySymbolStyleFromPortal = DictionarySymbolStyle(portalItem)
        // create a new dictionary renderer from the dictionary symbol style
        val dictionaryRendererFromPortal =
            DictionaryRenderer(dictionarySymbolStyleFromPortal, fieldMap, HashMap())

        // on web style click
        webStyleRadioButton.setOnClickListener {
            // set the feature layer renderer to the dictionary renderer from portal
            featureLayer.renderer = dictionaryRendererFromPortal
        }
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.