arcgissamples\geodatabase\BrowseData.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.geodatabase; import java.io.File; import java.io.IOException; import com.esri.arcgis.datasourcesGDB.AccessWorkspaceFactory; import com.esri.arcgis.datasourcesGDB.SdeWorkspaceFactory; import com.esri.arcgis.datasourcesfile.ArcInfoWorkspaceFactory; import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory; import com.esri.arcgis.geodatabase.FeatureClass; import com.esri.arcgis.geodatabase.FeatureDatasetName; import com.esri.arcgis.geodatabase.IDatasetName; import com.esri.arcgis.geodatabase.IEnumDatasetName; import com.esri.arcgis.geodatabase.IFeatureClassName; import com.esri.arcgis.geodatabase.Workspace; import com.esri.arcgis.geodatabase.WorkspaceFactory; import com.esri.arcgis.geodatabase.WorkspaceName; import com.esri.arcgis.geodatabase.esriDatasetType; import com.esri.arcgis.geometry.esriGeometryType; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; import com.esri.arcgis.trackinganalyst.DatasetNames; public class BrowseData { public BrowseData(){ } /** * Main Method - The console application entry point. * * @param args String[] Command line argument */ public static void main(String[] args) { System.out.println("Starting BrowseData - An ArcObjects SDK Developer Sample"); try{ //Initialize engine console application EngineInitializer.initializeEngine(); //Initialize ArcGIS license AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); //Data access setup String inShapePath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa"; BrowseData browseData = new BrowseData(); browseData.browse("Shapefile", inShapePath); System.out.println("Done."); //Ensure any ESRI libraries are unloaded in the correct order aoInit.shutdown(); }catch(Exception e){ System.out.println("Error: " + e.getMessage()); System.out.println("Sample failed. Exiting..."); e.printStackTrace(); System.exit(-1); } } /** * Checks to see if an ArcGIS Engine Runtime license or an ArcView License * is available. If so, then the appropriate ArcGIS License is initialized. * * @param aoInit The AoInitialize object instantiated in the main method. */ 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();} } /** * Creates a workspace based on datatype * * @param Type - datatype {Shape|Access|ArcInfo|SDE} * @param Input - a directory path on disk where the data exists or the filepath * @throws IOException if couldn't browse data */ public void browse(String dataType, String inputPath) throws IOException { try { String fileName = null; WorkspaceFactory workspaceFactory = null; //Determine what type of workspace factory to instantiate if (dataType.equalsIgnoreCase("Access")) { //Access - use the common dialog to browse for .mdb file fileName = inputPath; if (fileName.lastIndexOf(".mdb") < 0) { return; } workspaceFactory = new WorkspaceFactory(new AccessWorkspaceFactory()); } else if (dataType.equalsIgnoreCase("ArcInfo")) { //ArcInfo - use the OpenWorkspace form fileName = inputPath; workspaceFactory = new WorkspaceFactory (new ArcInfoWorkspaceFactory()); } else if (dataType.equalsIgnoreCase("SDE")) { //SDE - use the common dialog to browse for SDE connection files fileName = inputPath; if (fileName.lastIndexOf(".sde") < 0) { return; } workspaceFactory = new WorkspaceFactory(new SdeWorkspaceFactory()); } else if (dataType.equalsIgnoreCase("Shapefile")) { //Shapefile - use the OpenWorkspace formfiles fileName = inputPath; workspaceFactory = new WorkspaceFactory(new ShapefileWorkspaceFactory()); } System.out.println("Workspace type: " + workspaceFactory.getWorkspaceDescription(false)); //Get the workspace Workspace workspace = new Workspace(workspaceFactory.openFromFile(fileName, 0)); IDatasetName datasetName = null; IEnumDatasetName datasetNames = null; if (workspaceFactory.getWorkspaceDescription(false).equalsIgnoreCase("Shapefiles")) { //Shapefiles are not within a feature dataset datasetNames = workspace.getDatasetNames(esriDatasetType.esriDTFeatureClass); //Get the first dataset name datasetName = datasetNames.next(); while (datasetName != null) { System.out.print("\tFeatureClass: " + datasetName.getName()); listFeatureClasses(workspaceFactory, workspace, datasetName.getName(), datasetName.getName()); datasetName = datasetNames.next(); } } else { datasetNames = workspace.getDatasetNames(esriDatasetType.esriDTFeatureDataset); //Get the first dataset name datasetName = datasetNames.next(); while (datasetName != null) { System.out.print("\tDataset: " + datasetName.getName()); listDatasets(workspaceFactory, workspace, datasetName.getName()); datasetName = datasetNames.next(); } } }catch (IOException e) {throw e;} } /** * List Feature classes in the dataset * * @param workspaceFactory * @param workspace * @param featureClasses * @param datasetNameString * @throws IOException */ private void listFeatureClasses(WorkspaceFactory workspaceFactory, Workspace workspace, String featureClasses, String datasetNameString) throws IOException { try { FeatureClass featureClass = null; if (workspaceFactory.getWorkspaceDescription(false).equalsIgnoreCase("ArcInfo Workspace")) { String featureClassNameString = datasetNameString + ":" + featureClasses; featureClass = new FeatureClass(workspace.openFeatureClass(featureClassNameString)); } else { featureClass = new FeatureClass(workspace.openFeatureClass(featureClasses)); } String shp = null; switch (featureClass.getShapeType()) { case esriGeometryType.esriGeometryBezier3Curve: shp = "Bezier Curve"; break; case esriGeometryType.esriGeometryCircularArc: shp = "Circular Arc"; break; case esriGeometryType.esriGeometryEnvelope: shp = "Envelope"; break; case esriGeometryType.esriGeometryLine: shp = "Line"; break; case esriGeometryType.esriGeometryMultiPatch: shp = "MultiPatch"; break; case esriGeometryType.esriGeometryMultipoint: shp = "MultiPoint"; break; case esriGeometryType.esriGeometryNull: shp = "Null Geometry"; break; case esriGeometryType.esriGeometryPath: shp = "Path"; break; case esriGeometryType.esriGeometryPoint: shp = "Point"; break; case esriGeometryType.esriGeometryPolygon: shp = "Polygon"; break; case esriGeometryType.esriGeometryPolyline: shp = "Polyline"; break; case esriGeometryType.esriGeometryRay: shp = "Ray"; break; case esriGeometryType.esriGeometryRing: shp = "Ring"; break; case esriGeometryType.esriGeometrySphere: shp = "Sphere"; break; case esriGeometryType.esriGeometryTriangleFan: shp = "Triangle Fan"; break; default: System.out.println("shpType: " + featureClass.getShapeType()); shp = "unsupported"; break; } System.out.println("\tShapetype: " + shp); } catch (IOException ex) {throw ex;} } /** * List datasets in the workspace * * @param workspaceFactory * @param workspace * @param datasetName * @throws IOException */ private void listDatasets(WorkspaceFactory workspaceFactory, Workspace workspace, String datasetName) throws IOException { try { //Create FeatureDataset name object and Dataset name object FeatureDatasetName localDatasetName = new FeatureDatasetName(); //Set the workspace name reference to the workspace name localDatasetName.setWorkspaceNameByRef((WorkspaceName) workspace.getFullName()); localDatasetName.setName(datasetName); //Get feature class names in the dataset DatasetNames featureClassNames = (DatasetNames) localDatasetName.getFeatureClassNames(); IFeatureClassName featureClassName = null; IDatasetName anotherDatasetName = featureClassNames.next(); if (anotherDatasetName != null) { featureClassName = (IFeatureClassName) anotherDatasetName; } else { featureClassName = null; } IDatasetName datasetNameOnFeatureClassName = null; while (featureClassName != null) { datasetNameOnFeatureClassName = (IDatasetName)featureClassName; System.out.println("Feature Classes: " + datasetNameOnFeatureClassName.getName()); listFeatureClasses(workspaceFactory, workspace, datasetNameOnFeatureClassName.getName(), datasetName); anotherDatasetName = featureClassNames.next(); if (anotherDatasetName != null) { featureClassName = (IFeatureClassName)anotherDatasetName; } else { featureClassName = null; } } }catch (IOException ex) {throw ex;} } }