using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Geodatabase;
using KGIS.Framework.AE;
using KGIS.Framework.AE.Enum;
using KGIS.Framework.Platform;
using KGIS.Framework.Platform.Helper;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
namespace Kingo.Plugin.YJJK.ModelEntity
{
public class ExportMDB
{
public string OutPath;
string sourcePath;
private IFeatureClass _SourceFeatureClass;
///
/// 数据源
///
public IFeatureClass SourceFeatureClass
{
get { return _SourceFeatureClass; }
set { _SourceFeatureClass = value; }
}
///
/// 导出MDB成果
///
/// 是否省级数据
/// 指定的MDB数据源
public void Export(bool isSJSJ = false,string sTheMDBPath = "")
{
IWorkspaceAPI t_WsAPI = null;
IWorkspaceAPI s_WsAPI = null;
IFeatureClassAPI s_FCAPI = null;
if (string.IsNullOrWhiteSpace(OutPath))
{
return;
}
//测试用路径
//targetPath = @"F:\测试工程\9.3矢量化8.28\MDB成果导出文件";
if (isSJSJ)
{
//校验模板文件是否存在
if (!Check_SJSJ_TemplateFile())
{
//MessageBox.Show("校验模板文件出错,请检查。", "提示");
return;
}
}
else
{
//校验模板文件是否存在
if (!CheckTemplateFile())
{
//MessageBox.Show("校验模板文件出错,请检查。", "提示");
return;
}
}
//sourcePath="D:\三调建库软件\Apps\工作空间\模板\新建数据库工程\导出成果数据.mdb";
//targetPath="E:\20180416163805成果数据.mdb";
File.Copy(sourcePath, OutPath);
try
{
string s_MdbPath = OutPath;
string t_MdbPath = "";// Env.Instance.Project.GetProjMDBPath();
if (string.IsNullOrWhiteSpace(sTheMDBPath) == false)
{
t_MdbPath = sTheMDBPath;
}
if (s_MdbPath.Equals(t_MdbPath))
{
//MessageBox.Show("数据源不能与目标源一致!");
return;
}
s_WsAPI = new WorkspaceAPI(s_MdbPath, WorkspaceTypeEnum.MDBFile);
t_WsAPI = new WorkspaceAPI(t_MdbPath, WorkspaceTypeEnum.MDBFile);
Dictionary t_TbList = t_WsAPI.GetFeatureClassName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
Dictionary t_TbList2 = t_WsAPI.GetFeatureClassName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass);
Dictionary t_TbList3 = t_WsAPI.GetFeatureClassName(esriDatasetType.esriDTTable);
#region 将目标图层作为字典项可进行下拉选择
List dicTable = new List();
dicTable.Add(new DataDicTionary() { NAME = "Null", CODE = "null" });
dicTable.Add(new DataDicTionary() { NAME = "追加图层", CODE = "Add" });
foreach (string item in t_TbList.Keys)
{
s_FCAPI = t_WsAPI.OpenFeatureClass(t_TbList[item]);
dicTable.Add(new DataDicTionary() { NAME = item, CODE = t_TbList[item], REMARK = s_FCAPI.FeatureClass.ShapeType.ToString() });
}
foreach (string item in t_TbList2.Keys)
{
s_FCAPI = t_WsAPI.OpenFeatureClass(t_TbList2[item]);
dicTable.Add(new DataDicTionary() { NAME = item, CODE = t_TbList2[item], REMARK = s_FCAPI.FeatureClass.ShapeType.ToString() });
}
foreach (string item in t_TbList3.Keys)
{
dicTable.Add(new DataDicTionary() { NAME = item, CODE = t_TbList3[item], REMARK = "Table" });
}
#endregion
Dictionary s_TbList = s_WsAPI.GetFeatureClassName(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
Dictionary s_TbList2 = s_WsAPI.GetFeatureClassName(esriDatasetType.esriDTFeatureClass);
Dictionary s_TbList3 = s_WsAPI.GetFeatureClassName(esriDatasetType.esriDTTable);
foreach (string item in s_TbList2.Keys)
{
if (!s_TbList.Keys.Contains(item))
s_TbList.Add(item, s_TbList2[item]);
}
foreach (string item in s_TbList3.Keys)
{
if (!s_TbList.Keys.Contains(item))
s_TbList.Add(item, s_TbList3[item]);
}
if (t_TbList != null && s_TbList != null)
{
#region 获取MappingList
List MappingList = new List();
foreach (string key in s_TbList.Keys)
{
TableMapping mapping = new TableMapping();
//mapping.PropertyChanged2 += mapping_PropertyChanged2;
//mapping.State = TBState.Waiting;
mapping.S_TableName = s_TbList[key];
mapping.S_TableAliasName = key;
if (s_TbList3.Values.Contains(mapping.S_TableName))
{
mapping.S_TableType = "Table";
}
else
{
s_FCAPI = s_WsAPI.OpenFeatureClass(s_TbList[key]);
mapping.S_Spatial = (s_FCAPI.FeatureClass as IGeoDataset).SpatialReference.Name;
mapping.S_TableType = s_FCAPI.FeatureClass.ShapeType.ToString();
s_FCAPI.CloseFeatureClass();
}
if (key == "注记")
{
List dicTionaries = dicTable.FindAll(x => x.NAME.Substring(x.NAME.Length - 2, 2) == key);
foreach (DataDicTionary dataDic in dicTionaries)
{
TableMapping mappingNew = new TableMapping();
mappingNew.S_TableName = mapping.S_TableName;
mappingNew.S_TableAliasName = mapping.S_TableAliasName;
mappingNew.S_TableType = mapping.S_TableType;
mappingNew.S_Spatial = mapping.S_Spatial;
mappingNew.T_TableAliasName = dataDic.NAME;
mappingNew.T_TableName = dataDic.CODE;
mappingNew.T_TableType = dataDic.REMARK;
mappingNew.T_TableList = dicTable.Where(w => w.REMARK == mappingNew.S_TableType || w.CODE == "null" || w.CODE == "Add").ToList();
//SetDefaultFieldMapping(mappingNew, s_WsAPI, t_WsAPI);
/*
* s_WsAPI 目标空间
* t_WsAPI 源数据空间
*/
//修改人:李忠盼 修改时间:20190307 导出注记图层无数据问题处理
//SetDefaultFieldMapping(mappingNew, t_WsAPI, s_WsAPI);
SetDefaultFieldMapping(mappingNew, s_WsAPI, t_WsAPI);
MappingList.Add(mappingNew);
}
continue;
}
DataDicTionary dic = dicTable.FirstOrDefault(f => f.NAME == key || f.CODE == s_TbList[key]);
if (dic != null)
{
mapping.T_TableAliasName = dic.NAME;
mapping.T_TableName = dic.CODE;
mapping.T_TableType = dic.REMARK;
}
mapping.T_TableList = dicTable.Where(w => w.REMARK == mapping.S_TableType || w.CODE == "null" || w.CODE == "Add").ToList();
SetDefaultFieldMapping(mapping, t_WsAPI, s_WsAPI,isSJSJ);
MappingList.Add(mapping);
}
#endregion
#region 设置目标数据库坐标参考
ESRI.ArcGIS.Geometry.ISpatialReference sp = GeoDBAPI.GetGeoDatasetSpatialReference(t_WsAPI.CurrentWorkspace);
if (sp != null)
{
GeoDBAPI.SetGeoDatasetSpatialReference(s_WsAPI.CurrentWorkspace, sp, 0.0001);
}
#endregion
#region 对里面的单表进行字段操作
int i = 0;
ProgressHelper.ShowProcessBar("正在导出数据,请等待……");
int Count = MappingList.Count;
ProgressHelper.CountProgress = Count;
foreach (TableMapping tableMap in MappingList)
{
IFeatureClass t_FC = null;
ITable it_FC = null;
ITable iSourceFeatureClass = null;
if (tableMap.S_TableType == "Table")
{
it_FC = s_WsAPI.OpenTable(tableMap.S_TableName).ITable;
iSourceFeatureClass = t_WsAPI.OpenTable(tableMap.T_TableName).ITable;
using (ComReleaser releaser = new ComReleaser())
{
//字段映射信息
List mFieldMapping = tableMap.FieldMapping;
ICursor cursor = it_FC.Insert(true);
IRowBuffer newFeature = null;
releaser.ManageLifetime(cursor);
ICursor s_Cursor = iSourceFeatureClass.Search(null, false);
releaser.ManageLifetime(s_Cursor);
IRow feature = null;
while ((feature = s_Cursor.NextRow()) != null)
{
if (ProgressHelper.IsCancel)
{
break;
}
if (newFeature == null)
newFeature = it_FC.CreateRowBuffer();
//newFeature.Shape = feature.ShapeCopy;
foreach (FieldMapping item in mFieldMapping)
{
if (item.S_FieldIndex < 0 || item.T_FieldIndex < 0)
continue;
newFeature.set_Value(item.T_FieldIndex, feature.get_Value(item.S_FieldIndex));
}
cursor.InsertRow(newFeature);
}
if (newFeature != null)
cursor.Flush();
}
}
else
{
t_FC = s_WsAPI.OpenFeatureClass(tableMap.S_TableName).FeatureClass;
SourceFeatureClass = t_WsAPI.OpenFeatureClass(tableMap.T_TableName).FeatureClass;
using (ComReleaser releaser = new ComReleaser())
{
//字段映射信息
List mFieldMapping = tableMap.FieldMapping;
IFeatureCursor cursor = t_FC.Insert(true);
IFeatureBuffer newFeature = null;
releaser.ManageLifetime(cursor);
IFeatureCursor s_Cursor = SourceFeatureClass.Search(null, false);
releaser.ManageLifetime(s_Cursor);
IFeature feature = null;
while ((feature = s_Cursor.NextFeature()) != null)
{
if (ProgressHelper.IsCancel)
{
break;
}
if (newFeature == null)
newFeature = t_FC.CreateFeatureBuffer();
newFeature.Shape = feature.ShapeCopy;
if (mFieldMapping != null)
{
foreach (FieldMapping item in mFieldMapping)//mFieldMapping为空不循环 20190304 周旺华修复
{
if (item.S_FieldIndex < 0 || item.T_FieldIndex < 0)
continue;
newFeature.set_Value(item.T_FieldIndex, feature.get_Value(item.S_FieldIndex));
}
}
cursor.InsertFeature(newFeature);
}
if (newFeature != null)
cursor.Flush();
IFeatureClassManage fcManage = t_FC as IFeatureClassManage;
fcManage.UpdateExtent();
}
}
ProgressHelper.CurrentProgress = i++;
}
ProgressHelper.CloseProcessBar();
//MessageBox.Show("数据导出成功!", "提示");
//LogAPI.Debug("数据导出成功!");//测试用 王欢
#endregion
}
}
//判断异常错误 提示框,重置
catch (Exception ex)
{
ProgressHelper.CloseProcessBar();
MessageHelper.ShowError(ex.Message);
LogAPI.Debug(ex);
}
finally
{
if (s_WsAPI != null)
s_WsAPI.CloseWorkspace();
//if (t_WsAPI != null)
// t_WsAPI.CloseWorkspace();
}
}
public void SetDefaultFieldMapping(TableMapping tbMapping, IWorkspaceAPI pSourceWsAPI, IWorkspaceAPI pTargetWsAPI,bool isSJSJ=false)
{
bool IsSReleaser = false;
bool IsTReleaser = false;
if (tbMapping.T_TableName == "Add")
{
return;
}
if (pSourceWsAPI == null)
{
string s_MdbPath = sourcePath;
pSourceWsAPI = new WorkspaceAPI(s_MdbPath, WorkspaceTypeEnum.MDBFile);
IsSReleaser = true;
}
if (pTargetWsAPI == null)
{
string t_MdbPath = "";// Env.Instance.Project.GetProjMDBPath();
pTargetWsAPI = new WorkspaceAPI(t_MdbPath,WorkspaceTypeEnum.MDBFile);
IsTReleaser = true;
}
IFeatureClassAPI t_FcAPI = null;
ITableAPI t_TBAPI = null;
IFeatureClassAPI s_FcAPI = null;
ITableAPI s_TBAPI = null;
try
{
if (!string.IsNullOrWhiteSpace(tbMapping.S_TableName) && !string.IsNullOrWhiteSpace(tbMapping.T_TableName) && !tbMapping.T_TableName.ToUpper().Equals("NULL"))
{
if (tbMapping.T_TableType.ToUpper() == "TABLE")
{
t_TBAPI = pTargetWsAPI.OpenTable(tbMapping.T_TableName);
}
else
{
t_FcAPI = pTargetWsAPI.OpenFeatureClass(tbMapping.T_TableName);
}
if (tbMapping.S_TableType.ToUpper() == "TABLE")
{
s_TBAPI = pTargetWsAPI.OpenTable(tbMapping.S_TableName);
}
else
{
s_FcAPI = pSourceWsAPI.OpenFeatureClass(tbMapping.S_TableName);
}
List dicData = new List();
dicData.Add(new DataDicTionary() { DisplayName = "Null", NAME = "Null", CODE = "-1", REMARK = "" });
dicData.Add(new DataDicTionary() { DisplayName = "追加字段", NAME = "Add", CODE = "-2", REMARK = "" });
if (t_FcAPI != null)
{
for (int i = 0; i < t_FcAPI.FeatureClass.Fields.FieldCount; i++)
{
IField field = t_FcAPI.FeatureClass.Fields.get_Field(i);
if (!field.Editable || field.Name.ToUpper() == "SHAPE")
continue;
dicData.Add(new DataDicTionary() { DisplayName = string.IsNullOrWhiteSpace(field.AliasName) ? field.Name : field.AliasName, NAME = field.Name, CODE = i.ToString(), REMARK = field.Type.ToString() });
}
}
else if (t_TBAPI != null)
{
for (int i = 0; i < t_TBAPI.ITable.Fields.FieldCount; i++)
{
IField field = t_TBAPI.ITable.Fields.get_Field(i);
if (!field.Editable || field.Name.ToUpper() == "SHAPE")
continue;
dicData.Add(new DataDicTionary() { DisplayName = string.IsNullOrWhiteSpace(field.AliasName) ? field.Name : field.AliasName, NAME = field.Name, CODE = i.ToString(), REMARK = field.Type.ToString() });
}
}
if (s_FcAPI != null && s_FcAPI.FeatureClass != null)
{
List data = new List();
for (int i = 0; i < s_FcAPI.FeatureClass.Fields.FieldCount; i++)
{
IField field = s_FcAPI.FeatureClass.Fields.get_Field(i);
if (!field.Editable || field.Name.ToUpper() == "SHAPE")
continue;
FieldMapping item = new FieldMapping();
item.S_FieldName = string.IsNullOrWhiteSpace(field.AliasName) ? field.Name : field.AliasName;
item.S_FieldIndex = i;
DataDicTionary dic = dicData.FirstOrDefault(f => (f.NAME == field.Name || f.DisplayName == field.Name || f.NAME == field.AliasName || f.DisplayName == field.AliasName));//&& f.REMARK == field.Type.ToString()
//白明雅 2019-05-30 将地类图斑备注导到SJBZ字段里
if (isSJSJ && (item.S_FieldName.ToString().ToUpper()=="BZ".ToUpper() || item.S_FieldName.ToString() == "备注") && tbMapping.T_TableName=="DLTB")
{
dic = dicData.FirstOrDefault(f => (f.NAME == "SJBZ" || f.DisplayName == "SJBZ" || f.NAME == "SJBZ" || f.DisplayName == "SJBZ"));
}
//---------------------------------
if (dic != null)
{
item.T_FieldName = dic.DisplayName;
item.T_FieldIndex = Convert.ToInt16(dic.CODE);
}
else
{
item.T_FieldIndex = -1;
}
//item.PropertyType = enumPropertyType.ComboBox;
item.FieldList = dicData.FindAll(f => f.REMARK == field.Type.ToString() || f.REMARK.Contains("Double") || f.REMARK.Contains("Integer") || f.CODE == "-1" || f.CODE == "-2");
data.Add(item);
}
tbMapping.FieldMapping = data;
}
else if (s_TBAPI != null && s_TBAPI.ITable != null)
{
List data = new List();
for (int i = 0; i < s_TBAPI.ITable.Fields.FieldCount; i++)
{
IField field = s_TBAPI.ITable.Fields.get_Field(i);
if (!field.Editable || field.Name.ToUpper() == "SHAPE")
continue;
FieldMapping item = new FieldMapping();
item.S_FieldName = string.IsNullOrWhiteSpace(field.AliasName) ? field.Name : field.AliasName;
item.S_FieldIndex = i;
DataDicTionary dic = dicData.FirstOrDefault(f => (f.NAME == field.Name || f.DisplayName == field.Name || f.NAME == field.AliasName || f.DisplayName == field.AliasName));//&& f.REMARK == field.Type.ToString()
if (dic != null)
{
item.T_FieldName = dic.DisplayName;
item.T_FieldIndex = Convert.ToInt16(dic.CODE);
}
else
{
item.T_FieldIndex = -1;
}
//item.PropertyType = enumPropertyType.ComboBox;
item.FieldList = dicData.FindAll(f => f.REMARK == field.Type.ToString() || f.CODE == "-1" || f.CODE == "-2");
data.Add(item);
}
tbMapping.FieldMapping = data;
}
}
}
catch (Exception ex)
{
//MessageBox.Show("操作异常!" + ex.Message);
LogAPI.Debug(ex);
}
}
private bool CheckTemplateFile()
{
bool result = false;
sourcePath = SysAppPath.GetCurrentAppPath() + "\\工作空间\\模板\\新建数据库工程\\导出成果数据.mdb";
if (File.Exists(sourcePath))
{
result = true;
}
return result;
}
private bool Check_SJSJ_TemplateFile()
{
bool result = false;
sourcePath = SysAppPath.GetCurrentAppPath() + "\\工作空间\\模板\\新建数据库工程\\导出成果数据_省级数据.mdb";
if (File.Exists(sourcePath))
{
result = true;
}
return result;
}
}
///
/// 表映射关系
///
public class TableMapping : INotifyPropertyChanged
{
///
/// 源数据表名
///
public string S_TableName { get; set; }
///
/// 源数据表名
///
public string S_TableAliasName { get; set; }
///
/// 数据源表类型
///
public string S_TableType { get; set; }
///
/// 坐标系
///
public string S_Spatial { get; set; }
/////
///// 数据源要素类型
/////
//public string S_ShapeType { get; set; }
/////
///// 目标要素类型
/////
//public string T_ShapeType { get; set; }
private string _T_TableName;
///
/// 目标数据表名
///
public string T_TableName
{
get { return _T_TableName; }
set
{
_T_TableName = value;
if (PropertyChanged2 != null)
{
PropertyChanged2(this, new PropertyChangedEventArgs("T_TableName"));
}
}
}
///
/// 目标数据表名
///
public string T_TableAliasName { get; set; }
///
/// 目标表类型
///
public string T_TableType { get; set; }
private List _FieldMapping;
///
/// 字段映射信息
///
public List FieldMapping
{
get { return _FieldMapping; }
set
{
_FieldMapping = value;
int count = _FieldMapping.FindAll(f => f.S_FieldIndex == -1).Count;
if (count == 0)
{
FieldMappingMsg = "所有字段已匹配";
}
else
{
FieldMappingMsg = string.Format("{0} 个字段未匹配", count);
}
}
}
///
/// 目标数据表值域
///
public List T_TableList { get; set; }
private string _FieldMappingMsg;
public string FieldMappingMsg
{
get
{
return _FieldMappingMsg;
}
set
{
_FieldMappingMsg = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("FieldMappingMsg"));
}
}
}
#region
//private TBState _State;
//public TBState State
//{
// get { return _State; }
// set
// {
// _State = value;
// if (PropertyChanged != null)
// {
// PropertyChanged(this, new PropertyChangedEventArgs("State"));
// }
// }
//}
private int _Count;
///
/// 总进度
///
public int Count
{
get
{
return _Count;
}
set
{
_Count = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Count"));
}
}
}
private int _Progress;
///
/// 当前进度
///
public int Progress
{
get
{
return _Progress;
}
set
{
_Progress = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Progress"));
}
}
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangedEventHandler PropertyChanged2;
}
public class FieldMapping
{
///
/// 目标字段名
///
public string T_FieldName { get; set; }
///
/// 目标字段索引
///
public int T_FieldIndex { get; set; }
///
/// 源字段名
///
public string S_FieldName { get; set; }
///
/// 源字段索引
///
public int S_FieldIndex { get; set; }
///
/// 字段集合
///
public List FieldList { get; set; }
}
}