用到的引用
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Controls;
主要的类与接口
类/接口 | 所在类库 | 描述 |
IWorkspaceFactory | Geodatabase | 提供创建工作空间的方法 |
FileGDBWorkspaceFactoryClass | DataSourcesGDB | 连接和访问文件地理数据库的核心类 |
IWorkspace | Geodatabase | 提供对数据集的访问,打开具体数据需要向下类型转换为特定类型的工作空间。如Featureworkspace、RasterWorkspace等 |
IEnumDataset | Geodatabase | 访问数据集的枚举,可以是要素类数据集或栅格数据集或混合的枚举 |
IDataset | Geodatabase | 访问数据集,可以是要素类数据集或栅格数据集 |
IFeatureWorkspace | Geodatabase | 提供对要素数据集或要素类的访问 |
IFeatureLayer | Carto | 提供操作要素图层的方法 |
FeatureLayerClass | Carto | 创建要素图层的核心类 |
IRasterWorkspaceEx | Geodatabase | 提供对栅格工作空间的访问 |
IRasterWorkspaceEx | Geodatabase | 由此类实现由工作空间抽象类转换为栅格工作空间 |
IRasterLayer | Geodatabase | 提供操作栅格图层的方法 |
RasterLayer | Geodatabase | 创建栅格图层的核心类 |
步骤
1、打开工作空间
//文件地理数据库本身数据以文件形式存储,没有像shp那种类型的指定文件,因此以打开文件夹的格式打开
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string path = dlg.SelectedPath; //保存路径
IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
IWorkspace pworkspace = pWorkspaceFactory.OpenFromFile(path, 0);
2、遍历工作空间中的数据,将数据加载到显示
private void AddAllDataset(IWorkspace pWorkspace, AxMapControl mapControl)
{
//根据数据类型不同加载数据
//数据集枚举
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny); //加载各种类型的数据
//将IEnumDataset读到Dataset当中
IDataset pDataset = pEnumDataset.Next();
while (pDataset != null)
{
if(pDataset is IFeatureDataset) //要素数据集
{
IFeatureWorkspace pFeatureworkspace = (IFeatureWorkspace)pWorkspace;
IFeatureDataset pFeatureDataset = pFeatureworkspace.OpenFeatureDataset(pDataset.Name);
IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets; //获取要素集合
IDataset pDataset1 = pEnumDataset1.Next();
while (pDataset1 != null)
{
if (pDataset1 is IFeatureClass) //要素类
{
IFeatureLayer pFeatureLayer = new FeatureLayer();
pFeatureLayer.FeatureClass = pFeatureworkspace.OpenFeatureClass(pDataset1.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
if (pFeatureLayer.FeatureClass != null)
{
mapControl.AddLayer(pFeatureLayer);
}
}
pDataset1 = pEnumDataset1.Next();
}
}
else if(pDataset is IFeatureClass) //要素类
{
IFeatureWorkspace pFeatureworkspace = pWorkspace as IFeatureWorkspace;
IFeatureLayer pFlyr = new FeatureLayer();
pFlyr.FeatureClass = pFeatureworkspace.OpenFeatureClass(pDataset.Name);
pFlyr.Name = pDataset.Name;
if (pFlyr != null)
{
mapControl.AddLayer(pFlyr);
}
}
else if(pDataset is IRasterDataset) //栅格数据集
{
IRasterWorkspaceEx pRasterworkspace = pWorkspace as IRasterWorkspaceEx; //转为栅格空间
IRasterDataset pRasterDataset = pRasterworkspace.OpenRasterDataset(pDataset.Name);
if (pRasterDataset != null)
{
IRasterLayer pRasterLayer = new RasterLayer();
pRasterLayer.CreateFromDataset(pRasterDataset);
pRasterLayer.Name = pDataset.Name;
mapControl.AddLayer(pRasterLayer);
}
}
pDataset = pEnumDataset.Next();
}
}
代码示例:
private void 打开gdbToolStripMenuItem_Click(object sender, EventArgs e)
{
//文件地理数据库本身数据以文件形式存储,没有像shp那种类型的指定文件,因此以打开文件夹的格式打开
FolderBrowserDialog dlg = new FolderBrowserDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string path = dlg.SelectedPath; //保存路径
IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactoryClass();
IWorkspace pworkspace = pWorkspaceFactory.OpenFromFile(path, 0);
AddAllDataset(pworkspace, axMapControl1);
}
}
//将栅格数据加载到显示
private void AddAllDataset(IWorkspace pWorkspace, AxMapControl mapControl)
{
//根据数据类型不同加载数据
//数据集枚举
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTAny); //加载各种类型的数据
//将IEnumDataset读到Dataset当中
IDataset pDataset = pEnumDataset.Next();
while (pDataset != null)
{
if(pDataset is IFeatureDataset) //要素数据集
{
IFeatureWorkspace pFeatureworkspace = (IFeatureWorkspace)pWorkspace;
IFeatureDataset pFeatureDataset = pFeatureworkspace.OpenFeatureDataset(pDataset.Name);
IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets; //获取要素集合
IDataset pDataset1 = pEnumDataset1.Next();
while (pDataset1 != null)
{
if (pDataset1 is IFeatureClass) //要素类
{
IFeatureLayer pFeatureLayer = new FeatureLayer();
pFeatureLayer.FeatureClass = pFeatureworkspace.OpenFeatureClass(pDataset1.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
if (pFeatureLayer.FeatureClass != null)
{
mapControl.AddLayer(pFeatureLayer);
}
}
pDataset1 = pEnumDataset1.Next();
}
}
else if(pDataset is IFeatureClass) //要素类
{
IFeatureWorkspace pFeatureworkspace = pWorkspace as IFeatureWorkspace;
IFeatureLayer pFlyr = new FeatureLayer();
pFlyr.FeatureClass = pFeatureworkspace.OpenFeatureClass(pDataset.Name);
pFlyr.Name = pDataset.Name;
if (pFlyr != null)
{
mapControl.AddLayer(pFlyr);
}
}
else if(pDataset is IRasterDataset) //栅格数据集
{
IRasterWorkspaceEx pRasterworkspace = pWorkspace as IRasterWorkspaceEx; //转为栅格空间
IRasterDataset pRasterDataset = pRasterworkspace.OpenRasterDataset(pDataset.Name);
if (pRasterDataset != null)
{
IRasterLayer pRasterLayer = new RasterLayer();
pRasterLayer.CreateFromDataset(pRasterDataset);
pRasterLayer.Name = pDataset.Name;
mapControl.AddLayer(pRasterLayer);
}
}
pDataset = pEnumDataset.Next();
}
}