新建shape文件——创建FeatureClass存储为shape文件
1 /// <summary>创建点shapefile 2 /// </summary> 3 /// <param name="filePath">target point shapefile path</param> 4 /// <param name="fileName">target point shapefile name</param> 5 public static void createPointShapefile(IMap map, string filePath, string fileName) 6 { 7 //建立shape字段 8 IFields pFields = new FieldsClass(); 9 IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; 10 IField pField = new FieldClass(); 11 IFieldEdit pFieldEdit = pField as IFieldEdit; 12 pFieldEdit.Name_2 = "Shape"; 13 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; 14 15 //设置geometry definition 16 IGeometryDef pGeometryDef = new GeometryDefClass(); 17 IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit; 18 pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;//点、线、面 19 pGeometryDefEdit.SpatialReference_2 = map.SpatialReference; 20 pFieldEdit.GeometryDef_2 = pGeometryDef; 21 pFieldsEdit.AddField(pField); 22 23 //新建字段 24 pField = new FieldClass(); 25 pFieldEdit = pField as IFieldEdit; 26 pFieldEdit.Length_2 = 10; 27 pFieldEdit.Name_2 = "id"; 28 pFieldEdit.AliasName_2 = "id"; 29 pFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; 30 pFieldsEdit.AddField(pField); 31 //继续增加其它字段 32 33 IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); 34 IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace; 35 36 //IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass(); 37 //IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace; 38 39 int i = fileName.IndexOf(".shp"); 40 if (i == -1) 41 pFeatureWorkspace.CreateFeatureClass(fileName + ".shp", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); 42 else 43 pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); 44 45 //MessageBox.Show("OK"); 46 47 }