shp sde mdb 之间的转换
转自: http://www.gisall.com/html/63/34963-2009.html
调用用下面的方法可以将要素类从sde导出到shp或者mdb 或者从三个中的任意一个导出到另外一个。方法是:
/// <summary>
/// 将一个要素类从一个工作空间转移到另外一个工作空间
/// 注意目标工作空间不能有改要素类,必须先清除
/// </summary>
/// <param name=”sourceWorkspace”>源工作空间</param>
/// <param name=”targetWorkspace”>目标工作空间</param>
/// <param name=”nameOfSourceFeatureClass”>源要素类名</param>
/// <param name=”nameOfTargetFeatureClass”>目标要素类名</param>
public void IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
{
//create source workspace name
IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
//create source dataset name
IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
sourceDatasetName.WorkspaceName = sourceWorkspaceName;
sourceDatasetName.Name = nameOfSourceFeatureClass;
//create target workspace name
IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
//create target dataset name
IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
targetDatasetName.WorkspaceName = targetWorkspaceName;
targetDatasetName.Name = nameOfTargetFeatureClass;
//Open input Featureclass to get field definitions.
ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();
//Validate the field names because you are converting between different workspace types.
IFieldChecker fieldChecker = new FieldCheckerClass();
IFields targetFeatureClassFields;
IFields sourceFeatureClassFields = sourceFeatureClass.Fields;
IEnumFieldError enumFieldError;
// Most importantly set the input and validate workspaces!
fieldChecker.InputWorkspace = sourceWorkspace;
fieldChecker.ValidateWorkspace = targetWorkspace;
fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);
// Loop through the output fields to find the geomerty field
IField geometryField;
for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
{
if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
{
geometryField = targetFeatureClassFields.get_Field(i);
// Get the geometry field’s geometry defenition
IGeometryDef geometryDef = geometryField.GeometryDef;
//Give the geometry definition a spatial index grid count and grid size
IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
targetFCGeoDefEdit.GridCount_2 = 1;
targetFCGeoDefEdit.set_GridSize(0, 0);
//Allow ArcGIS to determine a valid grid size for the data loaded
targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
// we want to convert all of the features
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = “”;
// Load the feature class
IFeatureDataConverter fctofc = new FeatureDataConverterClass();
IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, “”, 1000, 0);
break;
}
}
}
只要设定了工作空间,不管工作空间是文件的 数据库的 还是什么的,可以自由转换。
比如我要将sde的要素类导出成shp文件,如下:
public void Export_Shp(IFeatureClass pFeatClass, string path)
{
IDataset pDataSet = pFeatClass as IDataset;
IWorkspace pWorkspace = pDataSet.Workspace;
string strShapeFolder = path;
string fileName = pFeatClass.AliasName;
string fullFileName = path + “//” + fileName + “.shp”;
fullFileName = fullFileName.Replace(@”//”, @”/”);
if (File.Exists(fullFileName))
{ //如果已经存在就先删除
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fis = di.GetFiles(fileName + “*”);
foreach (FileInfo fi in fis)
{
fi.Delete();
}
}
IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(strShapeFolder, 0);
this.IFeatureDataConverter_ConvertFeatureClass(pWorkspace, (IWorkspace)pWS, pFeatClass.AliasName, fileName);
}
转载自:https://blog.csdn.net/wang4978/article/details/6248868