|
|
|
|
using ESRI.ArcGIS.Geodatabase;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using ESRI.ArcGIS.esriSystem;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using ESRI.ArcGIS.DataSourcesGDB;
|
|
|
|
|
using ESRI.ArcGIS.Geometry;
|
|
|
|
|
using ESRI.ArcGIS.DataSourcesFile;
|
|
|
|
|
|
|
|
|
|
namespace Kingo.RuleCheck.AEHelper
|
|
|
|
|
{
|
|
|
|
|
public class WorkspaceAPI : IWorkspaceAPI
|
|
|
|
|
{
|
|
|
|
|
private string StrConn;
|
|
|
|
|
public IWorkspaceFactory2 wsFactory { get; private set; }
|
|
|
|
|
private WorkspaceTypeEnum _workSpaceType;
|
|
|
|
|
public WorkspaceTypeEnum workSpaceType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return this._workSpaceType;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
this._workSpaceType = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 连接属性
|
|
|
|
|
/// </summary>
|
|
|
|
|
private IPropertySet propertySet
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(this.StrConn))
|
|
|
|
|
{
|
|
|
|
|
IPropertySet result = new PropertySetClass();
|
|
|
|
|
if (_workSpaceType == WorkspaceTypeEnum.SDEConn)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> dicConn = GetConn(this.StrConn);
|
|
|
|
|
foreach (string key in dicConn.Keys)
|
|
|
|
|
{
|
|
|
|
|
result.SetProperty(key, dicConn[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (_workSpaceType == WorkspaceTypeEnum.SDEFile)
|
|
|
|
|
{
|
|
|
|
|
result = wsFactory.ReadConnectionPropertiesFromFile(this.StrConn);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.SetProperty("DATABASE", this.StrConn);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 解析连接字符串
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pStrConn"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private Dictionary<string, string> GetConn(string pStrConn)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] arr = pStrConn.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
foreach (string item in arr)
|
|
|
|
|
{
|
|
|
|
|
string[] kv = item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
result.Add(kv[0], kv[1]);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构造函数
|
|
|
|
|
/// </summary>
|
|
|
|
|
public WorkspaceAPI(string pStrConnection, WorkspaceTypeEnum pWorkspaceType)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (pWorkspaceType == WorkspaceTypeEnum.MDBFile)
|
|
|
|
|
{
|
|
|
|
|
if (!System.IO.File.Exists(pStrConnection))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (pWorkspaceType == WorkspaceTypeEnum.GDBFile)
|
|
|
|
|
{
|
|
|
|
|
if (!System.IO.Directory.Exists(pStrConnection))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(pStrConnection) || pWorkspaceType == WorkspaceTypeEnum.Default)
|
|
|
|
|
return;
|
|
|
|
|
this.StrConn = pStrConnection;
|
|
|
|
|
this.workSpaceType = pWorkspaceType;
|
|
|
|
|
this.wsFactory = WorkspaceFactory.GetWorkspaceFactory(pWorkspaceType);
|
|
|
|
|
if (wsFactory != null && !string.IsNullOrWhiteSpace(this.StrConn))
|
|
|
|
|
{
|
|
|
|
|
//if (this._CurrentWorkspace != null)
|
|
|
|
|
//{
|
|
|
|
|
// while (Marshal.ReleaseComObject(this._CurrentWorkspace) > 0) { }
|
|
|
|
|
//}
|
|
|
|
|
_CurrentWorkspace = wsFactory.Open(propertySet, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("打开工作空间失败!" + ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkspaceAPI(IWorkspace pWorkspace)
|
|
|
|
|
{
|
|
|
|
|
_CurrentWorkspace = pWorkspace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkspaceAPI()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前工作空间中指定类型的所有要素类名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pDatasetType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Dictionary<string, string> GetFeatureClassName(esriDatasetType pDatasetType)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
|
|
|
IEnumDataset pEnumDataset = _CurrentWorkspace.Datasets[pDatasetType];
|
|
|
|
|
if (pEnumDataset == null)
|
|
|
|
|
return result;
|
|
|
|
|
pEnumDataset.Reset();
|
|
|
|
|
IDataset pDataset = pEnumDataset.Next();
|
|
|
|
|
if (pDataset == null)
|
|
|
|
|
{
|
|
|
|
|
switch (pDatasetType)
|
|
|
|
|
{
|
|
|
|
|
case esriDatasetType.esriDTFeatureDataset:
|
|
|
|
|
pEnumDataset = _CurrentWorkspace.Datasets[esriDatasetType.esriDTFeatureClass];
|
|
|
|
|
if (pEnumDataset == null)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
pEnumDataset.Reset();
|
|
|
|
|
pDataset = pEnumDataset.Next();
|
|
|
|
|
break;
|
|
|
|
|
case esriDatasetType.esriDTFeatureClass:
|
|
|
|
|
pEnumDataset = _CurrentWorkspace.Datasets[esriDatasetType.esriDTFeatureDataset];
|
|
|
|
|
if (pEnumDataset == null)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
pEnumDataset.Reset();
|
|
|
|
|
pDataset = pEnumDataset.Next();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//如果数据集是IFeatureDataset,则遍历它下面的子类
|
|
|
|
|
while (pDataset != null)
|
|
|
|
|
{
|
|
|
|
|
if (pDatasetType != esriDatasetType.esriDTFeatureDataset)
|
|
|
|
|
{
|
|
|
|
|
IFeatureClass pFc = pDataset as IFeatureClass;
|
|
|
|
|
string key = pFc == null ? pDataset.BrowseName : pFc.AliasName;
|
|
|
|
|
if (!result.Keys.Contains(key))
|
|
|
|
|
result.Add(key, string.IsNullOrWhiteSpace(pDataset.BrowseName) ? pDataset.Name : pDataset.BrowseName);
|
|
|
|
|
}
|
|
|
|
|
IEnumDataset ed = pDataset.Subsets;
|
|
|
|
|
if (ed != null)
|
|
|
|
|
{
|
|
|
|
|
IDataset ds = ed.Next();
|
|
|
|
|
while (ds != null)
|
|
|
|
|
{
|
|
|
|
|
IFeatureClass fc = ds as IFeatureClass;
|
|
|
|
|
if (fc != null)
|
|
|
|
|
{
|
|
|
|
|
string key = fc == null ? ds.BrowseName : fc.AliasName;
|
|
|
|
|
if (!result.Keys.Contains(key))
|
|
|
|
|
result.Add(key, string.IsNullOrWhiteSpace(ds.BrowseName) ? ds.Name : ds.BrowseName);
|
|
|
|
|
}
|
|
|
|
|
ds = ed.Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pDataset = pEnumDataset.Next();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前工作空间中存在的对象(要素集和要素类)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pDatasetType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Dictionary<string, string> GetDataSetAndFeatureClassName(esriDatasetType pDatasetType)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
|
|
|
IEnumDataset pEnumDataset = _CurrentWorkspace.Datasets[pDatasetType];
|
|
|
|
|
if (pEnumDataset == null)
|
|
|
|
|
return result;
|
|
|
|
|
pEnumDataset.Reset();
|
|
|
|
|
IDataset pDataset = pEnumDataset.Next();
|
|
|
|
|
//如果数据集是IFeatureDataset,则遍历它下面的子类
|
|
|
|
|
while (pDataset != null)
|
|
|
|
|
{
|
|
|
|
|
if (pDatasetType != esriDatasetType.esriDTFeatureDataset)
|
|
|
|
|
{
|
|
|
|
|
IFeatureClass pFc = pDataset as IFeatureClass;
|
|
|
|
|
result.Add(pFc == null ? pDataset.BrowseName : pFc.AliasName, string.IsNullOrWhiteSpace(pDataset.BrowseName) ? pDataset.Name : pDataset.BrowseName);
|
|
|
|
|
}
|
|
|
|
|
result.Add(pDataset.BrowseName, pDataset.BrowseName);
|
|
|
|
|
pDataset = pEnumDataset.Next();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前工作空间中存在的属性表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pDatasetType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public Dictionary<string, string> GetPropertyTable(esriDatasetType pDatasetType)
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, string> result = new Dictionary<string, string>();
|
|
|
|
|
IEnumDataset pEnumDataset = _CurrentWorkspace.Datasets[pDatasetType];
|
|
|
|
|
if (pEnumDataset == null)
|
|
|
|
|
return result;
|
|
|
|
|
pEnumDataset.Reset();
|
|
|
|
|
IDataset pDataset = null;
|
|
|
|
|
//如果数据集是IFeatureDataset,则遍历它下面的子类
|
|
|
|
|
while ((pDataset = pEnumDataset.Next()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (pDatasetType != esriDatasetType.esriDTTable)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
result.Add(pDataset.BrowseName, pDataset.BrowseName);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取当前工作空间中指定类型的所有要素类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pDatasetType"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<IFeatureClass> GetAllFeatureClass(esriDatasetType pDatasetType)
|
|
|
|
|
{
|
|
|
|
|
List<IFeatureClass> result = new List<IFeatureClass>();
|
|
|
|
|
IEnumDataset pEnumDataset = _CurrentWorkspace.Datasets[pDatasetType];
|
|
|
|
|
if (pEnumDataset == null)
|
|
|
|
|
return result;
|
|
|
|
|
pEnumDataset.Reset();
|
|
|
|
|
IDataset pDataset = pEnumDataset.Next();
|
|
|
|
|
//如果数据集是IFeatureDataset,则遍历它下面的子类
|
|
|
|
|
while (pDataset != null)
|
|
|
|
|
{
|
|
|
|
|
if (pDatasetType != esriDatasetType.esriDTFeatureDataset)
|
|
|
|
|
{
|
|
|
|
|
IFeatureClass fc = pDataset as IFeatureClass;
|
|
|
|
|
if (fc != null)
|
|
|
|
|
result.Add(fc);
|
|
|
|
|
}
|
|
|
|
|
IEnumDataset ed = pDataset.Subsets;
|
|
|
|
|
if (ed != null)
|
|
|
|
|
{
|
|
|
|
|
IDataset ds = ed.Next();
|
|
|
|
|
while (ds != null)
|
|
|
|
|
{
|
|
|
|
|
IFeatureClass fc = ds as IFeatureClass;
|
|
|
|
|
if (fc != null)
|
|
|
|
|
result.Add(fc);
|
|
|
|
|
ds = ed.Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pDataset = pEnumDataset.Next();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IWorkspace _CurrentWorkspace;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当前工作空间对象
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IWorkspace CurrentWorkspace
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _CurrentWorkspace;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// 打开工作空间
|
|
|
|
|
///// </summary>
|
|
|
|
|
//public IWorkspace OpenWorkspace(string pStrConnection, WorkspaceTypeEnum pWorkspaceType)
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// 打开工作空间
|
|
|
|
|
///// </summary>
|
|
|
|
|
//public IWorkspace OpenWorkspace(IWorkspace pWorkspace)
|
|
|
|
|
//{
|
|
|
|
|
// _CurrentWorkspace = pWorkspace;
|
|
|
|
|
// return _CurrentWorkspace;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开要素类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pFeatureClassName">要素类名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IFeatureClassAPI OpenFeatureClass(string pFeatureClassName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (CurrentWorkspace != null)
|
|
|
|
|
{
|
|
|
|
|
return new FeatureClassAPI((CurrentWorkspace as IFeatureWorkspace).OpenFeatureClass(pFeatureClassName));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("打开要素类前,必须先打开对应的工作空间!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开要素类,如果没有则返回null
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pFeatureClassName">要素类名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IFeatureClassAPI OpenFeatureClass2(string pFeatureClassName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (CurrentWorkspace != null)
|
|
|
|
|
{
|
|
|
|
|
return new FeatureClassAPI((CurrentWorkspace as IFeatureWorkspace).OpenFeatureClass(pFeatureClassName));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("打开要素类前,必须先打开对应的工作空间!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开要素类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pFeatureClassName">要素类名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public ITableAPI OpenTable(string pTableName)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (CurrentWorkspace != null)
|
|
|
|
|
{
|
|
|
|
|
return new TableAPI((CurrentWorkspace as IFeatureWorkspace).OpenTable(pTableName));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("打开表前,必须先打开对应的工作空间!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭/释放工作空间
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="compactAccess">关闭工作空间时是否压缩数据库,默认压缩</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool CloseWorkspace(bool compactAccess = true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (this._CurrentWorkspace != null)
|
|
|
|
|
{
|
|
|
|
|
//EngineEditorClass engineEditorClass = new EngineEditorClass();
|
|
|
|
|
//if (compactAccess && engineEditorClass.EditState == esriEngineEditState.esriEngineStateNotEditing)
|
|
|
|
|
//{
|
|
|
|
|
// GeoDBAPI.CompactAccessDataBaseByWorkspace(this._CurrentWorkspace);
|
|
|
|
|
//}
|
|
|
|
|
//while (Marshal.ReleaseComObject(this._CurrentWorkspace) > 0) { }
|
|
|
|
|
Marshal.ReleaseComObject(this._CurrentWorkspace);
|
|
|
|
|
}
|
|
|
|
|
if (this.wsFactory != null)
|
|
|
|
|
{
|
|
|
|
|
Marshal.ReleaseComObject(this.wsFactory);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建本地工作空间
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pPath"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IWorkspace CreateLocalWorkspace(string pPath)
|
|
|
|
|
{
|
|
|
|
|
string gdbExtention = System.IO.Path.GetExtension(pPath).ToLower();
|
|
|
|
|
switch (gdbExtention)
|
|
|
|
|
{
|
|
|
|
|
case ".gdb":
|
|
|
|
|
_CurrentWorkspace = CreateWorkspace<FileGDBWorkspaceFactoryClass>(pPath);
|
|
|
|
|
return _CurrentWorkspace;
|
|
|
|
|
case ".mdb":
|
|
|
|
|
_CurrentWorkspace = CreateWorkspace<AccessWorkspaceFactoryClass>(pPath);
|
|
|
|
|
return _CurrentWorkspace;
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
//ESRI.ArcGIS.ConversionTools
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建本地工作空间
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">工作空间类型</typeparam>
|
|
|
|
|
/// <param name="pPath">路径</param>
|
|
|
|
|
/// <returns>IWorkspace</returns>
|
|
|
|
|
public IWorkspace CreateWorkspace<T>(string pPath) where T : IWorkspaceFactory, new()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(pPath))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string sDirName = System.IO.Path.GetDirectoryName(pPath);
|
|
|
|
|
string sFileName = System.IO.Path.GetFileName(pPath);
|
|
|
|
|
|
|
|
|
|
IWorkspaceName pWorkspaceName = new T().Create(sDirName, sFileName, null, 0);
|
|
|
|
|
if (pWorkspaceName == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return (pWorkspaceName as IName).Open() as IWorkspace;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过路径获取IWorkspace
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="sFilePath"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IWorkspace GetWorkspace<T>(string sFilePath) where T : ESRI.ArcGIS.Geodatabase.IWorkspaceFactory, new()
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(sFilePath))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory pWksFact = new T();
|
|
|
|
|
return pWksFact.OpenFromFile(sFilePath, 0);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据路径获得IWorkspace
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sFilePath"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IWorkspace GetWorkspaceByPath(string sFilePath)
|
|
|
|
|
{
|
|
|
|
|
string gdbExtention = System.IO.Path.GetExtension(sFilePath).ToLower();
|
|
|
|
|
switch (gdbExtention)
|
|
|
|
|
{
|
|
|
|
|
case ".gdb":
|
|
|
|
|
return GetWorkspace<FileGDBWorkspaceFactoryClass>(sFilePath);
|
|
|
|
|
case ".mdb":
|
|
|
|
|
return GetWorkspace<AccessWorkspaceFactoryClass>(sFilePath);
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除本地工作空间
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pPath">本地工作空间路径</param>
|
|
|
|
|
public bool DeleteLocalWorkspace(string pPath)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IWorkspace pWks = null;
|
|
|
|
|
if (_CurrentWorkspace != null)
|
|
|
|
|
pWks = _CurrentWorkspace;
|
|
|
|
|
else
|
|
|
|
|
pWks = GetWorkspaceByPath(pPath);
|
|
|
|
|
if (pWks == null)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
(pWks as IDataset).Delete();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在指定数据集中创建要素类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pFeatureClassName">要素类名称</param>
|
|
|
|
|
/// <param name="pFields">字段集合</param>
|
|
|
|
|
/// <param name="pFeatureDataSet">要素数据集</param>
|
|
|
|
|
public IFeatureClassAPI CreateFeatureClass(string pFeatureClassName, IFeatureDataset pFeatureDataset, ISpatialReference pSpatialReference, esriGeometryType esriGeoType = esriGeometryType.esriGeometryPolygon, IFields pFields = null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(pFeatureClassName) || pFeatureDataset == null || pSpatialReference == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (pFields == null)
|
|
|
|
|
{
|
|
|
|
|
pFields = new FieldsClass();
|
|
|
|
|
CreateMainField(pFields as IFieldsEdit, esriGeoType, pSpatialReference);
|
|
|
|
|
}
|
|
|
|
|
IFeatureClass fc = pFeatureDataset.CreateFeatureClass(
|
|
|
|
|
pFeatureClassName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
|
|
|
|
|
return new FeatureClassAPI(fc);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在当前工作空间中创建要素类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pFeatureClassName">要素类名称</param>
|
|
|
|
|
/// <param name="pFields">字段集合</param>
|
|
|
|
|
public IFeatureClassAPI CreateFeatureClass(string pFeatureClassName, ISpatialReference pSpatialReference, IFields pFields)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(pFeatureClassName) || pSpatialReference == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (pFields == null)
|
|
|
|
|
{
|
|
|
|
|
pFields = new FieldsClass();
|
|
|
|
|
CreateMainField(pFields as IFieldsEdit, esriGeometryType.esriGeometryPolygon, pSpatialReference);
|
|
|
|
|
}
|
|
|
|
|
IFeatureWorkspace featureWS = this.CurrentWorkspace as IFeatureWorkspace;
|
|
|
|
|
if (featureWS != null)
|
|
|
|
|
{
|
|
|
|
|
IFeatureClass fc = featureWS.CreateFeatureClass(pFeatureClassName, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
|
|
|
|
|
return new FeatureClassAPI(fc);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在当前工作空间中创建要素类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pFeatureClassName">要素类名称</param>
|
|
|
|
|
/// <param name="pFields">字段集合</param>
|
|
|
|
|
public IFeatureClassAPI CreateFeatureClass(string pFeatureClassName, ISpatialReference pSpatialReference, esriGeometryType pGeometryType)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(pFeatureClassName) || pSpatialReference == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
IFields pFields = new FieldsClass();
|
|
|
|
|
CreateMainField(pFields as IFieldsEdit, pGeometryType, pSpatialReference);
|
|
|
|
|
IFeatureWorkspace featureWS = this.CurrentWorkspace as IFeatureWorkspace;
|
|
|
|
|
if (featureWS != null)
|
|
|
|
|
{
|
|
|
|
|
IFeatureClass fc = featureWS.CreateFeatureClass(pFeatureClassName, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");
|
|
|
|
|
return new FeatureClassAPI(fc);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 在当前工作空间中创建表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pTableName">表名</param>
|
|
|
|
|
/// <param name="pFields">字段集合</param>
|
|
|
|
|
public ITableAPI CreateTable(string pTableName, IFields pFields)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(pTableName))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
IFeatureWorkspace featureWS = this.CurrentWorkspace as IFeatureWorkspace;
|
|
|
|
|
if (featureWS != null)
|
|
|
|
|
{
|
|
|
|
|
ITable tb = featureWS.CreateTable(pTableName, pFields, null, null, "");
|
|
|
|
|
return new TableAPI(tb);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除指定要素类
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="featureClassName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool DeleteFeatureClass(string featureClassName)
|
|
|
|
|
{
|
|
|
|
|
if (this.CurrentWorkspace == null || string.IsNullOrEmpty(featureClassName))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
IEnumDatasetName pEnumDatasetName;
|
|
|
|
|
IFeatureWorkspace pFeaWorkspace;
|
|
|
|
|
IDatasetName pDatasetName;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pFeaWorkspace = CurrentWorkspace as IFeatureWorkspace;
|
|
|
|
|
pEnumDatasetName = CurrentWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass ^ esriDatasetType.esriDTFeatureDataset);
|
|
|
|
|
pEnumDatasetName.Reset();
|
|
|
|
|
pDatasetName = pEnumDatasetName.Next();
|
|
|
|
|
while (pDatasetName != null)
|
|
|
|
|
{
|
|
|
|
|
if (pDatasetName.Type == esriDatasetType.esriDTFeatureDataset)
|
|
|
|
|
{
|
|
|
|
|
//如果是要素集,则对要素集内的要素类进行查找
|
|
|
|
|
IEnumDatasetName pEnumFcName = (pDatasetName as IFeatureDatasetName).FeatureClassNames;
|
|
|
|
|
IDatasetName pFcName = pEnumFcName.Next();
|
|
|
|
|
while (pFcName != null)
|
|
|
|
|
{
|
|
|
|
|
if (pFcName.Name.Equals(featureClassName, StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
DeleteByName(pFeaWorkspace, pFcName);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
pFcName = pEnumFcName.Next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (pDatasetName.Name.Equals(featureClassName, StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
DeleteByName(pFeaWorkspace, pDatasetName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pDatasetName = pEnumDatasetName.Next();
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
* 此处是为了删除GDB_GeomColumns表对应的TableName=要删除表名的记录,GDB_GeomColumns是记录当前数据库所有存在的矢量表记录
|
|
|
|
|
* 同时删除对应图层的SHAPE_Index表
|
|
|
|
|
*/
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CurrentWorkspace.ExecuteSQL(string.Format("DELETE FROM GDB_GeomColumns WHERE TableName='{0}'", featureClassName));
|
|
|
|
|
CurrentWorkspace.ExecuteSQL("Drop Table " + featureClassName + "SHAPE_Index");
|
|
|
|
|
CurrentWorkspace.ExecuteSQL("Drop Table " + featureClassName);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 是否存在要素类:此方法性能很慢,慎用
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="featureClassName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool ExistFeatureClass(string featureClassName)
|
|
|
|
|
{
|
|
|
|
|
if (this.CurrentWorkspace == null || string.IsNullOrEmpty(featureClassName))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
IEnumDatasetName pEnumDatasetName = null;
|
|
|
|
|
IFeatureWorkspace pFeaWorkspace = null;
|
|
|
|
|
IDatasetName pDatasetName = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
pFeaWorkspace = CurrentWorkspace as IFeatureWorkspace;
|
|
|
|
|
pEnumDatasetName = CurrentWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass ^ esriDatasetType.esriDTFeatureDataset);
|
|
|
|
|
if (pEnumDatasetName == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
pEnumDatasetName.Reset();
|
|
|
|
|
while ((pDatasetName = pEnumDatasetName.Next()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (pDatasetName.Type == esriDatasetType.esriDTFeatureDataset)
|
|
|
|
|
{
|
|
|
|
|
//如果是要素集,则对要素集内的要素类进行查找
|
|
|
|
|
IEnumDatasetName pEnumFcName = (pDatasetName as IFeatureDatasetName).FeatureClassNames;
|
|
|
|
|
if (pEnumFcName == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pEnumFcName.Reset();
|
|
|
|
|
IDatasetName pFcName = null;
|
|
|
|
|
while ((pFcName = pEnumFcName.Next()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (featureClassName.Equals(pFcName.Name, StringComparison.CurrentCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (pDatasetName.Name.Equals(featureClassName))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//删除名称对象
|
|
|
|
|
private void DeleteByName(IFeatureWorkspace pFeaWorkspace, IDatasetName pDatasetName)
|
|
|
|
|
{
|
|
|
|
|
IFeatureWorkspaceManage pWorkspaceManager = pFeaWorkspace as IFeatureWorkspaceManage;
|
|
|
|
|
pWorkspaceManager.DeleteByName(pDatasetName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region 打开指定工作空间的元素类 2020-6-1 沈超
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 打开指定工作空间中的要素类,如果没有则返回null 2020-6-1 沈超
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sFeatureClassName">要素类名称</param>
|
|
|
|
|
/// <param name="iTheWorkspace">对应的工作空间</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IFeatureClassAPI ToOpenFeatureClass(string sFeatureClassName, IWorkspace iTheWorkspace)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (iTheWorkspace != null)
|
|
|
|
|
{
|
|
|
|
|
return new FeatureClassAPI((iTheWorkspace as IFeatureWorkspace).OpenFeatureClass(sFeatureClassName));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("打开要素类前,必须先打开对应的工作空间!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 建立主要字段(OBJECTID和SHAPE)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pFieldsEdit">IFieldsEdit,把OBJECTID和SHAPE字段加进去</param>
|
|
|
|
|
/// <param name="pSpatialReference">空间参数</param>
|
|
|
|
|
public static void CreateMainField(IFieldsEdit pFieldsEdit, esriGeometryType pGeometryType, ISpatialReference pSpatialReference)
|
|
|
|
|
{
|
|
|
|
|
IField pField;
|
|
|
|
|
IFieldEdit pFieldEdit;
|
|
|
|
|
//建立OBJECTID字段
|
|
|
|
|
pField = new FieldClass();
|
|
|
|
|
pFieldEdit = pField as IFieldEdit;
|
|
|
|
|
pFieldEdit.Name_2 = "OBJECTID";
|
|
|
|
|
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
|
|
|
|
|
pFieldsEdit.AddField(pField);
|
|
|
|
|
//建立shap字段
|
|
|
|
|
pField = new FieldClass();
|
|
|
|
|
pFieldEdit = pField as IFieldEdit;
|
|
|
|
|
pFieldEdit.Name_2 = "SHAPE";
|
|
|
|
|
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
|
|
|
|
|
IGeometryDef pGeometryDef = new GeometryDefClass();
|
|
|
|
|
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
|
|
|
|
|
pGeometryDefEdit.GeometryType_2 = pGeometryType;
|
|
|
|
|
pGeometryDefEdit.SpatialReference_2 = pSpatialReference;
|
|
|
|
|
pFieldEdit.GeometryDef_2 = pGeometryDef;
|
|
|
|
|
pFieldsEdit.AddField(pField);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|