using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.Geodatabase; using KGIS.Framework.AE; using KGIS.Framework.AE.Enum; using KGIS.Framework.AE.ExtensionMethod; using KGIS.Framework.Maps; using KGIS.Framework.Platform; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using Kingo.PluginServiceInterface; using ReactiveUI; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml; using KUI.Windows; namespace Kingo.Plugin.BGResultManager.ViewModel { public class ExportYBGCGViewModel : ReactiveObject, IScreen { private string _OutDir; public string OutDir { get { return _OutDir; } set { this.RaiseAndSetIfChanged(ref _OutDir, value); } } private bool _IsHZPC; public bool IsHZPC { get { return _IsHZPC; } set { this.RaiseAndSetIfChanged(ref _IsHZPC, value); } } public List Items { get; set; } /// /// 输出成果根目录 /// public string OutDataRootDir { get; set; } public RoutingState Router { get; } public ReactiveCommand ExportClick { get; protected set; } public ExportYBGCGViewModel() { Items = GetResultsCatalog(); ExportClick = ReactiveCommand.Create(); ExportClick.Subscribe(x => { try { if (Items == null) return; if (string.IsNullOrWhiteSpace(OutDir)) { MessageHelper.Show("请选择输出目录!"); return; } this.ShowLoading("正在进行预变更成果导出......", 0, 0); System.Threading.Thread.Sleep(1000); OutDataRootDir = Items[0].Name; string dirpath = Path.Combine(OutDir, OutDataRootDir); if (Directory.Exists(dirpath)) PluginServiceInterface.CommonHelper.DelectDir(dirpath); CreateCatalogDir(Items); this.CloseLoading(); MessageHelper.ShowTips("导出完成!"); } catch (Exception ex) { this.CloseLoading(); MessageHelper.ShowError("导出失败:" + ex.Message); LogAPI.Debug(ex); } }); } private void CreateCatalogDir(List pCatalog) { try { if (pCatalog == null) return; List qsDic = Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.QSDM); foreach (ResultsCatalog item in pCatalog) { string TempCode = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE; item.Path = item.Path.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE); item.Name = item.Name.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE).Replace("年代代码4位", DateTime.Now.Year.ToString()).Replace("比例尺代码1位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ScaleCode); if (item.IsChecked == false) continue; string sPath = string.Empty; switch (item.Type) { case "Directory": this.UpdateMsg(string.Format("正在创建目录:{0}", item.Name)); sPath = Path.Combine(OutDir, item.Path); if (!Directory.Exists(sPath)) Directory.CreateDirectory(sPath); if (item.SubCatalog != null) CreateCatalogDir(item.SubCatalog); break; case "GDBFile": sPath = Path.Combine(OutDir, item.Path); this.UpdateMsg(string.Format("正在导出【{0}】", item.Name)); ExportGDB(sPath, item.Name); break; } } } catch (Exception ex) { LogAPI.Debug("创建节点目录生成异常:" + ex); throw ex; } } private void ExportGDB(string t_targetPath, string gdbname) { try { string gdbFolder = Path.GetDirectoryName(t_targetPath); if (Directory.Exists(t_targetPath)) PluginServiceInterface.CommonHelper.DelectDir(t_targetPath); IWorkspaceFactory pFtWsFct = new FileGDBWorkspaceFactory(); IWorkspaceName workspaceName = pFtWsFct.Create(gdbFolder, gdbname, null, 0); IFeatureClass xzqgxFC = MapsManager.Instance.MapService.GetFeatureClassByName("XZQGX"); IFeatureClass xzqgxgcFC = MapsManager.Instance.MapService.GetFeatureClassByName("XZQGXGC"); IWorkspaceAPI psWorkspaceAPI = new WorkspaceAPI(t_targetPath, WorkspaceTypeEnum.GDBFile); IFeatureClassAPI xzqgxFeatureclassAPI = psWorkspaceAPI.CreateFeatureClass("XZQGX", (xzqgxFC as IGeoDataset).SpatialReference, xzqgxFC.Fields); IFeatureClassAPI xzqgxgcFeatureclassAPI = psWorkspaceAPI.CreateFeatureClass("XZQGXGC", (xzqgxgcFC as IGeoDataset).SpatialReference, xzqgxgcFC.Fields); IFeatureClassAPI fcAPI = new FeatureClassAPI(xzqgxFC); fcAPI.FcToFc(xzqgxFeatureclassAPI.FeatureClass, null, false); fcAPI = new FeatureClassAPI(xzqgxgcFC); fcAPI.FcToFc(xzqgxgcFeatureclassAPI.FeatureClass, null, false); ICursor pCur = null; IRow row = null; IQueryFilter queryFilter = new QueryFilterClass() { WhereClause = "BGXW='0' OR BGXW='3'" }; if (IsHZPC && xzqgxFC.FeatureCount(null) != 0 && xzqgxgcFC.FeatureCount(queryFilter) != 0) { Dictionary keyValuePairs = new Dictionary(); queryFilter.WhereClause = "BGXW='3'";//仅划出区域图斑 double MaxArea = 0; //double SumArea = 0; if (xzqgxgcFC.FeatureCount(queryFilter) == 0) { IQueryDef pQDef = ((xzqgxgcFC as FeatureClass).Workspace as IFeatureWorkspace).CreateQueryDef(); pQDef.Tables = "XZQGXGC"; pQDef.WhereClause = " 1=1 AND BGXW<>'0'"; pQDef.SubFields = "OBJECTID,BGMJ"; pCur = pQDef.Evaluate(); while ((row = pCur.NextRow()) != null) { double TempArea = row.get_Value(1).ToString().ToDouble(); if (MaxArea < TempArea) MaxArea = TempArea; //SumArea += TempArea; keyValuePairs[row.get_Value(0).ToString()] = TempArea; } } else { IQueryDef pQDef = ((xzqgxgcFC as FeatureClass).Workspace as IFeatureWorkspace).CreateQueryDef(); pQDef.Tables = "XZQGXGC";//划入区域(划入且划出当做划入处理) pQDef.WhereClause = " 1=1 AND BGXW='3'"; pQDef.SubFields = "OBJECTID,BGMJ"; pCur = pQDef.Evaluate(); while ((row = pCur.NextRow()) != null) { double TempArea = row.get_Value(1).ToString().ToDouble(); if (MaxArea < TempArea) MaxArea = TempArea; //SumArea += TempArea; keyValuePairs[row.get_Value(0).ToString()] = TempArea; } } if (!keyValuePairs.ContainsValue(MaxArea)) return; int MaxAreaOID = keyValuePairs.FirstOrDefault(x => x.Value.Equals(MaxArea)).Key.ToInt(); IFeature MaxFeature = xzqgxgcFC.GetFeature(MaxAreaOID);//获取最大面积的图斑 //double MaxF_Area = MaxFeature.ShapeCopy.GetEllipseArea(); decimal Sum_Area = keyValuePairs.Values.ToList().Sum().ToString().ToDecimal(); //decimal SumTemp_Area = 0; //foreach (var item in keyValuePairs.Values) //{ // SumTemp_Area += item.ToString().ToDecimal(); //} decimal ChaValue = Sum_Area.ToString().ToDecimal() - Math.Round(Sum_Area, 1); int BGMJIndex = xzqgxgcFC.FindField("BGMJ"); int BGXWIndex = xzqgxgcFC.FindField("BGXW"); int BGHBSMIndex = xzqgxgcFC.FindField("BGHBSM"); MaxFeature.Value[BGMJIndex] = MaxFeature.Value[BGMJIndex].ToString().ToDecimal() - ChaValue; MaxFeature.Store(); if (ChaValue != 0 && !MaxFeature.Value[BGXWIndex].ToString().Equals("0")) { string GXBSMValue = MaxFeature.Value[BGHBSMIndex].ToString(); queryFilter.WhereClause = $"BSM='{GXBSMValue}'"; IFeature GXFeature = xzqgxFC.Update(queryFilter, true).NextFeature(); if (GXFeature != null) return; int JSMJIndex = xzqgxFC.FindField("JSMJ"); GXFeature.Value[JSMJIndex] = GXFeature.Value[JSMJIndex].ToString().ToDecimal() - ChaValue; GXFeature.Store(); } } if (psWorkspaceAPI != null) psWorkspaceAPI.CloseWorkspace(); if (xzqgxFeatureclassAPI != null) xzqgxFeatureclassAPI.CloseFeatureClass(); if (xzqgxgcFeatureclassAPI != null) xzqgxgcFeatureclassAPI.CloseFeatureClass(); } catch (Exception ex) { LogAPI.Debug("预变更成果数据(矢量数据)面积平差导出异常:" + ex); throw; } } #region public static List GetResultsCatalog() { List result = new List(); try { XmlDocument doc = new XmlDocument(); string strPath = SysAppPath.ResultsCatalogCfgForYBGCG(); doc.Load(strPath); XmlNode nodeDataCatalog = doc.SelectSingleNode("ResultsCatalog"); if (nodeDataCatalog == null) return result; List qsDic = Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.QSDM); foreach (XmlNode item in nodeDataCatalog.ChildNodes) { if (item.Name == "DataCatalog") { ResultsCatalog rootCatalog = new ResultsCatalog { Name = item.Attributes["Name"] == null ? "" : item.Attributes["Name"].Value }; rootCatalog.Path = rootCatalog.Name; rootCatalog.Type = "Directory"; if (item.ChildNodes != null) { List tree = GetSubCatalog(item, rootCatalog); if (rootCatalog.SubCatalog == null) rootCatalog.SubCatalog = new List(); rootCatalog.SubCatalog.AddRange(tree); } if (qsDic != null) { DataDicTionary dic = qsDic.FirstOrDefault(f => f.CODE.Length == 2); if (dic != null) { rootCatalog.Path = rootCatalog.Path.Replace("*省", dic.NAME); rootCatalog.Name = rootCatalog.Name.Replace("*省", dic.NAME); } dic = qsDic.FirstOrDefault(f => f.CODE.Length == 4); if (dic != null) { rootCatalog.Path = rootCatalog.Path.Replace("*市", dic.NAME); rootCatalog.Name = rootCatalog.Name.Replace("*市", dic.NAME); } dic = qsDic.FirstOrDefault(f => f.CODE == (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE); if (dic != null) { rootCatalog.Path = rootCatalog.Path.Replace("*县", dic.NAME); rootCatalog.Name = rootCatalog.Name.Replace("*县", dic.NAME); } } rootCatalog.Path = rootCatalog.Path.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE); rootCatalog.Name = rootCatalog.Name.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE).Replace("年代代码4位", DateTime.Now.Year.ToString()).Replace("比例尺代码1位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ScaleCode); result.Add(rootCatalog); } } return result; } catch (Exception ex) { LogAPI.Debug(ex); throw; } } public static List GetSubCatalog(XmlNode pNode, ResultsCatalog pCatalog) { List result = new List(); if (pNode != null && pNode.ChildNodes != null) { string sPName = pNode.Attributes["Name"] == null ? "" : pNode.Attributes["Name"].Value.ToString(); //rootCatalog.Name = rootCatalog.Name.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE).Replace("年代代码4位", DateTime.Now.Year.ToString()).Replace("比例尺代码1位", "I"); sPName = sPName.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE).Replace("年代代码4位", DateTime.Now.Year.ToString()).Replace("比例尺代码1位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ScaleCode); List qsDic = Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.QSDM); if (qsDic != null) { DataDicTionary dic = qsDic.FirstOrDefault(f => f.CODE.Length == 2); if (dic != null) { sPName = sPName.Replace("*省", dic.NAME); } dic = qsDic.FirstOrDefault(f => f.CODE.Length == 4); if (dic != null) { sPName = sPName.Replace("*市", dic.NAME); } dic = qsDic.FirstOrDefault(f => f.CODE == (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE); if (dic != null) { sPName = sPName.Replace("*县", dic.NAME); } } foreach (XmlNode item in pNode.ChildNodes) { ResultsCatalog catalog = new ResultsCatalog(); try { if (item.Attributes != null && item.Attributes["ShowFilter"] != null) { string[] showFilter = item.Attributes["ShowFilter"].Value.ToString().Split('_'); if (!(MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE.StartsWith(showFilter[0]) && !sPName.StartsWith(showFilter[1])) { continue; } } catalog.Name = item.Attributes["Name"] == null ? "" : item.Attributes["Name"].Value; catalog.Path = System.IO.Path.Combine(pCatalog.Path, catalog.Name); catalog.CopyFile = item.Attributes["CopyFile"] == null ? "" : item.Attributes["CopyFile"].Value; catalog.Type = item.Name; catalog.Parent = pCatalog; if (qsDic != null) { DataDicTionary dic = qsDic.FirstOrDefault(f => f.CODE.Length == 2); if (dic != null) { catalog.Path = catalog.Path.Replace("*省", dic.NAME); catalog.Name = catalog.Name.Replace("*省", dic.NAME); } dic = qsDic.FirstOrDefault(f => f.CODE.Length == 4); if (dic != null) { catalog.Path = catalog.Path.Replace("*市", dic.NAME); catalog.Name = catalog.Name.Replace("*市", dic.NAME); } dic = qsDic.FirstOrDefault(f => f.CODE == (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE); if (dic != null) { catalog.Path = catalog.Path.Replace("*县", dic.NAME); catalog.Name = catalog.Name.Replace("*县", dic.NAME); } } catalog.Path = catalog.Path.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE); catalog.Name = catalog.Name.Replace("县行政区划代码6位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE).Replace("年代代码4位", DateTime.Now.Year.ToString()).Replace("比例尺代码1位", (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ScaleCode); result.Add(catalog); } catch (Exception ex) { } if (item.ChildNodes != null) { if ("ReportExcel,ReportWord".Contains(item.Name)) { ReadAttribute(item, catalog); } else { catalog.SubCatalog = GetSubCatalog(item, catalog); } } } } return result; } private static void ReadAttribute(XmlNode pNode, ResultsCatalog pCatalog) { if (pNode != null && pNode.ChildNodes != null) { foreach (XmlNode item in pNode.ChildNodes) { switch (item.Name) { case "ReportType": pCatalog.FileType = item.InnerText; break; case "TempalateFileName": pCatalog.FileTempalate = item.InnerText; break; default: break; } } } } #endregion } }