Read Shape Files Using ArcObject

In GIS, Shapefile is an intermediate file between GIS software programs. If we have micro stations or AutoCAD, we can export the feature drawings in shapefile format. Once we export the drawing file in shapefile format other than ArcGIS software, we want to read that shapefile from ArcGIS programmatically. For that, we need to write some lines of code in ArcObject.
 
There are some interfaces available in ArcObject using that we can read those shapefiles and perform the desired operation once we get the object of shapefiles. Using ArcObject we can read shapefiles and its attributes. For example, we have some shapefiles. We want to read shapefile name and Geometry type in order to display the Name & Geometry type in the DataGrid View.The below code will read shapefile name and shape type to display in DataGrid.
 
Apart from name and shape type, we can read all properties of shapefiles which we can see in ArcCatalog. Once we get the object of shapefiles, we can read each individual feature spatial as well as non-spatial information also.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using ESRI.ArcGIS.DataSourcesFile;  
  9. using ESRI.ArcGIS.DataSourcesGDB;  
  10. using ESRI.ArcGIS.Geodatabase;  
  11. using ESRI.ArcGIS.esriSystem;  
  12. using ESRI.ArcGIS.Geometry;  
  13. using System.Collections;  
  14. namespace WindowsApp {  
  15.     public partial class Form1: Form {  
  16.         public Form1() {  
  17.             InitializeComponent();  
  18.         }  
  19.         private void button1_Click(object sender, EventArgs e) {  
  20.             IWorkspaceFactory pwsf = new ShapefileWorkspaceFactory();  
  21.             IWorkspace pws = pwsf.OpenFromFile(@ "D:\YS\ShapeFiles", 0);  
  22.             IEnumDataset pEnumDs = pws.Datasets[esriDatasetType.esriDTAny];  
  23.             IDataset pds = pEnumDs.Next();  
  24.             ArrayList lst = new ArrayList();  
  25.             ShapeDetail shap = null;  
  26.             while (pds != null) {  
  27.                 string name = pds.Name;  
  28.                 string shapeGeomType = "Unknown";  
  29.                 IFeatureClass pfc = (IFeatureClass) pds;  
  30.                 esriGeometryType shp = pfc.ShapeType;  
  31.                 if (shp == esriGeometryType.esriGeometryLine || shp == esriGeometryType.esriGeometryLine) {  
  32.                     shapeGeomType = "Polyline";  
  33.                 }  
  34.                 if (shp == esriGeometryType.esriGeometryMultipoint || shp == esriGeometryType.esriGeometryPoint) {  
  35.                     shapeGeomType = "Point";  
  36.                 }  
  37.                 if (shp == esriGeometryType.esriGeometryPolygon) {  
  38.                     shapeGeomType = "Polygon";  
  39.                 }  
  40.                 int cntFeat = pfc.FeatureCount(null);  
  41.                 shap = new ShapeDetail(name, shapeGeomType, cntFeat);  
  42.                 lst.Add(shap);  
  43.                 pds = pEnumDs.Next();  
  44.             }  
  45.             dataGridView1.DataSource = lst;  
  46.         }  
  47.     }  
  48.     public class ShapeDetail {  
  49.         public string ShapeName {  
  50.             get;  
  51.             set;  
  52.         }  
  53.         public string Shapetype {  
  54.             get;  
  55.             set;  
  56.         }  
  57.         public int featureCount {  
  58.             get;  
  59.             set;  
  60.         }  
  61.         public ShapeDetail(string name, string typeShp, int cnt) {  
  62.             ShapeName = name;  
  63.             Shapetype = typeShp;  
  64.             featureCount = cnt;  
  65.         }  
  66.     }  
  67. }  
Next Recommended Reading Get Feature Class Name Using ArcObject