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.