Sunday, February 22, 2015

Open Shape File Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Open Shape File




 //Open Shape file  
      public void openShapefile(String workspacePath, String shapefileName) {  
           /*  
            * In this code, the file extension, .shp is not used when specifying  
            * the name of the shapefile to be opened.  
            */  
           try {  
                ShapefileWorkspaceFactory wsf = new ShapefileWorkspaceFactory();  
                Workspace work = new Workspace(wsf.openFromFile(workspacePath, 0));  
                IFeatureClass featureClass = work.openFeatureClass(shapefileName);  
                FeatureLayer layer = new FeatureLayer();  
                layer.setFeatureClassByRef(featureClass);  
                layer.setName(featureClass.getAliasName());  
                mxDoc.getFocusMap().addLayer(layer);  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  


This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Open Feature Class Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Print Field Information of Feature Class


 // Opens feature Class from database  
      public void openfeatureclass(String workspacePath, String fcName){  
        try{  
                mxDoc = (IMxDocument) app.getDocument();  
                FileGDBWorkspaceFactory wsf = new FileGDBWorkspaceFactory();  
                Workspace work = new Workspace(wsf.openFromFile(workspacePath, 0));  
          IFeatureClass featureClass = work.openFeatureClass(fcName);  
          FeatureLayer layer = new FeatureLayer();  
          layer.setFeatureClassByRef(featureClass);  
          layer.setName(featureClass.getAliasName());  
          mxDoc.getFocusMap().addLayer(layer);  
        }  
        catch (Exception e){  
          e.printStackTrace();  
        }  
      }  


This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Print Field Information of Feature Class Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Print Field Information of Feature Class


 static void displayFields(IFeatureClass featureClass) throws Exception {  
           IFields fields = featureClass.getFields();  
           IField field;  
           for (int i = 0; i < fields.getFieldCount(); i++) {  
                field = fields.getField(i);  
                displayFieldInformation(field);  
           }  
      }  
      static void displayFieldInformation(IField field) throws Exception {  
           // Physical name.  
           System.out.println("Name : " + field.getName());  
           // Alias name.  
           System.out.println("Alias Name : " + field.getAliasName());  
           // Field type.  
           String value = "";  
           switch (field.getType()) {  
           case esriFieldType.esriFieldTypeSmallInteger:  
                value = "Small Integer";  
                break;  
           case esriFieldType.esriFieldTypeInteger:  
                value = "Long Integer";  
                break;  
           case esriFieldType.esriFieldTypeSingle:  
                value = "Single-precision floating-point number";  
                break;  
           case esriFieldType.esriFieldTypeDouble:  
                value = "Double-precision floating-point number";  
                break;  
           case esriFieldType.esriFieldTypeString:  
                value = "Character string";  
                break;  
           case esriFieldType.esriFieldTypeDate:  
                value = "Date";  
                break;  
           case esriFieldType.esriFieldTypeOID:  
                value = "Long Integer representing an object identifier";  
                break;  
           case esriFieldType.esriFieldTypeGeometry:  
                value = "Geometry";  
                break;  
           case esriFieldType.esriFieldTypeBlob:  
                value = "Binary Large Object, Blob Storage";  
                break;  
           case esriFieldType.esriFieldTypeRaster:  
                value = "Raster";  
                break;  
           case esriFieldType.esriFieldTypeGUID:  
                value = "Globally Unique Identifier";  
                break;  
           case esriFieldType.esriFieldTypeGlobalID:  
                value = "ESRI Global ID";  
                break;  
           case esriFieldType.esriFieldTypeXML:  
                value = "XML Document";  
                break;  
           }  
           System.out.println("Type : " + value);  
           // Field length—this is only valid for fields of Type  
           // esriFieldTypeString.  
           System.out.println("Length : " + field.getLength());  
           // Field precision.  
           System.out.println("Precision : " + field.getPrecision());  
           // Field scale.  
           System.out.println("Scale : " + field.getScale());  
           // Editable.  
           System.out.println("Editable : " + field.isEditable());  
           // Default value.  
           System.out.println("Default Value : " + field.getDefaultValue());  
           IGeometryDef geomDef = field.getGeometryDef();  
           if (geomDef != null) {  
                System.out.println("GeometryDef Properties");  
                System.out.println("AvgNumPoints : " + geomDef.getAvgNumPoints());  
                System.out.println("Grid Count : " + geomDef.getGridCount());  
                for (int i = 0; i < geomDef.getGridCount(); i++) {  
                     System.out.println("Grid Size[" + i + "]="  
                               + geomDef.getGridSize(i));  
                }  
                System.out.println("Has Measures : " + geomDef.isHasM());  
                System.out.println("Has Z : " + geomDef.isHasZ());  
                System.out.println("Spatial Ref : "  
                          + geomDef.getSpatialReference().getName());  
           }  
           System.out.println("");  
      }  


This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Print values of attributes in Feature Class Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Print values of attributes in Feature Class


      //Access values of attributes in fields  
      static void printDistinctFieldValueNames(IFeatureClass featureClass, String Condition)  
                throws Exception {  
           QueryFilter queryFilter = new QueryFilter();  
           #queryFilter.setWhereClause("Name='Ahmed'");  
           queryFilter.setWhereClause(Condition);  
           IFeatureCursor featureCursor = featureClass.search(queryFilter, true);  
           IFields fields = featureCursor.getFields();  
           int fieldCount = fields.getFieldCount();  
           for (int i = 0; i < fieldCount; i++) {  
                IField fieldI = fields.getField(i);  
                String fieldName = fieldI.getName();  
                System.out.print(fieldName + "\t");  
           }  
           System.out.println();  
           IFeature feature = featureCursor.nextFeature();  
           while (feature != null) {  
                StringBuffer row = new StringBuffer();  
                for (int i = 0; i < fieldCount; i++) {  
                     int fieldType = feature.getFields().getField(i).getType();  
                     switch (fieldType) {  
                     case esriFieldType.esriFieldTypeDate:  
                     case esriFieldType.esriFieldTypeDouble:  
                     case esriFieldType.esriFieldTypeGlobalID:  
                     case esriFieldType.esriFieldTypeGUID:  
                     case esriFieldType.esriFieldTypeInteger:  
                     case esriFieldType.esriFieldTypeOID:  
                     case esriFieldType.esriFieldTypeSingle:  
                     case esriFieldType.esriFieldTypeSmallInteger:  
                     case esriFieldType.esriFieldTypeString:  
                          row.append(feature.getValue(i) + "\t");  
                          break;  
                     case esriFieldType.esriFieldTypeBlob:  
                          row.append("(blob)" + "\t");  
                          break;  
                     case esriFieldType.esriFieldTypeGeometry:  
                          row.append("(geometry)" + "\t");  
                          break;  
                     case esriFieldType.esriFieldTypeRaster:  
                          row.append("(raster)" + "\t");  
                          break;  
                     }  
                }  
                if (row.length() > 0) {  
                     System.out.println(row);  
                }  
                feature = featureCursor.nextFeature();  
           }  
      }  


This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Print values of selected features in Feature Class Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Print values of selected features in Feature Class


 // display values of selected features in shape file (file system)  
      static void getselectedfeaturesvalueShp(IApplication app) {  
           IMxDocument mxDoc;  
           try {  
                mxDoc = (IMxDocument) app.getDocument();  
                int count = mxDoc.getActiveView().getFocusMap().getLayerCount();  
                for (int layerno = 0; layerno < count; layerno++) {  
                     if (mxDoc.getActiveView().getFocusMap().getLayer(layerno).isVisible()) {  
                          System.out.println("Layer " + layerno + " visible");  
                     }  
                }  
                Map map = (Map) mxDoc.getMaps().getItem(0);  
                MapSelection mapSelection = new MapSelection(map.getFeatureSelection());  
                mapSelection.reset();  
                IFeature feature = mapSelection.next();  
                while (feature != null) {  
                     int oid = (int) feature.getOID();  
                     System.out.println("OID of Selected feature = " + oid);  
                     int fieldcount = feature.getTable().getRow(oid).getFields().getFieldCount();  
                     System.out.println(" fieldcount = " + fieldcount);  
                     for (int q=0; q<fieldcount; q++){  
                          System.out.println("Field Name = "+ feature.getTable().getFields().getField(q).getName());  
                          System.out.println("Field Value = "+ feature.getTable().getRow(oid).getValue(q));  
                     }  
                     feature = mapSelection.next();       
                     System.out.println("***");                      
                }  
           } catch (AutomationException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  



This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Print Field Names and Field Properties of Feature Class Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Print Field Names of Shapefile, Feature Class


 // display field properties of each field in detail  
 static void displayFieldProperties(IFeatureClass featureClass)  
           throws Exception {  
      // Get the fields collection for the feature class.  
      IFields fields = featureClass.getFields();  
      IField field = null;  
      for (int i = 0; i < fields.getFieldCount(); i++) {  
           field = fields.getField(i);  
           // Get the standard field properties.  
           System.out.println("Name: " + field.getName());  
           System.out.println("Alias: " + field.getAliasName());  
           System.out.println("Type: " + field.getType());  
           System.out.println("VarType: " + field.getVarType());  
           System.out.println("Default Value: " + field.getDefaultValue());  
           System.out.println("Length: " + field.getLength());  
           System.out.println("Precision: " + field.getPrecision());  
           System.out.println("Scale: " + field.getScale());  
           System.out.println("Is Editable: " + field.isEditable());  
           System.out.println("Is Nullable: " + field.isNullable());  
           System.out.println("Is Required: " + field.isRequired());  
           // Check if the field is the shape field.  
           if (field.getType() == esriFieldType.esriFieldTypeGeometry) {  
                IGeometryDef geometryDef = field.getGeometryDef();  
                System.out.println("Geometry Type: "  
                          + geometryDef.getGeometryType());  
                System.out.println("Has M Values: " + geometryDef.isHasM());  
                System.out.println("Has Z Values: " + geometryDef.isHasZ());  
           }  
           // Insert an empty line for readability.  
           System.out.println();  
      }  
 }  





This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Print Field Names of Shapefile Feature Class Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Print Field Names of Shapefile, Feature Class

 // printDistinctFieldAliasNames work with shape file  
 static void printDistinctFieldAliasNames(IFeatureClass featureClass)  
           throws Exception {  
      // get the Fields collection from the feature class  
      IFields fields = featureClass.getFields();  
      IField field;  
      // on a zero based index iterate through the fields in the collection  
      for (int i = 0; i < fields.getFieldCount(); i++) {  
           // get the field at the given index  
           field = fields.getField(i);  
           System.out.println("field.getName() = " + field.getName());  
           if (!field.getName().equals(field.getAliasName())) {  
                System.out  
                          .println(field.getName() + ":" + field.getAliasName());  
           }  
      }  
      IFeatureCursor featureCursor = featureClass.search(null, true);  
      IFields fields1 = featureCursor.getFields();  
      System.out.println("fields1 = " + fields1);  
      int fieldCount = fields1.getFieldCount();  
      System.out.println("fieldCount = " + fieldCount);  
      for (int i = 0; i < fieldCount; i++) {  
           IField fieldI = fields1.getField(i);  
           String fieldName = fieldI.getName();  
           System.out.print(fieldName + "\t");  
      }  
      System.out.println();  
 }  



This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Load Pre Defined Projections Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Load Pre-Defined Projections


 // print pre defined projections in arcobjects  
 static void printPreDefinedProjections() throws Exception {  
      ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironment();  
      ISet projectionSet = spatialReferenceFactory  
                .createPredefinedProjections();  
      System.out.println("Number of predefined Projections = "  
                + projectionSet.getCount());  
      projectionSet.reset();  
      for (int i = 0; i < projectionSet.getCount(); i++) {  
           IProjection projection = (IProjection) projectionSet.next();  
           System.out.println(projection.getName());  
      }  
 }  





This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Load Projected Coordinate System from file Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Load Geographic Coordinate System from file



 // load Projected projected system from file  
 static IProjectedCoordinateSystem loadProjectedCoordinateSystem()  
           throws Exception {  
      ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironment();  
      IProjectedCoordinateSystem projectedCoordinateSystem = (IProjectedCoordinateSystem) spatialReferenceFactory  
                .createESRISpatialReferenceFromPRJFile("D:/work/WGS1984UTMZone43N.prj");  
      return projectedCoordinateSystem;  
 }  




This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Load Geographic Coordinate System from file Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Load Geographic Coordinate System from file


 // load Geographic cordinate system from file  
 static IGeographicCoordinateSystem loadGeographicCoordinateSystem()  
           throws Exception {  
      ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironment();  
      IGeographicCoordinateSystem geographicCoordinateSystem = (IGeographicCoordinateSystem) spatialReferenceFactory  
                .createESRISpatialReferenceFromPRJFile("D:/work/WGS1984.prj");  
      return geographicCoordinateSystem;  
 }  





This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Create Fields Collection Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Create Fields Collection



 // create field collection for table  
 public IFields createFieldsCollectionForTable() throws Exception {  
      // Create a new fields collection.  
      IFields fields = new Fields();  
      // Cast for IFieldsEdit to modify the properties of the fields  
      // collection.  
      IFieldsEdit fieldsEdit = (IFieldsEdit) fields;  
      // Set the FieldCount to the total number of fields to be created.  
      fieldsEdit.setFieldCount(2);  
      // Create the ObjectID field.  
      IField fieldUserDefined = new Field();  
      // Cast for IFieldEdit to modify the properties of the new field.  
      IFieldEdit fieldEdit = (IFieldEdit) fieldUserDefined;  
      fieldEdit.setAliasName("FID");  
      fieldEdit.setName("ObjectID");  
      fieldEdit.setType(esriFieldType.esriFieldTypeOID);  
      // Add the new field to the fields collection.  
      fieldsEdit.setFieldByRef(0, fieldUserDefined);  
      // Create the text field.  
      fieldUserDefined = new Field();  
      fieldEdit = (IFieldEdit) fieldUserDefined;  
      fieldEdit.setLength(30); // Only string fields require that you set the  
                                         // length.  
      fieldEdit.setName("Owner");  
      fieldEdit.setType(esriFieldType.esriFieldTypeString);  
      // Add the new field to the fields collection.  
      fieldsEdit.setFieldByRef(1, fieldUserDefined);  
      return fields;  
 }  




This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Saturday, February 21, 2015

Create Feature Class Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Create Feature Class


 // Create Feature Class  
 static IFeatureClass createFeatureClass(IFeatureDataset featureDataset,  
           String nameOfFeatureClass) throws Exception {  
      // This function creates a new feature class in a supplied feature  
      // dataset by creating all fields from scratch. IFeatureClassDescription (or  
      // IObjectClassDescription if the table is  
      // created at the workspace level) can be used to get the required  
      // fields and are used to  
      // get the InstanceClassID and ExtensionClassID.  
      // Create new fields collection with the number of fields you plan to  
      // add. Must add at least two fields  
      // for a feature class: Object ID and Shape field.  
      IFields fields = new Fields();  
      IFieldsEdit fieldsEdit = (IFieldsEdit) fields;  
      fieldsEdit.setFieldCount(3);  
      // Create Object ID field.  
      IField fieldUserDefined = new Field();  
      IFieldEdit fieldEdit = (IFieldEdit) fieldUserDefined;  
      fieldEdit.setName("OBJECTID");  
      fieldEdit.setAliasName("OBJECT ID");  
      fieldEdit.setType(esriFieldType.esriFieldTypeOID);  
      fieldsEdit.setFieldByRef(0, fieldUserDefined);  
      // Create Shape field.  
      fieldUserDefined = new Field();  
      fieldEdit = (IFieldEdit) fieldUserDefined;  
      // Set up geometry definition for the Shape field.  
      // You do not have to set the spatial reference, as it is inherited from  
      // the feature dataset.  
      IGeometryDef geometryDef = new GeometryDef();  
      IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit) geometryDef;  
      geometryDefEdit.setGeometryType(esriGeometryType.esriGeometryPolyline);  
      // By setting the grid size to 0, you are allowing ArcGIS to determine  
      // the appropriate grid sizes for the feature class.  
      // If in a personal geodatabase, the grid size is 1,000. If in a file or  
      // ArcSDE geodatabase, the grid size  
      // is based on the initial loading or inserting of features.  
      geometryDefEdit.setGridCount(1);  
      geometryDefEdit.setGridSize(0, 0);  
      geometryDefEdit.setHasM(false);  
      geometryDefEdit.setHasZ(false);  
      // Set standard field properties.  
      fieldEdit.setName("SHAPE");  
      fieldEdit.setType(esriFieldType.esriFieldTypeGeometry);  
      fieldEdit.setGeometryDefByRef(geometryDef);  
      fieldEdit.setIsNullable(true);  
      fieldEdit.setRequired(true);  
      fieldsEdit.setFieldByRef(1, fieldUserDefined);  
      // Create a field of type double to hold some information for the  
      // features.  
      fieldUserDefined = new Field();  
      fieldEdit = (IFieldEdit) fieldUserDefined;  
      fieldEdit.setName("Avg_income");  
      fieldEdit.setAliasName("Average income for 1999-2000");  
      fieldEdit.setEditable(true);  
      fieldEdit.setIsNullable(false);  
      fieldEdit.setPrecision(2);  
      fieldEdit.setScale(5);  
      fieldEdit.setType(esriFieldType.esriFieldTypeDouble);  
      fieldsEdit.setFieldByRef(2, fieldUserDefined);  
      // Create a feature class description object to use for specifying the  
      // CLSID and EXTCLSID.  
      IFeatureClassDescription fcDesc = new FeatureClassDescription();  
      IObjectClassDescription ocDesc = (IObjectClassDescription) fcDesc;  
      return featureDataset.createFeatureClass(nameOfFeatureClass, fields,  
                ocDesc.getInstanceCLSID(), ocDesc.getClassExtensionCLSID(),  
                esriFeatureType.esriFTSimple, "SHAPE", "");  
 }  



This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Create Feature Data Set Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Create Feature Dataset



 // Create feature dataset  
 static IFeatureDataset createFeatureDataset(IWorkspace workspace,  
           String fdsName, ISpatialReference fdsSR) throws Exception {  
      IFeatureWorkspace featureWorkspace = new IFeatureWorkspaceProxy(  
                workspace);  
      return featureWorkspace.createFeatureDataset(fdsName, fdsSR);  
 }  




This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Create Workspace Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Create Workspace


 // Create Workspace  
 IWorkspace openFromString_fGDB_Workspace(String connectionString)  
           throws Exception {  
      IWorkspaceFactory2 workspaceFactory = new FileGDBWorkspaceFactory();  
      return workspaceFactory.openFromString(connectionString, 0);  
 }  


This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.

Create Database Esri ArcObjects Snippets - Series

Esri ArcObjects Snippets - Series

Create Database



 // Create Database
 public void createdatabase() {  
      String file = "D:\\work\\PAKdatabase";  
      // ***Create Database  
      try {  
           // Create a FileGDB workspace factory  
           IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();  
           // Create a new File geodatabase  
           IWorkspaceName workspaceName = workspaceFactory  
                     .create("D:\\work\\", "MyFGDB.gdb", null, 0);  
           System.out.println("workspaceName = " + workspaceName.toString());  
           // Cast for IName  
           IName name = (IName) workspaceName;  
           System.out.println("name = " + name.toString());  
           // Open a reference to the access workspace through the name object  
           IWorkspace workspace = new IWorkspaceProxy(name.open());  
      } catch (Exception e) {  
           e.printStackTrace();  
      }  
 }  

This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.