Image service mosaic rule
Download Sample ViewerDescription
This sample shows how to create a mosaic rule and apply it to an Image Service layer. Note that due to the clean nature of this Image Service, changes may not be visible.
Code snippet
MosaicRule mosaicRule = new MosaicRule();
// get the selected mosaic rule from the combo box
mosaicRule.setMethod(methodMap.get(cbxMosaicMethods.getSelectedItem()));
imageServiceLayer.setMosaicRule(mosaicRule);
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.LinkedHashMap;
import java.util.Map;
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.MosaicRule;
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 mosaic rule and apply it to an Image Service layer.
* Note that due to the clean nature of this Image Service, changes may not be visible.
*/
public class MosaicRuleApp {
private JMap jMap;
private ArcGISImageServiceLayer imageServiceLayer;
// initial mosaic rule
private int mosaicMethod = MosaicRule.METHOD_NORTHWEST;
private static final int COMPONENT_HEIGHT = 25;
private static final int COMPONENT_WIDTH = 220;
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
public MosaicRuleApp() {
}
// ------------------------------------------------------------------------
// 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
MosaicRuleApp app = new MosaicRuleApp();
// 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);
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 Mosaic 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 to North America
map.setExtent(new Envelope(-9001310, 4192541, -8996619, 4195697));
LayerList layers = map.getLayers();
// add layers
ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer(
"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
layers.add(tiledLayer);
imageServiceLayer = new ArcGISImageServiceLayer(
"http://sampleserver6.arcgisonline.com/arcgis/rest/services/CharlotteLAS/ImageServer");
MosaicRule mosaicRule = new MosaicRule();
mosaicRule.setMethod(mosaicMethod);
imageServiceLayer.setMosaicRule(mosaicRule);
layers.add(imageServiceLayer);
return map;
}
// ------------------------------------------------------------------------
// Private methods
// ------------------------------------------------------------------------
/**
* Creates the user panel
*/
private JPanel createUserPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setSize(COMPONENT_WIDTH, COMPONENT_HEIGHT*2 + 20);
panel.setLocation(10, 10);
JLabel lblMosaicMethod = new JLabel("Mosaic Method:");
lblMosaicMethod.setForeground(Color.WHITE);
lblMosaicMethod.setAlignmentX(Component.LEFT_ALIGNMENT);
lblMosaicMethod.setMaximumSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));
final Map<String, Integer> methodMap = new LinkedHashMap<>();
methodMap.put("ByAttribute", Integer.valueOf(MosaicRule.METHOD_ATTRIBUTE));
methodMap.put("Center", Integer.valueOf(MosaicRule.METHOD_CENTER));
methodMap.put("LockRaster", Integer.valueOf(MosaicRule.METHOD_LOCKRASTER));
methodMap.put("Nadir", Integer.valueOf(MosaicRule.METHOD_NADIR));
methodMap.put("None", Integer.valueOf(MosaicRule.METHOD_NONE));
methodMap.put("Northwest", Integer.valueOf(MosaicRule.METHOD_NORTHWEST));
methodMap.put("Seamline", Integer.valueOf(MosaicRule.METHOD_SEAMLINE));
methodMap.put("Viewpoint", Integer.valueOf(MosaicRule.METHOD_VIEWPOINT));
final JComboBox<String> cbxMosaicMethods = new JComboBox<>(methodMap.keySet().toArray(new String[methodMap.size()]));
cbxMosaicMethods.setMaximumSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));
cbxMosaicMethods.setAlignmentX(Component.LEFT_ALIGNMENT);
cbxMosaicMethods.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
MosaicRule mosaicRule = new MosaicRule();
mosaicRule.setMethod(methodMap.get(cbxMosaicMethods.getSelectedItem()).intValue());
System.out.println("Setting mosaic rule to : " + mosaicRule.getMethod());
imageServiceLayer.setMosaicRule(mosaicRule);
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(cbxMosaicMethods);
panel.setBorder(BorderFactory.createEmptyBorder(5,10,10,10));
return panel;
}
}