const string strFolder = @"E:\"; //目标文件夹
const string strName = "myshapefile"; //文件名
const string strShapeFieldName = "Shape";
//Open the folder to contain the shapefile as a workspace
IFeatureWorkspace pFWS;
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0) as IFeatureWorkspace;
//Set up a simple fields collection
IFields pFields = new FieldsClass(); //字段
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
//Make the shape field
//it will need a geometry definition, with a spatial reference
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = strShapeFieldName;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline; //此处可以是点、线、面
pGeometryDefEdit.SpatialReference_2 = this.axMapControl1.SpatialReference; //坐标系
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
//Add another field
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = 30;
pFieldEdit.Name_2 = "Name";
pFieldEdit.AliasName_2 = "AliasName";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
//Create the shapefile
IFeatureClass featureClass = pFWS.CreateFeatureClass(strName, pFields, null, null, esriFeatureType.esriFTSimpleJunction , "Shape", ""); //新建shp
IFeatureLayer featurelayer = new FeatureLayerClass();
featurelayer.Name = "myfeaturelayer";
featurelayer.FeatureClass = featureClass;
ILayer layer = featurelayer as ILayer;
备注:
1、IFeatureClass featureClass = pFWS.CreateFeatureClass(strName, pFields, null, null, esriFeatureType.esriFTSimpleJunction , "Shape", "");
运行这句话时要注意,如果目标文件夹下已经有相同shp文件,则会报错。将其删除即可,最好加判断是否重复语句。
2、esriFeatureType 接口有以下几个函数
esriFTSimple:简单要素
esriFTSimpleJunction:简单的接线功能
esriFTSimpleEdge:简单的边缘特征
esriFTComplexJunction:复杂的接线功能
esriFTComplexEdge:复杂的边缘特征
esriFTAnnotation:注释功能
esriFTCoverageAnnotation:覆盖注释功能
esriFTDimension:维特征
esriFTRasterCatalogItem:栅格目录项