年度变更建库软件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.

568 lines
26 KiB

using DevExpress.Xpf.Editors;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using KGIS.Framework.Maps;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using KUI.Windows;
using ESRI.ArcGIS.DataSourcesFile;
using KGIS.Framework.Platform;
using KGIS.Framework.AE;
namespace Kingo.Plugin.DTBJK.View
{
/// <summary>
/// 导出单图斑建库-变更范围数据 的交互逻辑
/// </summary>
public partial class UCExportDTBJKData : BaseWindow
{
/// <summary>
/// 视图列表
/// </summary>
private List<BGFWZJResult> bGFWZJResults = new List<BGFWZJResult>();
private IFeatureLayer BGfeatureLayer { get; set; }
/// <summary>
/// 监测图层
/// </summary>
private IFeatureLayer JCDatafeatureLayer { get; set; }
private ProjectInfo projectInfo { get; set; }
public bool AllSelecting { get; set; }
public UCExportDTBJKData(IEngineEditor m_pEditor, ProjectInfo projectInfos)
{
InitializeComponent();
projectInfo = projectInfos;
if (BGfeatureLayer == null)
BGfeatureLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG");
LoadData();
AllSelecting = false;
this.DataContext = this;
}
private void LoadData()
{
bGFWZJResults = new List<BGFWZJResult>();
IFeatureCursor BGcursor = null;
IQueryFilter queryFilter = new QueryFilterClass();
try
{
if (BGfeatureLayer != null)
{
queryFilter.WhereClause = "BID='' OR BID is Null";
BGcursor = BGfeatureLayer.FeatureClass.Search(queryFilter, true);
IFeature BGfeature = null;
int TBBHIndex = BGfeatureLayer.FeatureClass.FindField("TBYBH");
int BSMIndex = BGfeatureLayer.FeatureClass.FindField("BSM");
int JCZTIndex = BGfeatureLayer.FeatureClass.FindField("JCZT");
int GLTCIndex = BGfeatureLayer.FeatureClass.FindField("GLTC");
//int BGZTIndex = BGfeatureLayer.FeatureClass.FindField("BGZT");//已输出—未输出
while ((BGfeature = BGcursor.NextFeature()) != null)
{
IFeatureLayer featureLayer = MapsManager.Instance.MapService.GetFeatureLayerByName(BGfeature.Value[GLTCIndex].ToString());
bGFWZJResults.Add(new BGFWZJResult()
{
IsLoad = false,
WYBSM = BSMIndex == -1 ? "" : BGfeature.Value[BSMIndex].ToString(),
JCBH = BGfeature.Value[TBBHIndex].ToString(),
JCZT = BGfeature.Value[JCZTIndex].ToString() == "" ? "待完成" : BGfeature.Value[JCZTIndex].ToString(),
JCLayerName = GLTCIndex != -1 ? BGfeature.Value[GLTCIndex].ToString() : "",
//SCZT = BGfeature.Value[BGZTIndex].ToString()
LayerPath = featureLayer != null ? ((IDataset)featureLayer.FeatureClass).Workspace.PathName : ""
});
if (featureLayer != null)
Marshal.ReleaseComObject(featureLayer);
}
bGFWZJResults.Sort((a, b) => b.JCBH.CompareTo(a.JCBH));
BgDataSum.Text = $"图斑总数:{bGFWZJResults.Count}个";
dgTableMapping.ItemsSource = bGFWZJResults;
}
}
catch (Exception ex)
{
LogAPI.Debug(ex.Message);
}
finally
{
if (BGcursor != null) Marshal.ReleaseComObject(BGcursor);
if (queryFilter != null) Marshal.ReleaseComObject(queryFilter);
}
}
/// <summary>
/// 导出已完成变更范围
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectedBaseData_Click(object sender, RoutedEventArgs e)
{
try
{
if (BGfeatureLayer == null) return;
if ((dgTableMapping.ItemsSource as List<BGFWZJResult>).FirstOrDefault(x => x.IsLoad == true) == null)
{
MessageHelper.ShowTips("至少选择一项进行数据导出!");
return;
}
KGIS.Framework.Utils.Dialog.FolderBrowserDialog pBrowser = new KGIS.Framework.Utils.Dialog.FolderBrowserDialog
{
ShowNewFolderButton = true
};
if (pBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string savepath = pBrowser.SelectedPath;
if (projectInfo == null) return;
string fileName = projectInfo.CODE + "矢量成果" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
IQueryFilter queryfilter = new QueryFilterClass();
if (System.IO.File.Exists(System.IO.Path.Combine(savepath, fileName + ".shp")) || System.IO.File.Exists(System.IO.Path.Combine(savepath, fileName + ".SHP")))
{
if (MessageHelper.ShowYesNoAndTips("同名文件已存在是否替换?") != System.Windows.Forms.DialogResult.Yes)
{
return;
}
}
ExportFeaturesToShp(BGfeatureLayer.FeatureClass, queryfilter, savepath, fileName.Trim());
MessageHelper.ShowTips("导出成功!");
}
}
catch (Exception ex)
{
MessageHelper.ShowTips("数据导出失败!");
LogAPI.Debug("数据导出异常:" + ex);
}
}
/// <summary>
/// 刷新视图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRefresh_view_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
private void ChkSelectedAll_EditValueChanged(object sender, DevExpress.Xpf.Editors.EditValueChangedEventArgs e)
{
try
{
dgTableMapping.ItemsSource = null;
if ((e.Source as CheckEdit).IsChecked == true)
{
bGFWZJResults.FindAll(x => x.JCZT == "已完成").ForEach(x => x.IsLoad = true);
}
else
{
bGFWZJResults.FindAll(x => x.JCZT == "已完成").ForEach(x => x.IsLoad = false);
}
dgTableMapping.ItemsSource = bGFWZJResults;
dgTableMapping.RefreshData();
}
catch (Exception ex)
{
LogAPI.Debug(ex.Message);
}
}
/// <summary>
/// 选中框选中状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
try
{
if ((e.Source as CheckBox).Tag is string && (e.Source as CheckBox).Tag.ToString() != "已完成")
{
(e.Source as CheckBox).IsChecked = false;
if (bGFWZJResults.FirstOrDefault(x => x.IsLoad == false) == null)
{
AllSelecting = true;
}
}
if (bGFWZJResults.FirstOrDefault(x => x.IsLoad == true) == null)
{
AllSelecting = false;
//var header = chkSelectedAll.HeaderTemplate.LoadContent() as FrameworkElement;
//CheckEdit checkEdit = header.FindName("AllCheckEidt") as CheckEdit;k
//checkEdit.IsChecked = false;
//this.DataContext = this;
}
}
catch (Exception ex)
{
LogAPI.Debug("选中状态失败:" + ex.Message);
}
}
public DataTable GetFeatureClassTable(IFeatureClass featureClass, IQueryFilter queryFilter = null)
{
var dataTable = new DataTable();
// 添加列
var fields = featureClass.Fields;
for (int i = 0; i < fields.FieldCount; i++)
{
var field = fields.get_Field(i);
dataTable.Columns.Add(field.Name, GetSystemType(field.Type));
}
// 添加行
var cursor = featureClass.Search(queryFilter, true);
IFeature feature;
while ((feature = cursor.NextFeature()) != null)
{
var row = dataTable.NewRow();
for (int i = 0; i < fields.FieldCount; i++)
{
var value = feature.get_Value(i);
row[i] = value is DBNull ? DBNull.Value : value;
}
dataTable.Rows.Add(row);
}
// 关闭游标
Marshal.ReleaseComObject(cursor);
return dataTable;
}
/// <summary>
/// 获取字段类型
/// </summary>
/// <param name="fieldType"></param>
/// <returns></returns>
private Type GetSystemType(esriFieldType fieldType)
{
switch (fieldType)
{
case esriFieldType.esriFieldTypeSmallInteger:
case esriFieldType.esriFieldTypeInteger:
return typeof(int);
case esriFieldType.esriFieldTypeSingle:
return typeof(float);
case esriFieldType.esriFieldTypeDouble:
return typeof(double);
case esriFieldType.esriFieldTypeDate:
return typeof(DateTime);
default:
return typeof(string);
}
}
/// <summary>
/// 导出SHP数据
/// </summary>
/// <param name="pSourceFeatureClass"></param>
/// <param name="pQueryFilter"></param>
/// <param name="forlder"></param>
/// <param name="fileName"></param>
public void ExportFeaturesToShp(IFeatureClass pSourceFeatureClass, IQueryFilter pQueryFilter, string forlder, string fileName)
{
IWorkspaceFactory workFactory = null;
IFeatureWorkspace pFeatureWorkspace = null;
IFeatureCursor pFeatureCursor = null;
IFeatureClass pFeatureClass = null;
IFeatureCursor pInsertFeatureCursor = null;
try
{
if (pSourceFeatureClass == null)
{
throw new Exception("获未获取到要素集!");
}
if (!System.IO.Directory.Exists(forlder))
{
System.IO.Directory.CreateDirectory(forlder);
}
//count = pSourceFeatureClass.FeatureCount(pQueryFilter);
pFeatureCursor = pSourceFeatureClass.Search(pQueryFilter, true);
this.ShowLoading("正在导出,请稍候...", 0, 0);
workFactory = new ShapefileWorkspaceFactoryClass();
pFeatureWorkspace = workFactory.OpenFromFile(forlder, 0) as IFeatureWorkspace;
//创建字段信息
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
List<string> fieldNames = new List<string>();
//当前字段默认模板数据库全都存在(未做缺失判断)
//string Fields = "OBJECTID,SHAPE,TBBSM,TBBH,DLBM,DLMC,TBMJ,GDLX,TBXHDM,TBXHMC,ZZSXDM,ZZSXMC,GDDB,CZCSXM,DDTCBZ,DDTCMC,SFXML,XMLX,XMMC,XMBH,DKMC";
//当前字段默认模板数据库全都存在(未做缺失判断 - 新需求)
string Fields = "OBJECTID,SHAPE,XZQDM,JCBH,SDXZ,WBGYY,HCBGDL,GDLX,ZZSX,ZZZW,SZLX,LYZZZW,HFSX,DDTCBZ,LJBZ,LXJZGLBH,QKSM";
foreach (string Fielditem in Fields.ToString().Split(','))
{
if (pSourceFeatureClass.Fields.FindField(Fielditem) == -1)
{
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.IsNullable_2 = false;
pFieldEdit.Name_2 = Fielditem;
fieldNames.Add(pFieldEdit.Name);
pFieldEdit.AliasName_2 = Fielditem;
pFieldEdit.IsNullable_2 = false;
pFieldEdit.Length_2 = 50;
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldsEdit.AddField(pField);
continue;
}
IField field = pSourceFeatureClass.Fields.get_Field(pSourceFeatureClass.Fields.FindField(Fielditem));
//if (field.Name.Equals("BZ") || field.Name.Equals("BSM") || field.Name.Equals("TBBH")) continue;
if ((field.Type == esriFieldType.esriFieldTypeBlob || field.Type == esriFieldType.esriFieldTypeRaster) && field.Type != esriFieldType.esriFieldTypeGeometry)
continue;
if (field.Type == esriFieldType.esriFieldTypeGeometry)
{
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "Shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = pSourceFeatureClass.ShapeType;//pFeature.Shape.GeometryType;
if ((pSourceFeatureClass as IGeoDataset) != null)
{
pGeometryDefEdit.SpatialReference_2 = (pSourceFeatureClass as IGeoDataset).SpatialReference;//pFeature.Shape.SpatialReference;
}
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
}
else
{
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = field.Length;
if (field.AliasName == "种植属性名称")
{
pFieldEdit.Length_2 = field.Length + 10;
}
if (field.Name.Length > 10)
{
string fname = field.Name.Substring(0, 10);
int n = 1;
while (fieldNames.Contains(fname))
{
string end = n.ToString();
fname = fname.Remove(10 - end.Length) + end;
n++;
}
pFieldEdit.Name_2 = fname;
}
else
{
pFieldEdit.Name_2 = field.Name;
}
fieldNames.Add(pFieldEdit.Name);
pFieldEdit.AliasName_2 = field.AliasName;
pFieldEdit.Type_2 = field.Type;
pFieldsEdit.AddField(pField);
}
}
if (pSourceFeatureClass.FindField("SHAPE_Length") != -1)
{
IField field = pSourceFeatureClass.Fields.get_Field(pSourceFeatureClass.FindField("SHAPE_Length"));
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = field.Length;
if (field.Name.Length > 10)
{
string fname = field.Name.Substring(0, 10);
int n = 1;
while (fieldNames.Contains(fname))
{
string end = n.ToString();
fname = fname.Remove(10 - end.Length) + end;
n++;
}
pFieldEdit.Name_2 = fname;
}
else
{
pFieldEdit.Name_2 = field.Name;
}
fieldNames.Add(pFieldEdit.Name);
pFieldEdit.AliasName_2 = field.AliasName;
pFieldEdit.Type_2 = field.Type;
pFieldsEdit.AddField(pField);
}
if (pSourceFeatureClass.FindField("SHAPE_Area") != -1)
{
IField field = pSourceFeatureClass.Fields.get_Field(pSourceFeatureClass.FindField("SHAPE_Area"));
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = field.Length;
pFieldEdit.Name_2 = field.Name;
fieldNames.Add(pFieldEdit.Name);
pFieldEdit.AliasName_2 = field.AliasName;
pFieldEdit.Type_2 = field.Type;
pFieldsEdit.AddField(pField);
}
pFeatureClass = pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null, pSourceFeatureClass.FeatureType, "SHAPE", "");
// 设置 FeatureClass 的 SpatialReference
IGeoDatasetSchemaEdit schemaEdit = (IGeoDatasetSchemaEdit)pFeatureClass;
if (projectInfo != null && !string.IsNullOrWhiteSpace(projectInfo.PrjFileStr))
schemaEdit.AlterSpatialReference(projectInfo.GetCurentProjectedCoordinate());
pInsertFeatureCursor = pFeatureClass.Insert(true);
Dictionary<int, int> dicMatchFields = new Dictionary<int, int>();
GetMatchFieldsDirectory(pSourceFeatureClass, pFeatureClass, ref dicMatchFields, true);
IFeatureBuffer featureAdd = pFeatureClass.CreateFeatureBuffer();
IFeature pFeature = null;
int TBBSMIndex = pSourceFeatureClass.FindField("TBBSM");
int TBBHIndex = pSourceFeatureClass.FindField("TBYBH");
int TBXHDMIndex = pSourceFeatureClass.FindField("TBXHDM");
int DLBMIndex = pSourceFeatureClass.FindField("DLBM");
int ZZSXDMIndex = pSourceFeatureClass.FindField("ZZSXDM");
int HFSXIndex = pFeatureClass.FindField("HFSX");
int ZZSXIndex = pFeatureClass.FindField("ZZSX");
int XZQDMIndex = pFeatureClass.FindField("XZQDM");
int HCBGDLIndex = pFeatureClass.FindField("HCBGDL");
//DataDicTionary dataDicTionary = null;
while ((pFeature = pFeatureCursor.NextFeature()) != null)
{
featureAdd.Shape = pFeature.Shape;
if ((dgTableMapping.ItemsSource as List<BGFWZJResult>).FirstOrDefault(x => x.IsLoad == false && x.JCBH.Trim().Contains(pFeature.Value[TBBHIndex].ToString().Trim())) != null)
{
continue;
}
foreach (KeyValuePair<int, int> kvpMatchField in dicMatchFields)
{
if (pFeature.Value[kvpMatchField.Value] != DBNull.Value)
{
featureAdd.set_Value(kvpMatchField.Key, pFeature.get_Value(kvpMatchField.Value));
}
else
{
featureAdd.set_Value(kvpMatchField.Key, null);
}
}
featureAdd.Value[XZQDMIndex] = projectInfo.CODE;
featureAdd.Value[HCBGDLIndex] = pFeature.Value[DLBMIndex].ToString();
featureAdd.Value[ZZSXIndex] = pFeature.Value[ZZSXDMIndex].ToString();
featureAdd.Value[HFSXIndex] = pFeature.Value[TBXHDMIndex].ToString();
//dataDicTionary = Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.TBXHLX).FirstOrDefault(x => x.CODE.Equals(TBXHDMValue));
//if (dataDicTionary != null && !string.IsNullOrWhiteSpace(TBXHDMValue))
// featureAdd.set_Value(TBXHMCIndex, dataDicTionary.NAME);
//else
// featureAdd.set_Value(TBXHMCIndex, null);
pInsertFeatureCursor.InsertFeature(featureAdd);
}
pInsertFeatureCursor.Flush();
//关闭lock
IWorkspaceFactoryLockControl ipWsFactoryLock = (IWorkspaceFactoryLockControl)workFactory;
if (ipWsFactoryLock.SchemaLockingEnabled)
{
ipWsFactoryLock.DisableSchemaLocking();
}
this.CloseLoading();
}
catch (Exception ex)
{
LogAPI.Debug("导出SHAPE文件出错,可能的原因是:" + ex.Message);
throw new Exception("导出SHAPE文件出错,可能的原因是!" + ex.Message);
}
finally
{
if (pFeatureCursor != null)
{
Marshal.ReleaseComObject(pFeatureCursor);
}
if (pFeatureClass != null)
{
Marshal.ReleaseComObject(pFeatureClass);
}
if (pInsertFeatureCursor != null)
{
Marshal.ReleaseComObject(pInsertFeatureCursor);
}
if (pFeatureWorkspace != null)
{
Marshal.ReleaseComObject(pFeatureWorkspace);
}
if (workFactory != null)
{
Marshal.ReleaseComObject(workFactory);
}
this.CloseLoading();
}
}
/// <summary>
/// 获得匹配的字段索引集合
/// </summary>
/// <param name="pSourceFeatureClass">源要素类</param>
/// <param name="pTargetClass">目标要素类</param>
/// <param name="dicMatchFields">匹配字段集合-KEY为目标图层字段,VALUE为源图层字段</param>
/// <param name="isGetRequired">是否获取必须字段</param>
public static void GetMatchFieldsDirectory(IClass pSourceClass, IClass pTargetClass, ref Dictionary<int, int> dicMatchFields, bool isGetRequired = false)
{
for (int i = 0; i < pTargetClass.Fields.FieldCount; i++)
{
IField pTargetField = pTargetClass.Fields.get_Field(i);
//目标图层的字段必须为可编辑并且不是必须字段
if (pTargetField.Required == false && pTargetField.Editable == true)
{
int iSourceFeatureClassIndex = pSourceClass.Fields.FindField(pTargetField.Name);
if (pTargetField.Name == "SHAPE_Leng")
{
iSourceFeatureClassIndex = pSourceClass.Fields.FindField("SHAPE_Length");
}
//源要素类中该字段存在
if (iSourceFeatureClassIndex > -1)
{
IField pSourceField = pSourceClass.Fields.get_Field(iSourceFeatureClassIndex);
if ("XZQTZLX,BGZT,JCZT,TZ,BGFW,SJLY,SFJCTB,NYJY,NYYPDL,SFJZ,ONLYZLBG".Contains(pSourceField.Name))
continue;
//目标图层的字段也必须为可编辑并且不是必须字段
if (pSourceField.Required == false && pSourceField.Editable == true)
{
//添加到字段匹配集合中
dicMatchFields.Add(i, iSourceFeatureClassIndex);
}
else if (isGetRequired)
{
dicMatchFields.Add(i, iSourceFeatureClassIndex);
}
}
}
//此处为了保留原有要素ObjectID字段值
if (pTargetField.Name.Equals("OIDCOPY", StringComparison.CurrentCultureIgnoreCase))
{
dicMatchFields.Add(i, pSourceClass.FindField(pSourceClass.OIDFieldName));
}
}
}
}
public class BGFWZJResult
{
public bool IsLoad { get; set; }
/// <summary>
/// 数据来源名称
/// </summary>
public string JCLayerName { get; set; }
/// <summary>
/// 变更BSM
/// </summary>
public string WYBSM { get; set; }
/// <summary>
/// 对接图斑预编号
/// </summary>
public string JCBH { get; set; }
/// <summary>
/// 质检状态
/// </summary>
public string JCZT { get; set; }
/// <summary>
/// 输出状态
/// </summary>
public string SCZT { get; set; }
public string LayerPath { get; set; }
}
}