Mar 7, 2012

How to get fields & assigned domains of FeatureClass + ESRI + C#

here is the code to get fields of a featureclass and check for the assigned domain 

IFields fields = featureClass.Fields;
string domainName = string.Empty;
for (int i = 0; i < fields.FieldCount; i++)
if (fields.get_Field(i).DomainFixed)
domainName = fields.get_Field(i).Domain.Name;


Mar 6, 2012

How to get feature class from the shape file on Disk.

Below is the method that gets the feature class from the shape file
/// Get the FeatureClass from a Shapefile on disk (hard drive).
/// A System.String that is the directory where the shapefile is located.
Example: "C:\data\USA"

/// A System.String that is the shapefile name.
 Note: the shapefile extension's
///(.shp, .shx, .dbf, etc.) is not provided! Example: "States"</param>

public ESRI.ArcGIS.Geodatabase.IFeatureClass GetFeatureClassFromShapefileOnDisk
(System.String string_ShapefileDirectory, System.String string_ShapefileName)
     System.IO.DirectoryInfo directoryInfo_check = new System.IO.DirectoryInfo(                         
      if (directoryInfo_check.Exists)
        //We have a valid directory, proceed
        System.IO.FileInfo fileInfo_check = new System.IO.FileInfo(string_ShapefileDirectory + "\\" +           
                                                                     string_ShapefileName + ".shp");
    if (fileInfo_check.Exists)
        //We have a valid shapefile, proceed

         ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory =
                                                 new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); 
         ESRI.ArcGIS.Geodatabase.IWorkspace workspace =      
                                                 workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0); 
         ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace =
       // Explict Cast

        ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = 
        return featureClass;
     //Not valid shapefile
      return null;
    // Not valid directory
       return null;

How to find feature count that intersect an envelope in Geodatabase + C#

In our previous post, we discussed about connecting the File GeoDB and returning the Workspace.

Now will see how to find feature count that intersect an envelope in the File GeoDB

// The method FileGdbWorkspaceFromPropertySet definition is present in our previous post

IWorkspace featureWorkspace = FileGdbWorkspaceFromPropertySet(gdbPath);
        IFeatureWorkspace fws = (IFeatureWorkspace)featureWorkspace;

// Method that Gets Features Count From a FeatureClass

private static int GetCountFromFeatureClass(IFeatureWorkspace fws, string parcelName, IEnvelope envelope)
        // Open the feature classes used by the queries.
        IFeatureClass parcelsFeatureClass = fws.OpenFeatureClass(parcelName);      

        //// Create the spatial filter. Note that the SubFields property specifies that only
        //// the Shape field is retrieved, since the features' attributes aren't being inspected.
        ISpatialFilter spatialFilter = new SpatialFilterClass();
        spatialFilter.Geometry = envelope;
        spatialFilter.GeometryField = parcelsFeatureClass.ShapeFieldName;
        spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;
        spatialFilter.SubFields = "Shape";

        // Use IFeatureClass.FeatureCount to get a parcel count.
        return parcelsFeatureClass.FeatureCount(spatialFilter);