转自 在geodatabase中创建要素集
//在数据集中创建要素
private void menuItem25_Click(object sender, System.EventArgs e)
{
CreateFClassInPDB(@"C:ArcGISArcTutorBuildingaGeodatabaseMontgomery.mdb");
}
public static void CreateFClassInPDB(string filePath){
string shapeFieldName="shape";
IFeatureWorkspace pFWS;
try
{
pFWS=openPDB(filePath);
//IEnumDataset pDatasets;
//pDatasets=pWS.get_Datasets(esriDatasetType.esriDTFeatureDataset);
IFeatureDataset pFeatureDataset=pFWS.OpenFeatureDataset("Water");
//MessageBox.Show(pFeatureDataset.Name);
// IDataset pDataset=pDatasets.Next();
// IFeatureDataset pFeatureDataset=pDataset as IFeatureDataset;
IGeoDataset pGeoDataset=pFeatureDataset as IGeoDataset;
IFields pFields = null;
IFieldsEdit pFieldsEdit = null;
pFields = new FieldsClass();
pFieldsEdit=pFields as IFieldsEdit;
pFieldsEdit.FieldCount_2=2;
IField pField = null;
IFieldEdit pFieldEdit = null;
//Make the shape field it will need a geometry definition, with a spatial reference
pField=new FieldClass();
pFieldEdit=pField as IFieldEdit;
pFieldEdit.Name_2=shapeFieldName;
pFieldEdit.Type_2=esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeomDef = null;
IGeometryDefEdit pGeomDefEdit = null;
pGeomDef = new GeometryDefClass();
pGeomDefEdit =pGeomDef as IGeometryDefEdit;
pGeomDefEdit.GeometryType_2=esriGeometryType.esriGeometryPolygon;
pGeomDefEdit.SpatialReference_2=pGeoDataset.SpatialReference;//get the spatial reference
pFieldEdit.GeometryDef_2=pGeomDefEdit;
//pFieldsEdit.AddField(pField);
pFieldsEdit.set_Field(0,pField);
//Add another miscellaneous text field
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2=30;
pFieldEdit.Name_2="TextField";
pFieldEdit.Type_2=esriFieldType.esriFieldTypeString;
//pFieldsEdit.AddField(pField);
pFieldsEdit.set_Field(1,pField);
//pFeatureDataset.CreateFeatureClass("test",pFields,null,null,esriFeatureType.esriFTSimple,"Shape","");
UID pUID;
pUID = new UIDClass();
pUID.Value="esriGeoDatabase.Feature";
//pFeatureDataset.CreateFeatureClass("test",pFields,pUID,null,esriFeatureType.esriFTSimple,"Shape","");
pFeatureDataset.CreateFeatureClass("test",pFields,pUID,null,esriFeatureType.esriFTSimple,"Shape","");
MessageBox.Show("创建成功");
}
catch(Exception e){
MessageBox.Show(e.Message);
}
}
//创建shapefile文件
public static void createShapeFile(String folderName,String shapeName){
if(folderName==""||shapeName=="") return;
string shapeFieldName="shape";
try
{
IFeatureWorkspace pFWS = null;
IWorkspaceFactory pWorkspaceFactory = null;
pWorkspaceFactory = new Shapef
ileWorkspaceFactoryClass();
//if(pWorkspaceFactory.IsWorkspace(folderName)==false) return;
pFWS=pWorkspaceFactory.OpenFromFile(folderName,0) as IFeatureWorkspace;
IFields pFields = null;
IFieldsEdit pFieldsEdit = null;
pFields = new FieldsClass();
pFieldsEdit=pFields as IFieldsEdit;
IField pField = null;
IFieldEdit pFieldEdit = null;
//Make the shape field it will need a geometry definition, with a spatial reference
pField=new FieldClass();
pFieldEdit=pField as IFieldEdit;
pFieldEdit.Name_2=shapeFieldName;
pFieldEdit.Type_2=esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeomDef = null;
IGeometryDefEdit pGeomDefEdit = null;
pGeomDef = new GeometryDefClass();
pGeomDefEdit =pGeomDef as IGeometryDefEdit;
pGeomDefEdit.GeometryType_2=esriGeometryType.esriGeometryPolygon;
pGeomDefEdit.SpatialReference_2=new UnknownCoordinateSystemClass();
pFieldEdit.GeometryDef_2=pGeomDefEdit;
pFieldsEdit.AddField(pField);
//Add another miscellaneous text field
pField = new FieldClass();
pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2=30;
pFieldEdit.Name_2="TextField";
pFieldEdit.Type_2=esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
IFeatureClass pFeatClass = null;
pFeatClass = pFWS.CreateFeatureClass(shapeName, pFields, null, null,esriFeatureType.esriFTSimple, shapeFieldName, "");
MessageBox.Show("名为"+shapeName+"的shape文件创建成功");
}
catch(Exception e){
MessageBox.Show(e.Message);
}
}