Feature layer dictionary renderer

View on GitHub
Sample viewer app

Convert features into graphics to show them with mil2525d symbols.

Image of feature layer dictionary renderer

Use case

A dictionary renderer uses a style file along with a rule engine to display advanced symbology. This is useful for displaying features using precise military symbology.

How to use the sample

Pan and zoom around the map. Observe the displayed military symbology on the map.

How it works

  1. Create a Geodatabase using new Geodatabase(geodatabasePath).
  2. Load the geodatabase asynchronously using Geodatabase.loadAsync().
  3. Instantiate a DictionarySymbolStyle using DictionarySymbolStyle(dictionarySymbolStylePath).
  4. Load the symbol dictionary asynchronously using dictionarySymbolStyle.loadAsync().
  5. Wait for geodatabase to completely load by connecting to Geodatabase.addDoneLoadingListener().
  6. For each GeoDatabaseFeatureTable in the GeoDatabase, create a feature layer with it, then add it to the map using Map.getOperationalLayers().add(FeatureLayer).
  7. Create DictionaryRenderer(dictionarySymbolStyle) and attach to the feature layer using FeatureLayer.setRenderer(dictionaryRenderer).
  8. Set the viewpoint of the map view to the extent of the feature layer using MapView.setViewpointGeometryAsync(featureLayer.getFullExtent()).

Relevant API

  • DictionaryRenderer
  • DictionarySymbolStyle

Offline Data

  1. Download the data stylx file and geodatabase 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. Push the data into the scoped storage of the sample app: * adb push mil2525d.stylx /Android/data/com.esri.arcgisruntime.sample.featurelayerdictionaryrenderer/files/mil2525d.stylx

    • adb push militaryoverlay.geodatabase /Android/data/com.esri.arcgisruntime.sample.featurelayerdictionaryrenderer/files/militaryoverlay.geodatabase

Tags

military, symbol

Sample Code

MainActivity.java
                                                                                                                                
/* Copyright 2018 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.arcgisruntime.sample.featurelayerdictionaryrenderer;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.data.Geodatabase;
import com.esri.arcgisruntime.data.GeodatabaseFeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.DictionaryRenderer;
import com.esri.arcgisruntime.symbology.DictionarySymbolStyle;

public class MainActivity extends AppCompatActivity {

  private static final String TAG = MainActivity.class.getSimpleName();

  private MapView mMapView;
  // objects that implement Loadable must be class fields to prevent being garbage collected before loading
  private Geodatabase mGeodatabase;
  private DictionarySymbolStyle mSymbolDictionary;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    // get the reference to the map view
    mMapView = findViewById(R.id.mapView);

    ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
    // set the map to the map view
    mMapView.setMap(map);

    // load geo-database from local location
    mGeodatabase = new Geodatabase(
        getExternalFilesDir(null) + getString(R.string.militaryoverlay_geodatabase));
    mGeodatabase.loadAsync();

    // render tells layer what symbols to apply to what features
    mSymbolDictionary = DictionarySymbolStyle.createFromFile(getExternalFilesDir(null) + getString(R.string.mil2525d_stylx));
    mSymbolDictionary.loadAsync();

    mGeodatabase.addDoneLoadingListener(() -> {
      if (mGeodatabase.getLoadStatus() == LoadStatus.LOADED) {

        for (GeodatabaseFeatureTable table : mGeodatabase.getGeodatabaseFeatureTables()) {
          // add each layer to map
          FeatureLayer featureLayer = new FeatureLayer(table);
          featureLayer.loadAsync();
          // features no longer show after this scale
          featureLayer.setMinScale(1000000);
          mMapView.getMap().getOperationalLayers().add(featureLayer);

          mSymbolDictionary.addDoneLoadingListener(() -> {
            if (mSymbolDictionary.getLoadStatus() == LoadStatus.LOADED) {
              // displays features from layer using mil2525d symbols
              DictionaryRenderer dictionaryRenderer = new DictionaryRenderer(mSymbolDictionary);
              featureLayer.setRenderer(dictionaryRenderer);

              featureLayer.addDoneLoadingListener(() -> {
                if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
                  // initial viewpoint to encompass all graphics displayed on the map view
                  mMapView.setViewpointGeometryAsync(featureLayer.getFullExtent());
                } else {
                  String error = "Feature Layer Failed to Load: " + featureLayer.getLoadError().getMessage();
                  Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                  Log.e(TAG, error);
                }
              });
            } else {
              String error = "Dictionary Symbol Failed to Load: " + mSymbolDictionary.getLoadError().getMessage();
              Toast.makeText(this, error, Toast.LENGTH_LONG).show();
              Log.e(TAG, error);
            }
          });
        }
      } else {
        String error = "Geodatabase Failed to Load: " + mGeodatabase.getLoadError().getMessage();
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
        Log.e(TAG, error);
      }
    });
  }

  @Override
  protected void onPause() {
    mMapView.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mMapView.resume();
  }

  @Override
  protected void onDestroy() {
    mMapView.dispose();
    super.onDestroy();
  }
}