using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ExcelDataReader; using KGIS.Framework.Maps; using KGIS.Framework.OpenData.Control; using KGIS.Framework.OpenData.Filter; using KGIS.Framework.OpenData.InterFace; using KGIS.Framework.Platform; using KGIS.Framework.Utils; using KGIS.Framework.Utils.ExtensionMethod; using KGIS.Framework.Utils.Helper; using KGIS.Framework.Utils.Model; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using UIShell.OSGi; using KUI.Windows; using Path = System.IO.Path; using KGIS.Framework.AE; using KGIS.Framework.AE.GPHelper; namespace Kingo.Plugin.MapView.Views.AppMenuView { /// /// 工程属性 的交互逻辑 /// public partial class UCProjectInformation : BaseWindow { /// /// 工作目录服务接口 /// IWorkCatalog _workCatalog = null; //ObservableCollection ListZLXXInfo = null;//坐落信息变化数据 private ProjectInfo ProInfo = null; List Dics_QS = null;//权属字典 ObservableCollection ListZLXXInfo = null;//坐落信息变化数据 public UCProjectInformation() { InitializeComponent(); DevExpress.Xpf.Core.ThemeManager.SetTheme(this, DevExpress.Xpf.Core.Theme.Office2013LightGray); //工作目录接口 _workCatalog = BundleRuntime.Instance.GetFirstOrDefaultService(); ProInfo = MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo; if (ProInfo != null) InitData(ProInfo); } public void InitData(ProjectInfo pPrj) { try { if (_workCatalog != null) btnPrjDir.Text = _workCatalog.SaveWorkSapcePath; //县代码变化 cb_XZQDMChange.IsChecked = pPrj.XZQDMChange; //村名称变化 cb_ZLDWMCChange.IsChecked = pPrj.ZLDWMCChange; #region 工程类型 switch (pPrj.ProjType) { case EnumProjType.NDBG: projectType.SelectedIndex = 0; break; case EnumProjType.RCBG_ZJG: projectType.SelectedIndex = 1; break; case EnumProjType.RCBG_BCGD: projectType.SelectedIndex = 2; break; default: break; } #endregion #region 比例尺 List listScale = new List { new ScaleCodeTable() { Scale = "1:2000", ScaleCode = "I" }, new ScaleCodeTable() { Scale = "1:5000", ScaleCode = "H" }, new ScaleCodeTable() { Scale = "1:10000", ScaleCode = "G" }, new ScaleCodeTable() { Scale = "1:25000", ScaleCode = "F" }, new ScaleCodeTable() { Scale = "1:50000", ScaleCode = "E" }, new ScaleCodeTable() { Scale = "1:100000", ScaleCode = "D" }, new ScaleCodeTable() { Scale = "1:250000", ScaleCode = "C" }, new ScaleCodeTable() { Scale = "1:500000", ScaleCode = "B" } }; combScale.ItemsSource = listScale; if (!string.IsNullOrWhiteSpace(pPrj.ScaleCode)) { combScale.SelectedItem = listScale.FirstOrDefault(x => x.ScaleCode == pPrj.ScaleCode); } else if (!string.IsNullOrWhiteSpace(pPrj.ScaleCode)) { combScale.SelectedItem = listScale.FirstOrDefault(x => x.Scale == pPrj.Scale); } else { combScale.SelectedIndex = 0; } #endregion #region 容差 List listResolution = new List(); listResolution.Add(new ScaleCodeTable() { Scale = "万分之一", ScaleCode = "0.0001" }); listResolution.Add(new ScaleCodeTable() { Scale = "十万分之一", ScaleCode = "0.00001" }); combXYResolution.ItemsSource = listResolution; if (!string.IsNullOrWhiteSpace(pPrj.XYResolution.ToTrim()) && pPrj.XYResolution != 0) { combXYResolution.SelectedItem = listResolution.FirstOrDefault(x => x.ScaleCode == pPrj.XYResolution.ToTrim()); } else { combXYResolution.SelectedIndex = 0; } #endregion //坐标系 btnPrjFile.Text = pPrj.GetPRJName(); //坐落信息 //ListZLXXInfo = new ObservableCollection(); //ListZLXXInfo = pPrj.ListZLXXInfo.Copy(); if (pPrj.ListZLXXInfo.Count == 0) pPrj.ListZLXXInfo.Add(new ZLXXInfo() { ID = 0 }); gvCtrl.ItemsSource = pPrj.ListZLXXInfo; this.DataContext = pPrj; } catch (Exception ex) { LogAPI.Debug("加载工程信息页面数据时失败,异常原因: " + ex.Message + " ; "); MessageHelper.ShowTips(ex.Message); } } /// /// 保存工程信息 /// /// /// private void BtnOK_Click(object sender, RoutedEventArgs e) { try { if (combScale.SelectedItem is ScaleCodeTable)//比例尺 ProInfo.ScaleCode = (combScale.SelectedItem as ScaleCodeTable).ScaleCode; //陆地海岛面积 ProInfo.DCMJ = Convert.ToDouble(DCMJ.Text.Trim()); ProInfo.DCMJHD = Convert.ToDouble(DCMJHD.Text.Trim()); if (!string.IsNullOrWhiteSpace(btnCJDCQPath.Text.Trim())) ProInfo.DR_CJDCQPath = btnCJDCQPath.Text.Trim(); if (!string.IsNullOrWhiteSpace(btnXZQPath.Text.Trim())) ProInfo.DR_XZQPath = btnXZQPath.Text.Trim(); if (!string.IsNullOrWhiteSpace(btnczcPath.Text.Trim()))//调入城镇村 ProInfo.DR_CZCPath = btnczcPath.Text.Trim(); ProInfo.XZQDMChange = cb_XZQDMChange.IsChecked == true; ProInfo.ZLDWMCChange = cb_ZLDWMCChange.IsChecked == true; if (gvCtrl.ItemsSource is ObservableCollection && (gvCtrl.ItemsSource as ObservableCollection).Count == 1) if ((gvCtrl.ItemsSource as ObservableCollection).FirstOrDefault().ID == 0) (gvCtrl.ItemsSource as ObservableCollection).Clear(); ProInfo.ListZLXXInfo = (gvCtrl.ItemsSource as ObservableCollection); //工程保存 if (ProInfo.Save()) this.Close(); else MessageHelper.ShowWarning("工程修改失败!"); } catch (Exception ex) { LogAPI.Debug(ex.Message); } } #region 调入村级调查区 IFeatureClass drqCJDCQFc = null; private void BtnSelectedCJDCQPath_Click(object sender, RoutedEventArgs e) { string FcPath = string.Empty; drqCJDCQFc = GetSelectionFc(ref FcPath); btnCJDCQPath.Text = FcPath; } #endregion #region 行政区 IFeatureClass drqXZQFc = null; private void BtnSelectedXZQPath_Click(object sender, RoutedEventArgs e) { string FcPath = string.Empty; drqXZQFc = GetSelectionFc(ref FcPath); btnXZQPath.Text = FcPath; } #endregion #region GetSelectionFc private IFeatureClass GetSelectionFc(ref string strFcPath) { IFeatureClass result = null; try { // 获取源数据 OpenDataDialog pDialog = new OpenDataDialog(); ISpatialDataObjectFilter pOFilter; pOFilter = new FilterFeatureDatasetsAndFeatureClasses(); pDialog.AddFilter(pOFilter, true); pDialog.Title = "选择划调整范围数据"; pDialog.AllowMultiSelect = false; pDialog.RestoreLocation = true; pDialog.StartLocation = pDialog.FinalLocation; if (pDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && pDialog.Selection.Count > 0) { List distObj = pDialog.Selection; foreach (var obj in distObj) { if (obj.DatasetType == esriDatasetType.esriDTFeatureClass) { result = (obj.DatasetName as IName).Open() as IFeatureClass; strFcPath = obj.FullName; } } } else { return result; } } catch (Exception ex) { LogAPI.Debug("选择数据失败:" + ex.Message.ToString()); } return result; } #endregion #region 添加坐落变更信息 private void Txt_Add_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { ProInfo.ListZLXXInfo.Add(new ZLXXInfo() { ID = ProInfo.ListZLXXInfo.OrderByDescending(x => x.ID).FirstOrDefault() == null ? 0 : ProInfo.ListZLXXInfo.OrderByDescending(x => x.ID).FirstOrDefault().ID + 1, }); } #endregion #region 删除坐落变更信息 private void Txt_Delete_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { try { if (sender is TextBlock TextBlock) { var Info = ProInfo.ListZLXXInfo.FirstOrDefault(f => f.ID == TextBlock.Tag.ToInt()); ProInfo.ListZLXXInfo.Remove(Info); if (ProInfo.ListZLXXInfo.Count == 0) ProInfo.ListZLXXInfo.Add(new ZLXXInfo() { ID = 0 }); } } catch (Exception ex) { LogAPI.Debug("删除坐落信息失败:" + ex.Message); } } #endregion private void Validate(object sender, DevExpress.Xpf.Editors.ValidationEventArgs e) { if (e.Value != null) { if (e.Value is string) { if (string.IsNullOrWhiteSpace(e.Value as string)) { e.IsValid = false; e.ErrorContent = "值不能为空"; } } else if (e.Value is float || e.Value is double || e.Value is Int32 || e.Value is decimal) { double db = Convert.ToDouble(e.Value); if (db == 0) { e.IsValid = false; e.ErrorContent = "值不能为0"; } } } else { e.IsValid = false; e.ErrorContent = "值不能为空"; } } private void BtnCanel_Click(object sender, RoutedEventArgs e) { this.Close(); } #region 调入城镇村 IFeatureClass drqczcFc = null; private void btnSelectedCZCPath_Click(object sender, RoutedEventArgs e) { string FcPath = string.Empty; drqczcFc = GetSelectionFc(ref FcPath); btnczcPath.Text = FcPath; } #endregion private void BtnSelTB_Click(object sender, RoutedEventArgs e) { try { string xzdltbPaths = ""; IFeatureLayer BGFeatureClass = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG"); IFeatureLayer JClayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑"); IFeatureClass dltbFc = GetFeatureClass("请选择新增的地类图斑数据", ref xzdltbPaths); if (dltbFc != null) { btnNewData.Text = xzdltbPaths; IFeatureLayer SelLocation_Layer = GeoDBAPI.CreateFeatureLayerInmemeory("SelLocation", "选择范围图斑数据", (dltbFc as IGeoDataset).SpatialReference, dltbFc.ShapeType, dltbFc.Fields); (SelLocation_Layer.FeatureClass as ITable).DeleteSearchedRows(null); InsertDataToMemeoryLayer(dltbFc, SelLocation_Layer.FeatureClass, null); #region 选择的图斑与基础数据库相交,获取图形 并赋 QSDWDM 与 ZLDWDM if (SelLocation_Layer != null && SelLocation_Layer.FeatureClass.FeatureCount(null) > 0) { this.ShowLoading("正在根据矢量数据范围提取变更图斑,请稍后...", 0, 0); IFeatureLayer JCDLTB_DLTBBGLayer = null; string resultPath = Path.Combine(SysAppPath.GetCurrentAppPath(), "Temp\\Intersect_DLTBBG_DLTB"); if (!Directory.Exists(resultPath)) Directory.CreateDirectory(resultPath); else PluginServiceInterface.CommonHelper.DelectDir(resultPath); string savePath = Path.Combine(resultPath, "Intersect_DLTBBG_DLTB.gdb"); string templeteGDBPath = Path.Combine(SysAppPath.GetCurrentAppPath(), "Template", "TempGDB.gdb"); CopyDirectory(templeteGDBPath, savePath, true); string outPath = Path.Combine(savePath, "Intersect_DLTBBG_DLTB"); KGIS.Framework.AE.GPHelper.GPParamClass gPParamClass = new GPParamClass { FirstFeatureLayer = SelLocation_Layer, SecondFeatureLayer = JClayer, OutFeatureClassPath = outPath, IsGetOutPutFeature = true }; GeoprocessorHelper.IntersectAnalysis(gPParamClass, ref JCDLTB_DLTBBGLayer); if (JCDLTB_DLTBBGLayer != null) { //修改坐落单位代码 IFeatureClassAPI fcAPI = new FeatureClassAPI(JCDLTB_DLTBBGLayer.FeatureClass); fcAPI.FcToFc(BGFeatureClass.FeatureClass, null, true); if (fcAPI != null) fcAPI.CloseFeatureClass(); if (BGFeatureClass != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(BGFeatureClass); BGFeatureClass = null; } this.CloseLoading(); MessageHelper.ShowTips("提取完成!"); } #endregion } } catch (Exception ex) { this.CloseLoading(); LogAPI.Debug("选择范围图斑异常:" + ex.Message); LogAPI.Debug("选择范围图斑异常:" + ex.StackTrace); MessageHelper.ShowError("选择范围图斑异常:" + ex.Message); } finally { this.CloseLoading(); GC.Collect(); } } private void InsertDataToMemeoryLayer(IFeatureClass pSource, IFeatureClass pTarget, IQueryFilter pFilter = null) { try { if (pSource == null || pTarget == null) return; IFeatureClassAPI fcAPI = new FeatureClassAPI(pSource); if (pTarget != null) fcAPI.FcToFc(pTarget, pFilter, false); } catch (Exception ex) { LogAPI.Debug("插入数据失败:"); LogAPI.Debug(ex); } } public static IFeatureClass GetFeatureClass(string title, ref string paths) { IFeatureClass result = null; try { // 获取源数据 OpenDataDialog pDialog = new OpenDataDialog(); ISpatialDataObjectFilter pOFilter; pOFilter = new FilterFeatureDatasetsAndFeatureClasses(); pDialog.AddFilter(pOFilter, true); if (!string.IsNullOrWhiteSpace(title)) pDialog.Title = title; else pDialog.Title = "选择数据"; pDialog.AllowMultiSelect = false; pDialog.RestoreLocation = true; pDialog.StartLocation = pDialog.FinalLocation; if (pDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && pDialog.Selection.Count > 0) { List distObj = pDialog.Selection; foreach (var obj in distObj) { if (obj.DatasetType == esriDatasetType.esriDTFeatureClass) { IFeatureClass featureclass = (obj.DatasetName as IName).Open() as IFeatureClass; result = featureclass; paths += obj.FullName;//展示完整路径 //paths = featureclass.AliasName;//展示文件名 } } } return result; } catch (Exception ex) { LogAPI.Debug("选择数据失败:" + ex.Message.ToString()); return result; } } /// /// 拷贝模板gdb文件夹 /// /// /// /// public static void CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting) { try { SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\"; DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\"; if (Directory.Exists(SourcePath)) { if (Directory.Exists(DestinationPath) == false) Directory.CreateDirectory(DestinationPath); foreach (string fls in Directory.GetFiles(SourcePath)) { FileInfo flinfo = new FileInfo(fls); flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting); } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting); } } } catch (Exception ex) { throw ex; } } private void BtnOpenFile_Click(object sender, RoutedEventArgs e) { try { if (MessageHelper.ShowYesNoAndTips("是否要先获取当前坐落变更表格模板?") == System.Windows.Forms.DialogResult.Yes) { string savepaths = string.Empty; SaveFileDialog dialogCopy = new SaveFileDialog { DefaultExt = "xlsx", Filter = "Excel Files(*.xlsx)|*.xlsx", FileName = "变更坐落" + DateTime.Now.ToString("yyyyMMddHHmmssfff") }; if (dialogCopy.ShowDialog() == System.Windows.Forms.DialogResult.OK) savepaths = dialogCopy.FileName; else return; if (string.IsNullOrWhiteSpace(savepaths)) { MessageHelper.Show("未获取到文件路径!"); return; } if (File.Exists(savepaths)) { var result = MessageHelper.ShowYesNoAndTips("文件已存在,是否覆盖?"); if (result == System.Windows.Forms.DialogResult.No) return; } string strPath = AppDomain.CurrentDomain.BaseDirectory + @"工作空间\模板\坐落变更模板\坐落变更模板.xlsx"; File.Copy(strPath, savepaths, true); if (MessageHelper.ShowYesNoAndTips("模板下载已完成,是否打开?") == System.Windows.Forms.DialogResult.Yes) { System.Diagnostics.Process.Start("explorer.exe", savepaths); } return; } string filePath = string.Empty; KGIS.Framework.Utils.Dialog.OpenFileDialog dialog = new KGIS.Framework.Utils.Dialog.OpenFileDialog { DefaultExt = "xls", Filter = "Excel文件|*.xls;*.xlsx" }; if (dialog.ShowDialog()) { filePath = dialog.FileName; if (Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.QSDM) == null) { MessageHelper.ShowError("请先进行字典设置!"); return; } using (var streamData = File.Open(filePath, FileMode.Open, FileAccess.Read)) { using (var readerData = ExcelReaderFactory.CreateReader(streamData)) { var result = readerData.AsDataSet(); DataTable tempDT = result.Tables[0]; if (tempDT.Columns == null || tempDT.Columns.Count != 5) { MessageHelper.ShowError("导入模板错误!"); return; } tempDT.Columns[0].ColumnName = "变更前代码"; tempDT.Columns[1].ColumnName = "变更前名称"; tempDT.Columns[2].ColumnName = "变更后代码"; tempDT.Columns[3].ColumnName = "变更后名称"; tempDT.Columns[4].ColumnName = "是否仅权属变化"; tempDT.Rows.RemoveAt(0); if (tempDT != null && tempDT.Rows.Count > 0) { var Index = -1; ListZLXXInfo = new ObservableCollection(); foreach (DataRow row in tempDT.Rows) { var QS = Dics_QS.Where(x => x.CODE == row["变更后代码"].ToString()).FirstOrDefault(); if (QS != null) Index = Dics_QS.IndexOf(QS); if (Index > 0) ListZLXXInfo.Add(new ZLXXInfo() { ID = ListZLXXInfo.OrderByDescending(x => x.ID).FirstOrDefault() == null ? 0 : ListZLXXInfo.OrderByDescending(x => x.ID).FirstOrDefault().ID + 1, BGQDM = row["变更前代码"].ToString(), BGQMC = row["变更前名称"].ToString(), BGHDM = row["变更后代码"].ToString(), BGHMC = row["变更后名称"].ToString(), IsQS = row["是否仅权属变化"].ToString(), Data = Dics_QS, DataDicTionary = QS }); } ProInfo.ListZLXXInfo = ListZLXXInfo; gvCtrl.ItemsSource = null; gvCtrl.ItemsSource = ListZLXXInfo; ProInfo.Save(); } } } } } catch (Exception ex) { LogAPI.Debug("选择坐落变更代码数据表格数据导入异常:" + ex); MessageHelper.ShowError("选择坐落变更代码数据表格数据导入异常:" + ex.Message); } } } }