WMTS layer

Download Sample Viewer

Description

This application shows how to display a WMTSLayer from a Web Map Tile Service (WMTS). The application also shows how to display some properties about the layer and service, once the layer is initialized.

Code snippet


  // create and add the WMTSLayer
  WMTSLayer wmtsLayer = new WMTSLayer(WMTS_SERVICE_URL);
  jMap.getLayers().add(wmtsLayer);
  

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

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.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.core.ogc.wmts.WMTSLayerInfo;
import com.esri.core.ogc.wmts.WMTSServiceInfo;
import com.esri.map.JMap;
import com.esri.map.Layer.LayerStatus;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.WMTSLayer;

/**
 * This application shows how to display a <code>WMTSLayer</code> from a Web Map Tile Service (WMTS).
 * The application also shows how to display some properties about the layer and service, once the 
 * layer is initialized.
 */
public class WMTSLayerApp {

  private JMap map;
  private WMTSLayer wmtsLayer;
  private JComponent contentPane;
  private static final String WMTS_SERVICE_URL =
      "http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer/WMTS/";

  // Default constructor
  public WMTSLayerApp() { }

  // ------------------------------------------------------------------------
  // Core Functionality
  // ------------------------------------------------------------------------
  public JComponent createUI() throws Exception {
    // application content
    contentPane = createContentPane();

    // map
    map = new JMap();
    map.setWrapAroundEnabled(true);

    // title
    JLabel title = new JLabel("WMTS Layer");
    title.setHorizontalAlignment(SwingConstants.CENTER);
    title.setForeground(Color.WHITE);
    title.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));

    // area to display WMTS layer properties
    final JTextArea infoTextArea = new JTextArea();
    infoTextArea.setLineWrap(true);
    infoTextArea.setWrapStyleWord(true);
    infoTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));

    // group the above UI items into a panel
    final JPanel controlPanel = new JPanel();
    controlPanel.setLayout(new BorderLayout());
    controlPanel.setBackground(Color.DARK_GRAY);
    controlPanel.setLocation(10, 10);
    controlPanel.setSize(250, 250);
    controlPanel.setBorder(new LineBorder(Color.BLACK, 3, false));
    controlPanel.add(title, BorderLayout.NORTH);
    controlPanel.add(infoTextArea, BorderLayout.CENTER);

    // create the WMTS layer and display some layer and service info once it's initialized
    wmtsLayer = new WMTSLayer(WMTS_SERVICE_URL);
    wmtsLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {

      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        if (e.getLayer().getStatus() == LayerStatus.INITIALIZED) {
          infoTextArea.append("Layer name: " + wmtsLayer.getName() + "\n");
          infoTextArea.append("Layer identifier: " + wmtsLayer.getLayerId() + "\n");
          infoTextArea.append("Layer spatial reference: " + wmtsLayer.getDefaultSpatialReference().getID() + "\n");
          infoTextArea.append("Layer service mode: " + wmtsLayer.getServiceMode() + "\n");
          try {
            WMTSServiceInfo serviceInfo = wmtsLayer.getWmtsServiceInfo();
            infoTextArea.append("Service title: " + serviceInfo.getTitle() + "\n");
            infoTextArea.append("Service version: " + serviceInfo.getVersion() + "\n");
            WMTSLayerInfo layerInfo = serviceInfo.getLayerInfo(wmtsLayer.getLayerId());
            infoTextArea.append("Layer default style: " + layerInfo.getDefaultStyle().getIdentifier() + "\n");
            infoTextArea.append("Layer formats: " + layerInfo.getFormats() + "\n");
            infoTextArea.append("Layer style IDs: " + layerInfo.getStyleIds() + "\n");
            infoTextArea.append("Layer tile matrix set IDs: " + layerInfo.getTileMatrixSetIds());
          } catch (Exception e1) {
            JOptionPane.showMessageDialog(contentPane.getParent(),
                wrap("Unable to get the WMTS service info. " + e1.getLocalizedMessage()),
                "", JOptionPane.ERROR_MESSAGE);
          }
        } else {
          JOptionPane.showMessageDialog(contentPane.getParent(),
              wrap("Unable to load the WMTS service. " + e.getLayer().getInitializationError()),
              "", JOptionPane.ERROR_MESSAGE);
        }
      }
    });
    map.getLayers().add(wmtsLayer);

    contentPane.add(controlPanel);
    contentPane.add(map);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // 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
          WMTSLayerApp mapApp = new WMTSLayerApp();

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

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates the application window. Disposes the map when the window is closing.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("WMTS 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);
        if (map != null) map.dispose();
      }
    });
    return window;
  }

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

  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?