首页 > 代码库 > Arcgis Engine建立一个shp文件

Arcgis Engine建立一个shp文件

         这几天同事要求我在原来的项目的基础上添加一个生成shp文件的小功能,大体内容就是他给我提供文件夹路径名和图斑各个点的坐标(只是一个环),根据这些参数来生成一个shp文件。这个功能在刚来公司的时候实现过一次,但是后面一直没用就给忘记了(所以写的代码一定不要删呀,以后肯定用的着)。

         下面我贴一下建立shp文件的代码,欢迎大家指点。

   1:        private void CreateShpFile()
   2:          {
   3:              IWorkspaceFactory pWF = new ShapefileWorkspaceFactoryClass();
   4:              //这个例子将shp所在文件夹定死了,在项目中会根据参数来设置。
   5:              //当所选的文件夹不存在的时候程序会报错。
   6:              IFeatureWorkspace pFWs = pWF.OpenFromFile(@"E:\1", 0) as IFeatureWorkspace;
   7:   
   8:              IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
   9:              IObjectClassDescription ocDescription = fcDescription as IObjectClassDescription;
  10:   
  11:   
  12:   
  13:              IFields pFields = new ESRI.ArcGIS.Geodatabase.Fields();
  14:              IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
  15:              IField pField = new ESRI.ArcGIS.Geodatabase.Field();
  16:              IFieldEdit pFieldEdit = pField as IFieldEdit;
  17:   
  18:              //================这个部分是最重要的========================
  19:              //这段代码确定了这个shp文件的集合类型(polygon、polyline、point)
  20:              //这个字段的名称一定要设置,如果没有设置程序会出错。
  21:              //但是这个shp文件几何字段的名称并不是设置的这个。
  22:              IGeometryDef pGeometryDef = new GeometryDefClass();
  23:              IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
  24:              pGeometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
  25:              pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
  26:              pFieldEdit.GeometryDef_2 = pGeometryDef;
  27:              pFieldEdit.Name_2 = "shape";
  28:              pFieldsEdit.AddField(pField);
  29:              //============================================================
  30:   
  31:              //==============添加OID字段=========================
  32:              pField = new ESRI.ArcGIS.Geodatabase.Field();
  33:              pFieldEdit = pField as IFieldEdit;
  34:              pFieldEdit.Name_2 = "OID";
  35:              pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
  36:              pFieldsEdit.AddField(pField);
  37:   
  38:              //==============添加WaHa字段,没有实际意义,只是为了测试========
  39:              pField = new ESRI.ArcGIS.Geodatabase.Field();
  40:              pFieldEdit = pField as IFieldEdit;
  41:              pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
  42:              pFieldEdit.Name_2 = "WaHa";
  43:              pFieldEdit.Length_2 = 100;
  44:              pFieldsEdit.AddField(pField);
  45:   
  46:              //=============创建shp文件===================
  47:              IFeatureClass pFC = pFWs.CreateFeatureClass("123.shp", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
  48:              
  49:              //=============在shp文件中创建新记录=========
  50:              IFeature pFeature = pFC.CreateFeature();
  51:   
  52:   
  53:              //=========根据别人给的参数(这里是自己定死的)建立一个polygon字段值
  54:              object missing = Type.Missing;
  55:              IPointCollection pPc = new ESRI.ArcGIS.Geometry.Polygon();
  56:              IPoint point = new ESRI.ArcGIS.Geometry.Point();
  57:              point.X = 0;
  58:              point.Y = 0;
  59:              pPc.AddPoint(point, ref missing, ref missing);
  60:   
  61:              point = new ESRI.ArcGIS.Geometry.Point();
  62:              point.X = 10;
  63:              point.Y = 0;
  64:              pPc.AddPoint(point, ref missing, ref missing);
  65:   
  66:              point = new ESRI.ArcGIS.Geometry.Point();
  67:              point.X = 10;
  68:              point.Y = 10;
  69:              pPc.AddPoint(point, ref missing, ref missing);
  70:   
  71:              point = new ESRI.ArcGIS.Geometry.Point();
  72:              point.X = 0;
  73:              point.Y = 10;
  74:              pPc.AddPoint(point, ref missing, ref missing);
  75:   
  76:              point = new ESRI.ArcGIS.Geometry.Point();
  77:              point.X = 0;
  78:              point.Y = 0;
  79:              pPc.AddPoint(point, ref missing, ref missing);
  80:   
  81:              pFeature.Shape = pPc as IGeometry;
  82:              //对创建的这一行记录中的WaHa字段进行复制(值为abc)
  83:              pFeature.set_Value(pFeature.Fields.FindField("WaHa"), "abc");
  84:              //保存该记录
  85:              pFeature.Store();
  86:          }
<style></style>