GDB中创建要素数据集

本文介绍了如何在geodatabase中创建要素集以及如何创建Shapefile文件。通过使用ArcGIS API,演示了创建要素集的具体步骤,包括定义字段、设置几何类型等。同时也展示了创建Shapefile的过程。
摘要由CSDN通过智能技术生成

转自 在geodatabase中创建要素集

 

//在数据集中创建要素

private void menuItem25_Click(object sender, System.EventArgs e)

{

CreateFClassInPDB(@"C:\ArcGIS\ArcTutor\BuildingaGeodatabase\Montgomery.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);

}

} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值