// 创建shp (专三个参数,输出路径,shp文件名称,shp文件类型)
public static void CreateShpFeatureFiles( string shpfolder, string shpname, esriGeometryType pesriGeometryType)
{
IWorkspaceFactory pWorkSpaceFac = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkSpace = pWorkSpaceFac.OpenFromFile(shpfolder, 0) as IFeatureWorkspace;
如果 shpname已存在,提示是否删除
//IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(shpname);
//if (pFCChecker != null)
//{
// if (MessageBox.Show("此文件已经存在,确认删除?", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
// {
// IDataset pds = pFCChecker as IDataset;
// pds.Delete();
// }
// else
// {
// return;
// }
//}
//创建字段集2
IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;//创建必要字段
IFields fields = ocDescription.RequiredFields;
int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName);
IField field = fields.get_Field(shapeFieldIndex);
IGeometryDef geometryDef = field.GeometryDef;
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
//geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
//geometryDefEdit.SpatialReference_2 = spatialReference;
geometryDefEdit.GeometryType_2 = pesriGeometryType;
ISpatialReferenceFactory pSpatialRefFac = new SpatialReferenceEnvironmentClass();
IProjectedCoordinateSystem pcsSys = pSpatialRefFac.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_39);
//geometryDefEdit.SpatialReference_2 = pcsSys;
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null; //将传入字段 转成 validatedFields
fieldChecker.ValidateWorkspace = (IWorkspace)pFeatureWorkSpace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
pFeatureWorkSpace.CreateFeatureClass(shpname, validatedFields, ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");
}
private void buttonOK_Click(object sender, EventArgs e)
{
//创建一个新的shapfile工作空间
esriGeometryType pesriGeometryType;
switch(comboBoxType.SelectedIndex)
{
case 0:
pesriGeometryType = esriGeometryType.esriGeometryPoint;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 1:
pesriGeometryType = esriGeometryType.esriGeometryPolyline;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 2:
pesriGeometryType = esriGeometryType.esriGeometryPolygon;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 3:
pesriGeometryType = esriGeometryType.esriGeometryMultipoint;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 4:
pesriGeometryType = esriGeometryType.esriGeometryMultiPatch;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
}
}
public static void CreateShpFeatureFiles( string shpfolder, string shpname, esriGeometryType pesriGeometryType)
{
IWorkspaceFactory pWorkSpaceFac = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkSpace = pWorkSpaceFac.OpenFromFile(shpfolder, 0) as IFeatureWorkspace;
如果 shpname已存在,提示是否删除
//IFeatureClass pFCChecker = pFeatureWorkSpace.OpenFeatureClass(shpname);
//if (pFCChecker != null)
//{
// if (MessageBox.Show("此文件已经存在,确认删除?", "此删除不可恢复", MessageBoxButtons.YesNo) == DialogResult.Yes)
// {
// IDataset pds = pFCChecker as IDataset;
// pds.Delete();
// }
// else
// {
// return;
// }
//}
//创建字段集2
IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;//创建必要字段
IFields fields = ocDescription.RequiredFields;
int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName);
IField field = fields.get_Field(shapeFieldIndex);
IGeometryDef geometryDef = field.GeometryDef;
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
//geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
//geometryDefEdit.SpatialReference_2 = spatialReference;
geometryDefEdit.GeometryType_2 = pesriGeometryType;
ISpatialReferenceFactory pSpatialRefFac = new SpatialReferenceEnvironmentClass();
IProjectedCoordinateSystem pcsSys = pSpatialRefFac.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_Zone_39);
//geometryDefEdit.SpatialReference_2 = pcsSys;
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null; //将传入字段 转成 validatedFields
fieldChecker.ValidateWorkspace = (IWorkspace)pFeatureWorkSpace;
fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
pFeatureWorkSpace.CreateFeatureClass(shpname, validatedFields, ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");
}
private void buttonOK_Click(object sender, EventArgs e)
{
//创建一个新的shapfile工作空间
esriGeometryType pesriGeometryType;
switch(comboBoxType.SelectedIndex)
{
case 0:
pesriGeometryType = esriGeometryType.esriGeometryPoint;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 1:
pesriGeometryType = esriGeometryType.esriGeometryPolyline;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 2:
pesriGeometryType = esriGeometryType.esriGeometryPolygon;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 3:
pesriGeometryType = esriGeometryType.esriGeometryMultipoint;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
case 4:
pesriGeometryType = esriGeometryType.esriGeometryMultiPatch;
CreateShpFeatureFiles(OutShapepath, OutShapeName, pesriGeometryType);
break;
}
}