Feature layer

Download Sample Viewer

Description

This sample application shows how to display feature data from an online feature service using a FeatureLayer. First, a GeodatabaseFeatureServiceTable is created from a feature service URL and layer ID. Then, a FeatureLayer is created from this geodatabase feature service table. An infopopup overlay is added to the map to display feature attributes. Left-click on a feature to view the attributes of that feature.

Code snippet


    // create feature service table from feature service URL, layer ID
    featureServiceTable = new GeodatabaseFeatureServiceTable(
        FEATURE_SERVICE_URL, 0);
    // initialize the table (synchronous method shown)
    Status status = featureServiceTable.initialize();
    // create feature layer from table and add to map
    if (status == Status.INITIALIZED) {
      FeatureLayer featureLayer = new FeatureLayer(featureServiceTable);
      map.getLayers().add(featureLayer);
    }
  

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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.toolkit.overlays.InfoPopupOverlay;
import com.esri.core.geodatabase.GeodatabaseFeatureServiceTable;
import com.esri.core.geodatabase.GeodatabaseFeatureServiceTable.Status;
import com.esri.core.geometry.Envelope;
import com.esri.core.map.CallbackListener;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.FeatureLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;

/***
 * This sample application shows how to display feature data from an online feature 
 * service using a <code>FeatureLayer</code>. First, a <code>GeodatabaseFeatureServiceTable</code> 
 * is created from a feature service URL and layer ID. Then, a <code>FeatureLayer</code> 
 * is created from this geodatabase feature service table. An infopopup overlay is added 
 * to the map to display feature attributes. Left-click on a feature to view the attributes 
 * of that feature.
 */
public class FeatureLayerApp {

  // resources
  final String URL_TILED_LAYER = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer";
  final String FEATURE_SERVICE_URL = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer";

  // ArcGIS components
  private JMap map;
  private FeatureLayer featureLayer;
  private GeodatabaseFeatureServiceTable featureServiceTable; 

  // ------------------------------------------------------------------------
  // Constructors
  // ------------------------------------------------------------------------
  public FeatureLayerApp() {}

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Creates a GeodatabaseFeatureServiceTable using a feature service URL and layer ID. 
   */
  private void createGeodatabaseFeatureServiceTable() {

    featureServiceTable = new GeodatabaseFeatureServiceTable(
        FEATURE_SERVICE_URL, 0);
    featureServiceTable.initialize(new CallbackListener<GeodatabaseFeatureServiceTable.Status>() {
      
      @Override
      public void onError(Throwable e) {
             JOptionPane.showMessageDialog(map, 
                 wrap(e.getLocalizedMessage()), "",JOptionPane.ERROR_MESSAGE);
      }
      
      @Override
      public void onCallback(Status status) {
        if (Status.INITIALIZED == status) {
          createFeatureLayer();
        }
      }
    });
  }
  
  /**
   * Creates a FeatureLayer from the GeodatabaseFeatureServiceTable and adds 
   * an infopopup overlay to the map to display feature attributes.
   */
  private void createFeatureLayer() {
    
    featureLayer = new FeatureLayer(featureServiceTable);
    
    // featureLayer - this has the damage to residential building data
    map.getLayers().add(featureLayer);

    final InfoPopupOverlay infoPopupOverlay = new InfoPopupOverlay();
    infoPopupOverlay.setPopupTitle("Building Data");
    infoPopupOverlay.setItemTitle("Extent of damage: {typdamage}");
    map.addMapOverlay(infoPopupOverlay);

    featureLayer
    .addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {
      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent event) {
        // when the feature layer is initialized, let the InfoPopupOverlay know
        // so that the InfoPopupOverlay can show attributes of the damaged building
        infoPopupOverlay.addLayer(event.getLayer());
      }
    });
  }
  
  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application: creates the window, creates an 
   * instance of this application and calls method to create the UI.
   * 
   * @param args any arguments
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          FeatureLayerApp featureLayerApp = new FeatureLayerApp();

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

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates the map, description and various Swing 
   * panels used in the application.
   */
  public JComponent createUI() throws Exception {
    // application content
    JComponent contentPane = createContentPane();

    // map
    map = createMap();

    // description
    JPanel description = createDescription();
    contentPane.add(description);

    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window and disposes the map and feature service table. The dispose 
   * method should be called when you are finished using the map and feature Service 
   * table, for example on application exit.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Feature Layer - 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();
        if (featureServiceTable != null) 
          featureServiceTable.dispose();
      }
    });
    return window;
  }

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

  /**
   * Creates a map, adds a basemap and a feature layer to the map.
   * 
   * @return a map.
   */
  private JMap createMap() {
    final JMap jMap = new JMap();
    ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(
        URL_TILED_LAYER);

    jMap.getLayers().add(baseLayer);
    jMap.setExtent(new Envelope(-20042400, 856094, -2783530, 11716267));
   
    createGeodatabaseFeatureServiceTable();  

    return jMap;
  }

  /**
   * Creates a description for this application.
   * 
   * @return a JPanel that has the description text.
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setOpaque(false);
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));
    descriptionContainer.setSize(300, 120);
    descriptionContainer.setLocation(10, 10);
    JTextArea description = new JTextArea(
        "On application load, the feature layer displays features from the service " +
        "that are within the current map extent. As you change the map extent by panning " +
        "and zooming, any additional features are requested from the service and displayed. " +
        "Left-click on a feature to view the attributes of that feature.");
    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;
  }
  
  private String wrap(String str) {
    // create a HTML string that wraps text when longer
    return "<html><p style='width:200px;'>" + str + "</html>";
  }
}
Feedback on this topic?