using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using KGIS.Framework.AE;
using KGIS.Framework.Maps;
using KGIS.Framework.OpenData.Control;
using KGIS.Framework.OpenData.Filter;
using KGIS.Framework.OpenData.InterFace;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.ExtensionMethod;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace KGIS.Plugin.BoundaryReadjust
{
public class JXTZBGHelper
{
///
/// 面积调整(平差)
///
///
///
///
/// 地类图斑面积
public static void AreaAdjustment(List shpList, string pGeoPropertyName, double pKZMJ)
{
Type type = shpList[0].GetType();
System.Reflection.PropertyInfo pertyInfo = type.GetProperty(pGeoPropertyName);
if (shpList.Count == 1)
{
pertyInfo.SetValue(shpList[0], pKZMJ);
return;
}
double sumMJ = 0;
foreach (var item in shpList)
{
sumMJ += pertyInfo.GetValue(item).ToDouble();
}
shpList = shpList.OrderByDescending(o => pertyInfo.GetValue(o)).ToList();
double mjc = Math.Round(sumMJ - pKZMJ, 2);//打散后的图斑面积与压盖地类图斑面积是否一致
//平差
while (Math.Round(mjc, 2) != 0)
{
int tpsm = (int)(Math.Abs(Math.Round(mjc, 2)) / 0.01);
int e = tpsm / shpList.Count;
int f = tpsm % shpList.Count;
if (f != 0)
{
double tpmj = (e + 1) * 0.01;
for (int i = 0; i < f; i++)
{
if (mjc < 0)
pertyInfo.SetValue(shpList[i], pertyInfo.GetValue(shpList[i]).ToDouble() + tpmj);
else
pertyInfo.SetValue(shpList[i], pertyInfo.GetValue(shpList[i]).ToDouble() - tpmj);
}
}
if (e != 0)
{
double tpmj = e * 0.01;
for (int i = f; i < shpList.Count; i++)
{
if (mjc < 0)
pertyInfo.SetValue(shpList[i], pertyInfo.GetValue(shpList[i]).ToDouble() + tpmj);
else
pertyInfo.SetValue(shpList[i], pertyInfo.GetValue(shpList[i]).ToDouble() - tpmj);
}
}
sumMJ = 0;
foreach (var item in shpList)
{
sumMJ += pertyInfo.GetValue(item).ToDouble();
}
mjc = Math.Round(sumMJ, 2) - pKZMJ;
}
}
///
/// 插入数据到☞要素类
///
///
///
///
public static void InsertDataToMemeoryLayer(IFeatureClass pSource, IFeatureClass pTarget, IQueryFilter pFilter = null)
{
try
{
if (pSource == null || pTarget == null) return;
int idx = pSource.FindField("XZQTZLX");
if (idx != -1 && pFilter != null)
{
IField field = pSource.Fields.Field[idx];
if (field.Type == esriFieldType.esriFieldTypeString)
{
pFilter.WhereClause = "XZQTZLX = '3'";
}
}
IFeatureClassAPI fcAPI = new FeatureClassAPI(pSource);
if (pTarget != null)
fcAPI.FcToFc(pTarget, pFilter, false);
}
catch (Exception ex)
{
LogAPI.Debug(ex);
}
}
#region 获取最新标识码
public static string GetNewBSM(List fcList)
{
string result = string.Empty;
int xh = 0;
string leftStr = string.Empty;
foreach (var item in fcList)
{
string MaxBSM = GetMaxBSM(item);
if (MaxBSM.Length != 18)
continue;
int xh2 = Convert.ToInt32(MaxBSM.Substring(10));
leftStr = MaxBSM.Substring(0, 10);
if (xh < xh2)
xh = xh2;
}
xh++;
if (string.IsNullOrWhiteSpace(leftStr))
leftStr = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + "1210";
result = leftStr + xh.ToString().PadLeft(8, '0');
return result;
}
private static string GetMaxBSM(IFeatureClass fc)
{
System.Windows.Forms.Application.DoEvents();
string BSM = string.Empty;
int BSMIndex = fc.FindField("BSM");
if (BSMIndex == -1) return BSM;
ITable table = (ITable)fc;
// 创建一个ITableSort接口对象
ITableSort tableSort = new TableSortClass();
tableSort.Table = table;
tableSort.Fields = "BSM";
tableSort.set_Ascending("BSM", false);
tableSort.Sort(null);
ICursor cursor = tableSort.Rows;
IRow row = cursor.NextRow();
if (row != null)
{
int maxBSM = 0;
int currBSM = 0;
string BSMStr = row.Value[BSMIndex].ToString();
if (BSMStr.Length != 18) return BSM;
string subBSMStr = BSMStr.Substring(9);
try
{
currBSM = Convert.ToInt32(subBSMStr);
}
catch (Exception)
{
return BSM;
}
if (currBSM > maxBSM) maxBSM = currBSM;
if (BSMStr.Length != 18) return BSM;
string maxStr = maxBSM.ToString();
int zeroNum = 9 - maxStr.Length;
for (int i = 0; i < zeroNum; i++)
{
maxStr = 0 + maxStr;
}
BSM = BSMStr.Substring(0, 9) + maxStr;
}
return BSM;
}
#endregion
///
/// 删除文件夹以及子文件夹与子文件-
///
///
public static void DelectDir(string srcPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(srcPath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
File.Delete(i.FullName); //删除指定文件
}
}
}
catch (Exception)
{
throw;
}
}
///
/// 得到要素类集合
///
///
/// 路径集合
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;
}
}
}
return result;
}
catch (Exception ex)
{
LogAPI.Debug("选择数据失败:" + ex.Message.ToString());
return result;
}
}
}
}