AE 打开各种格式文件

http://blog.sina.com.cn/s/blog_7a3fc90501016qrg.html

/// <summary>
/// 打开ShapeFile文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="fileName">文件名</param>
/// <returns>IFeatureClass</returns>
public IFeatureClass GetShpFile(string filePath, string fileName)
{
IFeatureWorkspace featureWorkspace;
IFeatureClass featureClass;

featureWorkspace = GetShapeWorkspace(filePath) as IFeatureWorkspace;

try
{
featureClass = featureWorkspace.OpenFeatureClass(fileName);
}
catch
{
featureClass = null;
}

ReleaseAE.ReleaseAEObject(featureWorkspace);

return featureClass;
}

Coverage

Coverage是ArcInfo workstation的原生数据格式。该格式是基于文件夹存储的,是因为在windows资源管理器下,它的空间信息和属性信息是分别存放在两个文件夹里。coverage是一个非常成功的早期地理数据模型,二十多年来深受用户欢迎,很多早期的数据都是coverage格式的。ESRI不公开coverage的数据格式,但是提供了coverage格式转换的一个交换文件(interchange file,即E00),并公开数据格式。但是ESRI为推广其第三代数据模型geodatabase,从ArcGIS 8.3版本开始,屏蔽了对coverage的编辑功能。如果需要使用coverage格式的数据,可以安装ArcInfo workstation,或者将coverage数据转换为其他可编辑的数据格式。Coverage是一个集合,它可以包含一个或多个要素类。 Coverage数据的工作空间也是它所在的文件夹;由于Coverage可以包含多个要素类,得到工作空间后在打开具体的要素类时可以用'Coverage名称:要素类名称',例如下面代码中的'basin:polygon'。
IWorkspaceFactory pFactory = new ArcInfoWorkspaceFactoryClass();
IWorkspace pWorkspace = pFactory.OpenFromFile(@'D:\ArcTutor\TopologyData', 0);
IFeatureWorkspace pFeatWorkspace = pWorkspace as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeatWorkspace.OpenFeatureClass('basin:polygon');

/// <summary>
/// 打开Raster文件(如果数据格式为是ESRI GRID,fileName不带后缀,若为tiff则带后缀.tif)
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="fileName">文件名</param>
/// <returns>IRaster</returns>
public IRaster GetRasterFile(string filePath, string fileName)
{
IRasterWorkspace2 rasterWorkspace;
IRasterDataset rasterDataSet;
IRasterLayer rasterLayer;
IRaster raster;

rasterWorkspace = GetRasterWorkspace(filePath) as IRasterWorkspace2;

try
{
rasterDataSet = rasterWorkspace.OpenRasterDataset(fileName);

rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromDataset(rasterDataSet);

raster = rasterLayer.Raster;

//ReleaseAE.ReleaseAEObject(rasterDataSet);
//ReleaseAE.ReleaseAEObject(rasterLayer);
}
catch
{
raster = null;
}

ReleaseAE.ReleaseAEObject(rasterWorkspace);

return raster;
}

/// <summary>
/// 打开PersonalGeodatabase里的数据要素
/// </summary>
/// <param name="mdbFile">mdb文件(带后缀.mdb)</param>
/// <param name="featureName">要素名</param>
/// <returns></returns>
public IFeatureClass GetPersonalGeodatabase(string mdbFile, string featureName)
{
IFeatureWorkspace featureWorkspace;
IFeatureClass featureClass;

workspaceFactory = new AccessWorkspaceFactoryClass();
featureWorkspace = workspaceFactory.OpenFromFile(mdbFile, 0) as IFeatureWorkspace;
featureClass = featureWorkspace.OpenFeatureClass(featureName);

ReleaseAE.ReleaseAEObject(featureWorkspace);
ReleaseAE.ReleaseAEObject(workspaceFactory);

return featureClass;
}

/// <summary>
/// 打开FileGeodatabase里的数据要素
/// </summary>
/// <param name="gdbFile">gdb文件</param>
/// <param name="featureName">要素名</param>
/// <returns></returns>
public IFeatureClass GetFileGeodatabase(string gdbFile, string featureName)
{
IFeatureWorkspace featureWorkspace;
IFeatureClass featureClass;

workspaceFactory = new FileGDBWorkspaceFactoryClass();
featureWorkspace = workspaceFactory.OpenFromFile(gdbFile, 0) as IFeatureWorkspace;
featureClass = featureWorkspace.OpenFeatureClass(featureName);

ReleaseAE.ReleaseAEObject(featureWorkspace);
ReleaseAE.ReleaseAEObject(workspaceFactory);

return featureClass;
}

