arcgissamples\geoprocessing\MapFields.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 java.net.UnknownHostException; import com.esri.arcgis.geodatabase.Field; import com.esri.arcgis.geodatabase.IDETable; import com.esri.arcgis.geodatabase.IField; import com.esri.arcgis.geodatabase.IGPMessages; import com.esri.arcgis.geodatabase.esriFieldType; import com.esri.arcgis.geoprocessing.GPFieldMap; import com.esri.arcgis.geoprocessing.GPFieldMapping; import com.esri.arcgis.geoprocessing.GPUtilities; import com.esri.arcgis.geoprocessing.GeoProcessor; import com.esri.arcgis.geoprocessing.IGPFieldMap; import com.esri.arcgis.geoprocessing.IGeoProcessorResult; import com.esri.arcgis.geoprocessing.tools.conversiontools.TableToTable; 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.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; /** * This sample demonstrates how to set up field mapping for tools such as merge, table to table, featureclass to * featureclass, and append. */ public class MapFields { private GeoProcessor gp = null; public MapFields() { try { gp = new GeoProcessor(); gp.setOverwriteOutput(true); } catch (UnknownHostException e) { e.printStackTrace(); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * maps fields * @param inFeatures * @param outputFolder * @param outputFile */ private void mapFields(String inFeatures, String outputFolder, String outputFile) { try { // create a DETable data element GPUtilities gpUtilities = new GPUtilities(); IDETable inputTable = (IDETable) gpUtilities.makeDataElement(inFeatures, null, null); // create an array of input tables Array inputTables = new Array(); inputTables.add(inputTable); // Initialize the GPFIeldMapping GPFieldMapping fieldMapping = new GPFieldMapping(); fieldMapping.initialize(inputTables, null); // create a new output field Field tractidfield = new Field(); tractidfield.setName("TRACTID"); tractidfield.setType(esriFieldType.esriFieldTypeString); tractidfield.setLength(50); // Find field map "FIPS" containing the input field "FIPS". Add input field to the new field map. GPFieldMap tractid = new GPFieldMap(); tractid.setOutputFieldByRef(tractidfield); int fieldMapIndex = fieldMapping.findFieldMap("FIPS"); IGPFieldMap fipsFieldMap = fieldMapping.getFieldMap(fieldMapIndex); int fieldIndex = fipsFieldMap.findInputField(inputTable, "FIPS"); IField inputField = fipsFieldMap.getField(fieldIndex); tractid.addInputField(inputTable, inputField, 5, 11); // Add the new field map to the field mapping fieldMapping.addFieldMap(tractid); // execute the table to table tool using the fieldMapping TableToTable tableToTable = new TableToTable(inFeatures, outputFolder, outputFile); tableToTable.setFieldMapping(fieldMapping); IGeoProcessorResult output = (IGeoProcessorResult) gp.execute(tableToTable, null); System.out.println("Finished - please go look for a column called \"tractid\" in " + output.getMessages(0)); } catch (Exception e) { try { // if there was an error then you want to loop the messages // returned by the geoprocessor to look for the error IGPMessages gpMessages = gp.getReturnMessages(); for (int i = 0; i < gpMessages.getCount(); i++) { System.out.println(gpMessages.getMessage(i).getDescription()); } } catch (Exception e2) { } e.printStackTrace(); } } public static void main(String[] args) { System.out.println("Geoprocessing field mapping - An ArcObjects Java SDK Developer Sample"); try { // Initialize the engine and licenses. EngineInitializer.initializeEngine(); AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); // Change the following lines if you want to use different data String inFeatures = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa" + File.separator + "ortracts_simplified.shp"; //Create output workspace path String outputFolder = getOutputDir(); String outputFile = "ortract"; MapFields map = new MapFields(); map.mapFields(inFeatures, outputFolder, outputFile); //shut down engine aoInit.shutdown(); } catch (Exception e) { e.printStackTrace(); } } /** * 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(); } } /** * Retrieves output directory * @return */ 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; } }