AE加载不同数据的方法(GeoDatabase空间数据管理)
先看一下GeoDatabase核心结构模型图:
1 工作空间工厂WorkspaceFactory对象
WorkspaceFactory是GeoDatabase的入口,是一个抽象类,拥有很多子类,例如SdeWorkspaceFactory, AccessWorkspaceFactory, ShapfileWorkspaceFactory CadWorkspaceFactory等
IWorkspaceFactory接口定义了所有工作空间对象的一般属性和方法
public IWorkspaceName Create (string parentDirectory,string Name,IPropertySet ConnectionProperties,int hWnd);用于产生一个新的工作空间名称对象,前两个参数是数据库的路径和名称,第三个参数是一个属性集(properset)对象。
public IWorkspace Open ( IPropertySet ConnectionProperties,int hWnd);打开一个已经存在的工作空间像SDE数据库等;
public IWorkspace OpenFromFile (string fileName,int hWnd);打开一个文件类型的数据
2 工作空间WorkSpace对象
Workspace在逻辑上是一个包含空间数据集和非空间数据集的数据容器,数据包括要素类、栅格数据集、表等对象
Workspace对象主要的一些接口:
1 IWorkspace接口:定义了一个工作空间最普通的属性和方法。
2 IFeatureWorkspace接口
这个接口主要用于管理要素的数据集,如表(Table)、对象类(ObjectClass)、要素类(FeatureClass)、要素数据集(FeatureDataset)和关系类(RelationshipClass)等。
主要的成员:
public IFeatureClass OpenFeatureClass ( string Name) 打开一个已经存在的要素类,无论这个要素类是在工作空间还是在一个要素数据集中
public IFeatureDataset OpenFeatureDataset (string Name);打开一个已经存在的要素数据集
public ITable OpenTable (string Name);打开一个已经存在的表;
3 RasterWorkspace对象
主要实现了IRasterWorkspace接口
public IRasterDataset OpenRasterDataset ( string Name);
4 属性集Propertyset对象
Propertyset对象是一个专门用于设置属性的对象,它是一种name-value对应的集合,类似于Hash表。属性名必须是字符串,属性值可以是字符串、数值或日期也可以是一个对象
5 DataSet数据集对象
Dataset对象可分为两大类:Table和GeoDataset,GeoDataset是一个抽象类,代表了拥有空间属性的数据集,包括FeatureDataset、要素类FeatureClass、TIN和栅格数据集RasterDataset。
6 FeatureDataset对象
实现的主要接口:
1 IFeatureDataset接口
public IEnumDataset Subsets {get;}:Datasets contained within this dataset.
2 IFeatureClassContainer接口
用于管理要素数据集里面的要素类,该接口的ClassByName和Class(index)等属性可以用来获取数据集中的特定的要素类(这里很奇怪在帮助文档中只有属性没有方法但点进去却是一个方法Ok你大概不理解我说的)
public IFeatureClass get_Class (int ClassIndex);
public IFeatureClass get_ClassByID (int ID);
public IFeatureClass get_ClassByName (string Name);
接下来举几个加载各种数据的例子
1 加载Shapefiles文件
OpenFileDialog dlg=new OpenFileDialog();
dlg.Title="打开shapefile文件";
dlg.Filter="(*.shp)|*.shp";
dlg.ShowDialog();
string filename=dlg.FileName;
int index=filename.LastIndexOf(@"\");
string path=filename.Substring(0,index);
string name=filename.Substring(index+1);
IWorkspaceFactory pWsFactory = new ShapefileWorkspaceFactoryClass();
IFeatureWorkspace pWorkSpace = pWsFactory.OpenFromFile(path, 0) as IFeatureWorkspace;
IFeatureClass pFeatureClass = pWorkSpace.OpenFeatureClass(name);
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.Name = pFeatureClass.AliasName;
pFeatureLayer.FeatureClass = pFeatureClass;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
2.加载栅格数据
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "打开Raster文件";
dlg.Filter = " Layer File(*.lyr)|*.jpg;*.bmp;*.tiff ";
dlg.ShowDialog();
string filename = dlg.FileName;
int index = filename.LastIndexOf(@"\");
string path = filename.Substring(0, index);
string name = filename.Substring(index + 1);
IWorkspaceFactory pWsFactory =new RasterWorkspaceFactoryClass();
IRasterWorkspace pRasterWorkspace = pWsFactory.OpenFromFile(path, 0) as IRasterWorkspace;
IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(name);
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromDataset(pRasterDataset);
axMapControl1.Map.AddLayer(pRasterLayer);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
3 加载CAD数据
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "打开CAD文件";
dlg.Filter = " CAD(*.dwg)|*.dwg|All Files(*.*)|*.* ";
dlg.ShowDialog();
string filename = dlg.FileName;
int index = filename.LastIndexOf(@"\");
string path = filename.Substring(0, index);
string name = filename.Substring(index + 1);
IWorkspaceFactory pWsFactory = new CadWorkspaceFactoryClass();
IFeatureWorkspace pFeatureWorkspace = pWsFactory.OpenFromFile(path, 0) as IFeatureWorkspace;
IFeatureDataset pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(name);
IFeatureClassContainer pFCContainer = pFeatureDataset as IFeatureClassContainer;
for (int i = 0; i < pFCContainer.ClassCount; i++)
{
IFeatureClass pFeatureClass = pFCContainer.get_Class(i);
if (pFeatureClass.FeatureType == esriFeatureType.esriFTCoverageAnnotation)
{
IFeatureLayer pFeatureLayer = new CadAnnotationLayerClass();
}
else
{
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureClass;
pFeatureLayer.Name = pFeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
}
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
4 加载PersonGeodatabase数据
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "打开Geodatabase文件";
dlg.Filter = " Personal Geodatabase(*.mdb)|*.mdb|All Files(*.*)|*.* ";
dlg.ShowDialog();
string path = dlg.FileName;
IWorkspaceFactory pAccessWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;
pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
IWorkspace pWorkspace = pAccessWorkspaceFactory.OpenFromFile(path, 0);
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
pEnumDataset.Reset();
IDataset pDataset = pEnumDataset.Next();
if (pDataset is IFeatureDataset)
{
pFeatureWorkspace = (IFeatureWorkspace)pAccessWorkspaceFactory.OpenFromFile(path, 0);
pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
pEnumDataset1.Reset();
IDataset pDataset1 = pEnumDataset1.Next();
if (pDataset1 is IFeatureClass)
{
pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
}
else
{
pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}