/// <summary>
/// 打开TIN文件
/// </summary>
/// <param name="tinFile">文件路径</param>
/// <param name="fileName">文件名</param>
/// <returns>Tin</returns>
public ITin GetTinFile(string tinFile, string fileName)
{
ITinWorkspace pTinWorkspace;
ITin pTin;
workspaceFactory = new TinWorkspaceFactoryClass();
pTinWorkspace = workspaceFactory.OpenFromFile(tinFile, 0) as ITinWorkspace;
pTin = pTinWorkspace.OpenTin(fileName);

ReleaseAE.ReleaseAEObject(pTinWorkspace);
ReleaseAE.ReleaseAEObject(workspaceFactory);

return pTin;
}

/// <summary>
/// 打开CAD文件(featurename要带扩展名)
/// </summary>
/// <param name="cadFile">文件路径</param>
/// <param name="featureName">文件名</param>
/// <returns>featureClass</returns>
//public IFeatureClass GetCADFile(string cadFile, string featureName)
//{
// //IFeatureWorkspace featureWorkspace;
// //IFeatureClass featureClass;

// //workspaceFactory = new CadWorkspaceFactoryClass();
// //featureWorkspace = workspaceFactory.OpenFromFile(cadFile, 0) as IFeatureWorkspace;
// //featureClass = featureWorkspace.OpenFeatureClass(featureName);
// //return featureClass;
//}

/// <summary>
/// 打开数据表格
/// </summary>
/// <param name="connectionString">连接数据库字符串</param>
/// <param name="tableName">表名</param>
/// <returns>table</returns>
public ITable GetRDBMS(string connectionString, string tableName)
{
IFeatureWorkspace featureWorkspace;

workspaceFactory = new OLEDBWorkspaceFactoryClass();
IPropertySet pPropset = new PropertySetClass();
ITable pTable;
pPropset.SetProperties("CONNECTSTRING", connectionString);
featureWorkspace = workspaceFactory.Open(pPropset,0) as IFeatureWorkspace;
pTable = featureWorkspace.OpenTable(tableName);

ReleaseAE.ReleaseAEObject(featureWorkspace);
ReleaseAE.ReleaseAEObject(pPropset);

return pTable;
}

/// <summary>
/// 创建内存工作空间Name
/// </summary>
/// <param name="workspacename">工作空间名称</param>
/// <returns>工作空间Name</returns>
public IWorkspaceName CreatMemoryWorkspaceName(string workspacename)
{
IWorkspaceName workspaceName;

workspaceFactory = new InMemoryWorkspaceFactoryClass();

workspaceName = workspaceFactory.Create("d:\", workspacename, null, 0);

ReleaseAE.ReleaseAEObject(workspaceFactory);

return workspaceName;
}

/// <summary>
/// 创建内存工作空间
/// </summary>
/// <param name="workspacename">工作空间名称</param>
/// <returns>工作空间类型IWorkspace </returns>
public IWorkspace CreatMemoryWorkspace(string workspacename)
{
IWorkspaceName workspaceName;
IName name;
IWorkspace workspace;

workspaceName = CreatMemoryWorkspaceName(workspacename);

name = workspaceName as IName;
name.NameString = workspacename;
workspace = name.Open() as IWorkspace;

ReleaseAE.ReleaseAEObject(workspaceName);
ReleaseAE.ReleaseAEObject(name);

return workspace;
}

/// <summary>
/// 创建本地磁盘矢量工作空间
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public IWorkspace GetShapeWorkspace(string filePath)
{
IWorkspace workspace;

workspaceFactory = new ShapefileWorkspaceFactoryClass();

workspace = workspaceFactory.OpenFromFile(filePath, 0);

ReleaseAE.ReleaseAEObject(workspaceFactory);
workspaceFactory = null;

return workspace;
}

public IWorkspaceName GetShapeWorkspaceName(string spaceName)
{
IWorkspaceName workspaceName;

workspaceFactory = new ShapefileWorkspaceFactoryClass();

workspaceName = workspaceFactory.Create(Path.GetTempPath(), spaceName, null, 0);

ReleaseAE.ReleaseAEObject(workspaceFactory);

return workspaceName;
}

/// <summary>
/// 创建本地磁盘栅格工作空间
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public IWorkspace GetRasterWorkspace(string filePath)
{
IWorkspace workspace;

workspaceFactory = new RasterWorkspaceFactoryClass();
workspace = workspaceFactory.OpenFromFile(filePath, 0);

ReleaseAE.ReleaseAEObject(workspaceFactory);

return workspace;
}

上一篇:python os模块常用方法总结


下一篇:《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇02:滚屏》