arcgissamples\geoprocessing\EventListener.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.geoprocessing; import java.io.File; import java.io.IOException; import com.esri.arcgis.geodatabase.IGPMessage; import com.esri.arcgis.geodatabase.IGPMessages; import com.esri.arcgis.geoprocessing.GeoProcessor; import com.esri.arcgis.geoprocessing.IGPTool; import com.esri.arcgis.geoprocessing.IGeoProcessorEvents; import com.esri.arcgis.geoprocessing.tools.analysistools.Buffer; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.Array; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.IArray; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; public class EventListener { String inputDir, outputDir; AoInitialize ao = null; /** * default constructor */ public EventListener() { // TODO - create folder called airphotos in java\samples\data\raster\ // and copy seattle1 and 2 there //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); inputDir = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa"; // Verify that output folder exists. If it already exists, clean it, // delete it and re-create it. If it does not exist, create it. outputDir = getOutputDir() + File.separator + "gpeventslistener"; cleanAndRecreateDirectory(outputDir); } public static void main(String[] args) { System.out.println("Starting CreateFeatureClass - An ArcObjects Java SDK Developer Sample"); try { // Initialize the engine and licenses. EngineInitializer.initializeEngine(); AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); EventListener gpel = new EventListener(); gpel.executeTools(); aoInit.shutdown(); // Do not make any call to ArcObjects after ShutDownApplication() EngineInitializer.releaseAll(); } catch (Exception e) { e.printStackTrace(); } } /** * Creates a mosaic in Raster Catalog * * @throws Exception */ public void executeTools() throws Exception { System.out.print("\n**Creating GeoProcessor object..."); GeoProcessor geoprocessor = new GeoProcessor(); // instruct the geoprocessing engine to overrite existing datasets geoprocessor.setOverwriteOutput(true); System.out.println("done."); System.out.print("\n**Creating GeoProcessor Event Handler..."); IGeoProcessorEvents gpEventHandler = new IGeoProcessorEvents() { public void onMessageAdded(IGPMessage arg0) throws IOException, AutomationException { System.out.println("in onMessageAdded."); } public void preToolExecute(IGPTool arg0, IArray arg1, int arg2) throws IOException, AutomationException { System.out.println("in preToolExecute."); } public void postToolExecute(IGPTool arg0, IArray arg1, long arg2, IGPMessages arg3) throws IOException, AutomationException { System.out.println("in postToolExecute."); } public void toolboxChange() throws IOException, AutomationException { System.out.println("in toolboxChange."); } }; System.out.println("done."); /** * STEP 1: register the event helper in order to be able to listen to GP events */ // wire the GP events System.out.print("\n**Wiring GP Events..."); geoprocessor.registerGeoProcessorEvents(gpEventHandler); System.out.println("done."); System.out.print("\n**Executing Buffer GP tool..."); String inputFC = inputDir + File.separator + "InputPoint.shp"; String outputFC = outputDir + File.separator + "bufferOutput.shp"; String distance = "10 miles"; Buffer buffer = new Buffer(inputFC, outputFC, distance); Array params = new Array(); params.add(buffer.getInFeatures()); params.add(buffer.getOutFeatureClass()); params.add(buffer.getBufferDistanceOrField()); geoprocessor.execute(buffer, null); System.out.println("done."); // unwire the GP events System.out.print("\n**Unwiring GP Events..."); geoprocessor.unRegisterGeoProcessorEvents(gpEventHandler); System.out.println("done."); } /** * Initializes the lowest available ArcGIS License */ private static void initializeArcGISLicenses(AoInitialize aoInit) { try { if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) { aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); } else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) { aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); } else { System.err.println("Could not initialize an Engine or ArcView license. Exiting application."); System.exit(-1); } } catch (Exception e) { e.printStackTrace(); } } /** * Empties specified directory of all files, deletes and re-creates it * * @param dirName * String */ public void cleanAndRecreateDirectory(String dirName) { cleanAndDeleteDirectory(dirName); File dir = new File(dirName); dir.mkdir(); } /** * Deletes all files in specified directory and then deletes the directory as well * * @param Path * String */ public void cleanAndDeleteDirectory(String Path) { File src = new File(Path); if (src.isDirectory() && src.exists()) { File list[] = src.listFiles(); for (int i = 0; i < list.length; i++) { if (list[i].isDirectory()) { cleanAndDeleteDirectory(list[i].getPath()); list[i].delete(); } else { list[i].delete(); } } src.delete(); } else { src.delete(); } } private static String getOutputDir() { String userDir; if (System.getProperty("os.name").toLowerCase().indexOf("win") > -1) userDir = System.getenv("UserProfile"); else userDir = System.getenv("HOME"); String outputDir = userDir + File.separator + "arcgis_sample_output"; System.out.println("Creating output directory - " + outputDir); new File(outputDir).mkdir(); return outputDir; } }