C# 读取 Geodatabase
概述
在之前的博客中http://blog.csdn.net/mytudousi/article/details/31350807介绍了Geodatabase的大体情况,既然有了Geodatabase这么一个数据框架,想着我们使用AE读取的时候应该也不会太复杂,至少在读取的时候不用太关心其存储的方式。读取完数据得到的数据模型可以直接进行展示和编辑操作,而不用关系数据源的类型。AE确实有这样的一组接口来完成该工作。
在AE用来数据读取的主要的接口包括IWorkspaceFactory和IWorkspace两个接口。根据不同的数据源,AE提供了不同的WorkspaceFactory,通过WorkspaceFactory的Open方法得到不同的Workspace,通过Workspace就可以打开各种数据了。其接口体系图如下:
从上图我们可以看出当我们需要打开指定的Worksapce之前,我们要确定打开的时哪种类型的Workspace,然后实例化相应的WorkspaceFactory,接下来调用Open方法,当然在调用Open方法时要传入对用的Workspace的连接参数,最后就能得到Worksapce的实例了。得到该实例之后,我们就可以把得到的IWorkspace转换成相应的接口来打开要素类数据、栅格数据或其他数据。
下面就是打开常用Workspace的方法。
打开ShapeFile
在打开ShapeFile时,我们把shapeFile所在的文件夹作为ShapeFile的Workpace,ShapeFile只是该Workspace下面的一个要素类。
- ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
- // workspaceFactory.OpenFromFile,传入的参数是Shapefile文件所在的文件夹路径
- ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile("D:\\", 0);
- // OpenFeatureClass传入的参数是shape文件的文件名,不带后缀
- ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("道路");
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
// workspaceFactory.OpenFromFile,传入的参数是Shapefile文件所在的文件夹路径
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile("D:\\", 0);
// OpenFeatureClass传入的参数是shape文件的文件名,不带后缀
ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("道路");
打开SDE
打开SDE的时候,需要设置一些连接参数,例如服务地址、端口、用户名、密码和版本。
- 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);
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);
打开个人数据库 Acess
个人数据库就是Acess数据库了,不过现在已经不太推荐使用,都推荐使用ArcGIS的文件数据库即GDB
- string filePath="E:\\tt.mdb";
- AccessWorkspaceFactory fac=new AccessWorkspaceFactoryClass();
- IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
string filePath="E:\\tt.mdb";
AccessWorkspaceFactory fac=new AccessWorkspaceFactoryClass();
IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
OpenFromFile是IWorkspaceFactory的一个对于文件的快捷的打开方式,也可以使用IProperty打开。
- IPropertySet propertySet = new PropertySetClass();
- /参数database就是Acess文件的路径
- propertySet.SetProperty("DATABASE", database);
- IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
- return workspaceFactory.Open(propertySet, 0);
IPropertySet propertySet = new PropertySetClass();
/参数database就是Acess文件的路径
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
打开文件数据库 GDB
文件数据库就是GDB文件夹了,里面存储着一些二进制文件。打开GDB文件其实和打开Acess文件的方式类似。
- string filePath="E:\\tt.gdb";
- FileGDBWorkspaceFactoryClass fac=new FileGDBWorkspaceFactoryClass();
- IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
string filePath="E:\\tt.gdb";
FileGDBWorkspaceFactoryClass fac=new FileGDBWorkspaceFactoryClass();
IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
OpenFromFile是IWorkspaceFactory的一个对于文件的快捷的打开方式,也可以使用IProperty打开。
- IPropertySet propertySet = new PropertySetClass();
- /参数database就是gdb文件的路径
- propertySet.SetProperty("DATABASE", database);
- IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
- return workspaceFactory.Open(propertySet, 0);
IPropertySet propertySet = new PropertySetClass();
/参数database就是gdb文件的路径
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
AutoCAD dwg文件
AutoCAD dwg文件比较特殊,在CAD中似乎不太区分线和面的,也不太注重面是否有意义。我们使用ArcMap打开一个CAD文件时,可以看到里面有四个图层,分别是点、线、面和标注。所以我们使用ArcEngine打开一个CAD文件的结果应该是和ArcMap打开一样的。例如在D盘有一个0.dwg的CAD文件,打开改文件的代码如下:
- CadWorkspaceFactoryClass fac=new CadWorkspaceFactoryClass ();
- String filePath="D:\\";
- IFeatureWorkspace space=fac.OpenFromFile(filePath,0) as IFeatureWorkspace ;
- <pre name="code" class="csharp">//点
- IFeatureClass point=space.OpenFeatureClass ("0.dwg:Point");
- layer=new CadFeatureLayerClass ();
- layer.FeatureClass =point;
CadWorkspaceFactoryClass fac=new CadWorkspaceFactoryClass ();
String filePath="D:\\";
IFeatureWorkspace space=fac.OpenFromFile(filePath,0) as IFeatureWorkspace ;
<pre name="code" class="csharp">//点
IFeatureClass point=space.OpenFeatureClass ("0.dwg:Point");
layer=new CadFeatureLayerClass ();
layer.FeatureClass =point;
//线IFeatureClass polyline =space.OpenFeatureClass("0.dwg:Polyline");IFeatureLayer layer=new CadFeatureLayerClass ();layer.FeatureClass =polyline;//面IFeatureClass polygon=space.OpenFeatureClass ("0.dwg:Polygon");layer=new CadFeatureLayerClass();layer.FeatureClass
=polygon;//注记IFeatureClass anno=space.OpenFeatureClass ("0.dwg:Annotation"); layer=new CadAnnotationLayerClass();layer.FeatureClass =anno;
打开本地栅格数据 tif、img
打开本地的栅格数据有一个快捷的方式,就是通过RasterLayer打开,代码如下:
- IRasterLayer rasterLayer = new RasterLayerClass();
- <pre name="code" class="csharp">// fileName指存本地的栅格文件路径
- rasterLayer.CreateFromFilePath(fileName);
IRasterLayer rasterLayer = new RasterLayerClass();
<pre name="code" class="csharp">// fileName指存本地的栅格文件路径
rasterLayer.CreateFromFilePath(fileName);
用IRasterDataset接口打开栅格数据集
- WorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
- IWorkspace workspace;
- //inPath栅格数据存储路径,文件夹路径
- workspace = workspaceFactory.OpenFromFile(inPath, 0);
- IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
- IRasterDataset rastDataset;
- //inName栅格文件名
- rastDataset= rastWork.OpenRasterDataset(inName);
WorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
IWorkspace workspace;
//inPath栅格数据存储路径,文件夹路径
workspace = workspaceFactory.OpenFromFile(inPath, 0);
IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
IRasterDataset rastDataset;
//inName栅格文件名
rastDataset= rastWork.OpenRasterDataset(inName);
概述
在之前的博客中http://blog.csdn.net/mytudousi/article/details/31350807介绍了Geodatabase的大体情况,既然有了Geodatabase这么一个数据框架,想着我们使用AE读取的时候应该也不会太复杂,至少在读取的时候不用太关心其存储的方式。读取完数据得到的数据模型可以直接进行展示和编辑操作,而不用关系数据源的类型。AE确实有这样的一组接口来完成该工作。
在AE用来数据读取的主要的接口包括IWorkspaceFactory和IWorkspace两个接口。根据不同的数据源,AE提供了不同的WorkspaceFactory,通过WorkspaceFactory的Open方法得到不同的Workspace,通过Workspace就可以打开各种数据了。其接口体系图如下:
从上图我们可以看出当我们需要打开指定的Worksapce之前,我们要确定打开的时哪种类型的Workspace,然后实例化相应的WorkspaceFactory,接下来调用Open方法,当然在调用Open方法时要传入对用的Workspace的连接参数,最后就能得到Worksapce的实例了。得到该实例之后,我们就可以把得到的IWorkspace转换成相应的接口来打开要素类数据、栅格数据或其他数据。
下面就是打开常用Workspace的方法。
打开ShapeFile
在打开ShapeFile时,我们把shapeFile所在的文件夹作为ShapeFile的Workpace,ShapeFile只是该Workspace下面的一个要素类。
- ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
- // workspaceFactory.OpenFromFile,传入的参数是Shapefile文件所在的文件夹路径
- ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile("D:\\", 0);
- // OpenFeatureClass传入的参数是shape文件的文件名,不带后缀
- ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("道路");
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
// workspaceFactory.OpenFromFile,传入的参数是Shapefile文件所在的文件夹路径
ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile("D:\\", 0);
// OpenFeatureClass传入的参数是shape文件的文件名,不带后缀
ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("道路");
打开SDE
打开SDE的时候,需要设置一些连接参数,例如服务地址、端口、用户名、密码和版本。
- 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);
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);
打开个人数据库 Acess
个人数据库就是Acess数据库了,不过现在已经不太推荐使用,都推荐使用ArcGIS的文件数据库即GDB
- string filePath="E:\\tt.mdb";
- AccessWorkspaceFactory fac=new AccessWorkspaceFactoryClass();
- IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
string filePath="E:\\tt.mdb";
AccessWorkspaceFactory fac=new AccessWorkspaceFactoryClass();
IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
OpenFromFile是IWorkspaceFactory的一个对于文件的快捷的打开方式,也可以使用IProperty打开。
- IPropertySet propertySet = new PropertySetClass();
- /参数database就是Acess文件的路径
- propertySet.SetProperty("DATABASE", database);
- IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
- return workspaceFactory.Open(propertySet, 0);
IPropertySet propertySet = new PropertySetClass();
/参数database就是Acess文件的路径
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
打开文件数据库 GDB
文件数据库就是GDB文件夹了,里面存储着一些二进制文件。打开GDB文件其实和打开Acess文件的方式类似。
- string filePath="E:\\tt.gdb";
- FileGDBWorkspaceFactoryClass fac=new FileGDBWorkspaceFactoryClass();
- IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
string filePath="E:\\tt.gdb";
FileGDBWorkspaceFactoryClass fac=new FileGDBWorkspaceFactoryClass();
IFeatureWorkspace space=(IFeatureWorkspace)fac.OpenFromFile(filePath,0)
OpenFromFile是IWorkspaceFactory的一个对于文件的快捷的打开方式,也可以使用IProperty打开。
- IPropertySet propertySet = new PropertySetClass();
- /参数database就是gdb文件的路径
- propertySet.SetProperty("DATABASE", database);
- IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
- return workspaceFactory.Open(propertySet, 0);
IPropertySet propertySet = new PropertySetClass();
/参数database就是gdb文件的路径
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
AutoCAD dwg文件
AutoCAD dwg文件比较特殊,在CAD中似乎不太区分线和面的,也不太注重面是否有意义。我们使用ArcMap打开一个CAD文件时,可以看到里面有四个图层,分别是点、线、面和标注。所以我们使用ArcEngine打开一个CAD文件的结果应该是和ArcMap打开一样的。例如在D盘有一个0.dwg的CAD文件,打开改文件的代码如下:
- CadWorkspaceFactoryClass fac=new CadWorkspaceFactoryClass ();
- String filePath="D:\\";
- IFeatureWorkspace space=fac.OpenFromFile(filePath,0) as IFeatureWorkspace ;
- <pre name="code" class="csharp">//点
- IFeatureClass point=space.OpenFeatureClass ("0.dwg:Point");
- layer=new CadFeatureLayerClass ();
- layer.FeatureClass =point;
CadWorkspaceFactoryClass fac=new CadWorkspaceFactoryClass ();
String filePath="D:\\";
IFeatureWorkspace space=fac.OpenFromFile(filePath,0) as IFeatureWorkspace ;
<pre name="code" class="csharp">//点
IFeatureClass point=space.OpenFeatureClass ("0.dwg:Point");
layer=new CadFeatureLayerClass ();
layer.FeatureClass =point;
//线IFeatureClass polyline =space.OpenFeatureClass("0.dwg:Polyline");IFeatureLayer layer=new CadFeatureLayerClass ();layer.FeatureClass =polyline;//面IFeatureClass polygon=space.OpenFeatureClass ("0.dwg:Polygon");layer=new CadFeatureLayerClass();layer.FeatureClass
=polygon;//注记IFeatureClass anno=space.OpenFeatureClass ("0.dwg:Annotation"); layer=new CadAnnotationLayerClass();layer.FeatureClass =anno;
打开本地栅格数据 tif、img
打开本地的栅格数据有一个快捷的方式,就是通过RasterLayer打开,代码如下:
- IRasterLayer rasterLayer = new RasterLayerClass();
- <pre name="code" class="csharp">// fileName指存本地的栅格文件路径
- rasterLayer.CreateFromFilePath(fileName);
IRasterLayer rasterLayer = new RasterLayerClass();
<pre name="code" class="csharp">// fileName指存本地的栅格文件路径
rasterLayer.CreateFromFilePath(fileName);
用IRasterDataset接口打开栅格数据集
- WorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
- IWorkspace workspace;
- //inPath栅格数据存储路径,文件夹路径
- workspace = workspaceFactory.OpenFromFile(inPath, 0);
- IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
- IRasterDataset rastDataset;
- //inName栅格文件名
- rastDataset= rastWork.OpenRasterDataset(inName);
WorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
IWorkspace workspace;
//inPath栅格数据存储路径,文件夹路径
workspace = workspaceFactory.OpenFromFile(inPath, 0);
IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
IRasterDataset rastDataset;
//inName栅格文件名
rastDataset= rastWork.OpenRasterDataset(inName);
下一篇: ssh访问linux出现乱码