No Data tiles

Download Sample Viewer

Description

This sample shows how to set the map to either show 'no data' tiles when tiles of the given location and resolution are not available, or hide them by showing tiles from the closest available resolution.

Code snippet


  // map which shows 'no data' tiles
  showNoDataMap = new JMap();
  showNoDataMap.setHidingNoDataTiles(false);

  // map which hides 'no data' tiles
  hideNoDataMap = new JMap();
  hideNoDataMap.setHidingNoDataTiles(true);
  

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.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import com.esri.core.geometry.Point;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;

/***
 * This sample demonstrates how to hide tiles with no data.
 */
public class HideNoDataTiles {
  // resources
  final String URL = "http://services.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer";

  // JMap
  private JMap showNoDataMap;
  private JMap hideNoDataMap;

  //location in Edinburgh which has tiles with no data
  private Point locationWithoutDetail = new Point(-355153.745, 7550203.739);

  private static final int PANEL_WIDTH = 255;

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

  // ------------------------------------------------------------------------
  // 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
          HideNoDataTiles hideTileApp = new HideNoDataTiles();
          // create the UI, including the maps, for the application.
          JFrame appWindow = hideTileApp.createWindow();
          appWindow.add(hideTileApp.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 showNoDataMap, for this application.
   */
  public JComponent createUI() throws Exception {

    // content pane
    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BoxLayout(contentPane, 0));

    // showNoDataMap
    showNoDataMap = new JMap();
    showNoDataMap.setHidingNoDataTiles(false);
    showNoDataMap.setBorder(new LineBorder(new Color(0, 0, 0, 100), 1));
    final ArcGISTiledMapServiceLayer baseLayer = new ArcGISTiledMapServiceLayer(URL);
    showNoDataMap.getLayers().add(baseLayer);

    // hideNoDataMap
    hideNoDataMap = new JMap();
    hideNoDataMap.setHidingNoDataTiles(true);
    hideNoDataMap.setBorder(new LineBorder(new Color(0, 0, 0, 100), 1));
    final ArcGISTiledMapServiceLayer baseLayer2 = new ArcGISTiledMapServiceLayer(URL);
    hideNoDataMap.getLayers().add(baseLayer2);

    int buttonHeight = 30;
    // button - when clicked, zoom to a no data tile in Edinburgh
    //(the highest resolution with no data tiles)
    JButton btnZoomNoData = new JButton("Zoom to tile which has no data");
    btnZoomNoData.setMinimumSize(new Dimension(PANEL_WIDTH, buttonHeight));
    btnZoomNoData.setMaximumSize(new Dimension(PANEL_WIDTH, buttonHeight));
    btnZoomNoData.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        Double[] arrayList = showNoDataMap.getResolutionList().toArray(new Double[0]);
        showNoDataMap.zoomToResolution(arrayList[arrayList.length - 4].doubleValue(),
            locationWithoutDetail);
        hideNoDataMap.zoomToResolution(arrayList[arrayList.length - 4].doubleValue(),
            locationWithoutDetail);
      }
    });
    btnZoomNoData.setAlignmentX(Component.CENTER_ALIGNMENT);

    // button - when clicked, zoom to a data tile in Edinburgh
    //(the lowest resolution with data tiles)
    JButton btnZoomData = new JButton("Zoom to tile which has data");
    btnZoomData.setMinimumSize(new Dimension(PANEL_WIDTH, buttonHeight));
    btnZoomData.setMaximumSize(new Dimension(PANEL_WIDTH, buttonHeight));
    btnZoomData.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Double[] arrayList = showNoDataMap.getResolutionList().toArray(new Double[0]);
        showNoDataMap.zoomToResolution(arrayList[arrayList.length - 5].doubleValue(),
            locationWithoutDetail);
        hideNoDataMap.zoomToResolution(arrayList[arrayList.length - 5].doubleValue(),
            locationWithoutDetail);
      }
    });
    btnZoomData.setAlignmentX(Component.CENTER_ALIGNMENT);

    // button panel
    JPanel buttonPanel = new JPanel();
    buttonPanel.setBackground(Color.BLACK);
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
    buttonPanel.add(btnZoomNoData);
    buttonPanel.add(btnZoomData);
    buttonPanel.setBounds(10, 10, PANEL_WIDTH, buttonHeight*2);

    hideNoDataMap.add(buttonPanel);
    contentPane.add(hideNoDataMap);
    JComponent space = (JComponent) Box
        .createRigidArea(new Dimension(1, 5));
    contentPane.add(space);
    contentPane.add(showNoDataMap);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  /**
   * Creates a window.
   *
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Hide No-Data Tiles");
    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);
        showNoDataMap.dispose();
        hideNoDataMap.dispose();
      }
    });
    return window;
  }
}
Feedback on this topic?