Arcgis Engine(ae)接口详解(6):workspace操作

                //此处用的workspace来源与用户选择
IWorkspace workspace = null;

//workspace一般等同于数据库

//工作空间类型,也可理解为数据库类型
//esriFileSystemWorkspace:可能为shp
//esriLocalDatabaseWorkspace:可能为file gdb,mdb
//esriRemoteDatabaseWorkspace:可能为sde
esriWorkspaceType workspaceType = workspace.Type;

//路径,对于本地文件类型的(例如shp,file gdb,mdb)有意义,就是他们的文件路径
string path = workspace.PathName;

//数据库连接配置,通常对于sde有效
IPropertySet propertySet = workspace.ConnectionProperties;
//以下是遍历他的值的代码
object objNames = null;
object objValue = null;
//获取所有值,结构是类似字段的key/value格式,参数1是name的数组,参数2是value的数组
//参数类型是object,但实际分别是string数组和object数组
propertySet.GetAllProperties(out objNames, out objValue);
string[] names = (string[])objNames;
object[] values = (object[])objValue;
//遍历获取各个值
for (int i = 0; i < names.Length; i++)
{
//参数名
string name = names[i];
//参数值
string value = values[i].ToString();
}

//以下代码由于不具备测试数据,只看代码不运行
if (1 == 2)
{
//执行原生sql
//如果Workspace数据类型是数据库如sde,可以通过此方法执行原生sql
//这里只能执行不返回结果的sql,例如insert,update等,而不能select
workspace.ExecuteSQL("update xxx set eee=111");
}

//PS:以上代码可以获取sde数据库的配置,但密码获取到的是乱码,因为想在ae获取sde完整的连接参数(通常用于再次新建对sde的连接)是不可能的

//遍历workspace下所有的数据集
//get_Datasets方法用于获取workspace下的成员,参数1是获取的数据类型
//IEnum开头的接口类似于游标,用于遍历一些查询结果
IEnumDataset enumDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
enumDataset.Reset();
//因为下面还要遍历数据集下的featureClass,因此用了IFeatureDataset,其实也可以as到IDataset
IFeatureDataset featureDataset = null;
while ((featureDataset = enumDataset.Next() as IFeatureDataset) != null)
{
//数据集的基础属性

IDataset dataset = featureDataset as IDataset;
//数据集名称
string datasetName = dataset.Name;
//数据集类型
esriDatasetType datasetType = dataset.Type;

//IGeoDataset是几何相关的接口
IGeoDataset geoDataset = featureDataset as IGeoDataset;
//空间参考
ISpatialReference spatialReference2 = geoDataset.SpatialReference;


//获取数据集下所有的featureClass

IFeatureClassContainer featureClassContainer = featureDataset as IFeatureClassContainer;
IEnumFeatureClass enumFeatureClass = featureClassContainer.Classes;
enumFeatureClass.Reset();
IFeatureClass featureClass = null;
//遍历数据集里的要素类
while ((featureClass = enumFeatureClass.Next()) != null)
{


}
//IEnum开头的接口用完都要手动释放(与游标一样)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(enumFeatureClass);
}
//IEnum开头的接口用完都要手动释放(与游标一样)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(enumDataset);

//遍历workspace下的featureClass
//PS:结合上面的代码可以注意到,获取数据集下的featureClass和获取workspace下的featureClass的代码是分开的

enumDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
enumDataset.Reset();
IFeatureClass featureClass2 = enumDataset.Next() as IFeatureClass;
while ((featureClass2 = enumDataset.Next() as IFeatureClass) != null)
{

}
//IEnum开头的接口用完都要手动释放(与游标一样)
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(enumDataset);

//获取Workspace的所有FeatureClass(包括Dataset里面的)
List<IFeatureClass> lstFeatureClass = WorkspaceEdmHelper.GetAllFeatureClassInWorkspace(workspace as IFeatureWorkspace);

//Workspace关于featureClass操作的接口
IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace;
//以下代码由于不具备测试数据,只看代码不运行
if (1 == 2)
{
//通过名称获取featureClass
//PS:即使featureClass在数据集下也可以这样获取,这点跟遍历不同
featureClass2 = featureWorkspace.OpenFeatureClass("www");

//通过名称获取数据集
featureDataset = featureWorkspace.OpenFeatureDataset("rrr");


//通过文件路径或数据库连接参数,新建数据库或打开数据库

//创建File Gdb,参数1是文件所在文件夹,参数2是数据库名称
//由于一个file gdb数据库等同于一个workspace,因此创建file gdb等同于创建workspace
featureWorkspace = WorkspaceEdmHelper.CreateFileGdb("C:\\aaa", "test.gdb");

//创建personal gdb(mdb),参数1是文件所在文件夹,参数2是数据库名称
featureWorkspace = WorkspaceEdmHelper.CreatePersonalGdb("C:\\aaa", "test.mdb");
}
被调用的封装函数代码如下
        /// <summary>
/// 获取Workspace的所有FeatureClass(包括Dataset里面的)
/// </summary>
/// <param name="featureWorkspace"></param>
/// <returns></returns>
public static List<IFeatureClass> GetAllFeatureClassInWorkspace(IFeatureWorkspace featureWorkspace)
{
IWorkspace workspace = featureWorkspace as IWorkspace;
List<IFeatureClass> featureClassResult = new List<IFeatureClass>();

//首先遍历数据集
IEnumDataset enumDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
enumDataset.Reset();
IFeatureDataset featureDataset = enumDataset.Next() as IFeatureDataset;
while (featureDataset != null)
{
IFeatureClassContainer featureClassContainer = featureDataset as IFeatureClassContainer;
IEnumFeatureClass enumFeatureClassTarget = featureClassContainer.Classes;
enumFeatureClassTarget.Reset();
IFeatureClass featureClass = enumFeatureClassTarget.Next();
//遍历数据集里的要素类
while (featureClass != null)
{
featureClassResult.Add(featureClass);

featureClass = enumFeatureClassTarget.Next();
}
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(enumFeatureClassTarget);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(featureDataset);

featureDataset = enumDataset.Next() as IFeatureDataset;
}
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(enumDataset);

//然后遍历在数据库根目录的要素类
enumDataset = workspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
enumDataset.Reset();
IFeatureClass featureClass2 = enumDataset.Next() as IFeatureClass;
while (featureClass2 != null)
{
featureClassResult.Add(featureClass2);

featureClass2 = enumDataset.Next() as IFeatureClass;
}
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(enumDataset);

return featureClassResult;
}

/// <summary>
/// 创建Fiel Gdb
/// </summary>
/// <param name="directory"></param>
/// <param name="gdbName"></param>
/// <returns></returns>
public static IFeatureWorkspace CreateFileGdb(string directory, string gdbName)
{
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
IName name = (IName)workspaceFactory.Create(directory, gdbName, null, 0);
return (IFeatureWorkspace)name.Open();
}

/// <summary>
/// 创建Personal Gdb(Mdb)
/// </summary>
/// <param name="directory"></param>
/// <param name="gdbName"></param>
/// <returns></returns>
public static IFeatureWorkspace CreatePersonalGdb(string directory, string gdbName)
{
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
IName name = (IName)workspaceFactory.Create(directory, gdbName, null, 0);
return (IFeatureWorkspace)name.Open();
}

2 个评论

史上最好、最经典的ArcGIS Engine和ArcGIS Python开发学习教程重磅出炉了,观看地址: http://i.youku.com/gisxiaotian

下载地址:https://gisxiaotian.taobao.com/
你就吹吧

要回复文章请先登录注册