ArcEngine中打开各种数据源(WorkSpace)的连接(转)
ArcEngine中打开各种数据源(WorkSpace)的连接
(SDE、personal/File、ShapeFile、CAD数据、影像图、影像数据集)
ArcEngine 可以接受多种数据源。在开发过程中我们使用了如下几种数据源
1.企业数据库(SDE)
企业数据库需要使用SDE来管理,所以需要使用SDE的Workspace来表示连接。在AE接口中,Workspace是由Factory打开的。代码如下: // //准备连接参数
ESRI.ArcGIS.esriSystem.IPropertySet pPropSet=new PropertySetClass();
pPropSet.SetProperty("server","服务器机器名" );
pPropSet.SetProperty("instance","SDE运行的端口号");
pPropSet.SetProperty("user","用户名");
pPropSet.SetProperty("password","口令" );
pPropSet.SetProperty("version","版本");
SdeWorkspaceFactory sdeWkspFact=new SdeWorkspaceFactoryClass();
IFeatureWorkspace pFeaWksp=(IFeatureWorkspace)sdeWkspFact.Open(pPropSet,0);
其中唯一需要解释的可能就是版本(version).对于没有使用版本或者第一次连接(没有建立空间数据库,当然没有版本了). 该处使用sde.DEFAULT这个版本。这是默认的版本。
iPropertySet:: setProperty
public void setProperty(string name,object value)
2.个人数据库(PersonalGeodatabase、FileGeodatabase)
ArcEngine中个人数据库为Access。
string filePath="E:\\tt.mdb";
AccessWorkspaceFactory fac=new AccessWorkspaceFactoryClass();
IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0); 这是用的最多的一种方式.
如果是FileGeodatabasse,则将
FileGDBWorkspaceFactory pWSF = New FileGDBWorkspaceFactory;
IFeatureWorkspace pWS1= pWSF.OpenFromFile(sPath, 0);
如果用IPropertySet。如下:
//Personal Geodatabase e.g., database = "C:\\myData\\mypGDB.mdb"
public IWorkspace open_pGDB_Workspace(string database)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
3.Shapefile文件
Shapefile和Access的打开方式有一点相同。也有差异。
//这是Shape所在的目录(注意:是目录)
string spacePath="E:\\shapefile";
IShapefileWorkspaceFactory fac=new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(spacePath,0);
IFeatureClass fc=space.openFeatureClass(“china”);
4.AutoCAD dwg文件
AutoCAD文件和一般的ESRI格式不同。所以代码会有一点点奇怪。假设有一个dwg文件为E:\\cad\\107.dwg 下面是打开的代码:
CadWorkspaceFactoryClass fac=new CadWorkspaceFactoryClass ();
String filePath="E:\\cad";
IFeatureWorkspace space=fac.OpenFromFile(filePath,0) as IFeatureWorkspace ;
下面是打开FeatureClass的代码:
//线
IFeatureClass polyline =space.OpenFeatureClass("107.dwg:Polyline");
IFeatureLayer layer=new CadFeatureLayerClass ();
layer.FeatureClass =polyline;
//点
IFeatureClass point=space.OpenFeatureClass ("107.dwg:Point");
layer=new CadFeatureLayerClass ();
layer.FeatureClass =point;
//面
IFeatureClass polygon=space.OpenFeatureClass ("107.dwg:Polygon");
layer=new CadFeatureLayerClass();
layer.FeatureClass =polygon;
//注记
IFeatureClass anno=space.OpenFeatureClass ("107.dwg:Annotation"); layer=new CadAnnotationLayerClass();
layer.FeatureClass =anno;
5.影像图文件
//文件路径
string filePath="E:\\image\\117.tif";
IRasterLayer rasterLayer=new RasterLayerClass();
rasterLayer.CreateFromFilePath(filePath );
6.数据库中的影像数据集。
//数据库连接
IWorkspace space=OpenSpace();
//打开数据库的方式
IRasterWorkspaceEx rasterSpace=(IRasterWorkspaceEx)space;
IRasterDataset rasterDataset=rasterSpace.OpenRasterDataset (setName.Name );
IRasterLayer rasLayer=new RasterLayerClass();
rasLayer.CreateFromDataset(rasterDataset);
IRasterWorkspaceEx:提供创建和打开栅格目录或栅格数据集的方法。
但是IRasterWorkspaceEx,IRasterWorkspace, IRasterWorkspace2都有OpenRasterDataset。IRasterWorkspace经常也可用于打开栅格数据,如: pWorkspaceFactory = new RasterWorkspaceFactoryClass();
pRasterWorkspace = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0);
IRasterDataset pRasterDataset = (IRasterDataset)pRasterWorkspace.OpenRasterDataset(fileName);
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromDataset(pRasterDataset);