using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; using KGIS.Framework.AE; using KGIS.Framework.AE.GPHelper; using KGIS.Framework.Maps; using KGIS.Framework.Platform; using KGIS.Framework.Utils; using KGIS.Framework.Utils.ExtensionMethod; using KGIS.Framework.Utils.Helper; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows; using System.Windows.Controls; using KUI.Windows; namespace Kingo.Plugin.DataLoad.View { /// /// 初始化图斑变更 的交互逻辑 /// public partial class FrmSJTQ : BaseWindow { /// /// 是否乡镇提取 /// private bool _isXz = false; /// /// 坐落单位字典 /// private List ZLDMdataDic = null; private List DLBMDics = null; public FrmSJTQ() { InitializeComponent(); rbXZ.IsChecked = true; InitData(); } private void InitData() { ProjectInfo info = (MapsManager.Instance.CurrProjectInfo as ProjectInfo); //行政区下拉框绑定值 ComboxSelXZQ.ItemsSource = GetZLDWDMList().Values; //默认-地类图斑 SeleComboxLayer.SelectedIndex = 0; DLBMDics = Platform.Instance.DicHelper.GetDic(DicTypeEnum.DLBM); } /// /// 是否禁用乡镇下拉框 /// /// /// private void RadioButton_Checked(object sender, RoutedEventArgs e) { RadioButton radioButton = sender as RadioButton; if (radioButton.Content.ToString() == "提取全部基础地类图斑数据") { _isXz = false; ComboxSelXZQ.IsEnabled = false; } else { _isXz = true; ComboxSelXZQ.IsEnabled = true; } } /// /// 图层改变事件 /// /// /// private void SeleComboxLayer_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (SeleComboxLayer.SelectedIndex.ToString() == "0") { rbXZ.IsEnabled = false; rbALL.IsEnabled = false; ComboxSelXZQ.IsEnabled = false; } else { rbXZ.IsEnabled = true; rbALL.IsEnabled = true; ComboxSelXZQ.IsEnabled = true; if (rbALL.IsChecked == true) ComboxSelXZQ.IsEnabled = false; } } /// /// 提取图斑 /// /// /// private void BtnTBTQ_Clic(object sender, RoutedEventArgs e) { btnOK.IsEnabled = false; btnCanel.IsEnabled = false; try { //IFeatureClass jctbFC = MapsManager.Instance.MapService.GetFeatureClassByName("JCTB"); string SelIndex = SeleComboxLayer.SelectedIndex.ToString(); if (ComboxSelXZQ.SelectedItems.Count <= 0 && _isXz) { MessageHelper.ShowTips("请选择乡镇数据!"); return; } IFeatureClass TargetFC = MapsManager.Instance.MapService.GetFeatureClassByName("DLTBBG"); IFeatureLayer tempBG_Layer = GeoDBAPI.CreateFeatureLayerInmemeory("TempBG", "TempBG", (TargetFC as IGeoDataset).SpatialReference, TargetFC.ShapeType, TargetFC.Fields); ITable pTable = TargetFC as ITable; if (pTable.RowCount(null) != 0) { System.Windows.Forms.DialogResult result = MessageHelper.ShowYesNoCancelAndTips("变更图斑层数据不为空,是否清空现有数据?"); if (result == System.Windows.Forms.DialogResult.Yes) { pTable.DeleteSearchedRows(null); } else if (result == System.Windows.Forms.DialogResult.Cancel) { return; } } this.ShowLoading("正在进行数据提取~", 0, 0); int SJLYIndex = TargetFC.FindField("SJLY"); int BGTBYBHIndex = TargetFC.FindField("TBYBH"); int BGDLBMIndex = TargetFC.FindField("DLBM"); int BGDLMCIndex = TargetFC.FindField("DLMC"); if (SelIndex == "0") { #region 地类图斑提取数据 IFeatureClassAPI fCAPI_JCDLTB = new FeatureClassAPI(MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑").FeatureClass); string currentAppPath = SysAppPath.GetCurrentAppPath(); string templeteGDBPath = Path.Combine(currentAppPath, "Template", "TempGDB.gdb"); string resultPath = Path.Combine(currentAppPath, "Temp\\Temp_TQBGTB"); string savePath = Path.Combine(resultPath, Guid.NewGuid().ToString() + ".gdb"); DelectDir(resultPath); CopyDirectory(templeteGDBPath, savePath, true); WorkspaceAPI workspaceAPI_Temp = new WorkspaceAPI(savePath, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.GDBFile, false); IQueryFilter queryFilter = new QueryFilterClass(); if (_isXz) { string where = string.Empty; if (GetZLDWDMList().Count != ComboxSelXZQ.SelectedItems.Count) { foreach (var item in ComboxSelXZQ.SelectedItems) { where += string.Format(" ZLDWDM like '{0}%' or", item.ToString().Split('_')[0].Substring(0, 9)); } where = where.Substring(0, where.Length - 2); queryFilter.WhereClause = where; } fCAPI_JCDLTB.FcToFc(tempBG_Layer.FeatureClass, queryFilter, false); } else { fCAPI_JCDLTB.FcToFc(tempBG_Layer.FeatureClass, null, false); } IFeatureLayer JCTBFL = MapsManager.Instance.MapService.GetFeatureLayerByName("JCTB"); if (JCTBFL.FeatureClass.FeatureCount(null) == 0)//无监测图斑直接提取 { fCAPI_JCDLTB.FcToFc(TargetFC, queryFilter, false); this.CloseLoading(); MessageBox.Show("提取完成!"); return; } IFeatureLayer SelectFeatureLayer = null; GPParamClass gPParamClass = new GPParamClass { Tolerance = "0.0001", FirstFeatureLayer = JCTBFL, SecondFeatureLayer = tempBG_Layer, IsGetOutPutFeature = true, Overlap_type = "INTERSECT" }; GeoprocessorHelper.SelectLayerByLocationAnalysis(gPParamClass, ref SelectFeatureLayer); //基础图斑与监测图斑相交, IFeatureLayer featureLayer = null; gPParamClass = new GPParamClass { FirstFeatureLayer = tempBG_Layer, SecondFeatureLayer = new FeatureLayerClass() { FeatureClass = JCTBFL.FeatureClass }, OutFeatureClassPath = savePath + "\\DLTBGX_UNION", IsGetOutPutFeature = true }; GeoprocessorHelper.UnionAnalysis(gPParamClass, ref featureLayer); int indexBsm = featureLayer.FeatureClass.FindField("BSM"); int indexBSM = tempBG_Layer.FeatureClass.FindField("BSM"); StringBuilder stringBuilder = new StringBuilder(); StringBuilder JCOIDSBuilder = new StringBuilder(); StringBuilder JCSJLYBuilder = new StringBuilder(); Dictionary keyValuePairs = new Dictionary(); Dictionary JCValuePairs = new Dictionary(); ICursor pCur = null; IRow pRow = null; IQueryDef pQDef = ((featureLayer.FeatureClass as FeatureClass).Workspace as IFeatureWorkspace).CreateQueryDef(); pQDef.Tables = "DLTBGX_UNION"; pQDef.WhereClause = " 1=1 AND FID_JCTB<>-1 AND BSM<>'' AND SHAPE_Area>=10.0 "; pQDef.SubFields = "FID_JCTB,BSM,TBBH,FID_TempBG"; pCur = pQDef.Evaluate(); while ((pRow = pCur.NextRow()) != null) { //存储监测图斑OIDS JCOIDSBuilder.Append(string.Format("{0},", pRow.get_Value(0).ToString())); //存储基础库图斑OIDS stringBuilder.Append(string.Format("{0},", pRow.get_Value(3).ToString())); if (!string.IsNullOrWhiteSpace(pRow.get_Value(2).ToString())) { if (!keyValuePairs.ContainsKey(pRow.get_Value(1).ToString())) { //存储BSM和监测编号 keyValuePairs[pRow.get_Value(1).ToString()] = pRow.get_Value(2).ToString(); //存储BSM和监测OIDS JCValuePairs[pRow.get_Value(1).ToString()] = pRow.get_Value(0).ToString(); } else { //单个BSM对应多个JCBH if (!keyValuePairs[pRow.get_Value(1).ToString()].Contains(pRow.get_Value(2).ToString())) keyValuePairs[pRow.get_Value(1).ToString()] += "," + pRow.get_Value(2).ToString(); } } Marshal.ReleaseComObject(pRow); } Marshal.ReleaseComObject(pCur); //擦除变更层中监测图斑 IFeatureClassAPI JC_FcAPI = new FeatureClassAPI(JCTBFL.FeatureClass); IFeatureLayer jctb_Layer = GeoDBAPI.CreateFeatureLayerInmemeory("JCTBTemp", "JCTBTempData", (JCTBFL.FeatureClass as IGeoDataset).SpatialReference, JCTBFL.FeatureClass.ShapeType, JCTBFL.FeatureClass.Fields); //与基础库图斑相交上的监测图斑 if (JCOIDSBuilder.Length > 0) JC_FcAPI.FcToFc(jctb_Layer.FeatureClass, new QueryFilterClass() { WhereClause = string.Format("OBJECTID in({0}) ", JCOIDSBuilder.ToString().Trim(',')) }, false); else JC_FcAPI.FcToFc(jctb_Layer.FeatureClass, null, false); //上一步生成的变更图斑擦除监测图斑 IFeatureLayer pdltbbgFeatureLayer = null; gPParamClass = new GPParamClass { FirstFeatureLayer = tempBG_Layer, SecondFeatureLayer = jctb_Layer, OutFeatureClassPath = savePath + "\\" + "TempDLTBBG", IsGetOutPutFeature = true }; GeoprocessorHelper.EraseAnalysis(gPParamClass, ref pdltbbgFeatureLayer); IFeatureLayer TempTBBG_Erase = null; gPParamClass = new GPParamClass { FirstFeatureLayer = pdltbbgFeatureLayer, OutFeatureClassPath = savePath + "\\" + "TempTBBG_Erase", IsGetOutPutFeature = true }; GeoprocessorHelper.MultipartToSinglePath(gPParamClass, ref TempTBBG_Erase); IFeatureClassAPI Tebgfc_API = new FeatureClassAPI(TempTBBG_Erase.FeatureClass); queryFilter.WhereClause = " SHAPE_Area >= 0.1"; Tebgfc_API.FcToFc(TargetFC, queryFilter, false); IFeatureLayer featureLayer_Intercet = null; gPParamClass = new GPParamClass { FirstFeatureLayer = tempBG_Layer, SecondFeatureLayer = jctb_Layer, Tolerance = "0.0001", IsGetOutPutFeature = true, OutFeatureClassPath = savePath + "\\" + "TempTBBG_Intersect", }; GeoprocessorHelper.IntersectAnalysis(gPParamClass, ref featureLayer_Intercet); int Index_JCTBYBH = featureLayer_Intercet.FeatureClass.FindField("TBYBH"); int Index_JCBH = featureLayer_Intercet.FeatureClass.FindField("TBBH"); int Index_JCSJLY = featureLayer_Intercet.FeatureClass.FindField("SJLY"); ITable table_JC = featureLayer_Intercet.FeatureClass as ITable; ICursor cursor_JC = table_JC.Update(null, true); IRow row_JC = null; while ((row_JC = cursor_JC.NextRow()) != null) { row_JC.Value[Index_JCTBYBH] = row_JC.Value[Index_JCBH]; row_JC.Value[Index_JCSJLY] = "1"; cursor_JC.UpdateRow(row_JC); } cursor_JC.Flush(); IFeatureClassAPI Tejcfc_API = new FeatureClassAPI(featureLayer_Intercet.FeatureClass); Tejcfc_API.FcToFc(TargetFC, null, false); //根据变更BSM修改数据来源 //if (JCSJLYBuilder.Length != 0) // (TargetFC as FeatureClass).Workspace.ExecuteSQL(string.Format("Update DLTBBG set SJLY = '1' WHERE BSM IN({0})", JCSJLYBuilder.ToString().TrimEnd(','))); if (null != featureLayer) Marshal.FinalReleaseComObject(featureLayer); if (TargetFC != null) Marshal.FinalReleaseComObject(TargetFC); if (tempBG_Layer != null) Marshal.FinalReleaseComObject(tempBG_Layer); if (jctb_Layer != null) Marshal.FinalReleaseComObject(jctb_Layer); if (featureLayer_Intercet != null) Marshal.FinalReleaseComObject(featureLayer_Intercet); #endregion } this.CloseLoading(); MessageBox.Show("提取完成!"); } catch (Exception ex) { MessageBox.Show(ex.Message); LogAPI.Debug(ex.Message); this.CloseLoading(); } finally { btnOK.IsEnabled = true; btnCanel.IsEnabled = true; } } private void BtnTBTQ_Click(object sender, RoutedEventArgs e) { btnOK.IsEnabled = false; btnCanel.IsEnabled = false; IFeatureLayer featureLayer = null; IFeatureLayer tempBG_Layer = null; ICursor pCur = null; try { string SelIndex = SeleComboxLayer.SelectedIndex.ToString(); if (ComboxSelXZQ.SelectedItems.Count <= 0 && _isXz) { MessageHelper.ShowTips("请选择乡镇数据!"); return; } IFeatureClass TargetFC = MapsManager.Instance.MapService.GetFeatureClassByName("DLTBBG"); tempBG_Layer = GeoDBAPI.CreateFeatureLayerInmemeory("TempBG", "TempBG", (TargetFC as IGeoDataset).SpatialReference, TargetFC.ShapeType, TargetFC.Fields); ITable pTable = TargetFC as ITable; if (pTable.RowCount(null) != 0) { System.Windows.Forms.DialogResult result = MessageHelper.ShowYesNoCancelAndTips("变更图斑层数据不为空,是否清空现有数据?"); if (result == System.Windows.Forms.DialogResult.Yes) { pTable.DeleteSearchedRows(null); } else if (result == System.Windows.Forms.DialogResult.Cancel) { return; } } this.ShowLoading("正在进行数据提取~", 0, 0); int SJLYIndex = TargetFC.FindField("SJLY"); int BGTBYBHIndex = TargetFC.FindField("TBYBH"); int BGDLBMIndex = TargetFC.FindField("DLBM"); int BGDLMCIndex = TargetFC.FindField("DLMC"); if (SelIndex == "0") { #region 地类图斑提取数据 IFeatureClassAPI fCAPI_JCDLTB = new FeatureClassAPI(MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑").FeatureClass); string currentAppPath = SysAppPath.GetCurrentAppPath(); string templeteGDBPath = Path.Combine(currentAppPath, "Template", "TempGDB.gdb"); string resultPath = Path.Combine(currentAppPath, "Temp\\Temp_TQBGTB"); string savePath = Path.Combine(resultPath, Guid.NewGuid().ToString() + ".gdb"); DelectDir(resultPath); CopyDirectory(templeteGDBPath, savePath, true); WorkspaceAPI workspaceAPI_Temp = new WorkspaceAPI(savePath, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.GDBFile, false); IQueryFilter queryFilter = new QueryFilterClass(); if (_isXz) { string where = string.Empty; if (GetZLDWDMList().Count != ComboxSelXZQ.SelectedItems.Count) { foreach (var item in ComboxSelXZQ.SelectedItems) { where += string.Format(" ZLDWDM like '{0}%' or", item.ToString().Split('_')[0].Substring(0, 9)); } where = where.Substring(0, where.Length - 2); queryFilter.WhereClause = where; } fCAPI_JCDLTB.FcToFc(tempBG_Layer.FeatureClass, queryFilter, false); } else { fCAPI_JCDLTB.FcToFc(tempBG_Layer.FeatureClass, null, false); } IFeatureLayer JCTBFL = MapsManager.Instance.MapService.GetFeatureLayerByName("JCTB"); if (JCTBFL.FeatureClass.FeatureCount(null) == 0)//无监测图斑直接提取 { this.CloseLoading(); MessageBox.Show("监测图斑没有数据!"); return; } IFeatureLayer SelectFeatureLayer = null; GPParamClass gPParamClass = new GPParamClass { Tolerance = "0.0001", FirstFeatureLayer = JCTBFL, SecondFeatureLayer = tempBG_Layer, IsGetOutPutFeature = true, Overlap_type = "INTERSECT" }; GeoprocessorHelper.SelectLayerByLocationAnalysis(gPParamClass, ref SelectFeatureLayer); //基础图斑与监测图斑相交, gPParamClass = new GPParamClass { FirstFeatureLayer = tempBG_Layer, //SecondFeatureLayer = new FeatureLayerClass() { FeatureClass = JCTBFL.FeatureClass }, SecondFeatureLayer = JCTBFL, OutFeatureClassPath = savePath + "\\DLTBGX_UNION", IsGetOutPutFeature = true }; GeoprocessorHelper.UnionAnalysis(gPParamClass, ref featureLayer); int indexBsm = featureLayer.FeatureClass.FindField("BSM"); int indexBSM = tempBG_Layer.FeatureClass.FindField("BSM"); int DLBMIndex = TargetFC.FindField("DLBM"); StringBuilder JCSJLYBuilder = new StringBuilder(); IRow pRow = null; IQueryDef pQDef = ((featureLayer.FeatureClass as FeatureClass).Workspace as IFeatureWorkspace).CreateQueryDef(); pQDef.Tables = "DLTBGX_UNION"; pQDef.WhereClause = " 1=1 AND FID_JCTB<>-1 AND BSM<>'' AND SHAPE_Area>=10.0 "; pQDef.SubFields = "FID_JCTB,BSM,TBBH,FID_TempBG,NYRDDL";//NYRDDL替换当前基础图斑地类 pCur = pQDef.Evaluate(); while ((pRow = pCur.NextRow()) != null) { if (!string.IsNullOrWhiteSpace(pRow.get_Value(1).ToString()) && !string.IsNullOrWhiteSpace(pRow.get_Value(4).ToTrim())) { if (TargetFC.Fields.Field[DLBMIndex].Length >= pRow.get_Value(4).ToTrim().Length) { pRow.Value[DLBMIndex] = pRow.get_Value(4).ToTrim(); pCur.UpdateRow(pRow); } } Marshal.ReleaseComObject(pRow); } pCur.Flush(); //if (!string.IsNullOrWhiteSpace(JCSJLYBuilder.ToString().Trim(','))) queryFilter.WhereClause = " 1=1 AND FID_JCTB<>-1 AND BSM<>'' AND SHAPE_Area>=10.0 "; ////queryFilter.WhereClause = string.Format(" BSM IN({0})", JCSJLYBuilder.ToString().Trim(',')); //else //{ // MessageHelper.ShowTips("当前未提取到数据!"); // if (null != featureLayer) // Marshal.FinalReleaseComObject(featureLayer); // if (tempBG_Layer != null) // Marshal.FinalReleaseComObject(tempBG_Layer); // if (pCur != null) // Marshal.ReleaseComObject(pCur); // return; //} IFeatureClassAPI classAPI = new FeatureClassAPI(featureLayer.FeatureClass); classAPI.FcToFc(TargetFC, queryFilter, false); #endregion } this.CloseLoading(); MessageBox.Show("提取完成!"); } catch (Exception ex) { MessageBox.Show(ex.Message); LogAPI.Debug(ex.Message); this.CloseLoading(); } finally { this.CloseLoading(); btnOK.IsEnabled = true; btnCanel.IsEnabled = true; if (null != featureLayer) Marshal.FinalReleaseComObject(featureLayer); if (tempBG_Layer != null) Marshal.FinalReleaseComObject(tempBG_Layer); if (pCur != null) Marshal.ReleaseComObject(pCur); } } /// /// 删除该路径下的所有文件和目录 /// /// public static void DelectDir(string srcPath) { try { DirectoryInfo dir = new DirectoryInfo(srcPath); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { try { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } catch (Exception) { continue; } } } catch (Exception ex) { LogAPI.Debug(ex + ":此项错误不处理!");//文件占用报错 } } /// /// 拷贝模板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 Dictionary GetZLDWDMList() { try { ZLDMdataDic = Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.QSDM); Dictionary keyValuePairs = new Dictionary(); if (ZLDMdataDic != null) { foreach (var item in ZLDMdataDic.FindAll(x => x.CODE.Length == 9)) { keyValuePairs[item.CODE] = item.CODE + "_" + item.NAME; } } //验证代码BUG LogAPI.Debug("获取坐落代码数量:" + keyValuePairs.Count); return keyValuePairs; } catch (Exception ex) { LogAPI.Debug("获取坐落代码失败:" + ex.Message); return null; } } private void BtnCanel_Click(object sender, RoutedEventArgs e) { this.Close(); } } }