arcgissamples\cartography\RotationRendererSample.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Rotation renderer
arcgissamples\cartography\RotationRendererSample.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.cartography;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.FeatureLayer;
import com.esri.arcgis.carto.IRotationRenderer;
import com.esri.arcgis.carto.ProportionalSymbolRenderer;
import com.esri.arcgis.carto.esriSymbolRotationType;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.display.ArrowMarkerSymbol;
import com.esri.arcgis.display.esriArrowMarkerStyle;
import com.esri.arcgis.geodatabase.DataStatistics;
import com.esri.arcgis.geodatabase.ICursor;
import com.esri.arcgis.geodatabase.QueryFilter;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.IStatisticsResults;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;
import com.esri.arcgis.system.esriUnits;

/**
 * This sets up a proportional symbol renderer with additional settings in the
 * rotation renderer interface display arrow markers showing wind direction and
 * velocity.
 * The shape file "wind.shp" is used in this sample.
 * Two fields are used from the shape file: VELOCITY and DIRECTION
 * First velocity field corresponds to the size of the arrow marker
 * second the direction field corresponds to the rotation of the arrow marker.
 */
public class RotationRendererSample extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  Button buttonOpenFile = new Button();
  Button buttonSetRotationRenderer = new Button();
  TextField textVelocityField = new TextField();
  TextField textDirectionField = new TextField();
  FeatureLayer featureLayer = null;

  /**
   * Constructor
   */
  public RotationRendererSample(String workspacePath, String featureClassName) {
    initUI(workspacePath, featureClassName);
  }

  /**
   * Method to start the program execution.
   * @param s String[]
   */
  public static void main(String s[]) {
    System.out.println("Starting Rotation Renderer - An ArcObjects Java SDK Developer Sample");
    //Get DEVKITHOME Home
    String devKitHome = System.getenv("AGSDEVKITJAVA");
    
    if (!(new File(devKitHome).exists())) {
      System.out.println(devKitHome + " does not exist.\nExiting...");
      System.exit(-1);
    }
    //
    // Change the following lines if you want to use different data
    //
    String featureClassName = "wind";
    String workspacePath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa";
    File shapefileFile = new File(workspacePath, featureClassName + ".shp");
    if (!shapefileFile.exists()) {
      System.out.println("Shapefile does not exist: " + shapefileFile.getAbsolutePath());
      System.exit(0);
    }


    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      
      initializeArcGISLicenses();
      
      new RotationRendererSample(workspacePath, featureClassName);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  
  static void initializeArcGISLicenses() {
    try {
      aoInit = new AoInitialize();
      
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeBasic) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI(String path, String name) {
    this.setTitle("RotationRendererSample Java Sample");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    JPanel loadPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    Label labelComment = new Label();
    labelComment.setText("Open other point feature shapefile having velocity and direction components");
    this.buttonOpenFile.setLabel("Open File ...");
    loadPanel.add(labelComment);
    loadPanel.add(this.buttonOpenFile);

    JPanel fieldPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    Label labelVelocityField = new Label();
    labelVelocityField.setText("VelocityField");
    this.textVelocityField.setText("VELOCITY");
    Label labelDirectionField = new Label();
    labelDirectionField.setText("DirectionField");
    this.textDirectionField.setText("DIRECTION");
    this.buttonSetRotationRenderer.setLabel("Rotation Renderer");
    fieldPanel.add(labelVelocityField);
    fieldPanel.add(this.textVelocityField);
    fieldPanel.add(labelDirectionField);
    fieldPanel.add(this.textDirectionField);
    fieldPanel.add(this.buttonSetRotationRenderer);

    Panel toolPanel = new Panel(new GridLayout(2,1));
    toolPanel.add(fieldPanel);
    toolPanel.add(loadPanel);

    Panel nestPanel = new Panel();
    nestPanel.add(toolPanel);

    try {
      this.mapBean.clearLayers();
      this.mapBean.addShapeFile(path, name);
      this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
      this.featureLayer = (FeatureLayer) this.mapBean.getLayer(0);
    } catch (IOException e1) {
      System.out.println("Could not load shapefile.");
    }



    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(nestPanel, BorderLayout.SOUTH);
    this.setVisible(true);

    // set listeners
    this.buttonOpenFile.addActionListener(this);
    this.buttonSetRotationRenderer.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }

  /**
   * implements interface ActionListener
   * @see ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    Object eventSource = event.getSource();
    if (eventSource == this.buttonOpenFile) {
      try {
        FileDialog fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
        fileDialog.setVisible(true);
        String fullFilename = fileDialog.getDirectory() + fileDialog.getFile();
        String path = fullFilename.substring(0, fullFilename.lastIndexOf(File.separator));
        String name = fullFilename.substring(fullFilename.lastIndexOf(File.separator) + 1);
        this.mapBean.clearLayers();
        this.mapBean.addShapeFile(path, name);
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
        this.featureLayer = (FeatureLayer) this.mapBean.getLayer(0);
        System.out.println("Load Data");
      } catch (java.lang.Exception ex) {
        System.out.println("Can't load data, exception:" + ex);
      }
    }
    if (eventSource == this.buttonSetRotationRenderer) {
      try {
        // Use the statistics objects to calculate the min and max values
        // Initialize a query to use the velocity field
        QueryFilter queryFilter = new QueryFilter();
        queryFilter.addField(this.textVelocityField.getText());
        ICursor cursor = this.featureLayer.ITable_search(queryFilter, true);
        // The Statistics object can save us iterating through the feature class
        DataStatistics dataStatistics = new DataStatistics();
        dataStatistics.setCursorByRef(cursor);
        dataStatistics.setField(this.textVelocityField.getText());
        // Go and gather the statistics
        IStatisticsResults statisticsResult = dataStatistics.getStatistics();
        if (statisticsResult == null) {
          System.out.println("Failed to gather stats on the feature class");
          return;
        }
        // Statistics result object now contains max and min
        //  Now set up the markers symbol and the renderer
        // This is the smallest marker size
        ArrowMarkerSymbol arrowMarkerSymbol = new ArrowMarkerSymbol();
        arrowMarkerSymbol.setSize(6.0);
        arrowMarkerSymbol.setStyle(esriArrowMarkerStyle.esriAMSPlain);
        // Set up the proportional symbol renderer
        ProportionalSymbolRenderer proportionalSymbolRenderer = new ProportionalSymbolRenderer();
        proportionalSymbolRenderer.setValueUnit(esriUnits.esriUnknownUnits);
        proportionalSymbolRenderer.setField(this.textVelocityField.getText());
        proportionalSymbolRenderer.setFlanneryCompensation(false);
        proportionalSymbolRenderer.setMinDataValue(statisticsResult.getMinimum());
        proportionalSymbolRenderer.setMaxDataValue(statisticsResult.getMaximum());
        proportionalSymbolRenderer.setMinSymbol(arrowMarkerSymbol);
        // Set up a rotation renderer values on the proportional symbol renderer
        IRotationRenderer pRotationRenderer = proportionalSymbolRenderer;
        pRotationRenderer.setRotationField(this.textDirectionField.getText());
        pRotationRenderer.setRotationType(esriSymbolRotationType.esriRotateSymbolGeographic);
        // Create the legend symbols
        proportionalSymbolRenderer.setLegendSymbolCount(3);
        proportionalSymbolRenderer.createLegendSymbols();
        // set new renderer
        this.featureLayer.setRendererByRef(proportionalSymbolRenderer);
        // redraw map
        this.mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null);
      } catch (java.io.IOException ex) {
        System.out.println("Exception when setting RotationRenderer:" + ex);
      }
    }
  }

}