年度变更建库软件5.0版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

454 lines
20 KiB

6 months ago
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using KGIS.Framework.AE;
using KGIS.Framework.OpenData.Control;
using KGIS.Framework.OpenData.Filter;
using KGIS.Framework.OpenData.InterFace;
using KGIS.Framework.Platform;
using KGIS.Framework.Platform.Helper;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.PluginServiceInterface.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using TreeNode = Kingo.PluginServiceInterface.Model.TreeNode;
namespace Kingo.Plugin.General.ViewDTBJK
{
/// <summary>
/// 图斑列表右键功能页-变更范围导入 的交互逻辑
/// </summary>
public partial class UCBGDataImport : BaseWindow
{
private TaskPackage taskPackage { get; set; }
public string TBBSM { get; set; }
public IGeometry WYRWTB { get; set; }
public Action<bool, List<string>, TreeNode> ImpComplate;
public bool AllowEdit { get; set; } = true;
/// <summary>
/// 数据源路径
/// </summary>
public string DataSourcePath { get; set; }
private IFeatureLayer _TargetLayer;
public TreeNode TreeDanNode { get; set; }
/// <summary>
/// 目标图层
/// </summary>
public IFeatureLayer TargetLayer
{
get
{
return _TargetLayer;
}
set
{
_TargetLayer = value;
if (_TargetLayer != null)
txtTargetLayer.EditValue = _TargetLayer.Name;
}
}
/// <summary>
/// 数据源
/// </summary>
public IFeatureClass SourceFeatureClass { get; set; }
string strWhere = string.Empty;
public UCBGDataImport()
{
InitializeComponent();
}
/// <summary>
/// 选择数据源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectedImportDataPath_Click(object sender, RoutedEventArgs e)
{
if (TargetLayer == null)
{
MessageHelper.ShowTips("目标图层不能为空!");
return;
}
OpenDataDialog pDialog = new OpenDataDialog();
ISpatialDataObjectFilter pOFilter;
pOFilter = new FilterDatasetsAndLayers();
pDialog.AddFilter(pOFilter, true);
pDialog.Title = "选择导入的数据";
pDialog.AllowMultiSelect = false;
pDialog.RestoreLocation = true;
pDialog.StartLocation = pDialog.FinalLocation;
System.Windows.Forms.DialogResult dialogResult = pDialog.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK && pDialog.Selection.Count != 0)
{
foreach (ISpatialDataObject distObj in pDialog.Selection)
{
if (distObj.DatasetType == esriDatasetType.esriDTFeatureClass)
{
btnImportDataPath.EditValue = pDialog.FinalLocation;
if (SourceFeatureClass != null)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(SourceFeatureClass);
SourceFeatureClass = null;
}
SourceFeatureClass = (distObj.DatasetName as IName).Open() as IFeatureClass;
SetFieldMapping();
}
}
}
}
/// <summary>
/// 过滤条件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectedSQLDataPath_Click(object sender, RoutedEventArgs e)
{
try
{
IFeatureClass pFeatureClass = SourceFeatureClass;
if (pFeatureClass == null)
{
MessageHelper.Show("请添加数据源图层!");
return;
}
else
{
UCSQLFilterCondition ucSQLFilterContion = new UCSQLFilterCondition(pFeatureClass);
ucSQLFilterContion.SetValue += (wheresql) =>
{
btnSQLDataPath.Text = wheresql;
};
ucSQLFilterContion.ShowInMainWindow();
}
}
catch (Exception ex)
{
LogAPI.Debug(ex.Message);
}
}
/// <summary>
/// 设置字段映射
/// </summary>
public void SetFieldMapping()
{
try
{
//选择的不为空且数据类型一致
if (SourceFeatureClass != null && SourceFeatureClass.ShapeType == TargetLayer.FeatureClass.ShapeType)
{
List<DataDicTionary> dicData = new List<DataDicTionary>();
dicData.Add(new DataDicTionary() { DisplayName = "Null", NAME = "Null", CODE = "-1", REMARK = "" });
dicData.Add(new DataDicTionary() { DisplayName = "追加字段", NAME = "Add", CODE = "-2", REMARK = "" });
for (int i = 0; i < TargetLayer.FeatureClass.Fields.FieldCount; i++)
{
IField field = TargetLayer.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() });
//任务包监测图斑右键导入变更范围时,处理因 TBMJ 数据类型不匹配导致获取不到 TBMJ 数据的问题。
if (field.Name == "TBMJ" && field.Type.ToString() != "esriFieldTypeDouble")
{
dicData.Add(new DataDicTionary() { DisplayName = string.IsNullOrWhiteSpace(field.AliasName) ? field.Name : field.AliasName, NAME = field.Name, CODE = i.ToString(), REMARK = "esriFieldTypeDouble" });
}
else
{
dicData.Add(new DataDicTionary() { DisplayName = string.IsNullOrWhiteSpace(field.AliasName) ? field.Name : field.AliasName, NAME = field.Name, CODE = i.ToString(), REMARK = field.Type.ToString() });
}
}
List<FieldMapping> data = new List<FieldMapping>();
for (int i = 0; i < SourceFeatureClass.Fields.FieldCount; i++)
{
IField field = SourceFeatureClass.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());//&& 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.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);
}
dgFieldMapping.ItemsSource = null;
dgFieldMapping.ItemsSource = data;
}
else
{
MessageHelper.ShowTips("选择数据不正确,请确认数据是否为空或格式是否正确!");
return;
}
}
catch (Exception ex)
{
MessageHelper.ShowError("异常:" + ex.Message);
LogAPI.Debug(ex);
}
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
int RowCount = 0;
int ImportRowCount = 0;
IWorkspaceEdit pWsEdit = null;
try
{
if (string.IsNullOrEmpty(btnImportDataPath.Text))
{
MessageHelper.ShowTips("请先选择导入数据文件的路径!");
return;
}
#region 判断坐标系一致性
IFeatureClass t_FC = TargetLayer.FeatureClass;
//外部图层FeatureDataset为空,bug11428
if ((t_FC as FeatureClass).Workspace.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)
{
IEngineEditor edit = new EngineEditorClass();
if (edit.EditState == esriEngineEditState.esriEngineStateNotEditing)
{
MessageHelper.ShowTips("网络版数据库需要开启编辑后,再进行导入!");
return;
}
}
if (t_FC is IGeoDataset && SourceFeatureClass is IGeoDataset)
{
IGeoDataset t_Ds = t_FC as IGeoDataset;
IGeoDataset s_Ds = SourceFeatureClass as IGeoDataset;
if (t_Ds.SpatialReference != null && s_Ds.SpatialReference != null)
{
if (!GeoDBAPI.SpatialReferenceCompare(t_Ds.SpatialReference, s_Ds.SpatialReference))
{
MessageHelper.ShowTips("源要素坐标系与目标要素类坐标系不一致,无法进行数据导入!");
return;
}
}
}
#endregion
IDataset pDataset = t_FC as IDataset;
IWorkspace pWs = pDataset.Workspace;
pWsEdit = pWs as IWorkspaceEdit;
pWsEdit.StartEditing(true);
pWsEdit.StartEditOperation();
if (cbReplace.IsChecked == true)
{
var FeatureCount = t_FC.FeatureCount(null);
if (FeatureCount > 0)
{
System.Windows.Forms.DialogResult dialogResult = MessageHelper.ShowYesNoAndTips(string.Format("是否确定要覆盖当前任务图斑{0}变更范围数据?覆盖后数据不可恢复,请谨慎操作!", this.TBBSM));
if (dialogResult == System.Windows.Forms.DialogResult.Yes)
{
//清除数据
ITable pTable = t_FC as ITable;
IQueryFilter queryFilter = new QueryFilterClass()
{
WhereClause = $"bid='{(TreeDanNode.ParentNode.Data as TaskPackage).BID}' and tbbsm='{this.TBBSM}'"
};
pTable.DeleteSearchedRows(queryFilter);
}
else
{
return;
}
}
}
List<string> listBSM = new List<string>();
{
//字段映射信息
List<FieldMapping> mFieldMapping = dgFieldMapping.ItemsSource as List<FieldMapping>;
#region 新增字段
////提示不明确或缺少提示 王欢 2018-09-19
if (mFieldMapping == null)
{
MessageHelper.ShowTips("无法获取字段映射信息,数据导入失败!");
return;
}
List<FieldMapping> addFields = mFieldMapping.FindAll(f => f.T_FieldIndex == -2);
IFeatureClassAPI t_FcAPI = new FeatureClassAPI(t_FC);
foreach (FieldMapping item in addFields)
{
IField f = SourceFeatureClass.Fields.get_Field(item.S_FieldIndex);
if (t_FC.FindField(f.Name) > -1)
{
t_FcAPI.AddField(f.Name + "_a", f.Type, f.AliasName);
item.T_FieldIndex = t_FC.Fields.FindField(f.Name + "_a");
}
else
{
t_FcAPI.AddField(f.Name, f.Type, f.AliasName);
item.T_FieldIndex = t_FC.Fields.FindField(f.Name);
}
}
#endregion
string str = btnSQLDataPath.Text;
IQueryFilter filter = new QueryFilterClass()
{
WhereClause = str
};//GeoDBAPI.CreateSpatialFilter(this.WYRWTB, esriSpatialRelEnum.esriSpatialRelIntersects, str);
IFeatureCursor cursor = t_FC.Insert(true);
IFeatureBuffer newFeature = null;
IFeatureCursor s_Cursor = SourceFeatureClass.Search(filter, false);
int Count = SourceFeatureClass.FeatureCount(filter);
RowCount = Count;
int i = 0;
IFeature feature = null;
while ((feature = s_Cursor.NextFeature()) != null)
{
if (ProgressHelper.IsCancel)
{
break;
}
if (newFeature == null)
{
newFeature = t_FC.CreateFeatureBuffer();
}
int indexBID = newFeature.Fields.FindField("BID");
if (indexBID > -1 && TreeDanNode != null)
{
newFeature.set_Value(indexBID, (TreeDanNode.ParentNode.Data as TaskPackage).BID);
}
string bsm = Guid.NewGuid().ToString();
indexBID = newFeature.Fields.FindField("BSM");
if (indexBID > -1)
{
newFeature.set_Value(indexBID, bsm);
}
indexBID = newFeature.Fields.FindField("TBBSM");
if (indexBID > -1)
{
newFeature.set_Value(indexBID, this.TBBSM);
}
//处理 bug12457 (山东现场)数据库工程--湿地公园数据导入失败 (坐标系:2000,39 附件为湿地公园数据) 王欢 2018-09-19
IGeometry pGeo = feature.ShapeCopy;
//白明雅 2019-02-13 自己画的数据报错,添加判断
if (pGeo == null || pGeo.IsEmpty == true)
{
string FieldValue = feature.get_Value(feature.Fields.FindField("OBJECTID")).ToString();
LogAPI.Debug("OID:" + FieldValue + " 缺少图像数据(IGeometry)");
continue;
}
IZAware pZAware = pGeo as IZAware;
pZAware.ZAware = false;
newFeature.Shape = pGeo;
foreach (FieldMapping item in mFieldMapping)
{
if (item.S_FieldName.Equals("包ID", StringComparison.CurrentCultureIgnoreCase) ||
item.S_FieldName.Equals("标识码", StringComparison.CurrentCultureIgnoreCase) ||
item.S_FieldName.Equals("图斑标识码", StringComparison.CurrentCultureIgnoreCase) ||
item.S_FieldName.Equals("SFXML", StringComparison.CurrentCultureIgnoreCase) ||
item.S_FieldName.Equals("变更类型", StringComparison.CurrentCultureIgnoreCase))
{
continue;
}
if (item.S_FieldIndex < 0 || item.T_FieldIndex < 0)
{
continue;
}
if (string.IsNullOrWhiteSpace(feature.get_Value(item.S_FieldIndex).ToString()))
{
newFeature.set_Value(item.T_FieldIndex, DBNull.Value);
}
else
{
newFeature.set_Value(item.T_FieldIndex, feature.get_Value(item.S_FieldIndex));
}
}
string jkhhi = newFeature.Value[indexBID].ToString();
cursor.InsertFeature(newFeature);
listBSM.Add(bsm);
i++;
ImportRowCount = i;
if (i % 500 == 0 || i == Count)
{
ProgressHelper.CurrentProgress = i;
}
}
if (newFeature != null)
{
cursor.Flush();
}
pWsEdit.StopEditOperation();
pWsEdit.StopEditing(true);
if ((t_FC as FeatureClass).Workspace.Type != esriWorkspaceType.esriRemoteDatabaseWorkspace)
{
IFeatureClassManage fcManage = t_FC as IFeatureClassManage;
fcManage.UpdateExtent();
}
}
ImpComplate?.Invoke(true, listBSM, TreeDanNode);
int FailedRowCount = RowCount - ImportRowCount;
this.Close();
}
catch (Exception ex)
{
if (pWsEdit != null)
{
pWsEdit.AbortEditOperation();
pWsEdit.StopEditing(false);
}
LogAPI.Debug("数据导入异常:" + ex);
int FailedRowCount = RowCount - ImportRowCount;
string message = string.Format("共导入{0}条,成功{1}条,失败{2}条, 失败原因:{3}", RowCount, ImportRowCount, FailedRowCount, ex.Message);
MessageHelper.ShowWarning(message, true);
}
finally
{
ProgressHelper.CloseProcessBar();
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
public class FieldMapping
{
/// <summary>
/// 目标字段名
/// </summary>
public string T_FieldName { get; set; }
/// <summary>
/// 目标字段索引
/// </summary>
public int T_FieldIndex { get; set; }
/// <summary>
/// 源字段名
/// </summary>
public string S_FieldName { get; set; }
/// <summary>
/// 源字段索引
/// </summary>
public int S_FieldIndex { get; set; }
/// <summary>
/// 字段集合
/// </summary>
public List<DataDicTionary> FieldList { get; set; }
}
}