Image service stretch

Download Sample Viewer

Description

This sample shows how to create a Stretch rendering rule (or raster function) and apply it to an Image Service layer.

Code snippet


    RasterFunction renderingRule = new RasterFunction();
    renderingRule.setFunctionName("Stretch");
    renderingRule.setVariableName("Raster");
    Map<String, Object> arguments = new HashMap<String, Object>();
    arguments.put("StretchType", 0);
    arguments.put("NumberOfStandardDeviations", 1);
    // etc.
    renderingRule.setArgumentsAsMap(arguments);

    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.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

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

import com.esri.core.geometry.Envelope;
import com.esri.core.map.RasterFunction;
import com.esri.map.ArcGISDynamicMapServiceLayer;
import com.esri.map.ArcGISImageServiceLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.LayerList;

/**
 * This sample shows how to create a Stretch rendering rule (or raster function) and apply 
 * it to an Image Service layer.
 */
public class StretchApp {

  private JMap jMap;

  // Constructor
  public StretchApp() { }

  //------------------------------------------------------------------------
  // Core functionality
  //------------------------------------------------------------------------ 
  /**
   * Creates the JMap, adds a tiled layer and then an image service layer. Sets 
   * a 'Stretch' rendering rule (RasterFunction) when the image service layer 
   * is initialized.
   * 
   * @return a map
   */
  private JMap createMap() {

    final JMap map = new JMap();
    // set default extent to North America
    map.setExtent(new Envelope(-13486609,5713307,-13263258,5823117));

    LayerList layers = map.getLayers();

    // add layers
    ArcGISDynamicMapServiceLayer tiledLayer = new ArcGISDynamicMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Transportation/MapServer");
    layers.add(tiledLayer);

    final ArcGISImageServiceLayer imageServiceLayer = new ArcGISImageServiceLayer(
        "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Portland/CascadeLandsat/ImageServer");
    imageServiceLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {

      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        RasterFunction renderingRule = new RasterFunction();
        renderingRule.setFunctionName("Stretch");
        renderingRule.setVariableName("Raster");
        Map<String, Object> arguments = new HashMap<>();
        // Stretch Type - 0 (none), 3 (Standard Deviation), 4 (Histogram), 5 (MinMax)
        arguments.put("StretchType", Integer.valueOf(0));
        arguments.put("NumberOfStandardDeviations", Integer.valueOf(1));
        arguments.put("Statistics", new double[][]{
            { 0.2, 222.46, 99.35, 1.64 },
            { 5.56, 100.345, 45.4, 3.96 },
            { 0, 352.37, 172.284, 2 } });
        arguments.put("Gamma", new double[] {1.25, 2, 3.95});
        renderingRule.setArgumentsAsMap(arguments);
        imageServiceLayer.setRenderingRule(renderingRule);
        imageServiceLayer.setBandIds(new int[] {3, 1, 2});
        imageServiceLayer.refresh();
      }
    });

    layers.add(imageServiceLayer);

    return map;
  }

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

          // 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();

    contentPane.add(jMap);

    return contentPane;
  }

  //------------------------------------------------------------------------
  // Private methods
  //------------------------------------------------------------------------
  /**
   * Creates a window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame("Image Service 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);
        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;
  }
}
Feedback on this topic?