arcgissamples\geodatabase\DisplayFieldInfo.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.datasourcesfile.ShapefileWorkspaceFactory; import com.esri.arcgis.geodatabase.FeatureClass; import com.esri.arcgis.geodatabase.Field; import com.esri.arcgis.geodatabase.Fields; import com.esri.arcgis.geodatabase.GeometryDef; import com.esri.arcgis.geodatabase.Workspace; import com.esri.arcgis.geodatabase.esriFeatureType; import com.esri.arcgis.geodatabase.esriFieldType; 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; public class DisplayFieldInfo { public DisplayFieldInfo(){ } /** * Main Method - The console application entry point. * * @param args String[] Command line argument */ public static void main(String[] args) { System.out.println("Starting DisplayFieldInfo - 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 inPath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa"; String name = "states.shp"; DisplayFieldInfo displayFieldInfo = new DisplayFieldInfo(); displayFieldInfo.displayShapefileMetadata(inPath, name); 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();} } /** * Display shapefile metadata, including field metadata. * * @param shapefilePath Path to the shapefile * @param shapefileName Name of the shapefile */ private void displayShapefileMetadata(String shapefilePath, String shapefileName) { System.out.println("Shapefile: " + shapefileName); try { //Get and display the feature class' dataset name, alias name, and feature type description //for the shapefile. FeatureClass featureClass = getFeatureClass(shapefilePath, shapefileName); System.out.println("Feature Class Alias Name : " + featureClass.getAliasName()); System.out.println("Dataset Name : " + featureClass.getName()); System.out.println("Feature Type : " + getFeatureTypeDescription(featureClass.getFeatureType())); //Get the shape field String shapeFieldName = featureClass.getShapeFieldName(); Fields fields = (Fields) featureClass.getFields(); int lngIndex = fields.findField(shapeFieldName); Field field = (Field) fields.getField(lngIndex); //Get the geometry definition from the shape field. GeometryDef geomDef = (GeometryDef) field.getGeometryDef(); System.out.println("Geometry Type : " + getGeometryTypeDescription(geomDef.getGeometryType())); // Display the fields metadata. displayFieldsMetadata(featureClass); } catch (Exception e) { System.out.println("Exception in displayFieldInfo " + e.getMessage()); } } /** * Displays feature class fields one at a time. * * @param featureClass the feature class for which fields will be displayed. * @throws IOException if couldn't access field information */ private void displayFieldsMetadata(FeatureClass featureClass) throws IOException { try { //Get the fields collection from the feature class Fields fields = (Fields) featureClass.getFields(); //Get the number of fields int count = fields.getFieldCount(); System.out.println("Fields:"); //Loop through each field in the feature class for (int index = 0; index < count; index++) { //Get the field at the specified index Field field = (Field) fields.getField(index); if (field == null) { System.out.println("Field is null "); continue; } //Print out some metadata information System.out.println("\tPhysical Name : " + field.getName()); System.out.println("\tAlias Name : " + field.getAliasName()); System.out.println("\tType : " + getFieldTypeDescription(field.getType())); System.out.println("\tLength : " + field.getLength()); System.out.println("\tPrecision : " + field.getPrecision()); System.out.println("\tEditable : " + field.isEditable()); //Default Value Object defaultValue = field.getDefaultValue(); if (defaultValue == null) { System.out.println("\tDefault Value : <Unspecified>"); } else { System.out.println("\tDefault Value : " + defaultValue); } System.out.println(); } }catch (IOException e) { System.out.println("Couldn't access field information"); throw e; } } /** * Get feature type description. * * @param featureType The value representing the geometry type * @return String representing The description of the feature type. */ private String getFeatureTypeDescription(int featureType) { String description = "Unknown feature type"; switch (featureType) { case esriFeatureType.esriFTSimple: description = "Simple"; break; case esriFeatureType.esriFTSimpleJunction: description = "Simple Junction"; break; case esriFeatureType.esriFTSimpleEdge: description = "Simple Edge"; break; case esriFeatureType.esriFTComplexJunction: description = "Complex Junction"; break; case esriFeatureType.esriFTComplexEdge: description = "Complex Edge"; break; case esriFeatureType.esriFTAnnotation: description = "Annotation"; break; case esriFeatureType.esriFTCoverageAnnotation: description = "Coverage Annotation"; break; case esriFeatureType.esriFTDimension: description = "Dimension"; break; case esriFeatureType.esriFTRasterCatalogItem: description = "Raster Catalog Item"; break; } return description; } /** * Get geometry type description. * * @param geometryType The value representing the geometry type * @return String Representing the description of the geometry type. */ private String getGeometryTypeDescription(int geometryType) { String description = "Unknown geometry type"; switch (geometryType) { case esriGeometryType.esriGeometryPoint: description = "Point"; break; case esriGeometryType.esriGeometryMultipoint: description = "Multipoint"; break; case esriGeometryType.esriGeometryPolyline: description = "Polyline"; break; case esriGeometryType.esriGeometryPolygon: description = "Polygon"; break; } return description; } /** * Get field type description. * * @param geometryType The value representing the geometry type * @return String representing The description of the geometry type. */ private String getFieldTypeDescription(int fieldType) { String description = "Unknown field type"; switch (fieldType) { case esriFieldType.esriFieldTypeSmallInteger: description = "Small Integer"; break; case esriFieldType.esriFieldTypeInteger: description = "Integer"; break; case esriFieldType.esriFieldTypeSingle: description = "Single"; break; case esriFieldType.esriFieldTypeDouble: description = "Double"; break; case esriFieldType.esriFieldTypeString: description = "String"; break; case esriFieldType.esriFieldTypeDate: description = "Date"; break; case esriFieldType.esriFieldTypeOID: description = "Object Identifer"; break; case esriFieldType.esriFieldTypeGeometry: description = "Geometry"; break; case esriFieldType.esriFieldTypeBlob: description = "Blob Storage"; break; } return description; } /** * Get the feature class for the specified shapefile. * * @param path the path to the shapefile * @param name the name of the shapefile * @return IFeatureClass an object representing the feature class * @throws IOException if couldn't access the feature class. */ private FeatureClass getFeatureClass(String path, String name) throws IOException { FeatureClass featureClass = null; try { ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory(); Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(path, 0)); featureClass = new FeatureClass(workspace.openFeatureClass(name)); }catch (IOException e) { System.out.println("Couldn't access feature class :" + name + " in " + path); throw e; } return featureClass; } }