|
|
|
|
@ -1,749 +0,0 @@ |
|
|
|
|
using ESRI.ArcGIS.ADF; |
|
|
|
|
using ESRI.ArcGIS.Geodatabase; |
|
|
|
|
using ESRI.ArcGIS.Geometry; |
|
|
|
|
using KGIS.Framework.AE; |
|
|
|
|
using KGIS.Framework.AE.Enum; |
|
|
|
|
using KGIS.Framework.OpenData.Control; |
|
|
|
|
using KGIS.Framework.OpenData.Filter; |
|
|
|
|
using KGIS.Framework.OpenData.InterFace; |
|
|
|
|
using KGIS.Framework.Utils; |
|
|
|
|
using KGIS.Framework.Utils.Helper; |
|
|
|
|
using Kingo.OpenData.Filter; |
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.IO; |
|
|
|
|
using System.Runtime.InteropServices; |
|
|
|
|
using System.Windows; |
|
|
|
|
using System.Windows.Forms; |
|
|
|
|
using KUI.Windows; |
|
|
|
|
using Kingo.Plugin.DataLoad.Helper; |
|
|
|
|
|
|
|
|
|
namespace Kingo.Plugin.DataLoad.View |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// 数据转化 的交互逻辑 |
|
|
|
|
/// </summary> |
|
|
|
|
public partial class FrmDataConversion : BaseWindow |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// 源数据类型 |
|
|
|
|
/// </summary> |
|
|
|
|
private string ImportDataType; |
|
|
|
|
public FrmDataConversion() |
|
|
|
|
{ |
|
|
|
|
InitializeComponent(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void BtnSelectedYJGD_Click(object sender, RoutedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
using (ComReleaser com = new ComReleaser()) |
|
|
|
|
{ |
|
|
|
|
OpenDataDialog pDialog = new OpenDataDialog(); |
|
|
|
|
ISpatialDataObjectFilter pOFilter = new FilterGeoDatabasePersonal(); |
|
|
|
|
pDialog.AddFilter(pOFilter, true); |
|
|
|
|
pOFilter = new FilterGeoDatabaseFile(); |
|
|
|
|
pDialog.AddFilter(pOFilter, true); |
|
|
|
|
pDialog.AllowMultiSelect = false; |
|
|
|
|
pDialog.Title = "选择MDB/GDB数据"; |
|
|
|
|
pDialog.RestoreLocation = true; |
|
|
|
|
pDialog.StartLocation = pDialog.FinalLocation; |
|
|
|
|
DialogResult dialogResult = pDialog.ShowDialog(); |
|
|
|
|
if (dialogResult == System.Windows.Forms.DialogResult.OK && pDialog.Selection.Count != 0) |
|
|
|
|
{ |
|
|
|
|
ImportDataType = System.IO.Path.GetExtension(pDialog.FinalLocation).ToUpper().Replace('.', ' ').TrimStart(); |
|
|
|
|
if (string.IsNullOrWhiteSpace(ImportDataType) || (ImportDataType != "MDB" && ImportDataType != "GDB")) |
|
|
|
|
{ |
|
|
|
|
MessageHelper.Show("选择的数据路径有误,请根据过滤条件,重新选择数据库!!"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
else if ("MDB,GDB".Contains(ImportDataType)) |
|
|
|
|
{ |
|
|
|
|
rdG.IsChecked = ImportDataType != "GDB"; |
|
|
|
|
rdG.IsEnabled = ImportDataType != "GDB"; |
|
|
|
|
rdM.IsChecked = ImportDataType != "MDB"; |
|
|
|
|
rdM.IsEnabled = ImportDataType != "MDB"; |
|
|
|
|
} |
|
|
|
|
cobYJGDTextEdit.Text = pDialog.FinalLocation; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug("选择基础数据库失败:" + ex); |
|
|
|
|
MessageHelper.Show("选择基础数据库失败:" + ex.Message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void BtnGDBBaseData_Click(object sender, RoutedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
FolderBrowserDialog dialog = new FolderBrowserDialog(); |
|
|
|
|
DialogResult result = dialog.ShowDialog(); |
|
|
|
|
if (result == System.Windows.Forms.DialogResult.OK) |
|
|
|
|
cobTextEdit.Text = dialog.SelectedPath; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void BtnLoad_Click(object sender, RoutedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
#region 数据转化前置条件 |
|
|
|
|
if (!File.Exists(cobYJGDTextEdit.Text) && ImportDataType == "MDB") |
|
|
|
|
{ |
|
|
|
|
MessageHelper.Show("请先选择源数据库!"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (!Directory.Exists(cobYJGDTextEdit.Text) && ImportDataType == "GDB") |
|
|
|
|
{ |
|
|
|
|
MessageHelper.Show("请先选择源数据库!"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (string.IsNullOrWhiteSpace(cobTextEdit.Text)) |
|
|
|
|
{ |
|
|
|
|
MessageHelper.Show("请选择输出数据库路径!"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (rdG.IsChecked == false && rdM.IsChecked == false && rdS.IsChecked == false) |
|
|
|
|
{ |
|
|
|
|
MessageHelper.Show("请选择输出的数据类型!"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
this.ShowLoading("正在转化数据......", 0, 0); |
|
|
|
|
System.Threading.Thread.Sleep(1000); |
|
|
|
|
if (ImportDataType == "MDB" && rdG.IsChecked == true) |
|
|
|
|
{ |
|
|
|
|
this.UpdateMsg("正在进行MDB转化为GDB数据中.........."); |
|
|
|
|
MDBConversionGDB(cobYJGDTextEdit.Text, cobTextEdit.Text); |
|
|
|
|
} |
|
|
|
|
else if (ImportDataType == "MDB" && rdS.IsChecked == true) |
|
|
|
|
{ |
|
|
|
|
this.UpdateMsg("正在进行MDB转化为Shape数据中........."); |
|
|
|
|
MGDBConversionShape(cobYJGDTextEdit.Text, cobTextEdit.Text, ImportDataType); |
|
|
|
|
} |
|
|
|
|
else if (ImportDataType == "GDB" && rdM.IsChecked == true) |
|
|
|
|
{ |
|
|
|
|
this.UpdateMsg("正在进行GDB转化为MDB数据中.........."); |
|
|
|
|
GDBConversionMDB(cobYJGDTextEdit.Text, cobTextEdit.Text); |
|
|
|
|
} |
|
|
|
|
else if (ImportDataType == "GDB" && rdS.IsChecked == true) |
|
|
|
|
{ |
|
|
|
|
this.UpdateMsg("正在进行GDB转化为Shape数据中........."); |
|
|
|
|
MGDBConversionShape(cobYJGDTextEdit.Text, cobTextEdit.Text, ImportDataType); |
|
|
|
|
} |
|
|
|
|
this.CloseLoading(); |
|
|
|
|
MessageHelper.ShowTips("转化完成!"); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
this.CloseLoading(); |
|
|
|
|
MessageHelper.ShowTips("数据转化异常" + ex.Message); |
|
|
|
|
LogAPI.Debug("数据转化异常" + ex.Message); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
this.CloseLoading(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// MDB 转 GDB |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="S_Path">源路径</param> |
|
|
|
|
/// <param name="E_Path">输出路径</param> |
|
|
|
|
private void MDBConversionGDB(string S_MDBPath, string E_GDBPath) |
|
|
|
|
{ |
|
|
|
|
WorkspaceAPI s_WsAPI = null; |
|
|
|
|
WorkspaceAPI t_WsAPI = null; |
|
|
|
|
IEnumDataset pEnumDataset = null; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
s_WsAPI = new WorkspaceAPI(S_MDBPath, WorkspaceTypeEnum.MDBFile, true); |
|
|
|
|
string MDBName = System.IO.Path.GetFileNameWithoutExtension(S_MDBPath); |
|
|
|
|
string GDBTempPath = PluginServiceInterface.CommonHelper.CreateTempGDB(MDBName, E_GDBPath); |
|
|
|
|
t_WsAPI = new WorkspaceAPI(GDBTempPath, WorkspaceTypeEnum.GDBFile, true); |
|
|
|
|
pEnumDataset = s_WsAPI.CurrentWorkspace.Datasets[esriDatasetType.esriDTFeatureDataset]; |
|
|
|
|
if (pEnumDataset != null) |
|
|
|
|
{ |
|
|
|
|
pEnumDataset.Reset(); |
|
|
|
|
IDataset pDataset = null; |
|
|
|
|
//如果数据集是IFeatureDataset,则遍历它下面的子类 |
|
|
|
|
while ((pDataset = pEnumDataset.Next()) != null) |
|
|
|
|
{ |
|
|
|
|
IDataset newDataset = null; |
|
|
|
|
newDataset = DLAECommonHelper.CreateFeatureDataset(t_WsAPI); |
|
|
|
|
if (newDataset == null) continue; |
|
|
|
|
List<string> listFeatureClass = new List<string>(); |
|
|
|
|
List<string> listFeatureClassNameList = new List<string>(); |
|
|
|
|
IEnumDataset ed = pDataset.Subsets; |
|
|
|
|
if (ed != null) |
|
|
|
|
{ |
|
|
|
|
IDataset dataset = ed.Next(); |
|
|
|
|
while (dataset != null) |
|
|
|
|
{ |
|
|
|
|
if ((dataset as IFeatureClass) != null) |
|
|
|
|
{ |
|
|
|
|
string featureClassName = dataset.BrowseName; |
|
|
|
|
if (!string.IsNullOrWhiteSpace(featureClassName) && !listFeatureClass.Contains(featureClassName)) |
|
|
|
|
{ |
|
|
|
|
listFeatureClass.Add(System.IO.Path.Combine(pDataset.Workspace.PathName.ToString(), pDataset.BrowseName, featureClassName)); |
|
|
|
|
listFeatureClassNameList.Add(featureClassName); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Marshal.FinalReleaseComObject(dataset); |
|
|
|
|
dataset = ed.Next(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (listFeatureClass.Count > 0) |
|
|
|
|
InsertGDBFeatureClass(cobYJGDTextEdit.Text, listFeatureClassNameList, GDBTempPath, newDataset.BrowseName); |
|
|
|
|
if (newDataset != null) |
|
|
|
|
Marshal.FinalReleaseComObject(newDataset); |
|
|
|
|
} |
|
|
|
|
//若不存在数据集仅仅为要素类 |
|
|
|
|
List<IFeatureClass> featureClasses = s_WsAPI.GetAllFeatureClass(esriDatasetType.esriDTFeatureClass); |
|
|
|
|
List<string> FeatureClassNameList = new List<string>(); |
|
|
|
|
foreach (IFeatureClass item in featureClasses) |
|
|
|
|
{ |
|
|
|
|
if (!t_WsAPI.ExistFeatureClass((item as IDataset).Name) && !FeatureClassNameList.Contains((item as IDataset).Name)) |
|
|
|
|
{ |
|
|
|
|
FeatureClassNameList.Add((item as IDataset).Name); |
|
|
|
|
IFeatureClassAPI featureTemp = s_WsAPI.OpenFeatureClass2((item as IDataset).Name); |
|
|
|
|
if (featureTemp == null) continue; |
|
|
|
|
IFeatureClassAPI TempFL = t_WsAPI.CreateFeatureClass((item as IDataset).Name, (featureTemp.FeatureClass as IGeoDataset).SpatialReference, featureTemp.FeatureClass.Fields); |
|
|
|
|
featureTemp.FcToFc(TempFL.FeatureClass, null, true); |
|
|
|
|
featureTemp?.CloseFeatureClass(); |
|
|
|
|
TempFL?.CloseFeatureClass(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw ex; |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
t_WsAPI?.CloseWorkspace(); |
|
|
|
|
s_WsAPI?.CloseWorkspace(); |
|
|
|
|
if (pEnumDataset != null) |
|
|
|
|
Marshal.ReleaseComObject(pEnumDataset); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void GDBConversionMDB(string S_GDBFullPath, string E_MDBFullPath) |
|
|
|
|
{ |
|
|
|
|
WorkspaceAPI t_WsAPI = null; |
|
|
|
|
WorkspaceAPI s_WsAPI = null; |
|
|
|
|
IEnumDataset pEnumDataset = null; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
s_WsAPI = new WorkspaceAPI(S_GDBFullPath, WorkspaceTypeEnum.GDBFile, true); |
|
|
|
|
string GDBName = System.IO.Path.GetFileNameWithoutExtension(S_GDBFullPath); |
|
|
|
|
//创建mdb |
|
|
|
|
string temp = SysAppPath.GetTemplatePath() + "Temp.mdb"; |
|
|
|
|
E_MDBFullPath = System.IO.Path.Combine(E_MDBFullPath, GDBName + ".mdb"); |
|
|
|
|
if (File.Exists(temp)) |
|
|
|
|
File.Copy(temp, E_MDBFullPath, true); |
|
|
|
|
t_WsAPI = new WorkspaceAPI(E_MDBFullPath, WorkspaceTypeEnum.MDBFile, true); |
|
|
|
|
pEnumDataset = s_WsAPI.CurrentWorkspace.Datasets[esriDatasetType.esriDTFeatureDataset]; |
|
|
|
|
if (pEnumDataset != null) |
|
|
|
|
{ |
|
|
|
|
pEnumDataset.Reset(); |
|
|
|
|
IDataset pDataset = null; |
|
|
|
|
//如果数据集是IFeatureDataset,则遍历它下面的子类 |
|
|
|
|
while ((pDataset = pEnumDataset.Next()) != null) |
|
|
|
|
{ |
|
|
|
|
IDataset newDataset = null; |
|
|
|
|
newDataset = DLAECommonHelper.CreateFeatureDataset(t_WsAPI); |
|
|
|
|
if (newDataset == null) continue; |
|
|
|
|
List<string> listFeatureClass = new List<string>(); |
|
|
|
|
List<string> listFeatureClassNameList = new List<string>(); |
|
|
|
|
IEnumDataset ed = pDataset.Subsets; |
|
|
|
|
if (ed != null) |
|
|
|
|
{ |
|
|
|
|
IDataset dataset = ed.Next(); |
|
|
|
|
while (dataset != null) |
|
|
|
|
{ |
|
|
|
|
if ((dataset as IFeatureClass) != null) |
|
|
|
|
{ |
|
|
|
|
string featureClassName = dataset.BrowseName; |
|
|
|
|
if (!string.IsNullOrWhiteSpace(featureClassName) && !listFeatureClass.Contains(featureClassName)) |
|
|
|
|
{ |
|
|
|
|
listFeatureClass.Add(System.IO.Path.Combine(pDataset.Workspace.PathName.ToString(), pDataset.BrowseName, featureClassName)); |
|
|
|
|
listFeatureClassNameList.Add(featureClassName); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Marshal.FinalReleaseComObject(dataset); |
|
|
|
|
dataset = ed.Next(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (listFeatureClass.Count > 0) |
|
|
|
|
InsertMDBFeatureClass(S_GDBFullPath, listFeatureClassNameList, E_MDBFullPath, newDataset.BrowseName, null); |
|
|
|
|
if (newDataset != null) |
|
|
|
|
Marshal.FinalReleaseComObject(newDataset); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (t_WsAPI.ExistFeatureClass("MJHZJCTJB_YJJBNTTB")) |
|
|
|
|
t_WsAPI.DeleteFeatureClass("MJHZJCTJB_YJJBNTTB");//删除多余的表结构数据 |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug(ex.Message); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
t_WsAPI?.CloseWorkspace(); |
|
|
|
|
s_WsAPI?.CloseWorkspace(); |
|
|
|
|
if (pEnumDataset != null) |
|
|
|
|
Marshal.ReleaseComObject(pEnumDataset); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void MGDBConversionShape(string S_M_GDBPath, string E_ShapePath, string DataType) |
|
|
|
|
{ |
|
|
|
|
WorkspaceAPI s_WsAPI = null; |
|
|
|
|
IEnumDataset pEnumDataset = null; |
|
|
|
|
IWorkspaceFactory workFactory = null; |
|
|
|
|
IWorkspace pWorkspace = null; |
|
|
|
|
IWorkspaceAPI workspaceAPI = null; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (DataType == "MDB") |
|
|
|
|
{ |
|
|
|
|
s_WsAPI = new WorkspaceAPI(S_M_GDBPath, WorkspaceTypeEnum.MDBFile, true); |
|
|
|
|
} |
|
|
|
|
else if (DataType == "GDB") |
|
|
|
|
{ |
|
|
|
|
s_WsAPI = new WorkspaceAPI(S_M_GDBPath, WorkspaceTypeEnum.GDBFile, true); |
|
|
|
|
} |
|
|
|
|
string GDBName = System.IO.Path.GetFileNameWithoutExtension(S_M_GDBPath); |
|
|
|
|
E_ShapePath = System.IO.Path.Combine(E_ShapePath, GDBName); |
|
|
|
|
if (!Directory.Exists(E_ShapePath)) |
|
|
|
|
Directory.CreateDirectory(E_ShapePath); |
|
|
|
|
//PluginServiceInterface.CommonHelper.DelectDir(E_ShapePath); |
|
|
|
|
pEnumDataset = s_WsAPI.CurrentWorkspace.Datasets[esriDatasetType.esriDTFeatureDataset]; |
|
|
|
|
if (pEnumDataset != null) |
|
|
|
|
{ |
|
|
|
|
pEnumDataset.Reset(); |
|
|
|
|
IDataset pDataset = null; |
|
|
|
|
//如果数据集是IFeatureDataset,则遍历它下面的子类 |
|
|
|
|
while ((pDataset = pEnumDataset.Next()) != null) |
|
|
|
|
{ |
|
|
|
|
IEnumDataset ed = pDataset.Subsets; |
|
|
|
|
if (ed != null) |
|
|
|
|
{ |
|
|
|
|
IDataset dataset = ed.Next(); |
|
|
|
|
while (dataset != null) |
|
|
|
|
{ |
|
|
|
|
if ((dataset as IFeatureClass) != null) |
|
|
|
|
{ |
|
|
|
|
string featureClassName = dataset.BrowseName; |
|
|
|
|
DLAECommonHelper.DeleteShp((dataset as IFeatureClass).AliasName, E_ShapePath); |
|
|
|
|
DLAECommonHelper.ExportFeaturesToShp(dataset as IFeatureClass, null, E_ShapePath, featureClassName); |
|
|
|
|
} |
|
|
|
|
Marshal.FinalReleaseComObject(dataset); |
|
|
|
|
dataset = ed.Next(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
//若不存在数据集仅仅为要素类 |
|
|
|
|
List<IFeatureClass> featureClasses = s_WsAPI.GetAllFeatureClass(esriDatasetType.esriDTFeatureClass); |
|
|
|
|
workFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass(); |
|
|
|
|
pWorkspace = workFactory.OpenFromFile(E_ShapePath, 0); |
|
|
|
|
workspaceAPI = new WorkspaceAPI(pWorkspace); |
|
|
|
|
foreach (IFeatureClass item in featureClasses) |
|
|
|
|
{ |
|
|
|
|
if (!workspaceAPI.ExistFeatureClass((item as IDataset).Name)) |
|
|
|
|
{ |
|
|
|
|
IFeatureClassAPI featureTemp = s_WsAPI.OpenFeatureClass2((item as IDataset).Name); |
|
|
|
|
if (featureTemp == null) continue; |
|
|
|
|
DLAECommonHelper.DeleteShp(item.AliasName, E_ShapePath); |
|
|
|
|
IFeatureClassAPI TempFL = workspaceAPI.CreateFeatureClass((item as IDataset).Name, (featureTemp.FeatureClass as IGeoDataset).SpatialReference, featureTemp.FeatureClass.Fields); |
|
|
|
|
featureTemp.FcToFc(TempFL.FeatureClass, null, true); |
|
|
|
|
featureTemp?.CloseFeatureClass(); |
|
|
|
|
TempFL?.CloseFeatureClass(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw ex; |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
if (pEnumDataset != null) |
|
|
|
|
Marshal.ReleaseComObject(pEnumDataset); |
|
|
|
|
if (pWorkspace != null) |
|
|
|
|
Marshal.ReleaseComObject(pWorkspace); |
|
|
|
|
s_WsAPI?.CloseWorkspace(); |
|
|
|
|
workspaceAPI?.CloseWorkspace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 从MDB中拷贝要素类插入到GDB |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="sFromGDBPath">GDB地址</param> |
|
|
|
|
/// <param name="iFeatureClassNameList">GDB中要拷贝的要素类名称列表</param> |
|
|
|
|
/// <param name="iOutPutMDBPath">要插入到的MDB的地址路径</param> |
|
|
|
|
/// <param name="sOutPutDataSetName">输出的数据集的名字</param> |
|
|
|
|
public void InsertGDBFeatureClass(string sFromGDBPath, List<string> iFeatureClassNameList, string iOutPutMDBPath, string sOutPutDataSetName) |
|
|
|
|
{ |
|
|
|
|
IFeatureWorkspace fws_gdb = null; |
|
|
|
|
WorkspaceAPI wsapi_gdb = null; |
|
|
|
|
IFeatureClassAPI fcapi_gdb = null; |
|
|
|
|
IFeatureClass fc_gdb = null; |
|
|
|
|
IFeatureClass newFeatureClass = null; |
|
|
|
|
IFeatureWorkspace fws_mdb = null; |
|
|
|
|
|
|
|
|
|
int iAddSuccessCount = 0; |
|
|
|
|
int iAddFailCount = 0; |
|
|
|
|
int iFCToFCType = 0;//fc2fc类型(0不涉及到数据集的方式 1涉及到数据集的方式) |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
iFCToFCType = string.IsNullOrWhiteSpace(sOutPutDataSetName) == true ? 0 : 1; |
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(sFromGDBPath) == true) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug("从MDB中拷贝要素类插入到GDB中 时失败,原因: MDB地址 为空 ; "); |
|
|
|
|
throw new Exception("GDB地址 不能为空"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (iFeatureClassNameList == null || iFeatureClassNameList.Count <= 0) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug("从MDB中拷贝要素类插入到GDB中 时失败,原因: MDB中要拷贝的要素类名称列表 为空 ; "); |
|
|
|
|
throw new Exception("要转换的要素类 不能为空"); |
|
|
|
|
} |
|
|
|
|
wsapi_gdb = new WorkspaceAPI(sFromGDBPath, WorkspaceTypeEnum.MDBFile, true); |
|
|
|
|
fws_gdb = wsapi_gdb.CurrentWorkspace as IFeatureWorkspace; |
|
|
|
|
IWorkspace ws_gdb = fws_gdb as IWorkspace; |
|
|
|
|
WorkspaceAPI wsapi_mdb = new WorkspaceAPI(iOutPutMDBPath, WorkspaceTypeEnum.GDBFile, true); |
|
|
|
|
fws_mdb = wsapi_mdb.CurrentWorkspace as IFeatureWorkspace; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < iFeatureClassNameList.Count; i++) |
|
|
|
|
{ |
|
|
|
|
fcapi_gdb = wsapi_gdb.OpenFeatureClass(iFeatureClassNameList[i]); |
|
|
|
|
if (fcapi_gdb != null) |
|
|
|
|
{ |
|
|
|
|
fc_gdb = fcapi_gdb.FeatureClass; |
|
|
|
|
IGeoDataset geoDataset = fc_gdb as IGeoDataset; |
|
|
|
|
bool bFC2FCResult_Temp; |
|
|
|
|
if (iFCToFCType == 1)//fc2fc类型:1涉及到数据集的方式; |
|
|
|
|
{ |
|
|
|
|
bFC2FCResult_Temp = MDBFc2FcGDB(fc_gdb, fws_mdb as IWorkspace, iFeatureClassNameList[i], sOutPutDataSetName, null); |
|
|
|
|
#region 处理坐标参考系 |
|
|
|
|
IFeatureDataset dataSet = fws_mdb.OpenFeatureDataset(sOutPutDataSetName); |
|
|
|
|
ISpatialReference sp = geoDataset.SpatialReference; |
|
|
|
|
IGeoDataset pGeoDataset = dataSet as IGeoDataset; |
|
|
|
|
IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit; |
|
|
|
|
if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true) |
|
|
|
|
pGeoDatasetSchemaEdit.AlterSpatialReference(sp); |
|
|
|
|
Marshal.ReleaseComObject(dataSet); |
|
|
|
|
Marshal.ReleaseComObject(pGeoDataset); |
|
|
|
|
#endregion |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
bFC2FCResult_Temp = MDBFc2FcGDB(fc_gdb, fws_mdb as IWorkspace, iFeatureClassNameList[i]); |
|
|
|
|
#region 处理坐标参考系 |
|
|
|
|
newFeatureClass = fws_mdb.OpenFeatureClass(iFeatureClassNameList[i]); |
|
|
|
|
IGeoDataset gdataset = newFeatureClass as IGeoDataset; |
|
|
|
|
if (gdataset != null) |
|
|
|
|
{ |
|
|
|
|
IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = gdataset as IGeoDatasetSchemaEdit; |
|
|
|
|
if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true) |
|
|
|
|
pGeoDatasetSchemaEdit.AlterSpatialReference(geoDataset.SpatialReference); |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
Marshal.ReleaseComObject(gdataset); |
|
|
|
|
} |
|
|
|
|
Marshal.ReleaseComObject(geoDataset); |
|
|
|
|
if (bFC2FCResult_Temp == true) |
|
|
|
|
iAddSuccessCount += 1; |
|
|
|
|
else |
|
|
|
|
iAddFailCount += 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug("将要素类拷贝至GDB中失败,异常原因: " + ex + " ; "); |
|
|
|
|
LogAPI.DebugRunLog(ex, "将要素类拷贝至GDB中失败,异常原因: ", "InsertGDBFeatureClass"); |
|
|
|
|
throw new Exception("GDB转化成MDB地址失败"); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
if (fws_gdb != null) |
|
|
|
|
Marshal.ReleaseComObject(fws_gdb); |
|
|
|
|
wsapi_gdb?.CloseWorkspace(); |
|
|
|
|
fcapi_gdb?.CloseFeatureClass(); |
|
|
|
|
if (fc_gdb != null) |
|
|
|
|
Marshal.ReleaseComObject(fc_gdb); |
|
|
|
|
if (fws_mdb != null) |
|
|
|
|
{ |
|
|
|
|
Marshal.ReleaseComObject(fws_mdb); |
|
|
|
|
fws_mdb = null; |
|
|
|
|
} |
|
|
|
|
if (newFeatureClass != null) |
|
|
|
|
Marshal.ReleaseComObject(newFeatureClass); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 从GDB中拷贝要素类插入到MDB中 2020-6-1 沈超 |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="sFromGDBPath">GDB地址</param> |
|
|
|
|
/// <param name="iFeatureClassNameList">GDB中要拷贝的要素类名称列表</param> |
|
|
|
|
/// <param name="iOutPutMDBPath">要插入到的MDB的地址路径</param> |
|
|
|
|
/// <param name="sOutPutDataSetName">输出的数据集的名字</param> |
|
|
|
|
/// <param name="sSpatialReference">输出坐标参考:如果不是要素集下的图层,则要赋坐标参考</param> |
|
|
|
|
public void InsertMDBFeatureClass(string sFromGDBPath, List<string> iFeatureClassNameList, string iOutPutMDBPath, string sOutPutDataSetName, string sSpatialReference = null) |
|
|
|
|
{ |
|
|
|
|
IFeatureWorkspace fws_gdb = null; |
|
|
|
|
IWorkspace ws_gdb = null; |
|
|
|
|
WorkspaceAPI wsapi_gdb = null; |
|
|
|
|
IFeatureClassAPI fcapi_gdb = null; |
|
|
|
|
IFeatureClass fc_gdb = null; |
|
|
|
|
IFeatureClass newFeatureClass = null; |
|
|
|
|
IFeatureWorkspace fws_mdb = null; |
|
|
|
|
int iAddSuccessCount = 0; |
|
|
|
|
int iAddFailCount = 0; |
|
|
|
|
bool bFC2FCResult_Temp = false; |
|
|
|
|
int iFCToFCType = 0;//fc2fc类型(0不涉及到数据集的方式 1涉及到数据集的方式) |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
iFCToFCType = string.IsNullOrWhiteSpace(sOutPutDataSetName) == true ? 0 : 1; |
|
|
|
|
if (string.IsNullOrWhiteSpace(sFromGDBPath) == true) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug("从GDB中拷贝要素类插入到MDB中 时失败,原因: GDB地址 为空 ; "); |
|
|
|
|
throw new Exception("GDB地址 不能为空"); |
|
|
|
|
} |
|
|
|
|
if (iFeatureClassNameList == null || iFeatureClassNameList.Count <= 0) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug("从GDB中拷贝要素类插入到MDB中 时失败,原因: GDB中要拷贝的要素类名称列表 为空 ; "); |
|
|
|
|
throw new Exception("要转换的要素类 不能为空"); |
|
|
|
|
} |
|
|
|
|
WorkspaceAPI wsapi_MDB = new WorkspaceAPI(iOutPutMDBPath, WorkspaceTypeEnum.MDBFile, true); |
|
|
|
|
fws_mdb = wsapi_MDB.CurrentWorkspace as IFeatureWorkspace; |
|
|
|
|
if (fws_mdb == null) |
|
|
|
|
throw new Exception("MDB地址 不能为空"); |
|
|
|
|
wsapi_gdb = new WorkspaceAPI(sFromGDBPath, WorkspaceTypeEnum.GDBFile, true); |
|
|
|
|
fws_gdb = wsapi_gdb.CurrentWorkspace as IFeatureWorkspace; |
|
|
|
|
ws_gdb = fws_gdb as IWorkspace; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < iFeatureClassNameList.Count; i++) |
|
|
|
|
{ |
|
|
|
|
fcapi_gdb = wsapi_gdb.OpenFeatureClass(iFeatureClassNameList[i]); |
|
|
|
|
if (fcapi_gdb != null) |
|
|
|
|
{ |
|
|
|
|
fc_gdb = fcapi_gdb.FeatureClass; |
|
|
|
|
IGeoDataset geoDataset = fc_gdb as IGeoDataset; |
|
|
|
|
if (iFCToFCType == 1)//fc2fc类型:1涉及到数据集的方式; |
|
|
|
|
{ |
|
|
|
|
bFC2FCResult_Temp = GDBFc2FcMDB(fc_gdb, fws_mdb as IWorkspace, iFeatureClassNameList[i], sOutPutDataSetName, null); |
|
|
|
|
#region 处理坐标参考系 |
|
|
|
|
IFeatureDataset dataSet = fws_mdb.OpenFeatureDataset(sOutPutDataSetName); |
|
|
|
|
ISpatialReference sp = geoDataset.SpatialReference; |
|
|
|
|
IGeoDataset pGeoDataset = dataSet as IGeoDataset; |
|
|
|
|
IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = pGeoDataset as IGeoDatasetSchemaEdit; |
|
|
|
|
if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true) |
|
|
|
|
pGeoDatasetSchemaEdit.AlterSpatialReference(sp); |
|
|
|
|
Marshal.ReleaseComObject(dataSet); |
|
|
|
|
Marshal.ReleaseComObject(pGeoDataset); |
|
|
|
|
#endregion |
|
|
|
|
} |
|
|
|
|
else//fc2fc类型:0不涉及到数据集的方式; |
|
|
|
|
{ |
|
|
|
|
bFC2FCResult_Temp = GDBFc2FcMDB(fc_gdb, fws_mdb as IWorkspace, iFeatureClassNameList[i], null); |
|
|
|
|
#region 处理坐标参考系 |
|
|
|
|
newFeatureClass = fws_mdb.OpenFeatureClass(iFeatureClassNameList[i]); |
|
|
|
|
IGeoDataset gdataset = newFeatureClass as IGeoDataset; |
|
|
|
|
if (gdataset != null) |
|
|
|
|
{ |
|
|
|
|
IGeoDatasetSchemaEdit pGeoDatasetSchemaEdit = gdataset as IGeoDatasetSchemaEdit; |
|
|
|
|
if (pGeoDatasetSchemaEdit.CanAlterSpatialReference == true) |
|
|
|
|
pGeoDatasetSchemaEdit.AlterSpatialReference(geoDataset.SpatialReference); |
|
|
|
|
} |
|
|
|
|
#endregion |
|
|
|
|
Marshal.ReleaseComObject(gdataset); |
|
|
|
|
} |
|
|
|
|
Marshal.ReleaseComObject(geoDataset); |
|
|
|
|
|
|
|
|
|
if (bFC2FCResult_Temp == true) |
|
|
|
|
{ |
|
|
|
|
iAddSuccessCount += 1; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
iAddFailCount += 1; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
LogAPI.Debug("将要素类拷贝至MDB中失败,异常原因: " + ex + " ; "); |
|
|
|
|
throw new Exception("GDB转化成MDB地址失败"); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
if (fws_gdb != null) |
|
|
|
|
{ |
|
|
|
|
Marshal.ReleaseComObject(fws_gdb); |
|
|
|
|
fws_gdb = null; |
|
|
|
|
} |
|
|
|
|
wsapi_gdb?.CloseWorkspace(); |
|
|
|
|
fcapi_gdb?.CloseFeatureClass(); |
|
|
|
|
|
|
|
|
|
if (fc_gdb != null) |
|
|
|
|
{ |
|
|
|
|
Marshal.ReleaseComObject(fc_gdb); |
|
|
|
|
fc_gdb = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (fws_mdb != null) |
|
|
|
|
{ |
|
|
|
|
Marshal.ReleaseComObject(fws_mdb); |
|
|
|
|
fws_mdb = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (newFeatureClass != null) |
|
|
|
|
{ |
|
|
|
|
Marshal.ReleaseComObject(newFeatureClass); |
|
|
|
|
newFeatureClass = null; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public bool MDBFc2FcGDB(IFeatureClass pInFeatureClass, IWorkspace pOutWorkspace, string poutFatureName, string pOutDataSetName = null, IQueryFilter queryFilter = null) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (pInFeatureClass == null || pOutWorkspace == null || string.IsNullOrEmpty(poutFatureName)) return false; |
|
|
|
|
IFeatureClassName pInFeatureclassName; |
|
|
|
|
IDataset pIndataset; |
|
|
|
|
pIndataset = (IDataset)pInFeatureClass; |
|
|
|
|
pInFeatureclassName = (IFeatureClassName)pIndataset.FullName; |
|
|
|
|
IFeatureClassName pOutFeatureClassName; |
|
|
|
|
IDatasetName pOutDatasetName; |
|
|
|
|
IDataset pOutDataset; |
|
|
|
|
pOutDataset = (IDataset)pOutWorkspace; |
|
|
|
|
IEnumDatasetName dataset = pOutWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset); |
|
|
|
|
while ((pOutDatasetName = dataset.Next()) != null) |
|
|
|
|
{ |
|
|
|
|
if (pOutDatasetName.Name == pOutDataSetName) |
|
|
|
|
{ |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
pOutFeatureClassName = new FeatureClassNameClass() { Name = poutFatureName }; |
|
|
|
|
if (pOutDatasetName == null) |
|
|
|
|
{ |
|
|
|
|
pOutDatasetName = (IDatasetName)pOutFeatureClassName; |
|
|
|
|
pOutDatasetName.WorkspaceName = (IWorkspaceName)pOutDataset.FullName; |
|
|
|
|
pOutDatasetName.Name = poutFatureName; |
|
|
|
|
} |
|
|
|
|
pOutFeatureClassName.FeatureDatasetName = pOutDatasetName; |
|
|
|
|
IFields pInFields; |
|
|
|
|
IFieldChecker pFieldChecker = new FieldCheckerClass(); |
|
|
|
|
pInFields = pInFeatureClass.Fields; |
|
|
|
|
|
|
|
|
|
//pOutFields |
|
|
|
|
pFieldChecker.Validate(pInFields, out IEnumFieldError pError, out IFields pOutFields); |
|
|
|
|
Console.WriteLine(pError); |
|
|
|
|
IFeatureDataConverter one2another = new FeatureDataConverterClass(); |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (pOutWorkspace.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace) |
|
|
|
|
{ |
|
|
|
|
one2another.ConvertFeatureClass(pInFeatureclassName, null, null, pOutFeatureClassName, null, pOutFields, "", 1000, 0); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
one2another.ConvertFeatureClass(pInFeatureclassName, queryFilter, pOutDatasetName as IFeatureDatasetName, pOutFeatureClassName, null, pOutFields, "", 1000, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
Marshal.ReleaseComObject(one2another); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw ex; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public bool GDBFc2FcMDB(IFeatureClass pInFeatureClass, IWorkspace pOutWorkspace, string poutFatureName, string pOutDataSetName = null, IQueryFilter queryFilter = null) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (pInFeatureClass == null || pOutWorkspace == null || string.IsNullOrEmpty(poutFatureName)) return false; |
|
|
|
|
IFeatureClassName pInFeatureclassName; |
|
|
|
|
IDataset pIndataset; |
|
|
|
|
pIndataset = (IDataset)pInFeatureClass; |
|
|
|
|
pInFeatureclassName = (IFeatureClassName)pIndataset.FullName; |
|
|
|
|
IFeatureClassName pOutFeatureClassName; |
|
|
|
|
IDatasetName pOutDatasetName; |
|
|
|
|
IDataset pOutDataset; |
|
|
|
|
pOutDataset = (IDataset)pOutWorkspace; |
|
|
|
|
IEnumDatasetName dataset = pOutWorkspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset); |
|
|
|
|
while ((pOutDatasetName = dataset.Next()) != null) |
|
|
|
|
{ |
|
|
|
|
if (pOutDatasetName.Name == pOutDataSetName) |
|
|
|
|
{ |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
pOutFeatureClassName = new FeatureClassNameClass() { Name = poutFatureName }; |
|
|
|
|
if (pOutDatasetName == null) |
|
|
|
|
{ |
|
|
|
|
pOutDatasetName = (IDatasetName)pOutFeatureClassName; |
|
|
|
|
pOutDatasetName.WorkspaceName = (IWorkspaceName)pOutDataset.FullName; |
|
|
|
|
pOutDatasetName.Name = poutFatureName; |
|
|
|
|
} |
|
|
|
|
pOutFeatureClassName.FeatureDatasetName = pOutDatasetName; |
|
|
|
|
IFields pInFields, pOutFields; |
|
|
|
|
IFieldChecker pFieldChecker = new FieldCheckerClass(); |
|
|
|
|
IEnumFieldError pError; |
|
|
|
|
pInFields = pInFeatureClass.Fields; |
|
|
|
|
|
|
|
|
|
//pOutFields |
|
|
|
|
pFieldChecker.Validate(pInFields, out pError, out pOutFields); |
|
|
|
|
Console.WriteLine(pError); |
|
|
|
|
IFeatureDataConverter one2another = new FeatureDataConverterClass(); |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (pOutWorkspace.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace) |
|
|
|
|
{ |
|
|
|
|
one2another.ConvertFeatureClass(pInFeatureclassName, null, null, pOutFeatureClassName, null, |
|
|
|
|
pOutFields, "", 1000, 0); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
one2another.ConvertFeatureClass(pInFeatureclassName, queryFilter, pOutDatasetName as IFeatureDatasetName, pOutFeatureClassName, null, pOutFields, "", 1000, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
Marshal.ReleaseComObject(one2another); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw ex; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void BtnClose_Click(object sender, RoutedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
this.Close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |