IFeatureCursor cursor;
IFeatureBuffer featureBuffer;
IFeatureClass featureClass;
featureClass = operateShp.CreateShapefile(path, name, polygon.GeometryType);
cursor = featureClass.Insert(true);
featureBuffer = featureClass.CreateFeatureBuffer();
featureBuffer.Shape = polygon;
cursor.InsertFeature(featureBuffer);
/// <summary>
/// 创建Shapefile文件,返回该文件的IFeatureClass
/// </summary>
/// <param name="path"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public IFeatureClass CreateShapefile(string path, string fileName,esriGeometryType featureType)
{
string filePathName = path + fileName;
if (File.Exists(filePathName))
{
MessageBox.Show("文件已经存在!");
return null;
}
IFeatureWorkspace featureWorkspace;
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
IField field = new FieldClass();
IFieldEdit fieldEdit;
IFields fields = new FieldsClass();
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
IGeometryDef geometryDef = new GeometryDefClass();
IGeometryDefEdit geometryDefEdit;
featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(path, 0);
fieldEdit = (IFieldEdit)field;
fieldEdit.Name_2 = "shape";
fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
geometryDefEdit = (IGeometryDefEdit)geometryDef;
//switch (featureType)
//{
// case esriGeometryType.esriGeometryMultipoint:
// geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
// break;
// case esriGeometryType.esriGeometryPolyline:
// geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
// break;
// case esriGeometryType.esriGeometryPolyline:
// geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
// break;
//}
geometryDefEdit.GeometryType_2 = featureType;
geometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();
fieldEdit.GeometryDef_2 = geometryDef;
fieldsEdit.AddField(field);
return featureWorkspace.CreateFeatureClass(fileName, fields, null, null, esriFeatureType.esriFTSimple, "shape", "");
}