Time-aware dynamic layer

Download Sample Viewer

Description

This application demonstrates the use of the JTimeSlider to display hurricane paths changing with the passage of time. To use the sample, move the start date and end date slider to the time interval you want to start with, for example 1851-1861. Push the play button to automate the display of hurricane paths on the map. The time slider can also be manipulated manually. For example, you can change the time interval or use the forward and rewind buttons to manually cycle through the hurricane paths being displayed. This sample displays hurricane paths and intensity off the coast of the United States between the year 1851 and 2007. An ArcGIS Server dynamic map service is hosting the hurricane data. The legend (a screenshot of the legend in ArcMap) provides a key to the symbology used along a hurricanes path to indicate the intensity of the event.

Code snippet


    JTimeSlider jTimeSlider = new JTimeSlider();
    jTimeSlider.setTitle("Hurricane Paths");
    jTimeSlider.addLayer(timeAwareLayer);
    jTimeSlider.setTimeMode(TimeMode.TimeExtent);
    jTimeSlider.setTimeExtent(new TimeExtent(new GregorianCalendar(1851, 6,
        25), new GregorianCalendar(2007, 12, 16)), 10, Units.Years);
    jTimeSlider.setTimeInterval(new TimeExtent(new GregorianCalendar(1851, 6,
        25), new GregorianCalendar(1861, 6, 25)));
  

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.dynamiclayers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.net.URL;
import java.util.GregorianCalendar;

import javax.imageio.ImageIO;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.toolkit.sliders.JTimeSlider;
import com.esri.toolkit.sliders.JTimeSlider.TimeMode;
import com.esri.core.geometry.Envelope;
import com.esri.core.map.TimeExtent;
import com.esri.core.map.TimeOptions.Units;
import com.esri.map.ArcGISDynamicMapServiceLayer;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;
import com.esri.map.TimeAwareLayer;

/**
 * This application demonstrates the use of {@link JTimeSlider} to display
 * hurricane paths changing with the passage of time.
 * <p>
 * To use the sample, move the start date and end date slider to the time
 * interval you want to start with, for example 1851-1861. Push the play button
 * to automate the display of hurricane paths on the map. The time slider can
 * also be manipulated manually. For example, you can change the time interval
 * or use the forward and rewind buttons to manually cycle through the hurricane
 * paths being displayed.
 * <p>
 * This sample displays hurricane paths and intensity off the coast of the
 * United States between the year 1851 and 2007. An ArcGIS Server dynamic map
 * service is hosting the hurricane data. The legend (a screenshot of the
 * legend in ArcMap) provides a key to the symbology used along a hurricanes
 * path to indicate the intensity of the event.
 *
 */
public class TimeMapServiceApp {

  // JMap
  private JMap map;
  // resources
  private final String worldImagery =
      "http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer";
  private final String hurricanePath =
      "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Hurricanes/NOAA_Tracks_1851_2007/MapServer";
  private ArcGISDynamicMapServiceLayer hurricanePathsLayer;

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

  // ------------------------------------------------------------------------
  // Core functionality
  // ------------------------------------------------------------------------
  /**
   * Adds a JTimeSlider.
   * @param TimeAwareLayer layer to which the time slider will be associated.
   */
  private JTimeSlider addTimeSlider(TimeAwareLayer layer) {
    JTimeSlider jTimeSlider = new JTimeSlider();
    jTimeSlider.setTitle("Hurricane Paths");
    jTimeSlider.addLayer(layer);
    jTimeSlider.setTimeMode(TimeMode.TimeExtent);
    jTimeSlider.setTimeExtent(new TimeExtent(new GregorianCalendar(1851, 6,
        25), new GregorianCalendar(2007, 12, 16)), 10, Units.Years);
    jTimeSlider.setTimeInterval(new TimeExtent(new GregorianCalendar(1851, 6,
        25), new GregorianCalendar(1861, 6, 25)));

    return jTimeSlider;
  }

  // ------------------------------------------------------------------------
  // 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
          TimeMapServiceApp app = new TimeMapServiceApp();

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

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

    //legend
    JPanel legend = createLegend();
    legend.setLocation(10, description.getHeight() + 20);
    contentPane.add(legend);

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

    //time slider
    JTimeSlider jTimeSlider= addTimeSlider(hurricanePathsLayer);
    contentPane.add(jTimeSlider, BorderLayout.SOUTH);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Time Map Service 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.setBounds(100, 100, 1000, 700);
    contentPane.setLayout(new BorderLayout(0, 0));
    contentPane.setVisible(true);
    return contentPane;
  }

  /**
   * Creates a map.
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();

    // -----------------------------------------------------------------------------------------
    // Base Layer
    // -----------------------------------------------------------------------------------------
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(worldImagery);

    // focus on US
    jMap.setExtent(new Envelope(-15141796.142,113237.394,5976634.055,8887493.525));

    LayerList layers = jMap.getLayers();
    layers.add(baseLayer);

    // -----------------------------------------------------------------------------------------
    // Time Aware Layer - hurricane paths
    // -----------------------------------------------------------------------------------------
    hurricanePathsLayer = new ArcGISDynamicMapServiceLayer(hurricanePath);
    layers.add(hurricanePathsLayer);

    return jMap;
  }

  /**
   * Creates a legend for the hurricane intensity
   *
   * @return legend
   */
  private JPanel createLegend() {
    JPanel legendContainer = new JPanel();
    legendContainer.setLayout(new BoxLayout(legendContainer, 0));
    legendContainer.setSize(143, 143);
    legendContainer.setBackground(new Color(0, 0, 0, 0));

    try {
      URL url = this.getClass().getResource("resources/hurricane_legend.png");
      BufferedImage image = null;
      image = ImageIO.read(url);

      JLabel legend = new JLabel();
      legend.setIcon(new ImageIcon(image));
      legendContainer.add(legend);

    } catch (Exception e) {
      e.printStackTrace();
    }
    legendContainer.setBorder(new LineBorder(Color.BLACK, 3, true));
    return legendContainer;
  }

  /**
   * Creates a description for this application.
   * @return description
   */
  private JPanel createDescription() {
    JPanel descriptionContainer = new JPanel();
    descriptionContainer.setLayout(new BoxLayout(descriptionContainer, 0));

    descriptionContainer.setSize(240, 200);
    JTextArea description = new JTextArea(
        "To use the sample, move the start date and end date slider to the time " +
            "interval you want to start with, for example 1851-1861. Push the play button " +
            "to automate the display of hurricane paths on the map. The time slider can " +
            "also be manipulated manually. For example, you can change the time interval " +
            "or use the forward and rewind buttons to manually cycle through the hurricane " +
        "paths being displayed.");
    description.setFont(new Font("Verdana", Font.PLAIN, 11));
    description.setForeground(Color.BLACK);
    description.setBackground(Color.WHITE);
    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, true));
    return descriptionContainer;
  }
}
Feedback on this topic?