Shapefile feature layer

Download Sample Viewer

Description

This sample application shows how to display local shapefile data using a FeatureLayer. First, a ShapefileFeatureTable is created with the local data path. Then, a feature layer is created using the shapefile feature table. A renderer must be set in the feature layer to display the shapefile data. Note: the shapefile must be in the same spatial reference as the basemap used, or else it will not display.

Code snippet


	// Create shapefile feature table from path to .shp file
    shapefileTable = new ShapefileFeatureTable("path/to/Shapefile.shp");
    featureLayer = new FeatureLayer(shapefileTable);
      
    // create a renderer
    UniqueValueRenderer uvRenderer = new UniqueValueRenderer(
          new SimpleFillSymbol(new Color(0, 0, 0, 255), null), "CONTINENT");
    // ...

    // set renderer
    featureLayer.setRenderer(uvRenderer);
    jMap.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.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.esri.core.geodatabase.ShapefileFeatureTable;
import com.esri.core.renderer.UniqueValueInfo;
import com.esri.core.renderer.UniqueValueRenderer;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.FeatureLayer;
import com.esri.map.JMap;
import com.esri.runtime.ArcGISRuntime;

/***
 * This sample application shows how to display local shapefile data using a 
 * <code>FeatureLayer</code>. First, a <code>ShapefileFeatureTable</code> is 
 * created with the local data path. Then, a feature layer is created using 
 * the shapefile feature table. A renderer must be set in the feature layer 
 * to display the shapefile data. Note: the shapefile must be in the same 
 * spatial reference as the basemap used, or else it will not display. 
 */
public class ShapefileApp {

  // ArcGIS components
  private JMap map;
  private FeatureLayer featureLayer;
  private ShapefileFeatureTable shapefileTable;

  private static final String BASEMAP_URL = "http://services.arcgisonline.com/arcgis/rest/services/ESRI_Imagery_World_2D/MapServer";
  private static final String FSP = System.getProperty("file.separator");

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

  // ------------------------------------------------------------------------
  // Core Functionality
  // ------------------------------------------------------------------------

  /**
   * Creates the map with a basemap and a feature layer from a shapefile 
   * feature table. A unique value renderer is set to display the shapefile
   * data.
   *  
   * @return a map.
   */
  private JMap createMap() {
    // map
    JMap jMap = new JMap();

    ArcGISTiledMapServiceLayer basemap = new ArcGISTiledMapServiceLayer(BASEMAP_URL);
    jMap.getLayers().add(basemap);

    // Add shapefile
    try {
      shapefileTable = new ShapefileFeatureTable(getPathSampleData() + "shapefiles" + FSP + "Continents.shp");
      featureLayer = new FeatureLayer(shapefileTable);

      // create a renderer
      UniqueValueRenderer uvRenderer = new UniqueValueRenderer(
          new SimpleFillSymbol(new Color(0, 0, 0, 255), null), "CONTINENT");
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "Asia" },
          new SimpleFillSymbol(Color.RED, null)));
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "Europe" },
          new SimpleFillSymbol(Color.GREEN, null)));
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "Antarctica" },
          new SimpleFillSymbol(Color.MAGENTA, null)));
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "Africa" },
          new SimpleFillSymbol(Color.BLUE, null)));
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "Australia" },
          new SimpleFillSymbol(Color.YELLOW, null)));
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "North America" },
          new SimpleFillSymbol(Color.CYAN, null)));
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "South America" },
          new SimpleFillSymbol(Color.ORANGE, null)));
      uvRenderer.addValue(new UniqueValueInfo(new Object[] { "Oceania" },
          new SimpleFillSymbol(Color.ORANGE, null)));

      // set renderer
      featureLayer.setRenderer(uvRenderer);
      jMap.getLayers().add(featureLayer);
    } catch (FileNotFoundException e) {
      JOptionPane.showMessageDialog(map, 
          wrap(e.getLocalizedMessage()), "",JOptionPane.ERROR_MESSAGE);
    }

    return jMap;
  }

  // ------------------------------------------------------------------------
  // 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
          ShapefileApp shapefileApp = new ShapefileApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = shapefileApp.createWindow();
          appWindow.add(shapefileApp.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();

    // 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 from Shapefile");
    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);
        if (map != null ) 
          map.dispose();
        if (shapefileTable != null) 
          shapefileTable.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;
  }

  private String getPathSampleData() {
    String dataPath = null;
    String javaPath = ArcGISRuntime.getInstallDirectory();
    if (javaPath != null) {
      if (!(javaPath.endsWith("/") || javaPath.endsWith("\\"))){
        javaPath += FSP;
      }
      dataPath = javaPath + "sdk" + FSP + "samples" + FSP + "data" + FSP;
    }
    File dataFile = new File(dataPath);
    if (!dataFile.exists()) { 
      dataPath = ".." + FSP + "data" + FSP;
    }
    return dataPath;
  }

  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?