Unique Value renderer

Demonstrates how to use an UniqueValueRenderer to display Features from a FeatureLayer using different symbols. An unique value renderer sets an attribute to look for any matches within it's unique values. If there is a match between the value from the unique value and value set to the Feature then the symbol from the unique will be applied to that Feature.

In this sample the unique value renderer is looking for the "STATE_ABBR" attribute within each feature of the feature layer. For example we will have a unique value renderer that has a unique value set to the abbreviation for California.

Setting a Unique Value

int colorRed = 0xFFFF0000;
SimpleFillSymbol californiaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, colorRed,
    new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, colorRed, 2));

List<Object> californiaValue = new ArrayList<>();
californiaValue.add("CA");
uniqueValueRenderer.getUniqueValues().add(new UniqueValue("State of California", "California",
    californiaFillSymbol, californiaValue));

When this unique value renderer comes across the feature whos key "STATE_ABBR" is value "CA" it will assign the symbol from the unique value above to this feature.

How it works

To display different Symbol for different Graphic`s:

  1. Create a ArcGISMap's with Basemap.
  2. Create a FeatureLayer and add it to the map, ArcGISMap.getOperationalLayers().add().
  3. Add the map to the view, MapView.setMap().
  4. Create a UniqueValueRenderer.
    • specify default feature attribute to look for, UniqueValueRenderer.getFieldNames().add()
    • set default symbol for renderer, UniqueValueRenderer.setDefaultSymbol()
    • set renderer to feature layer, FeatureLayer.setRenderer(Renderer)
  5. Create a set of UniqueValue(Description, Name, Symbol, Value).
    • description, description for this unique value
    • name, name for this unique value
    • symbol, symbol to be displayed for the values listed here
    • value, list of values that will use the symbol set here (Example: List of state name abbreviations "CA")
  6. Add unique values to renderer, UniqueValueRenderer.getUniqueValues().add(UniqueValue).

Code

/*
 * Copyright 2017 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.samples.symbology.unique_value_renderer;

import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.geometry.Envelope;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.UniqueValueRenderer;
import com.esri.arcgisruntime.symbology.UniqueValueRenderer.UniqueValue;

public class UniqueValueRendererSample extends Application {

  private MapView mapView;

  private final String SAMPLE_SERVICE_URL =
      "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";

  // colors for symbols
  private static final int GRAY = 0xFFD3D3D3;
  private static final int RED = 0xFFFF0000;
  private static final int GREEN = 0xFF00FF00;
  private static final int BLUE = 0xFF0000FF;

  @Override
  public void start(Stage stage) throws Exception {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Unique Value Renderer Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(scene);
      stage.show();

      // create ArcGISMap with topographic basemap
      final ArcGISMap map = new ArcGISMap(Basemap.createTopographic());

      // create a view and set ArcGISMap to it
      mapView = new MapView();
      mapView.setMap(map);

      // create service feature table
      ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(SAMPLE_SERVICE_URL);

      // create the feature layer using the service feature table
      final FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);

      // override the feature layer renderer with a new unique value renderer
      UniqueValueRenderer uniqueValueRenderer = new UniqueValueRenderer();
      // field name is a key, in a key/value pair, of a feature's attributes
      // can be a list but only looking for one in this case
      uniqueValueRenderer.getFieldNames().add("STATE_ABBR");

      // create the symbols to be used in the renderer
      SimpleFillSymbol defaultFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.NULL, 0x00000000,
          new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, GRAY, 2));
      SimpleFillSymbol californiaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, RED,
          new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, RED, 2));
      SimpleFillSymbol arizonaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, GREEN,
          new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, GREEN, 2));
      SimpleFillSymbol nevadaFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, BLUE, new SimpleLineSymbol(
          SimpleLineSymbol.Style.SOLID, BLUE, 2));

      // set the default symbol
      uniqueValueRenderer.setDefaultSymbol(defaultFillSymbol);
      uniqueValueRenderer.setDefaultLabel("Other");

      // set value for California, Arizona, and Nevada
      List<Object> californiaValue = new ArrayList<>();
      californiaValue.add("CA");
      uniqueValueRenderer.getUniqueValues().add(new UniqueValue("State of California", "California",
          californiaFillSymbol, californiaValue));

      List<Object> arizonaValue = new ArrayList<>();
      arizonaValue.add("AZ");
      uniqueValueRenderer.getUniqueValues().add(new UniqueValue("State of Arizona", "Arizona", arizonaFillSymbol,
          arizonaValue));

      List<Object> nevadaValue = new ArrayList<>();
      nevadaValue.add("NV");
      uniqueValueRenderer.getUniqueValues().add(new UniqueValue("State of Nevada", "Nevada", nevadaFillSymbol,
          nevadaValue));

      // set the renderer on the feature layer
      featureLayer.setRenderer(uniqueValueRenderer);

      // add the layer to the ArcGISMap
      map.getOperationalLayers().add(featureLayer);

      // create initial viewpoint using a envelope
      Envelope envelope = new Envelope(-13893029.0, 3573174.0, -12038972.0, 5309823.0, SpatialReferences
          .getWebMercator());

      // set viewpoint on ArcGISMap
      Viewpoint viewpoint = new Viewpoint(envelope);
      map.setInitialViewpoint(viewpoint);

      // add the map view and control panel to stack pane
      stackPane.getChildren().add(mapView);
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() throws Exception {

    // release resources when the application closes
    if (mapView != null) {
      mapView.dispose();
    }
  }

  /**
   * Opens and runs application.
   * 
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {

    Application.launch(args);
  }
}


In this topic
  1. How it works
  2. Code