arcgissamples\geoprocessing\ConvertSDEToLayer.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.IGPMessages; import com.esri.arcgis.geoprocessing.GeoProcessor; import com.esri.arcgis.geoprocessing.tools.datamanagementtools.AddJoin; import com.esri.arcgis.geoprocessing.tools.datamanagementtools.MakeFeatureLayer; import com.esri.arcgis.geoprocessing.tools.datamanagementtools.SaveToLayerFile; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; public class ConvertSDEToLayer { GeoProcessor gp = null; public ConvertSDEToLayer(String sdeConnFile) { try { gp = new GeoProcessor(); gp.setOverwriteOutput(true); gp.setEnvironmentValue("workspace", sdeConnFile); } catch (UnknownHostException e) { e.printStackTrace(); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { System.out.println("Geoprocessing SDE Example - An ArcObjects Java SDK Developer Sample"); try { if(args.length != 1) { System.out.println("Because this sample uses SDE, you must first ensure that a SDE connection file exists. " + "This connection file must be provided to this sample as command line argument."); System.exit(-1); } // Initialize the engine and licenses. EngineInitializer.initializeEngine(); AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); //Create output workspace path String outputWorkspace = getOutputDir(); ConvertSDEToLayer convert = new ConvertSDEToLayer(args[0]); convert.convertSDEFeatureClassToLayer(outputWorkspace); aoInit.shutdown(); } catch (Exception e) { System.out.println("Threw an exception creating the geoprocessor"); e.printStackTrace(); } } /** * Converts a Feature Ckass or table in SDE to layer * @param outputWorkspace */ public void convertSDEFeatureClassToLayer(String outputWorkspace) { try { //Please change the feature class name below, to use your own SDE fc String tableName = gp.qualifyTableName("User.FC", (String) gp.getEnvironmentValue("workspace")); System.out.println("Here is the FC: " + tableName); MakeFeatureLayer makeFeatureLayer = new MakeFeatureLayer("ERIC4490.SanBernardino_Demographics/ERIC4490.tgr06071trt00", "tractLayer"); gp.execute(makeFeatureLayer, null); AddJoin addJoin = new AddJoin("tractLayer", "STFID", "ERIC4490.tgr06000sf1trt", "STFID"); gp.execute(addJoin, null); // save the joined data to a layer file SaveToLayerFile saveToLayerFile = new SaveToLayerFile("tractLayer", outputWorkspace + File.separator + "tractlayer.lyr"); gp.execute(saveToLayerFile, null); // shutdown everything new AoInitialize().shutdown(); System.out.println("Finished"); } 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(); } } /** * 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; } /** * 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(); } } }