Pages

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(                         
                                                                                 string_ShapefileDirectory);
      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 =
                                                  (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
       // Explict Cast

        ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = 
                                                featureWorkspace.OpenFeatureClass(string_ShapefileName);
        return featureClass;
}
else
{
     //Not valid shapefile
      return null;
}
}
 else
{
    // Not valid directory
       return null;
}
}

No comments: