将shp文件导入到GeoDatabase中
(C#语言)
private void ImportFeatureClassToNewWorkSpace(IFeatureDataset apFD)
{
//打开SHP文件
string StrFilter = "SHP文件(.shp) | *.shp";
string ImportShapeFileName = OpenFile(StrFilter);
if (ImportShapeFileName == "") { return; }
string ImportFileShortName = System.IO.Path.GetFileNameWithoutExtension(ImportShapeFileName);
string ImportFilePath = System.IO.Path.GetDirectoryName(ImportShapeFileName);
IWorkspaceName pInWorkspaceName;
IFeatureDatasetName pOutFeatureDSName;
IFeatureClassName pInFeatureClassName;
IDatasetName pInDatasetName;
IFeatureClassName pOutFeatureClassName;
IDatasetName pOutDatasetName;
long iCounter;
IFields pOutFields, pInFields;
IFieldChecker pFieldChecker;
IField pGeoField;
IGeometryDef pOutGeometryDef;
IGeometryDefEdit pOutGeometryDefEdit;
IName pName;
IFeatureClass pInFeatureClass;
IFeatureDataConverter pShpToClsConverter;
IEnumFieldError pEnumFieldError = null;
//得到一个输入SHP文件的工作空间,
pInWorkspaceName = new WorkspaceNameClass();
pInWorkspaceName.PathName = ImportFilePath;
pInWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1";
//创建一个新的要素类名称,目的是为了以来PNAME接口的OPEN方法打开SHP文件
pInFeatureClassName = new FeatureClassNameClass();
pInDatasetName = (IDatasetName)pInFeatureClassName;
pInDatasetName.Name = ImportFileShortName;
pInDatasetName.WorkspaceName = pInWorkspaceName;
//打开一个SHP文件,将要读取它的字段集合
pName = (IName)pInFeatureClassName;
pInFeatureClass = (IFeatureClass)pName.Open();
//通过FIELDCHECKER检查字段的合法性,为输入要素类获得字段集合
pInFields = pInFeatureClass.Fields;
pFieldChecker = new FieldChecker();
pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
//通过循环查找几何字段
pGeoField = null;
for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
{
if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
{
pGeoField = pOutFields.get_Field((int)iCounter);
break;
}
}
//得到几何字段的几何定义
pOutGeometryDef = pGeoField.GeometryDef;
//设置几何字段的空间参考和网格
pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;
pOutGeometryDefEdit.GridCount_2 = 1;
pOutGeometryDefEdit.set_GridSize(0, 1500000);
//创建一个新的要素类名称作为可用的参数
pOutFeatureClassName = new FeatureClassNameClass();
pOutDatasetName = (IDatasetName)pOutFeatureClassName;
pOutDatasetName.Name = pInDatasetName.Name;
//创建一个新的数据集名称作为可用的参数
pOutFeatureDSName = (IFeatureDatasetName)new FeatureDatasetName();
//因为ConvertFeatureClass需要传入一个IFeatureDatasetName的参数,通过它确定导入生成的要素类的工作空间和要素集合
//情况一
//如果本函数的参数(IFeatureDataset)是一个确切的值,那么将它转换成IFeatureDatasetName接口就可以了。因为ConvertFeatureClass根据该接口就
//可以确定工作空间和要素集合,IFeatureClassName就可以不考虑上述问题
//情况二
//如果本函数的参数(IFeatureDataset)是一个NULL值,表示要创建独立要素类,
//那么ConvertFeatureClass函数无法根据IFeatureDatasetName参数确定工作空间和要素集合
//这个时候需要IFeatureClassName参数确定工作空间和要素集合
//如果参数的值是NULL,说明要创建独立要素类
if (apFD == null)
{
//创建一个不存在的要素集合pFDN,通过它将IFeatureClassName和工作空间连接起来,而ConvertFeatureClass函数并不使用该变量作为参数,
IFeatureDatasetName pFDN = new FeatureDatasetNameClass();
IDatasetName pDN = (IDatasetName)pFDN;
IDataset pDS = (IDataset)pWorkspace;
pDN.WorkspaceName = (IWorkspaceName)pDS.FullName;
pDN.Name = pDS.Name;
pOutFeatureClassName.FeatureDatasetName = (IDatasetName)pFDN;
//将pOutFeatureDSName设置为Null,将它做为参数给ConvertFeatureClass函数,因为IFeatureClassName本身已经和工作空间关联了,生成的
//要素类在工作空间的根目录下,即独立要素类
pOutFeatureDSName = null;
}
else//创建的要素类是在给定的参数(要素集合)下
{
pOutFeatureDSName = (IFeatureDatasetName)apFD.FullName;
}
//开始导入
pShpToClsConverter = new FeatureDataConverterClass();
pShpToClsConverter.ConvertFeatureClass(pInFeatureClassName, null, pOutFeatureDSName, pOutFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
MessageBox.Show("导入成功","系统提示");
}