Image service rendering rule

Download Sample Viewer

Description

This sample shows how to create a rendering rule (or raster function) and apply it to an Image Service layer. Shown are a shaded relief, hillshade, and slope rendering rules with some example arguments passed in as a HashMap.

Code snippet


    renderingRule.setVariableName("DEM");
    Map<String, Object> mapArguments = new HashMap<String, Object>();
    mapArguments.put("Azimuth", 215);
    mapArguments.put("Altitude", 60.0);
    mapArguments.put("ZFactor", 4);
    renderingRule.setArgumentsAsMap(mapArguments);

    imageServiceLayer.setRenderingRule(renderingRule);
    imageServiceLayer.refresh();
  

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.Component;
import java.awt.Dimension;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.esri.core.geometry.Envelope;
import com.esri.core.map.ImageServiceParameters.IMAGE_FORMAT;
import com.esri.core.map.RasterFunction;
import com.esri.map.ArcGISImageServiceLayer;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerList;

/**
 * This sample shows how to create a rendering rule (or raster function) and apply it 
 * to an Image Service layer. Shown are a shaded relief, hillshade, and slope rendering 
 * rules with some example arguments passed in as a HashMap.
 */
public class ShadedReliefImageServiceApp {

  private JMap jMap;
  private ArcGISImageServiceLayer imageServiceLayer;
  private JComboBox<String> cbxRasterFunctions;
  private static final int COMPONENT_HEIGHT = 25;
  private static final int COMPONENT_WIDTH = 220;

  //------------------------------------------------------------------------
  //Constructor
  //------------------------------------------------------------------------
  public ShadedReliefImageServiceApp() {

  }

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

          // 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
  // ------------------------------------------------------------------------

  public JComponent createUI() {
    // application content
    JComponent contentPane = createContentPane();

    // map
    jMap = createMap();

    // input panel
    JPanel userPanel = createUserPanel();
    userPanel.setVisible(true);
    userPanel.setLocation(10, 10);

    // select an initial rendering rule
    cbxRasterFunctions.setSelectedIndex(1);

    contentPane.add(userPanel);
    contentPane.add(jMap);

    return contentPane;
  }

  //------------------------------------------------------------------------
  //Private methods
  //------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Image Service Rendering Rule App");
    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);
        jMap.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 the JMap, adds the layers to display
   * @return a map
   */
  private JMap createMap() {

    final JMap map = new JMap();
    // set default extent
    map.setExtent(new Envelope(-13102696, 3714986, -13905597, 5291723));

    LayerList layers = map.getLayers();

    // add layers
    ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer");
    layers.add(tiledLayer);

    imageServiceLayer = new ArcGISImageServiceLayer(
        "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/CaliforniaDEM/ImageServer");
    imageServiceLayer.setImageFormat(IMAGE_FORMAT.JPG_PNG);
    layers.add(imageServiceLayer);

    return map;
  }

  /**
   * Creates the rendering rule panel
   */
  private JPanel createUserPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setSize(COMPONENT_WIDTH, (COMPONENT_HEIGHT*2)+15);
    panel.setLocation(10, 10);

    JLabel lblMosaicMethod = new JLabel("Rendering Rule:");
    lblMosaicMethod.setForeground(Color.WHITE);
    lblMosaicMethod.setAlignmentX(Component.LEFT_ALIGNMENT);

    final Set<String> functionNames = new HashSet<>();
    functionNames.add("ShadedRelief");
    functionNames.add("Hillshade");
    functionNames.add("Slope");

    cbxRasterFunctions = new JComboBox<>(functionNames.toArray(new String[3]));
    cbxRasterFunctions.setMaximumSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));
    cbxRasterFunctions.setAlignmentX(Component.LEFT_ALIGNMENT);
    cbxRasterFunctions.addItemListener(new ItemListener() {

      @Override
      public void itemStateChanged(ItemEvent arg0) {
        if (arg0.getStateChange() == ItemEvent.SELECTED) {

          String selectedFunction = (String) arg0.getItem();
          RasterFunction renderingRule = new RasterFunction();
          renderingRule.setFunctionName(selectedFunction);

          if ("ShadedRelief".equals(selectedFunction)) {
            // ShadedRelief
            renderingRule.setVariableName("Raster");
            Map<String, Object> mapArguments = new HashMap<>();
            mapArguments.put("Azimuth", Double.valueOf(215));
            mapArguments.put("Altitude", Double.valueOf(60));
            mapArguments.put("ZFactor", Double.valueOf(4));
            mapArguments.put("Colormap", createColorMap());
            renderingRule.setArgumentsAsMap(mapArguments);
          } else if ("Hillshade".equals(selectedFunction)) {
            // Hillshade
            renderingRule.setVariableName("DEM");
            Map<String, Object> mapArguments = new HashMap<>();
            mapArguments.put("Azimuth", Double.valueOf(215));
            mapArguments.put("Altitude", Double.valueOf(60));
            mapArguments.put("ZFactor", Double.valueOf(4));
            renderingRule.setArgumentsAsMap(mapArguments);
          } else {
            // selected function is "Slope"
            renderingRule.setVariableName("DEM");
            Map<String, Object> mapArguments = new HashMap<>();
            mapArguments.put("ZFactor", Double.valueOf(4));
            renderingRule.setArgumentsAsMap(mapArguments);
          }

          imageServiceLayer.setRenderingRule(renderingRule);
          imageServiceLayer.refresh();
        }
      }
    });

    // layout all the components together into a panel
    panel.setBackground(new Color(0, 0, 0, 120));
    panel.add(lblMosaicMethod);
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(cbxRasterFunctions);
    panel.add(Box.createRigidArea(new Dimension(0, 15)));
    panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
    return panel;
  }

  private int[][] createColorMap() {
    int[][] sampleColormap = new int[][]{

        new int[]{0,175,240,233},
        new int[]{3,175,240,222},
        new int[]{7,177,242,212},
        new int[]{11,177,242,198},
        new int[]{15,176,245,183},
        new int[]{19,185,247,178},
        new int[]{23,200,247,178},
        new int[]{27,216,250,177},
        new int[]{31,232,252,179},
        new int[]{35,248,252,179},
        new int[]{39,238,245,162},
        new int[]{43,208,232,135},
        new int[]{47,172,217,111},
        new int[]{51,136,204,88},
        new int[]{55,97,189,66},
        new int[]{59,58,176,48},
        new int[]{63,32,161,43},
        new int[]{67,18,148,50}
    };
    return sampleColormap;
  }
}
Feedback on this topic?