Feature rendering

Download Sample Viewer

Description

This sample demonstrates different rendering options for feature layers. The rivers layer is rendered using a simple renderer. The cities layer is rendered by a unique value renderer according to whether the city is a State capital or not. The states layer uses a class breaks renderer that renders symbology based on a population density field. The states layer is added to the map first, the rivers layer second, and finally the cities layer, such that the the cities are visible on top of both other layers, and the rivers are visible on top of the states layer.

Code snippet


    ArcGISFeatureLayer citiesLayer = new ArcGISFeatureLayer(
        "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");

    UniqueValueRenderer uvRenderer = new UniqueValueRenderer();
    uvRenderer.setAttributeName1("CAPITAL");
    uvRenderer.addValue(new UniqueValueInfo(new Object[] {"Y"}, 
        new SimpleMarkerSymbol(Color.YELLOW, 14, Style.DIAMOND)));
    uvRenderer.addValue(new UniqueValueInfo(new Object[] {"N"}, 
        new SimpleMarkerSymbol(Color.GREEN, 9, Style.CIRCLE)));
    citiesLayer.setRenderer(uvRenderer);

    map.getLayers().add(citiesLayer);
  

Sample Code

/* Copyright 2014 Esri

All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.

You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.

See the use restrictions.*/
package com.esri.client.samples.featurelayers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.core.geometry.Envelope;
import com.esri.core.renderer.ClassBreaksRenderer;
import com.esri.core.renderer.SimpleRenderer;
import com.esri.core.renderer.UniqueValueInfo;
import com.esri.core.renderer.UniqueValueRenderer;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.Style;
import com.esri.map.ArcGISFeatureLayer;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;

/**
 * This sample demonstrates different rendering options for feature layers.
 * The rivers layer is rendered using a simple renderer.
 * The cities layer is rendered by a unique value renderer according to whether
 * the city is a capital or not.
 * The states layer uses a class breaks renderer that renders symbology based on a population
 * density field.
 * <p>
 * The states layer is added to the map first, the rivers layer second, and finally
 * the cities layer, such that the the cities are visible on top of both other layers,
 * and the rivers are visible on top of the states layer.
 */
public class RenderingApp {

  // symbology
  final static SimpleLineSymbol   SYM_STATE_BORDER   = new SimpleLineSymbol(Color.WHITE, 1);
  final static SimpleMarkerSymbol SYM_CAPITAL        = new SimpleMarkerSymbol(Color.YELLOW, 14, Style.DIAMOND);
  final static SimpleMarkerSymbol SYM_NON_CAPITAL    = new SimpleMarkerSymbol(Color.GREEN, 9, Style.CIRCLE);
  final static SimpleLineSymbol   SYM_RIVER          = new SimpleLineSymbol(Color.BLUE, 1);

  // resources
  final String URL_TOPO = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
  final String URL_CENSUS_BY_STATE = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5";
  final String URL_RIVERS = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/1";
  final String URL_CITIES = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0";

  // JMap
  private JMap map;

  // ------------------------------------------------------------------------
  // Constructor
  // ------------------------------------------------------------------------
  public RenderingApp() {
  }

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates a map with different symbology for different types of features.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();

    // -----------------------------------------------------------------------------------------
    // remote map service layer
    // -----------------------------------------------------------------------------------------
    ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(URL_TOPO);
    // focus on US extent
    jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000));
    final LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    // -----------------------------------------------------------------------------------------
    // Feature Layer - states rendered using a class breaks renderer
    // -----------------------------------------------------------------------------------------
    ArcGISFeatureLayer censusByStateLayer = new ArcGISFeatureLayer(URL_CENSUS_BY_STATE);

    // classification renderer based on range of population density (attribute POP07_SQMI)
    ClassBreaksRenderer stateClassificationRenderer = new ClassBreaksRenderer(null, "POP07_SQMI");
    Color midColor = new Color(128, 128, 128, 200);
    Color lighter = new Color(182, 182, 182, 200);
    Color darker = new Color(85, 85, 85, 200);
    stateClassificationRenderer.addBreak(0, 50, new SimpleFillSymbol(lighter, SYM_STATE_BORDER));
    stateClassificationRenderer.addBreak(50, 200, new SimpleFillSymbol(midColor, SYM_STATE_BORDER));
    stateClassificationRenderer.addBreak(200, 50000, new SimpleFillSymbol(darker, SYM_STATE_BORDER));
    censusByStateLayer.setRenderer(stateClassificationRenderer);

    layers.add(censusByStateLayer);

    // -----------------------------------------------------------------------------------------
    // Feature Layer - rivers rendered using a simple renderer.
    // -----------------------------------------------------------------------------------------
    final ArcGISFeatureLayer riversLayer = new ArcGISFeatureLayer(URL_RIVERS);
    riversLayer.setRenderer(new SimpleRenderer(SYM_RIVER));
    layers.add(riversLayer);

    // -----------------------------------------------------------------------------------------
    // Feature Layer - cities rendered by a unique value renderer,
    // differentiating between capital and non-capital cities.
    // -----------------------------------------------------------------------------------------
    ArcGISFeatureLayer citiesLayer = new ArcGISFeatureLayer(URL_CITIES);

    UniqueValueRenderer uvRenderer = new UniqueValueRenderer();
    uvRenderer.setAttributeName1("CAPITAL");
    uvRenderer.addValue(new UniqueValueInfo(new Object[] {"Y"}, SYM_CAPITAL));
    uvRenderer.addValue(new UniqueValueInfo(new Object[] {"N"}, SYM_NON_CAPITAL));
    citiesLayer.setRenderer(uvRenderer);

    layers.add(citiesLayer);

    return jMap;
  }

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          RenderingApp renderingApp = new RenderingApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = renderingApp.createWindow();
          appWindow.add(renderingApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {
    // application content
    JComponent contentPane = createContentPane();

    // description
    JPanel description = createDescription();
    description.setLocation(10, 10);
    contentPane.add(description);

    // map
    map = createMap();
    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Feature Layer Rendering Application");
    window.setBounds(100, 100, 1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        map.dispose();
      }
    });
    return window;
  }

  /**
   * Creates a content pane.
   * @return a content pane.
   */
  private static JLayeredPane createContentPane() {
    JLayeredPane contentPane = new JLayeredPane();
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

  /**
   * Creates a description for this application.
   * @return description
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setSize(270, 125);
    JTextArea description = new JTextArea(
        "This sample shows how to create renderers for feature layers: " +
        "a simple renderer (rivers), a unique value renderer (cities), and " +
        "a class breaks renderer (states). " +
        "Zoom and pan the map to get other cities to display (the service " +
        "returns up to 1000 features).");
    description.setFont(new Font("Verdana", Font.PLAIN, 11));
    description.setForeground(Color.WHITE);
    description.setBackground(new Color(0, 0, 0, 180));
    description.setEditable(false);
    description.setLineWrap(true);
    description.setWrapStyleWord(true);
    description.setBorder(BorderFactory.createEmptyBorder(5,10,5,5));
    descriptionContainer.add(description);
    descriptionContainer.setBackground(new Color(0, 0, 0, 0));
    descriptionContainer.setBorder(new LineBorder(Color.BLACK, 3, false));
    return descriptionContainer;
  }
}
Feedback on this topic?