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

1208 lines
57 KiB

6 months ago
using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Grid;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using KGIS.Framework.AE;
using KGIS.Framework.AE.Enum;
using KGIS.Framework.AE.GPHelper;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.ThreadManager;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.ExtensionMethod;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.Views;
using KUI.Windows;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
namespace Kingo.Plugin.AttributeMaintain.View
{
/// <summary>
/// FrmSXWH_20.xaml 的交互逻辑
/// </summary>
public partial class FrmSXWH_20 : UserControl, IDockPanel2
{
IFeatureLayer pjbntFeatureLayer = null;
private static string TempfilePath = string.Empty;
private static int SHAPE_Area = 30;
private static bool IsShow = false;
private IFeatureClass targetFc;
private DataTable IntoData { get; set; }
public FrmSXWH_20()
{
InitializeComponent();
DevExpress.Xpf.Core.ThemeManager.SetTheme(this, DevExpress.Xpf.Core.Theme.Office2013LightGray);
_MapService = MapsManager.Instance.MapService;
this.Loaded += ViewXZQInto_Loaded;
IsShowInMap = true;
DockAreas = DockStyle.DockBottom;
DockHeight = 380;
DefaultArea = DockStyle.DockBottom;
ShowCloseButton = true;
ShowAutoHideButton = false;
Title = "变更图斑";
IsShow = false;
}
private void ViewXZQInto_Loaded(object sender, RoutedEventArgs e)
{
ThreadManager.Instance.QueueUserWorkItem(new System.Threading.WaitCallback(Init), null);
}
private void Init(object parm)
{
try
{
this.Dispatcher.Invoke(() =>
{
loading.Visibility = Visibility.Visible;
});
LoadData();
}
catch (Exception ex)
{
LogAPI.Debug(ex);
}
finally
{
this.Dispatcher.Invoke(() =>
{
loading.Visibility = Visibility.Collapsed;
});
}
}
#region IDockPanel2接口
public bool IsShowInMap { get; set; }
public Guid ID { get; set; }
public DockStyle DockAreas { get; set; }
public System.Drawing.Size FloatSize { get; set; }
public int DockWidth { get; set; }
public int DockHeight { get; set; }
public DockStyle DefaultArea { get; set; }
public bool ShowCloseButton { get; set; }
public bool ShowAutoHideButton { get; set; }
public string Title { get; set; }
public event EventHandler CloseViewHandler;
public void ClosePanel()
{
Platform.Instance.CloseView(this);
}
public void ClosePanelInvoke()
{
CloseViewHandler?.Invoke(null, null);
}
public void ShowPanel()
{
Platform.Instance.OpenView(this, false);
MapsManager.Instance.MapService.ProjectClosed += (s, e) =>
{
this.ClosePanel();
};
}
#endregion
private void CheckBox_Click_1(object sender, RoutedEventArgs e)
{
try
{
if (dgInto.SelectedItem != null)
{
DataRowView dr = (dgInto.SelectedItem as DataRowView);
if ((sender as CheckBox).IsChecked == true)
{
dr["IsValid"] = true;
}
else
{
dr["IsValid"] = false;
}
}
int length = 0;
foreach (DataRow item in (dgInto.ItemsSource as DataTable).Rows)
{
if (item[0].ToString() == "True")
{
length++;
}
}
}
catch (Exception ex)
{
LogAPI.Debug(ex);
}
}
private void DgInto_CustomColumnGroup(object sender, DevExpress.Xpf.Grid.CustomColumnSortEventArgs e)
{
}
private void LoadData()
{
string WhereClause = string.Empty;
targetFc = MapsManager.Instance.MapService.GetFeatureClassByName("DLTBBG");
if (targetFc == null)
return;
IntoData = new DataTable();
if (!IsShow)
{
WhereClause = $" 1=2 ";
}
else
{
//WhereClause = $" SHAPE_Area<{SHAPE_Area} or SJLY='CZCDYD' ";
WhereClause = $"SJLY='CZCDYD' ";
}
IFeatureCursor cursor = targetFc.Search(new QueryFilter() { WhereClause = WhereClause }, true);
try
{
ConstructColumn(targetFc.Fields);
IFeature f = null;
while ((f = cursor.NextFeature()) != null)
{
DataRow dr = IntoData.NewRow();
bool IsInto = true;
for (int i = 0; i < IntoData.Columns.Count; i++)
{
if (i == 0)
{
dr[i] = false;
continue;
}
object obj = f.get_Value((int)IntoData.Columns[i].ExtendedProperties["index"]);
if (obj == null)
{
continue;
}
else
{
if ((obj.ToString()).Contains("1899/12/30 0:00:00"))
{
obj = System.DBNull.Value;
}
}
if (obj is string)
{
obj = obj.ToString().Trim();//字符串时,去空格
}
dr[i] = obj;
}
if (IsInto)
IntoData.Rows.Add(dr);
}
this.Dispatcher.Invoke(() =>
{
dgInto.ItemsSource = null;
dgInto.ItemsSource = IntoData;
dgInto.Columns[0].AllowEditing = DevExpress.Utils.DefaultBoolean.True;
});
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 构造列获取记录条数
/// </summary>
/// <param name="fields"></param>
private void ConstructColumn(IFields fields)
{
if (fields != null)
{
IntoData = new DataTable();
DataColumn col1 = new DataColumn();
col1.ColumnName = "IsValid";
col1.Caption = "选中";
col1.DataType = typeof(bool);
IntoData.Columns.Add(col1);
for (int i = 0; i < fields.FieldCount; i++)
{
IField field = fields.get_Field(i);
if (field.Name.ToUpper().EndsWith("SHAPE"))
continue;
DataColumn col = new DataColumn();
col.ExtendedProperties.Add("index", i);
col.ColumnName = field.Name;
col.Caption = field.AliasName;
switch (field.Type)
{
case esriFieldType.esriFieldTypeSmallInteger:
col.DataType = typeof(short);
break;
case esriFieldType.esriFieldTypeInteger:
col.DataType = typeof(int);
break;
case esriFieldType.esriFieldTypeSingle:
break;
case esriFieldType.esriFieldTypeDouble:
col.DataType = typeof(double);
break;
case esriFieldType.esriFieldTypeString:
col.DataType = typeof(string);
break;
case esriFieldType.esriFieldTypeDate:
col.DataType = typeof(DateTime);
break;
case esriFieldType.esriFieldTypeOID:
col.DataType = typeof(Int32);
break;
case esriFieldType.esriFieldTypeGeometry:
break;
case esriFieldType.esriFieldTypeBlob:
break;
case esriFieldType.esriFieldTypeRaster:
break;
case esriFieldType.esriFieldTypeGUID:
break;
case esriFieldType.esriFieldTypeGlobalID:
break;
case esriFieldType.esriFieldTypeXML:
break;
default:
break;
}
col.ReadOnly = !field.Editable;//编辑状态启用默认是否可以编辑
IntoData.Columns.Add(col);
System.Runtime.InteropServices.Marshal.ReleaseComObject(field);
}
}
}
private void DgInto_AutoGeneratedColumns(object sender, RoutedEventArgs e)
{
GridControl grid = sender as GridControl;
foreach (GridColumn column in grid.Columns)
{
if (IntoData.Columns.Contains(column.FieldName))
{
string caption = IntoData.Columns[column.FieldName].Caption;
if (caption != null)
{
column.EditSettings = new TextEditSettings() { HorizontalContentAlignment = EditSettingsHorizontalAlignment.Left };
column.Header = caption;
}
}
}
}
#region 根据基础城镇村等用地提取变更图斑
private void BtnImportData_Click(object sender, RoutedEventArgs e)
{
btnImportData.IsEnabled = false;
btnMergeData.IsEnabled = false;
try
{
this.ShowLoading("正在生成地类图斑变更层,请稍后...", 0, 0);
if (this.isBG.IsChecked == true)
{
ImportData(false);
}
if (this.isPassiveBG.IsChecked == true)
{
ImportData1(false);
}
this.CloseLoading();
MessageHelper.ShowTips("地类图斑变更层赋值完成!");
IsShow = true;
LoadData();
}
catch (Exception ex)
{
MessageHelper.ShowError("根据基础城镇村等用地提取变更图斑异常:" + ex.Message);
LogAPI.Debug("根据基础城镇村等用地提取变更图斑异常:" + ex);
}
finally
{
this.CloseLoading();
btnImportData.IsEnabled = true;
btnMergeData.IsEnabled = true;
}
}
private void ImportData1(bool IsMergeData)
{
IFeatureLayer DLTBBG_Intersect = null;
IFeatureLayer DLTB_UnionLayer = null;
IFeatureLayer DLTB_SelectLayer = null;
IFeatureLayer JC_CZCDYDLayer = null;
IFeatureLayer JC_DLTBLayer = null;
IFeatureLayer DLTBBG_NotJSYD = null;
IFeatureLayer DLTBBGLayer = null;
IWorkspaceFactory pFtWsFct = null;
IFeature s = null;
IFeatureCursor pCursor = null;
IFeatureCursor t_cursor = null;
IPolygon pPolygon = null;
GPParamClass paramClass = null;
IWorkspaceAPI tempBGAPI = null;
try
{
//if (IsMergeData)
//{
// this.UpdateMsg("正在生成地类图斑变更层,请稍后...");
//}
//else
//{
// this.ShowLoading("正在生成地类图斑变更层,请稍后...", 0, 0);
//}
DLTBBGLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG");//地类图斑变更层
JC_CZCDYDLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("城镇村等用地");
JC_DLTBLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑");
string gdbFolder = Directory.GetCurrentDirectory() + "\\Temp\\DLTBBG_20PassiveBG";
if (!Directory.Exists(gdbFolder))
{
Directory.CreateDirectory(gdbFolder);
}
PluginServiceInterface.CommonHelper.DelectDir(gdbFolder);//能删除就删除 删除报错不处理
pFtWsFct = new FileGDBWorkspaceFactory();
string gdbFileName = Guid.NewGuid().ToString();
string path = System.IO.Path.Combine(gdbFolder, gdbFileName);
pFtWsFct.Create(path, "TempGDB", null, 0);
TempfilePath = System.IO.Path.Combine(path, "TempGDB.gdb");//临时数据存放路径
tempBGAPI = new WorkspaceAPI(TempfilePath, KGIS.Framework.AE.Enum.WorkspaceTypeEnum.GDBFile);
IFeatureClassAPI dltbbg = tempBGAPI.CreateFeatureClass("DLTBBGtemp", (DLTBBGLayer.FeatureClass as IGeoDataset).SpatialReference, DLTBBGLayer.FeatureClass.Fields);
IFeatureClassAPI fcAPI = new FeatureClassAPI(DLTBBGLayer.FeatureClass);
fcAPI.FcToFc(dltbbg.FeatureClass, new QueryFilterClass() { WhereClause = " DLBM not in('1001','1002','1003','1007','1008','1009','1109') " }, true);
DLTBBG_NotJSYD = new FeatureLayer() { FeatureClass = dltbbg.FeatureClass, Name = "地类图斑变更" };
#region 获取变更图斑所在的基础地类图斑
paramClass = new GPParamClass
{
Tolerance = "-0.01",
FirstFeatureLayer = JC_DLTBLayer,
SecondFeatureLayer = DLTBBG_NotJSYD,
IsGetOutPutFeature = true,
Overlap_type = "INTERSECT",
GPType = EnumGPType.SelectLayerByLocation
};
Kingo.Plugin.AttributeMaintain.Helper.GPHelper.Instance.ExeGPForProces(paramClass, ref DLTB_SelectLayer);
paramClass = new GPParamClass()
{
FirstFeatureLayer = DLTBBG_NotJSYD,
SecondFeatureLayer = DLTB_SelectLayer,
OutFeatureClassPath = $"{TempfilePath}\\Union",
IsGetOutPutFeature = true,
Tolerance = "-0.01",
PreserveAttributes = "ALL"
};
GeoprocessorHelper.UnionAnalysis(paramClass, ref DLTB_UnionLayer);
#endregion
#region 更新层与基础地类图斑相交
paramClass = new GPParamClass()
{
FirstFeatureLayer = DLTB_UnionLayer,
SecondFeatureLayer = JC_CZCDYDLayer,
OutFeatureClassPath = $"{TempfilePath}\\Intersect",
IsGetOutPutFeature = true,
Tolerance = "-0.01",
PreserveAttributes = "ALL"
};
GeoprocessorHelper.IntersectAnalysis2(paramClass, ref DLTBBG_Intersect);
#endregion
#region 拆分多部件
paramClass = new GPParamClass();
paramClass.FirstFeatureLayer = DLTBBG_Intersect;
paramClass.OutFeatureClassPath = $"{TempfilePath}\\Multipart";
paramClass.IsGetOutPutFeature = true;
GeoprocessorHelper.MultipartToSinglePath(paramClass, ref pjbntFeatureLayer);
#endregion
this.UpdateMsg("正在进行地类图斑变更层属性赋值,请稍后...");
#region 获取对应关系
Dictionary<int, int> target_SourMaping = new Dictionary<int, int>();
Dictionary<int, int> target_SourMaping_jc = new Dictionary<int, int>();
Dictionary<string, object> fieldValues = new Dictionary<string, object>();
Dictionary<string, object> fieldValues_jc = new Dictionary<string, object>();
for (int i = 0; i < DLTBBGLayer.FeatureClass.Fields.FieldCount; i++)
{
IField field = DLTBBGLayer.FeatureClass.Fields.Field[i];
if (!field.Editable || field.Name.Contains(DLTBBGLayer.FeatureClass.ShapeFieldName)) continue;
int sIndex = pjbntFeatureLayer.FeatureClass.FindField(field.Name);
if (sIndex != -1)
{
target_SourMaping.Add(i, sIndex);
fieldValues.Add(field.Name, sIndex);
}
sIndex = pjbntFeatureLayer.FeatureClass.FindField($"{field.Name}_1");
if (sIndex != -1)
{
target_SourMaping_jc.Add(i, sIndex);
fieldValues_jc.Add(field.Name, sIndex);
}
}
#endregion
#region 属性赋值
t_cursor = DLTBBGLayer.FeatureClass.Insert(true);
var count = pjbntFeatureLayer.FeatureClass.FeatureCount(new QueryFilter() { WhereClause = $" (CZCSXM <> CZCLX or CZCSXM is null) and (czcsxm_1<>czclx or czcsxm_1 is null) and SHAPE_Area>{SHAPE_Area} " });
var featurecount = 0;
pCursor = pjbntFeatureLayer.FeatureClass.Search(new QueryFilter() { WhereClause = $" (CZCSXM <> CZCLX or CZCSXM is null) and (czcsxm_1<>czclx or czcsxm_1 is null) and SHAPE_Area>{SHAPE_Area} " }, true);
int i_CZCDYDLX = pjbntFeatureLayer.FeatureClass.Fields.FindField("CZCLX");
int i_CZCSXM = DLTBBGLayer.FeatureClass.Fields.FindField("CZCSXM");
int i_sjly = DLTBBGLayer.FeatureClass.Fields.FindField("sjly");
int i_bg_dlbm = DLTBBGLayer.FeatureClass.Fields.FindField("DLBM");
int i_temp = pjbntFeatureLayer.FeatureClass.Fields.FindField("FID_DLTBBGtemp");
List<string> jsyd = new List<string>() { "1001", "1002", "1003", "1007", "1008", "1009", "1109" };
while ((s = pCursor.NextFeature()) != null)
{
featurecount++;
if (featurecount % 100 == 0 || featurecount == count)
this.UpdateMsg($"正在进行变更数据属性维护...【{featurecount}/{count}】");
pPolygon = s.ShapeCopy as IPolygon;
List<IFeature> bgFeatures = FeatureAPI.Identify2(pPolygon, DLTBBGLayer);
if (this.isPassiveBG.IsChecked == false)
{
if (bgFeatures.Count == 0) continue;
}
bool iscontinue = false;
foreach (IFeature item in bgFeatures)
{
#region 道路河流不涉及分割
if (jsyd.Contains(item.Value[i_bg_dlbm].ToTrim()))
{
iscontinue = true;
continue;
}
#endregion
IGeometry DiffGeo = FeatureAPI.Difference(item.ShapeCopy, pPolygon);
if (DiffGeo.IsEmpty)
{
if (string.IsNullOrEmpty(item.Value[i_CZCSXM].ToTrim()))
{
item.Value[i_CZCSXM] = s.Value[i_CZCDYDLX].ToTrim();
item.Store();
}
iscontinue = true;
continue;
}
ITopologicalOperator topologicalOperator = DiffGeo as ITopologicalOperator;
if (topologicalOperator == null) continue;
topologicalOperator.Simplify();
if (item.ShapeCopy.GeometryType == esriGeometryType.esriGeometryPolygon)
{
if ((topologicalOperator as IPolygon4) == null || (topologicalOperator as IPolygon4).ExteriorRingCount <= 0)
{
continue;
}
//外环图形
if (!((topologicalOperator as IPolygon4).ExteriorRingBag is GeometryBag ExterGeometryBag)) continue;
//IGeometry firstGeo = null;
IGeometryCollection ExterRingGeometryCollection = ExterGeometryBag as IGeometryCollection;
for (int i = 0; i < ExterRingGeometryCollection.GeometryCount; i++)
{
IGeometryCollection geometry = new PolygonClass() as IGeometryCollection;
IGeometry ExterGeometry = ExterRingGeometryCollection.get_Geometry(i);
if (ExterGeometry != null && !ExterGeometry.IsEmpty)
{
geometry.AddGeometry(ExterGeometry);
}
else
{
continue;
}
//内环图形
IGeometryBag InteriorBag = (topologicalOperator as IPolygon4).get_InteriorRingBag(ExterGeometry as IRing);
if (InteriorBag != null)
{
if (!(InteriorBag is IGeometryCollection InteriorRingGeometryCollection)) continue;
for (int IG = 0; IG < InteriorRingGeometryCollection.GeometryCount; IG++)
{
IGeometry interiorGeo = InteriorRingGeometryCollection.get_Geometry(IG);
if (interiorGeo != null && !interiorGeo.IsEmpty)
{
geometry.AddGeometry(interiorGeo);
}
}
}
if (i == 0)
{
item.Shape = geometry as IGeometry;
item.Store();
}
else
{
IFeature newFeaturte = (item.Class as IFeatureClass).CreateFeature();
IFeatureEdit pFeatureEdit = item as IFeatureEdit;
pFeatureEdit.SplitAttributes(newFeaturte);
(geometry as IGeometry).SpatialReference = item.ShapeCopy.SpatialReference;
newFeaturte.Shape = geometry as IGeometry;
newFeaturte.Store();
}
}
}
}
if (iscontinue) continue;
IFeatureBuffer buf = DLTBBGLayer.FeatureClass.CreateFeatureBuffer();
var dltbbg_temp = s.Value[i_temp].ToString();
if (dltbbg_temp == "-1")
{
foreach (var item in target_SourMaping_jc)
{
if (fieldValues_jc["CZCSXM"].ToString() == item.Value.ToString())
{
if (string.IsNullOrEmpty(s.Value[item.Value].ToTrim()))
{
buf.Value[item.Key] = s.Value[i_CZCDYDLX];
}
else
{
buf.Value[item.Key] = s.Value[item.Value];
}
}
else
{
buf.Value[item.Key] = s.Value[item.Value];
}
}
}
else
{
foreach (var item in target_SourMaping)
{
if (fieldValues["CZCSXM"].ToString() == item.Value.ToString())
{
if (string.IsNullOrEmpty(s.Value[item.Value].ToTrim()))
{
buf.Value[item.Key] = s.Value[i_CZCDYDLX];
}
else
{
buf.Value[item.Key] = s.Value[item.Value];
}
}
else
{
buf.Value[item.Key] = s.Value[item.Value];
}
}
}
buf.Value[i_sjly] = "CZCDYD1";
buf.Shape = s.ShapeCopy;
t_cursor.InsertFeature(buf);
Marshal.ReleaseComObject(s);
}
t_cursor.Flush();
#endregion
//this.CloseLoading();
//MessageHelper.ShowTips("地类图斑变更层赋值完成!");
//IsShow = true;
//LoadData();
}
catch (Exception ex)
{
LogAPI.Debug("生成地类图斑变更数据失败:" + ex.Message);
//this.CloseLoading();
throw ex;
}
finally
{
//this.CloseLoading();
if (DLTBBGLayer != null)
Marshal.ReleaseComObject(DLTBBGLayer);
if (JC_CZCDYDLayer != null)
Marshal.ReleaseComObject(JC_CZCDYDLayer);
//if (Temp_DLTBGXGC != null)
// Marshal.ReleaseComObject(Temp_DLTBGXGC);
if (s != null)
Marshal.ReleaseComObject(s);
if (pCursor != null)
Marshal.ReleaseComObject(pCursor);
if (t_cursor != null)
Marshal.ReleaseComObject(t_cursor);
if (pPolygon != null)
Marshal.ReleaseComObject(pPolygon);
}
}
private void ImportData(bool IsMergeData)
{
IFeatureLayer Temp_DLTBGXGC = null;
IFeatureLayer JC_CZCDYDLayer = null;
IFeatureLayer DLTBBG_NotJSYD = null;
IFeatureLayer DLTBBGLayer = null;
IWorkspaceFactory pFtWsFct = null;
IFeature s = null;
IFeatureCursor pCursor = null;
IFeatureCursor t_cursor = null;
IPolygon pPolygon = null;
try
{
//if (!IsMergeData)
// this.ShowLoading("正在生成地类图斑变更层,请稍后...", 0, 0);
DLTBBGLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG");//地类图斑变更层
JC_CZCDYDLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("城镇村等用地");
DLTBBG_NotJSYD = GeoDBAPI.CreateFeatureLayerInmemeory("DLTBBG", "地类图斑变更非道路", (JC_CZCDYDLayer.FeatureClass as IGeoDataset).SpatialReference, DLTBBGLayer.FeatureClass.ShapeType, DLTBBGLayer.FeatureClass.Fields);
InsertDataToMemeoryLayer(DLTBBGLayer.FeatureClass, DLTBBG_NotJSYD.FeatureClass, new QueryFilterClass() { WhereClause = " DLBM not in('1001','1002','1003','1007','1008','1009','1109') " });
string gdbFolder = Directory.GetCurrentDirectory() + "\\Temp\\DLTBBG_20BG";
if (!Directory.Exists(gdbFolder))
{
Directory.CreateDirectory(gdbFolder);
}
PluginServiceInterface.CommonHelper.DelectDir(gdbFolder);//能删除就删除 删除报错不处理
pFtWsFct = new FileGDBWorkspaceFactory();
string gdbFileName = Guid.NewGuid().ToString();
string path = System.IO.Path.Combine(gdbFolder, gdbFileName);
pFtWsFct.Create(path, "TempGDB", null, 0);
TempfilePath = System.IO.Path.Combine(path, "TempGDB.gdb");//临时数据存放路径
#region 更新层与基础地类图斑相交
GPParamClass paramClass = new GPParamClass()
{
FirstFeatureLayer = DLTBBG_NotJSYD,
SecondFeatureLayer = JC_CZCDYDLayer,
OutFeatureClassPath = $"{TempfilePath}\\DLTBBG",
IsGetOutPutFeature = true,
Tolerance = "0.0001",
PreserveAttributes = "ALL"
};
GeoprocessorHelper.IntersectAnalysis2(paramClass, ref Temp_DLTBGXGC);
#endregion
#region 拆分多部件
GPParamClass gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureLayer = Temp_DLTBGXGC;
gPParamClass.OutFeatureClassPath = $"{TempfilePath}\\DLTBBG_Multipart";
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.MultipartToSinglePath(gPParamClass, ref pjbntFeatureLayer);
#endregion
#region 获取对应关系
Dictionary<int, int> target_SourMaping = new Dictionary<int, int>();
Dictionary<string, object> fieldValues = new Dictionary<string, object>();
for (int i = 0; i < DLTBBGLayer.FeatureClass.Fields.FieldCount; i++)
{
IField field = DLTBBGLayer.FeatureClass.Fields.Field[i];
if (!field.Editable || field.Name.Contains(DLTBBGLayer.FeatureClass.ShapeFieldName)) continue;
int sIndex = pjbntFeatureLayer.FeatureClass.FindField(field.Name);
if (sIndex != -1)
{
target_SourMaping.Add(i, sIndex);
fieldValues.Add(field.Name, sIndex);
}
}
#endregion
#region 属性赋值
t_cursor = DLTBBGLayer.FeatureClass.Insert(true);
var count = pjbntFeatureLayer.FeatureClass.FeatureCount(new QueryFilter() { WhereClause = " SHAPE_Area>30 " });
var featurecount = 0;
pCursor = pjbntFeatureLayer.FeatureClass.Search(new QueryFilter() { WhereClause = " SHAPE_Area>30 " }, true);
int i_CZCDYDLX = pjbntFeatureLayer.FeatureClass.Fields.FindField("CZCLX");
int i_SHAPE_Area = pjbntFeatureLayer.FeatureClass.Fields.FindField("SHAPE_Area");
int i_CZCSXM = DLTBBGLayer.FeatureClass.Fields.FindField("CZCSXM");
int i_sjly = DLTBBGLayer.FeatureClass.Fields.FindField("sjly");
while ((s = pCursor.NextFeature()) != null)
{
featurecount++;
if (featurecount % 100 == 0 || featurecount == count)
this.UpdateMsg($"正在进行变更数据属性维护...【{featurecount}/{count}】");
pPolygon = s.ShapeCopy as IPolygon;
List<IFeature> jcFeatures = FeatureAPI.Identify2(pPolygon, DLTBBGLayer);
bool iscontinue = false;
foreach (IFeature item in jcFeatures)
{
IGeometry DiffGeo = FeatureAPI.Difference(item.ShapeCopy, pPolygon);
if (DiffGeo.IsEmpty)
{
if (string.IsNullOrEmpty(item.Value[i_CZCSXM].ToTrim()))
{
item.Value[i_CZCSXM] = s.Value[i_CZCDYDLX].ToTrim();
item.Store();
}
iscontinue = true;
continue;
}
ITopologicalOperator topologicalOperator = DiffGeo as ITopologicalOperator;
if (topologicalOperator == null) continue;
topologicalOperator.Simplify();
if (item.ShapeCopy.GeometryType == esriGeometryType.esriGeometryPolygon)
{
if ((topologicalOperator as IPolygon4) == null || (topologicalOperator as IPolygon4).ExteriorRingCount <= 0)
{
continue;
}
//外环图形
if (!((topologicalOperator as IPolygon4).ExteriorRingBag is GeometryBag ExterGeometryBag)) continue;
//IGeometry firstGeo = null;
IGeometryCollection ExterRingGeometryCollection = ExterGeometryBag as IGeometryCollection;
for (int i = 0; i < ExterRingGeometryCollection.GeometryCount; i++)
{
IGeometryCollection geometry = new PolygonClass() as IGeometryCollection;
IGeometry ExterGeometry = ExterRingGeometryCollection.get_Geometry(i);
if (ExterGeometry != null && !ExterGeometry.IsEmpty)
{
geometry.AddGeometry(ExterGeometry);
}
else
{
continue;
}
//内环图形
IGeometryBag InteriorBag = (topologicalOperator as IPolygon4).get_InteriorRingBag(ExterGeometry as IRing);
if (InteriorBag != null)
{
if (!(InteriorBag is IGeometryCollection InteriorRingGeometryCollection)) continue;
for (int IG = 0; IG < InteriorRingGeometryCollection.GeometryCount; IG++)
{
IGeometry interiorGeo = InteriorRingGeometryCollection.get_Geometry(IG);
if (interiorGeo != null && !interiorGeo.IsEmpty)
{
geometry.AddGeometry(interiorGeo);
}
}
}
if (i == 0)
{
item.Shape = geometry as IGeometry;
item.Store();
}
else
{
IFeature newFeaturte = (item.Class as IFeatureClass).CreateFeature();
IFeatureEdit pFeatureEdit = item as IFeatureEdit;
pFeatureEdit.SplitAttributes(newFeaturte);
(geometry as IGeometry).SpatialReference = item.ShapeCopy.SpatialReference;
newFeaturte.Shape = geometry as IGeometry;
newFeaturte.Store();
}
}
}
}
if (iscontinue) continue;
IFeatureBuffer buf = DLTBBGLayer.FeatureClass.CreateFeatureBuffer();
foreach (var item in target_SourMaping)
{
if (fieldValues["CZCSXM"].ToString() == item.Value.ToString())
{
if (string.IsNullOrEmpty(s.Value[item.Value].ToTrim()))
{
buf.Value[item.Key] = s.Value[i_CZCDYDLX];
}
else
{
buf.Value[item.Key] = s.Value[item.Value];
}
}
else
{
buf.Value[item.Key] = s.Value[item.Value];
}
}
buf.Shape = s.ShapeCopy;
buf.Value[i_sjly] = "CZCDYD";
t_cursor.InsertFeature(buf);
Marshal.ReleaseComObject(s);
}
t_cursor.Flush();
#endregion
//if (!IsMergeData)
//{
// this.CloseLoading();
// MessageHelper.ShowTips("地类图斑变更层赋值完成!");
// IsShow = true;
// LoadData();
//}
//else
//{
// MergeData(false);
//}
}
catch (Exception ex)
{
//if (!IsMergeData)
// this.CloseLoading();
LogAPI.Debug("生成地类图斑变更数据失败:" + ex.Message);
throw ex;
}
finally
{
if (DLTBBGLayer != null)
Marshal.ReleaseComObject(DLTBBGLayer);
if (JC_CZCDYDLayer != null)
Marshal.ReleaseComObject(JC_CZCDYDLayer);
if (Temp_DLTBGXGC != null)
Marshal.ReleaseComObject(Temp_DLTBGXGC);
if (s != null)
Marshal.ReleaseComObject(s);
if (pCursor != null)
Marshal.ReleaseComObject(pCursor);
if (t_cursor != null)
Marshal.ReleaseComObject(t_cursor);
if (pPolygon != null)
Marshal.ReleaseComObject(pPolygon);
if (DLTBBG_NotJSYD != null)
Marshal.ReleaseComObject(DLTBBG_NotJSYD);
}
}
#endregion
#region InsertDataToMemeoryLayer
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);
}
}
#endregion
#region 将叠置结果层面积小于 30 平方米的碎块,找变更地类图斑层同城镇村类型且同坐落的数据合并
//1.面积<10的新增20图斑不需切分,使用原图形--质检规则允许
//2.面积<10的非新增20图斑不处理
//3.10<面积<30的 需要提取基础地类图斑 与20范围相交的部分(不包含1004,1006)
//相邻存在变更图斑 相邻边长最大的且同城镇村类型且同坐落的数据合并(不包含1004,1006)
//相邻不存在变更图斑 提取相邻边长最大的且同城镇村类型且同坐落的基础地类图斑数据合并(不包含1004,1006)
private void BtnMergeData_Click(object sender, RoutedEventArgs e)
{
btnImportData.IsEnabled = false;
btnMergeData.IsEnabled = false;
try
{
if (dgInto.SelectedItem == null || ((dgInto.ItemsSource as DataTable).Rows.Count == 0))
{
MessageHelper.ShowTips("未检测到需要合并的数据...");
return;
}
MergeData();
}
catch (Exception ex)
{
LogAPI.Debug(ex.Message + ex.StackTrace);
}
finally
{
btnImportData.IsEnabled = true;
btnMergeData.IsEnabled = true;
}
}
private bool isImportData = false;
private int LastMergeDataCount = 0;
private int MaxMergrDataCount = 0;
private void MergeData(bool IsFirst = true)
{
IPolygon pPolygon = null;
IFeatureCursor pDLTBBGCursor = null;
IFeature s = null;
IFeatureLayer DLTBBGLayer = null;
IFeatureLayer JC_DLTBLayer = null;
IFeatureLayer JC_DLTBLayer_BG = null;
IFeatureClass pjbntFeatureClass = null;
WorkspaceAPI wsAPI = null;
ITable dltb = null;
string WhereClause = string.Empty;
string strObj = string.Empty;
IFeatureCursor t_cursor = null;
try
{
if (!string.IsNullOrEmpty(TempfilePath))
{
wsAPI = new WorkspaceAPI(TempfilePath, WorkspaceTypeEnum.GDBFile);
if (wsAPI != null)
{
if (IsFirst)
this.ShowLoading("正在将同城镇村类型且同坐落的数据合并...", 0, 0);
System.Threading.Thread.Sleep(1000);
DLTBBGLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG");//地类图斑变更层
JC_DLTBLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑");//基础地类图斑
pjbntFeatureClass = wsAPI.OpenFeatureClass("Multipart").FeatureClass;
int i_CZCDYDLX = pjbntFeatureClass.Fields.FindField("CZCLX");
int i_CZCSXM = DLTBBGLayer.FeatureClass.Fields.FindField("CZCSXM");
#region 将叠置结果层面积小于 30 平方米的碎块,找变更地类图斑层同城镇村类型且同坐落的数据合并
if (!string.IsNullOrEmpty(SArea.Text))
{
if (SArea.Text.ToTrim().ToInt() > 0)
SHAPE_Area = SArea.Text.ToTrim().ToInt();
}
if (dgInto.SelectedItem != null)
{
foreach (DataRow item in (dgInto.ItemsSource as DataTable).Rows)
{
if (item["IsValid"].ToString() == "True")
strObj += item["OBJECTID"] + ",";
}
}
if (!string.IsNullOrEmpty(strObj))
{
WhereClause = $"OBJECTID in ({strObj.Substring(0, strObj.Length - 1)})";
JC_DLTBLayer_BG = JC_DLTBLayer;
}
else
{
WhereClause = $" SHAPE_Area<{SHAPE_Area} ";
//筛选出基础地类图斑与变更图斑有共边的图形
GPParamClass gPParamClass = new GPParamClass
{
Tolerance = "-0.0001655555",
FirstFeatureLayer = JC_DLTBLayer,
SecondFeatureLayer = DLTBBGLayer,
IsGetOutPutFeature = true,
Overlap_type = "SHARE_A_LINE_SEGMENT_WITH"
};
GeoprocessorHelper.SelectLayerByLocationAnalysis(gPParamClass, ref JC_DLTBLayer_BG);
gPParamClass = new GPParamClass
{
FirstFeatureLayer = JC_DLTBLayer,
OutFeatureClassPath = TempfilePath,
FcName = "Temp_JC_DLTB",
IsGetOutPutFeature = true
};
GeoprocessorHelper.FeatureClassToFeatureClass(gPParamClass, ref JC_DLTBLayer_BG);
}
pDLTBBGCursor = DLTBBGLayer.FeatureClass.Search(new QueryFilter() { WhereClause = WhereClause }, true);
int iZLDWDM = DLTBBGLayer.FeatureClass.FindField("ZLDWDM");
int iZCZSXM = DLTBBGLayer.FeatureClass.FindField("ZCZSXM");
int iSHAPR_Area = DLTBBGLayer.FeatureClass.FindField("SHAPE_Area");
int iTBMJ = DLTBBGLayer.FeatureClass.FindField("TBMJ");
int iBSM = DLTBBGLayer.FeatureClass.FindField("BSM");
int iDLBM = DLTBBGLayer.FeatureClass.FindField("DLBM");
int ijcDLBM = JC_DLTBLayer_BG.FeatureClass.FindField("DLBM");
List<int> delOID = new List<int>();
var count = DLTBBGLayer.FeatureClass.FeatureCount(new QueryFilter() { WhereClause = WhereClause });
int featurecount = 0;
#region 获取对应关系
Dictionary<int, int> target_SourMaping = new Dictionary<int, int>();
Dictionary<string, object> fieldValues = new Dictionary<string, object>();
for (int i = 0; i < DLTBBGLayer.FeatureClass.Fields.FieldCount; i++)
{
IField field = DLTBBGLayer.FeatureClass.Fields.Field[i];
if (!field.Editable || field.Name.Contains(DLTBBGLayer.FeatureClass.ShapeFieldName)) continue;
int sIndex = JC_DLTBLayer.FeatureClass.FindField(field.Name);
if (sIndex != -1)
{
target_SourMaping.Add(i, sIndex);
fieldValues.Add(field.Name, sIndex);
}
}
#endregion
t_cursor = DLTBBGLayer.FeatureClass.Insert(true);
while ((s = pDLTBBGCursor.NextFeature()) != null)
{
featurecount++;
if (featurecount % 100 == 0 || featurecount == count)
this.UpdateMsg($"正在将同城镇村类型且同坐落的数据合并...【{featurecount}/{count}】");
var sBSM = s.Value[iBSM].ToTrim();
var sZLDWDM = s.Value[iZLDWDM].ToTrim();
var sCZCSXM = s.Value[i_CZCSXM].ToTrim();
var sSHAPR_Area = s.Value[iSHAPR_Area].ToDouble();
var sDLBM = s.Value[iDLBM].ToTrim();
var sTBMJ = s.Value[iTBMJ].ToDouble();
double maxmj = 0;
pPolygon = s.ShapeCopy as IPolygon;
List<IFeature> bgFeatures = FeatureAPI.Identify(pPolygon, DLTBBGLayer);
IFeature temppFeature = null;
foreach (var item in bgFeatures)
{
if (s.OID == item.OID) continue;
var itemBSM = item.Value[iBSM].ToTrim();
var itemZLDWDM = item.Value[iZLDWDM].ToTrim();
var itemCZCSXM = item.Value[i_CZCSXM].ToTrim();
var itemSHAPR_Area = item.Value[iSHAPR_Area].ToDouble();
var itemTBMJ = item.Value[iTBMJ].ToDouble();
var itemDLBM = item.Value[iDLBM].ToTrim();
//面积<10的新增20图斑不需切分,使用原图形--质检规则允许
if (itemZLDWDM == sZLDWDM && sSHAPR_Area < 10 && !string.IsNullOrEmpty(itemCZCSXM))
{
temppFeature = item;
continue;
}
//标识码,面积相同的 1004/1006合并
if (itemBSM == sBSM && itemTBMJ == sTBMJ && sDLBM == "1004")
{
temppFeature = item;
continue;
}
if (itemZLDWDM == sZLDWDM && itemCZCSXM == sCZCSXM && sDLBM != "1004" && sDLBM != "1006" && itemDLBM != "1004" && itemDLBM != "1006")
{
if (itemSHAPR_Area > maxmj)
{
maxmj = itemSHAPR_Area;
temppFeature = item;
}
}
}
if (temppFeature != null)
{
var itemLengthOfSide = FeatureAPI.LengthOfSide(temppFeature.ShapeCopy, pPolygon);
if (itemLengthOfSide == 0) continue;
temppFeature.Shape = FeatureAPI.Union(temppFeature.ShapeCopy, s.ShapeCopy);
temppFeature.Store();
delOID.Add(s.OID);
}
else
{
if (sDLBM == "1004" || sDLBM == "1006") continue;
temppFeature = null;
//相邻不存在变更图斑 提取相邻边长最大的且同城镇村类型且同坐落的基础地类图斑数据合并(不包含1004,1006)
List<IFeature> jcFeatures = FeatureAPI.Identify(pPolygon, JC_DLTBLayer_BG);
List<string> JSYDDMs = new List<string>() { "1001", "1002", "1003", "1007", "1008", "1009", "1109" };
var maxLengthOfSide = 0.0;
foreach (var item in jcFeatures)
{
if (FeatureAPI.Identify2(item.ShapeCopy, DLTBBGLayer).Count > 0) continue;
if (JSYDDMs.Contains(item.Value[ijcDLBM].ToTrim())) continue;//基础地类图斑中的道路不提取
var itemLengthOfSide = FeatureAPI.LengthOfSide(item.ShapeCopy, pPolygon);
if (itemLengthOfSide > maxLengthOfSide)
{
maxLengthOfSide = itemLengthOfSide;
temppFeature = item;
}
}
if (temppFeature != null)
{
IFeatureBuffer buf = DLTBBGLayer.FeatureClass.CreateFeatureBuffer();
foreach (var item in target_SourMaping)
{
if (temppFeature.Value[item.Value] != null && temppFeature.Value[item.Value].ToString().Length <= DLTBBGLayer.FeatureClass.Fields.get_Field(item.Key).Length)
buf.Value[item.Key] = temppFeature.Value[item.Value];
}
buf.Shape = FeatureAPI.Union(temppFeature.ShapeCopy, s.ShapeCopy);
t_cursor.InsertFeature(buf);
isImportData = true;
delOID.Add(s.OID);
}
}
Marshal.ReleaseComObject(s);
t_cursor.Flush();
}
this.CloseLoading();
dltb = DLTBBGLayer.FeatureClass as ITable;
if (delOID.Count > 0)
dltb.DeleteSearchedRows(new QueryFilter() { WhereClause = $" OBJECTID in({String.Join(",", delOID)}) " });//删除更新过程层数据
#endregion
if ((isImportData && LastMergeDataCount != count) || MaxMergrDataCount > 4)
{
LastMergeDataCount = count;
MaxMergrDataCount++;
//this.UpdateMsg("正在生成地类图斑变更层,请稍后...");
ImportData1(true);
}
else
{
this.CloseLoading();
MessageHelper.ShowTips("同城镇村类型且同坐落的数据合并完成!");
MaxMergrDataCount = 0;
LoadData();
}
}
}
}
catch (Exception ex)
{
LogAPI.Debug("变更地类图斑层同城镇村类型且同坐落的数据合并:" + ex.Message + ex.StackTrace);
this.CloseLoading();
MessageHelper.ShowError("变更地类图斑层同城镇村类型且同坐落的数据异常!");
}
finally
{
#region 释放变量
this.CloseLoading();
if (pPolygon != null)
Marshal.ReleaseComObject(pPolygon);
if (pDLTBBGCursor != null)
Marshal.ReleaseComObject(pDLTBBGCursor);
if (s != null)
Marshal.ReleaseComObject(s);
if (DLTBBGLayer != null)
Marshal.ReleaseComObject(DLTBBGLayer);
if (pjbntFeatureClass != null)
Marshal.ReleaseComObject(pjbntFeatureClass);
if (wsAPI != null)
wsAPI.CloseWorkspace();
if (t_cursor != null)
Marshal.ReleaseComObject(t_cursor);
if (JC_DLTBLayer_BG != null)
Marshal.ReleaseComObject(JC_DLTBLayer_BG);
if (dltb != null)
Marshal.ReleaseComObject(dltb);
#endregion
}
}
#endregion
#region 定位选中图斑
private IMapService _MapService { get; set; }
private void TvAttr2_RowDoubleClick(object sender, RowDoubleClickEventArgs e)
{
try
{
DataRowView obj = e.Source.FocusedRowData.Row as DataRowView;
string layerName = "DLTBBG";
if (obj != null)
{
_MapService.SelectFeature(layerName, obj["OBJECTID"].ToTrim(), true);
}
}
catch (Exception ex)
{
MessageHelper.Show("定位失败!" + ex.Message);
}
}
#endregion
#region 刷新数据
private void BtnRefreshData_Click(object sender, RoutedEventArgs e)
{
LoadData();
}
#endregion
#region MyRegion
/// <summary>
/// 反选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CbUnSelect_Click(object sender, RoutedEventArgs e)
{
foreach (DataRow item in IntoData.Rows)
{
item["IsValid"] = !(bool)item["IsValid"];
}
}
/// <summary>
/// 取消选择
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CbSelectAll_Click(object sender, RoutedEventArgs e)
{
foreach (DataRow item in IntoData.Rows)
{
item["IsValid"] = false;
}
}
#endregion
}
}