arcgissamples\cartography\DisplayRenderersMain.java
/* Copyright 2010 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 arcgissamples.cartography; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Dimension; 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.UIManager; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.carto.ChartRenderer; import com.esri.arcgis.carto.ClassBreaksRenderer; import com.esri.arcgis.carto.DotDensityRenderer; import com.esri.arcgis.carto.FeatureLayer; import com.esri.arcgis.carto.IRendererFields; import com.esri.arcgis.carto.ProportionalSymbolRenderer; import com.esri.arcgis.carto.SimpleRenderer; import com.esri.arcgis.carto.UniqueValueRenderer; import com.esri.arcgis.carto.esriViewDrawPhase; import com.esri.arcgis.display.AlgorithmicColorRamp; import com.esri.arcgis.display.BarChartSymbol; import com.esri.arcgis.display.DotDensityFillSymbol; import com.esri.arcgis.display.HsvColor; import com.esri.arcgis.display.IChartSymbol; import com.esri.arcgis.display.IColor; import com.esri.arcgis.display.IEnumColors; import com.esri.arcgis.display.IMarkerSymbol; import com.esri.arcgis.display.IRgbColor; import com.esri.arcgis.display.ISymbolArray; import com.esri.arcgis.display.RandomColorRamp; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.display.SimpleFillSymbol; import com.esri.arcgis.display.SimpleLineSymbol; import com.esri.arcgis.display.SimpleMarkerSymbol; import com.esri.arcgis.display.esriColorRampAlgorithm; import com.esri.arcgis.display.esriSimpleFillStyle; import com.esri.arcgis.display.esriSimpleMarkerStyle; import com.esri.arcgis.geodatabase.DataStatistics; import com.esri.arcgis.geodatabase.ICursor; import com.esri.arcgis.geodatabase.IRow; import com.esri.arcgis.geodatabase.IRowBuffer; import com.esri.arcgis.geodatabase.QueryFilter; import com.esri.arcgis.interop.AutomationException; 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 sample shows a standalone Java application that uses the ArcObjects * components to load the US states shape file. The main aim is to illustrate * some of the different renderer objects that are available by selecting * an appropriate button. * Application uses the "states" shapefiles from the developer kit samples. * If user want to load other shapefile then attribute fieldnames should be * changed appropriately. ClassBreaks and UniqueValue renderers use * STATE_NAME, POP1990, POP1999 and STATE_FIPS fields. */ public class DisplayRenderersMain extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; MapBean mapBean = new MapBean(); TextField textNameField = new TextField(); TextField textPopField1 = new TextField(); TextField textPopField2 = new TextField(); TextField textStateFips = new TextField(); Button buttonSimpleRendererButton = new Button(); Button buttonUniqueValueButton = new Button(); Button buttonClassBreaksButton = new Button(); Button buttonProportionalButton = new Button(); Button buttonBarChartButton = new Button(); Button buttonDotDensityButton = new Button(); FeatureLayer featureLayer = null; /** * Method to start the program execution. */ public static void main(String s[]) { System.out.println("Starting \"Display Renderers\" - 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 start with different data // String workspacePath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa"; String featureClassName = "states"; 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(); DisplayRenderersMain thisApp = new DisplayRenderersMain(); thisApp.initUI(workspacePath, featureClassName); } catch (Exception ex) { System.out.println("Sample failed: " + ex.getMessage()); ex.printStackTrace(); } } static void initializeArcGISLicenses() { try { AoInitialize ao = new AoInitialize(); if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); } catch (Exception e) { e.printStackTrace(); } } /* * Lays out the User Interface, and sets up event listeners */ private void initUI(String path, String name) throws AutomationException, IOException { this.setTitle("DisplayRenderers Java Sample: Simple, UniqueValue, ClassBreak, Proportional, BarChart, DotDensity renderers"); this.setSize(new Dimension(800, 600)); this.getContentPane().setLayout(new BorderLayout()); Panel firstPanel = new Panel(new FlowLayout(FlowLayout.LEADING)); Label labelNameField = new Label(); labelNameField.setText("NameField:"); this.textNameField.setText("STATE_NAME"); Label labelStateFips = new Label(); labelStateFips.setText("StateFips:"); this.textStateFips.setText("STATE_FIPS"); this.buttonSimpleRendererButton.setLabel("Simple"); this.buttonUniqueValueButton.setLabel("Unique Value"); this.buttonClassBreaksButton.setLabel("Class Breaks"); firstPanel.add(labelNameField); firstPanel.add(this.textNameField); firstPanel.add(labelStateFips); firstPanel.add(this.textStateFips); firstPanel.add(this.buttonSimpleRendererButton); firstPanel.add(this.buttonUniqueValueButton); firstPanel.add(this.buttonClassBreaksButton); Panel secondPanel = new Panel(new FlowLayout(FlowLayout.LEADING)); Label labelPopField1 = new Label(); labelPopField1.setText("PopField1:"); labelNameField.setText("NameField:"); this.textPopField1.setText("POP1990"); Label labelPopField2 = new Label(); labelPopField2.setText("PopField1:"); this.textPopField2.setText("POP1999"); this.buttonProportionalButton.setLabel("Proportional"); this.buttonBarChartButton.setLabel("BarChart"); this.buttonDotDensityButton.setLabel("DotDensity"); secondPanel.add(labelPopField1); secondPanel.add(this.textPopField1); secondPanel.add(labelPopField2); secondPanel.add(this.textPopField2); secondPanel.add(this.buttonProportionalButton); secondPanel.add(this.buttonBarChartButton); secondPanel.add(this.buttonDotDensityButton); Panel toolPanel = new Panel(new GridLayout(3,1)); toolPanel.add(firstPanel); toolPanel.add(secondPanel); Panel nestPanel = new Panel(); nestPanel.add(toolPanel); this.getContentPane().add(this.mapBean, BorderLayout.CENTER); this.getContentPane().add(nestPanel, BorderLayout.SOUTH); this.mapBean.clearLayers(); this.mapBean.addShapeFile(path, name); this.featureLayer = (FeatureLayer) this.mapBean.getLayer(0); // update map this.setVisible(true); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); // Add action listeners this.buttonSimpleRendererButton.addActionListener(this); this.buttonUniqueValueButton.addActionListener(this); this.buttonClassBreaksButton.addActionListener(this); this.buttonProportionalButton.addActionListener(this); this.buttonBarChartButton.addActionListener(this); this.buttonDotDensityButton.addActionListener(this); // Add windows close listener this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { new AoInitialize().shutdown(); } catch(IOException ex) { // exit anyway } System.out.println("Done."); System.exit(0); } }); } /** * @see ActionListener#actionPerformed */ public void actionPerformed(ActionEvent event) { Object evt = event.getSource(); if (evt == this.buttonSimpleRendererButton) { simpleRenderer(); } if (evt == this.buttonUniqueValueButton) { uniqueValueRenderer(); } if (evt == this.buttonClassBreaksButton) { classBreaksRenderer(); } if (evt == this.buttonProportionalButton) { proportionalRenderer(); } if (evt == this.buttonBarChartButton) { barChartRenderer(); } if (evt == this.buttonDotDensityButton) { dotDensityRenderer(); } } /** * The method creates SimpleRenderer object and attach it to the map. */ private void simpleRenderer() { try { SimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol(); RgbColor color = new RgbColor(); color.setRed(213); color.setGreen(212); color.setBlue(252); simpleFillSymbol.setColor(color); SimpleLineSymbol outlineSymbol = new SimpleLineSymbol(); outlineSymbol.setColor(new RgbColor()); // 0,0,0 outlineSymbol.setWidth(1.0); simpleFillSymbol.setOutline(outlineSymbol); SimpleRenderer simpleRenderer = new SimpleRenderer(); simpleRenderer.setSymbolByRef(simpleFillSymbol); FeatureLayer fl = (FeatureLayer) this.mapBean.getLayer(0); fl.setRendererByRef(simpleRenderer); this.mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null); // esriViewGeography doens't work } catch (java.lang.Exception ex) { ex.printStackTrace(); // never happened } } /** * The method creates UniqueValueRenderer object and attach it to the map. */ private void uniqueValueRenderer() { try { UniqueValueRenderer uniqueValueRenderer = new UniqueValueRenderer(); int fieldNumber = this.featureLayer.findField(this.textNameField.getText()); if (fieldNumber == -1) { System.out.println("Can't find field : " + this.textNameField); return; } uniqueValueRenderer.setFieldCount(1); uniqueValueRenderer.setField(0, this.textNameField.getText()); RandomColorRamp colorRamp = new RandomColorRamp(); colorRamp.setStartHue(0); colorRamp.setMinValue(99); colorRamp.setMaxSaturation(15); colorRamp.setEndHue(360); colorRamp.setMaxValue(100); colorRamp.setMaxSaturation(30); colorRamp.setSize(100); boolean rampBool[] = new boolean[1]; rampBool[0] = true; colorRamp.createRamp(rampBool); IEnumColors enumRamp = colorRamp.getColors(); QueryFilter queryFilter = new QueryFilter(); queryFilter.addField(this.textNameField.getText()); ICursor cursor = this.featureLayer.ITable_search(queryFilter, true); IRow nextRow = cursor.nextRow(); while (nextRow != null) { IRow nextRowBuffer = nextRow; Object codeValue = nextRowBuffer.getValue(fieldNumber); IColor nextUniqueColor = enumRamp.next(); if (nextUniqueColor == null) { enumRamp.reset(); nextUniqueColor = enumRamp.next(); } SimpleFillSymbol simpleFillSymbol = new SimpleFillSymbol(); simpleFillSymbol.setColor(nextUniqueColor); uniqueValueRenderer.addValue((String) codeValue, (String) codeValue, simpleFillSymbol); nextRow =cursor.nextRow(); } this.featureLayer.setRendererByRef(uniqueValueRenderer); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (java.lang.Exception ex) { System.out.println("Unique Value Renderer, exception:" + ex); } } /** * The method creates ClassBreaksRenderer object and attach it to the map. */ private void classBreaksRenderer() { try { double classes[] = new double[3]; classes[0] = 1; classes[1] = 20; classes[2] = 56; ClassBreaksRenderer classBreaksRenderer = new ClassBreaksRenderer(); classBreaksRenderer.setField(this.textStateFips.getText()); classBreaksRenderer.setBreakCount(3); classBreaksRenderer.setSortClassesAscending(true); HsvColor fromColor = new HsvColor(); fromColor.setHue(60); // Yellow fromColor.setSaturation(100); fromColor.setValue(96); HsvColor toColor = new HsvColor(); toColor.setHue(0); // Red toColor.setSaturation(100); toColor.setValue(96); AlgorithmicColorRamp ramp = new AlgorithmicColorRamp(); ramp.setAlgorithm(esriColorRampAlgorithm.esriHSVAlgorithm); ramp.setFromColor(fromColor); ramp.setToColor(toColor); ramp.setSize(3); boolean ok[] = new boolean[1]; ramp.createRamp(ok); IEnumColors enumColors = ramp.getColors(); for (int breakIndex = 0; breakIndex < 3; breakIndex++) { IColor color = enumColors.next(); SimpleFillSymbol fillSymbol = new SimpleFillSymbol(); fillSymbol.setColor(color); fillSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid); classBreaksRenderer.setSymbol(breakIndex, fillSymbol); classBreaksRenderer.setBreak(breakIndex, classes[breakIndex]); } this.featureLayer.setRendererByRef(classBreaksRenderer); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (java.lang.Exception ex) { System.out.println("Class Breaks Renderer, exception:" + ex); } } /** * The method creates ProportionalRenderer object and attach it to the map. */ private void proportionalRenderer() { try { QueryFilter queryFilter = new QueryFilter(); queryFilter.addField(this.textPopField1.getText().trim()); ICursor cursor = this.featureLayer.ITable_search(queryFilter, true); DataStatistics dataStatistics = new DataStatistics(); dataStatistics.setCursorByRef(cursor); dataStatistics.setField(this.textPopField1.getText()); IStatisticsResults statisticsResult = dataStatistics.getStatistics(); if (statisticsResult == null) { System.out.println("Failed to gather stats on the feature class"); return; } SimpleFillSymbol fillSymbol = new SimpleFillSymbol(); fillSymbol.setColor(getRGBColor(239, 228, 190)); // Tan SimpleMarkerSymbol simpleMarkerSymbol = new SimpleMarkerSymbol(); simpleMarkerSymbol.setStyle(esriSimpleMarkerStyle.esriSMSSquare); simpleMarkerSymbol.setColor(getRGBColor(255, 0, 0)); // Red simpleMarkerSymbol.setSize(2); simpleMarkerSymbol.setOutline(true); simpleMarkerSymbol.setOutlineColor(getRGBColor(0, 0, 0)); // Black ProportionalSymbolRenderer proportionalSymbolRenderer = new ProportionalSymbolRenderer(); proportionalSymbolRenderer.setValueUnit(esriUnits.esriUnknownUnits); proportionalSymbolRenderer.setField(this.textPopField1.getText()); proportionalSymbolRenderer.setFlanneryCompensation(false); proportionalSymbolRenderer.setMinDataValue(statisticsResult.getMinimum()); proportionalSymbolRenderer.setMaxDataValue(statisticsResult.getMaximum()); proportionalSymbolRenderer.setBackgroundSymbol(fillSymbol); proportionalSymbolRenderer.setMinSymbol(simpleMarkerSymbol); this.featureLayer.setRendererByRef(proportionalSymbolRenderer); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (java.lang.Exception ex) { ex.printStackTrace(); System.out.println("Proportional Renderer, exception:" + ex); } } /** * The method creates BarChartRenderer object and attach it to the map. */ private void barChartRenderer() { try { ChartRenderer chartRenderer = new ChartRenderer(); IRendererFields rendererFields = chartRenderer; rendererFields.addField(this.textPopField1.getText(), null); rendererFields.setFieldAlias(0, rendererFields.getField(0)); rendererFields.addField(this.textPopField2.getText(), null); rendererFields.setFieldAlias(1, rendererFields.getField(1)); QueryFilter queryFilter = new QueryFilter(); queryFilter.addField(this.textPopField1.getText()); queryFilter.addField(this.textPopField2.getText()); ICursor cursor = this.featureLayer.ITable_search(queryFilter, true); int numFields = 2; // Number of bars int[] fieldIndecies = new int[numFields]; fieldIndecies[0] = this.featureLayer.findField(this.textPopField1.getText()); fieldIndecies[1] = this.featureLayer.findField(this.textPopField2.getText()); double maxValue = 0; boolean firstValue = true; IRowBuffer row = cursor.nextRow(); while (row != null) { for (int fieldIndex = 0; fieldIndex < numFields; fieldIndex++) { double fieldValue = Double.parseDouble(row.getValue(fieldIndecies[fieldIndex]).toString()); if (firstValue) { maxValue = fieldValue; firstValue = false; } if (fieldValue > maxValue) maxValue = fieldValue; } row = cursor.nextRow(); } if (maxValue <= 0) { System.out.println("Failed to calculate the maximum value or max value is 0."); return; } BarChartSymbol barChartSymbol = new BarChartSymbol(); IChartSymbol chartSymbol = barChartSymbol; barChartSymbol.setWidth(6); IMarkerSymbol markerSymbol = barChartSymbol; chartSymbol.setMaxValue(maxValue); markerSymbol.setSize(16); ISymbolArray symbolArray = barChartSymbol; SimpleFillSymbol fillSymbol1 = new SimpleFillSymbol(); fillSymbol1.setColor(getRGBColor(213, 212, 252)); symbolArray.addSymbol(fillSymbol1); SimpleFillSymbol fillSymbol2 = new SimpleFillSymbol(); fillSymbol2.setColor(getRGBColor(193, 252, 179)); symbolArray.addSymbol(fillSymbol2); chartRenderer.setChartSymbolByRef(barChartSymbol); chartRenderer.setLabel("Population"); SimpleFillSymbol fillSymbol3 = new SimpleFillSymbol(); fillSymbol3.setColor(getRGBColor(239, 228, 190)); chartRenderer.setBaseSymbolByRef(fillSymbol3); chartRenderer.setUseOverposter(false); this.featureLayer.setRendererByRef(chartRenderer); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (java.lang.Exception ex) { System.out.println("BarChart Renderer, exception : " + ex); } } /** * The method creates DotDensityRenderer object and attach it to the map. */ private void dotDensityRenderer() { try { DotDensityRenderer dotDensityRenderer = new DotDensityRenderer(); IRendererFields rendererFields = dotDensityRenderer; rendererFields.addField(this.textPopField1.getText(), null); DotDensityFillSymbol dotDensityFillSymbol = new DotDensityFillSymbol(); dotDensityFillSymbol.setDotSize(3); dotDensityFillSymbol.setColor(getRGBColor(0, 0, 0)); dotDensityFillSymbol.setBackgroundColor(getRGBColor(239, 228, 190)); // tan ISymbolArray symbolArray = dotDensityFillSymbol; SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(); markerSymbol.setStyle(esriSimpleMarkerStyle.esriSMSCircle); markerSymbol.setSize(3); markerSymbol.setColor(getRGBColor(0, 0, 0)); // Black symbolArray.addSymbol(markerSymbol); dotDensityRenderer.setDotDensitySymbolByRef(dotDensityFillSymbol); dotDensityRenderer.setDotValue(200000); this.featureLayer.setRendererByRef(dotDensityRenderer); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (java.lang.Exception ex) { System.out.println("Dot Density Renderer, exception:" + ex); } } /** * The method creates color object from color components. */ private IRgbColor getRGBColor(int red, int green, int blue) { IRgbColor rgbColor = null; try { //Create rgb color and grab hold of the IRGBColor interface rgbColor = new RgbColor(); rgbColor.setRed(red); rgbColor.setGreen(green); rgbColor.setBlue(blue); rgbColor.setUseWindowsDithering(true); rgbColor = (RgbColor)rgbColor; } catch (Exception ex) { ex.printStackTrace(); // never happened } return rgbColor; } }