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

1842 lines
92 KiB

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
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.ExtensionMethod;
using KGIS.Framework.AE.GPHelper;
using KGIS.Framework.DBOperator;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.Platform.Helper;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.ExtensionMethod;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.Utils.Utility;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using KUI.Windows;
namespace Kingo.Plugin.CZC_IDG.Helper
{
public class CZCDYDHelper : IDataChange
{
private IFeatureLayer _DLTBFc = null;
public string IDataChangeName { get => "CZCDYDHelper"; }
#region ExecuteDataChange
public void ExecuteDataChange(DataChangeParameter pParm)
{
IFeatureClass _CZCFc = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("城镇村等用地").FeatureClass;
Implement(_CZCFc, true, false);
}
#endregion
#region implement
public void Implement(IFeatureClass _CZCFc, bool ckbDelMinMJ, bool ckbXDM)
{
try
{
ExtractExe(_CZCFc, ckbDelMinMJ);
string time = new DateTime(DateTime.Now.Year - 1, 12, 31).ToShortDateString();
string dbPath = $"{(MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ProjDir}\\BGTJ.sqlite";
IRDBHelper rdbHelper = RDBFactory.CreateDbHelper("Data Source=" + dbPath, DatabaseType.SQLite);
//DataTable bhxxbDt = rdbHelper.ExecuteDatatable("BHXXB", "select * from BHXXB where BZ='ZLBG' and BGXW='1'", true);
//Dictionary<string, string> dicZLBG = new Dictionary<string, string>();
//Dictionary<string, int> dicXZQDM = new Dictionary<string, int>();
//List<string> BGQTBBSM = new List<string>();
//foreach (DataRow dr in bhxxbDt.Rows)
//{
// string bgqzldwdm = dr["BGQZLDWDM"].ToTrim();
// string bghzldwdm = dr["BGHZLDWDM"].ToTrim();
// string bgqzldwmc = dr["BGQZLDWMC"].ToTrim();
// string bghzldwmc = dr["BGHZLDWMC"].ToTrim();
// string bgqtbbsm = dr["BGQTBBSM"].ToTrim();
// string bgq = bgqzldwdm + "_" + bgqzldwmc;
// string bgh = bghzldwdm + "_" + bghzldwmc;
// if (bgq != bgh && !dicZLBG.ContainsKey(bgq))
// {
// dicZLBG.Add(bgq, bgh);
// if (!dicXZQDM.ContainsKey(bgqzldwdm.Substring(0, 9)))
// dicXZQDM.Add(bgqzldwdm.Substring(0, 9), 0);
// }
// if (bgq != bgh)
// {
// if (!BGQTBBSM.Contains(bgqtbbsm))
// {
// BGQTBBSM.Add(bgqtbbsm);
// }
// }
//}
IGeometry BGQDLTBUnion = null;
_DLTBFc = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑");
int jcbsmIndex = _DLTBFc.FeatureClass.FindField("BSM");
IFeature f = null;
#region 融合
string gdbFolder = Directory.GetCurrentDirectory() + "\\Temp\\CZCDYDBGTempFile";
if (!Directory.Exists(gdbFolder))
{
Directory.CreateDirectory(gdbFolder);
}
try
{
DelectDir(gdbFolder);//能删除就删除 删除报错不处理
}
catch
{ }
IWorkspaceFactory pFtWsFct = new FileGDBWorkspaceFactory();
string gdbFileName = Guid.NewGuid().ToString();
string path = System.IO.Path.Combine(gdbFolder, gdbFileName);
pFtWsFct.Create(path, "TempGDB", null, 0);
string TempfilePath = System.IO.Path.Combine(path, "TempGDB.gdb");//临时数据存放路径
IWorkspaceAPI wsAPI = new WorkspaceAPI(TempfilePath, WorkspaceTypeEnum.GDBFile);
IFeatureClassAPI CZCDYDBGTempFeatureclassAPI = wsAPI.CreateFeatureClass("CZCDYDBGTemp", (_DLTBFc.FeatureClass as IGeoDataset).SpatialReference, _DLTBFc.FeatureClass.Fields);
IFeatureCursor t_cursor = CZCDYDBGTempFeatureclassAPI.FeatureClass.Insert(true);
IFeatureCursor bgqcursor = _DLTBFc.Search(new QueryFilterClass() { SubFields = "BSM,Shape" }, true);
while ((f = bgqcursor.NextFeature()) != null)
{
string bsm = f.Value[jcbsmIndex].ToTrim();
//if (!BGQTBBSM.Contains(bsm)) continue;
IFeatureBuffer buf = CZCDYDBGTempFeatureclassAPI.FeatureClass.CreateFeatureBuffer();
buf.Shape = f.ShapeCopy;
t_cursor.InsertFeature(buf);
}
t_cursor.Flush();
GPParamClass gPParamClass = new GPParamClass()
{
FirstFeatureClass = CZCDYDBGTempFeatureclassAPI.FeatureClass,
OutFeatureClassPath = TempfilePath + "\\" + "NewCZCDYD",
IsGetOutPutFeature = true,
};
IFeatureLayer featureLayer = null;
GeoprocessorHelper.DissolveAnalysis(gPParamClass, ref featureLayer, true);
bgqcursor = featureLayer.FeatureClass.Search(null, true);
IFeature bgqF = bgqcursor.NextFeature();
if (bgqF == null)
{
MessageHelper.Show("完成");
return;
}
BGQDLTBUnion = bgqF.ShapeCopy;
#endregion
IFeatureClass czcgxgc = MapsManager.Instance.MapService.GetFeatureClassByName("CZCDYDGXGC");
ITable czcgxgcTable = czcgxgc as ITable;
ICursor cur = czcgxgcTable.Search(new QueryFilterClass() { SubFields = "BGQBSM" }, true);
int bgqbsmIdx = czcgxgcTable.FindField("BGQBSM");
IRow row = null;
Dictionary<string, int> dicBSM = new Dictionary<string, int>();
while ((row = cur.NextRow()) != null)
{
string bgqbsm = row.Value[bgqbsmIdx].ToTrim();
if (!dicBSM.ContainsKey(bgqbsm))
{
dicBSM.Add(bgqbsm, 0);
}
}
IFeatureClass czcgx = MapsManager.Instance.MapService.GetFeatureClassByName("CZCDYDGX");
IFeatureCursor gxgcInsCur = czcgxgc.Insert(true);
IFeatureCursor gxInsCur = czcgx.Insert(true);
IFeatureBuffer gxgcBuf = null;
IFeatureBuffer gxBuf = null;
Dictionary<int, int> gcFieldMapp = new Dictionary<int, int>();
for (int i = 0; i < czcgxgc.Fields.FieldCount; i++)
{
IField field = czcgxgc.Fields.Field[i];
if (field.Name.ToUpper().Contains("SHAPE") || !field.Editable) continue;
int idx = _CZCFc.FindField(field.Name.ToUpper().Replace("BGQ", "").Replace("BGH", ""));
if (idx == -1) continue;
gcFieldMapp.Add(i, idx);
}
Dictionary<int, int> gxFieldMapp = new Dictionary<int, int>();
for (int i = 0; i < czcgx.Fields.FieldCount; i++)
{
IField field = czcgx.Fields.Field[i];
if (field.Name.ToUpper().Contains("SHAPE") || !field.Editable) continue;
int idx = _CZCFc.FindField(field.Name);
if (idx == -1) continue;
gxFieldMapp.Add(i, idx);
}
IFeatureCursor cursor = _CZCFc.Search(new SpatialFilterClass()
{
WhereClause = "CZCLX<>'201'",
Geometry = BGQDLTBUnion,
SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
}, true);
f = null;
int bsmIdx = _CZCFc.FindField("BSM");
int bgxwIdx = czcgxgc.FindField("BGXW");
int bghbsmIdx = czcgxgc.FindField("BGHBSM");
int bghczclxIdx = czcgxgc.FindField("BGHCZCLX");
int bghczcdmIdx = czcgxgc.FindField("BGHCZCDM");
int bghczcmcIdx = czcgxgc.FindField("BGHCZCMC");
//变更前
int bgqczclxIdx = czcgxgc.FindField("BGQCZCLX");
int bgqczcdmIdx = czcgxgc.FindField("BGQCZCDM");
int bgqczcmcIdx = czcgxgc.FindField("BGQCZCMC");
int bgmjIdx = czcgxgc.FindField("BGMJ");
int GXSJIdx = czcgxgc.FindField("GXSJ");
int czcmjIdx = _CZCFc.FindField("CZCMJ");
int gxBSMIdx = _CZCFc.FindField("BSM");
int czcdmIdx = _CZCFc.FindField("CZCDM");
int czcmcIdx = _CZCFc.FindField("CZCMC");
int CZCGXSJIdx = czcgx.FindField("GXSJ");
List<DataDicTionary> qsDic = Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.QSDM);
int JC_DLTBBSM = _DLTBFc.FeatureClass.FindField("BSM");
string PrjCode = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE;
while ((f = cursor.NextFeature()) != null)
{
string bsm = f.Value[bsmIdx].ToTrim();
if (dicBSM.ContainsKey(bsm)) continue;
if (ckbXDM == false)
{
if (!FeatureAPI.IsInterSect(f.ShapeCopy, BGQDLTBUnion)) continue;
}
if (gxgcBuf == null)
gxgcBuf = czcgxgc.CreateFeatureBuffer();
gxgcBuf.Shape = f.ShapeCopy;
foreach (KeyValuePair<int, int> idx in gcFieldMapp)
{
gxgcBuf.Value[idx.Key] = f.Value[idx.Value];
}
string bghczclx = gxgcBuf.Value[bghczclxIdx].ToTrim();
string bghczcdm = gxgcBuf.Value[bghczcdmIdx].ToTrim();
string bghczcmc = gxgcBuf.Value[bghczcmcIdx].ToTrim();
//变更前
string bgqczclx = gxgcBuf.Value[bgqczclxIdx].ToTrim();
string bgqczcdm = gxgcBuf.Value[bgqczcdmIdx].ToTrim();
string bgqczcmc = gxgcBuf.Value[bgqczcmcIdx].ToTrim();
string bgh = bghczcdm + "_" + bghczcmc;
string strCZCDM = bghczcdm;
string strCZCMC = bghczcmc;
if (bghczclx == "203" || bghczclx == "203A" || bghczclx == "204" || bghczclx == "205")
{
//if (dicZLBG.ContainsKey(bgh))
//{
// string[] zlArr = dicZLBG[bgh].Split('_');
// strCZCDM = zlArr[0];
// strCZCMC = zlArr[1];
// gxgcBuf.Value[bghczcdmIdx] = zlArr[0];
// gxgcBuf.Value[bghczcmcIdx] = zlArr[1];
//}
}
else if (bghczclx == "202")
{
//foreach (KeyValuePair<string, string> kvp in dicZLBG)
//{
// if (kvp.Key.StartsWith(bgh.Substring(0, 9)))
// {
// strCZCDM = kvp.Value.Substring(0, 9).PadRight(19, '0');
// gxgcBuf.Value[bghczcdmIdx] = strCZCDM;
// if (qsDic != null)
// {
// DataDicTionary dic = qsDic.FirstOrDefault(qs => qs.CODE == strCZCDM || qs.CODE.StartsWith(strCZCDM.Substring(0, 9)));
// if (dic != null)
// {
// strCZCMC = dic.NAME;
// gxgcBuf.Value[bghczcmcIdx] = strCZCMC;
// }
// }
// break;
// }
//}
}
bghczcdm = gxgcBuf.Value[bghczcdmIdx].ToTrim();
bghczcmc = gxgcBuf.Value[bghczcmcIdx].ToTrim();
if (bghczclx.Trim() == bgqczclx.Trim() && bghczcdm.Trim() == bgqczcdm.Trim() && bghczcmc.Trim() == bgqczcmc.Trim())
continue;//前后这三个字段没有前后变化则不插入
string bghbsm = gxgcBuf.Value[bghbsmIdx].ToTrim();
if (!bghbsm.StartsWith(PrjCode))
{
string strR = bghbsm.Substring(0, 6);
gxgcBuf.Value[bghbsmIdx] = bghbsm.Replace(strR, PrjCode);
}
gxgcBuf.Value[bgxwIdx] = "1";
gxgcBuf.Value[bgmjIdx] = f.Value[czcmjIdx];
gxgcBuf.Value[GXSJIdx] = time;
gxgcInsCur.InsertFeature(gxgcBuf);
if (gxBuf == null)
gxBuf = czcgx.CreateFeatureBuffer();
gxBuf.Shape = f.ShapeCopy;
foreach (KeyValuePair<int, int> idx in gxFieldMapp)
{
gxBuf.Value[idx.Key] = f.Value[idx.Value];
}
string gxbsm = gxBuf.Value[gxBSMIdx].ToTrim();
if (!gxbsm.StartsWith(PrjCode))
{
string strR = gxbsm.Substring(0, 6);
gxBuf.Value[gxBSMIdx] = gxbsm.Replace(strR, PrjCode);
}
gxBuf.Value[czcdmIdx] = strCZCDM;
gxBuf.Value[czcmcIdx] = strCZCMC;
gxBuf.Value[CZCGXSJIdx] = time;
gxInsCur.InsertFeature(gxBuf);
}
gxgcInsCur.Flush();
gxInsCur.Flush();
if (ckbXDM == true)
{
cur = czcgxgcTable.Search(new QueryFilterClass() { SubFields = "BGQBSM" }, true);
bgqbsmIdx = czcgxgcTable.FindField("BGQBSM");
row = null;
dicBSM = new Dictionary<string, int>();
while ((row = cur.NextRow()) != null)
{
string bgqbsm = row.Value[bgqbsmIdx].ToTrim();
if (!dicBSM.ContainsKey(bgqbsm))
{
dicBSM.Add(bgqbsm, 0);
}
}
cursor = _CZCFc.Search(null, true);
f = null;
while ((f = cursor.NextFeature()) != null)
{
string bsm = f.Value[bsmIdx].ToTrim();
if (dicBSM.ContainsKey(bsm)) continue;
if (gxgcBuf == null)
gxgcBuf = czcgxgc.CreateFeatureBuffer();
gxgcBuf.Shape = f.ShapeCopy;
foreach (KeyValuePair<int, int> idx in gcFieldMapp)
{
gxgcBuf.Value[idx.Key] = f.Value[idx.Value];
}
string bghbsm = gxgcBuf.Value[bghbsmIdx].ToTrim();
string bghczclx = gxgcBuf.Value[bghczclxIdx].ToTrim();
string bghczcdm = gxgcBuf.Value[bghczcdmIdx].ToTrim();
string bghczcmc = gxgcBuf.Value[bghczcmcIdx].ToTrim();
//变更前
string bgqbsm = gxgcBuf.Value[bgqbsmIdx].ToTrim();
string bgqczclx = gxgcBuf.Value[bgqczclxIdx].ToTrim();
string bgqczcdm = gxgcBuf.Value[bgqczcdmIdx].ToTrim();
string bgqczcmc = gxgcBuf.Value[bgqczcmcIdx].ToTrim();
bghbsm = gxgcBuf.Value[bghbsmIdx].ToTrim();
if (!bghbsm.StartsWith(PrjCode))
{
string strR = bghbsm.Substring(0, 6);
gxgcBuf.Value[bghbsmIdx] = bghbsm.Replace(strR, PrjCode);
}
if (!bghczcdm.StartsWith(PrjCode))
{
string strR = bghczcdm.Substring(0, 6);
gxgcBuf.Value[bghczcdmIdx] = bghczcdm.Replace(strR, PrjCode);
}
gxgcBuf.Value[bgxwIdx] = "1";
gxgcBuf.Value[bgmjIdx] = f.Value[czcmjIdx];
gxgcBuf.Value[GXSJIdx] = time;
gxgcInsCur.InsertFeature(gxgcBuf);
if (gxBuf == null)
gxBuf = czcgx.CreateFeatureBuffer();
gxBuf.Shape = f.ShapeCopy;
foreach (KeyValuePair<int, int> idx in gxFieldMapp)
{
gxBuf.Value[idx.Key] = f.Value[idx.Value];
}
string gxbsm = gxBuf.Value[gxBSMIdx].ToTrim();
if (!gxbsm.StartsWith(PrjCode))
{
string strR = gxbsm.Substring(0, 6);
gxBuf.Value[gxBSMIdx] = gxbsm.Replace(strR, PrjCode);
}
string gxCZCDM = gxBuf.Value[czcdmIdx].ToTrim();
if (!gxCZCDM.StartsWith(PrjCode))
{
string strR = gxCZCDM.Substring(0, 6);
gxBuf.Value[czcdmIdx] = gxCZCDM.Replace(strR, PrjCode);
}
gxBuf.Value[CZCGXSJIdx] = time;
gxInsCur.InsertFeature(gxBuf);
}
gxgcInsCur.Flush();
gxInsCur.Flush();
}
}
catch (Exception ex)
{
LogAPI.Debug("城镇村范围变更失败:");
LogAPI.Debug(ex);
throw ex;
}
}
#endregion
public void ExtractExe(IFeatureClass jc_Fc, bool IsDelMinMJ)
{
IFeatureLayer jc_czcLayer = null;
IFeatureLayer dltbgxLayer = null;
IFeatureLayer dltbhrLayer = null;
IFeatureLayer jcdltbLayer = null;
IFeatureLayer dltbTempNMKLayer = null;
IWorkspaceAPI Tempworkspace = null;
IWorkspaceAPI Schemeworkspace = null;
IFeatureClassAPI dltbFeatureClassAPI = null;
IFeatureClassAPI czcdydFeatureClassAPI = null;
IFeatureCursor pFeatureCursor = null;
IFeatureCursor pInsertCursor = null;
IFeatureClassAPI LSczcdydFeatureclassAPI = null;
IFeatureClassAPI pFeatureClass_4 = null;
IFeatureClassAPI CZCDLTBFeatureClass = null;
IWorkspaceFactory outputWorkspaceFactory = null;
IWorkspaceFactory pOutWorkFactory = null;
string TempfilePath = string.Empty;
try
{
ProjectInfo projectInfo = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo);
List<DataDicTionary> qsDic = Platform.Instance.DicHelper.GetNoGroupDic(DicTypeEnum.QSDM);
if (qsDic == null)
{
LogAPI.Debug($"权属单位代码表获取失败。");
return;
}
//string filePath = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).GetProjDBPath();
//string schemeDBPath = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).GetSchemeDBPath();
//ProjGDBwkspace = new WorkspaceAPI(filePath, WorkspaceTypeEnum.GDBFile);
//Schemeworkspace = new WorkspaceAPI(schemeDBPath, WorkspaceTypeEnum.GDBFile);
#region 临时数据
this.UpdateMsg("正在提取城镇村等用地数据......");
TempfilePath = GetTempPath();//临时数据存放路径
if (string.IsNullOrEmpty(TempfilePath))
{
LogAPI.Debug($"获取到城镇村等用地数据模板失败。");
return;
}
#region 地类图斑
IFeatureClass bgtbFc = MapsManager.Instance.MapService.GetFeatureClassByName("DLTBBG");
dltbgxLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBGX");
//dltbhrLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBHR");
jcdltbLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("地类图斑");
jc_czcLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("城镇村等用地");
FeatureClass featureClass = jc_czcLayer.FeatureClass as FeatureClass;
if (jc_czcLayer.FeatureClass.FeatureCount(null) == 0)
{
MessageHelper.Show("基础城镇村等用地图层未找到数据,请确认基础城镇村数据是否导入!");
return;
}
IFeatureLayer tempJCCZCLayer = null;
//设置基础地类图斑选中
GPParamClass gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureLayer = jc_czcLayer;
gPParamClass.SecondFeatureClass = dltbgxLayer.FeatureClass;
//gPParamClass.Tolerance = "-0.0001655555";
GeoprocessorHelper.SelectLayerByLocationAnalysis(gPParamClass, "INTERSECT", ref tempJCCZCLayer);
//选中的基础地类图斑导出至临时gdb
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureLayer = jc_czcLayer;
gPParamClass.OutFeatureClassPath = TempfilePath;
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.FeatureClassToFeatureClass(gPParamClass, "JC_CZCDYD", ref tempJCCZCLayer);
try
{
ESRI.ArcGIS.Carto.IFeatureSelection selection = jc_czcLayer as IFeatureSelection;
selection.Clear();
}
catch (Exception ex)
{
LogAPI.Debug(ex.Message);
}
IFeatureLayer TempJCDLTB = GeoDBAPI.CreateFeatureLayerInmemeory("CZC203", "城镇村203范围", (jcdltbLayer.FeatureClass as IGeoDataset).SpatialReference, jcdltbLayer.FeatureClass.ShapeType, jcdltbLayer.FeatureClass.Fields);
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureLayer = jcdltbLayer;
gPParamClass.SecondFeatureClass = tempJCCZCLayer.FeatureClass;
gPParamClass.OutFeatureClassPath = TempfilePath + "\\" + "TempJCDLTB20X";
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.IntersectAnalysis(gPParamClass, ref dltbTempNMKLayer);
IWorkspace tempWs = (dltbTempNMKLayer.FeatureClass as FeatureClass).Workspace;
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='201' where CZCLX='201'");
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='201A' where CZCLX='201A'");
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='202' where CZCLX='202'");
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='202A' where CZCLX='202A'");
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='203' where CZCLX='203'");
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='203A' where CZCLX='203A'");
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='204' where CZCLX='204'");
tempWs.ExecuteSQL("Update TempJCDLTB20X set CZCSXM='205' where CZCLX='205'");
IFeatureClassAPI dltb20xAPI = new FeatureClassAPI(dltbTempNMKLayer.FeatureClass);
dltb20xAPI.FcToFc(TempJCDLTB.FeatureClass, null, false);
IFeatureLayer msTBLayer = GeoDBAPI.CreateFeatureLayerInmemeory("MSDLTB", "灭失图斑", (bgtbFc as IGeoDataset).SpatialReference, bgtbFc.ShapeType, bgtbFc.Fields);
IQueryFilter filter2 = new QueryFilterClass();
filter2.WhereClause = " XZQTZLX='2' or XZQTZLX='4'";
(msTBLayer.FeatureClass as ITable).DeleteSearchedRows(null);
InsertDataToMemeoryLayer(bgtbFc, msTBLayer.FeatureClass, filter2);
IFeatureLayer pjbntFeatureLayer = null;
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureClass = TempJCDLTB.FeatureClass;
gPParamClass.SecondFeatureClass = msTBLayer.FeatureClass;
gPParamClass.OutFeatureClassPath = TempfilePath + "\\" + "TempDLTBNM";
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.EraseAnalysis(gPParamClass, ref pjbntFeatureLayer);
IFeatureLayer TempDLTB_MToS = null;
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureClass = pjbntFeatureLayer.FeatureClass;
gPParamClass.OutFeatureClassPath = TempfilePath + "\\" + "TempDLTBNM_MToS";
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.MultipartToSinglePath(gPParamClass, ref TempDLTB_MToS);
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureLayer = TempDLTB_MToS;
gPParamClass.SecondFeatureLayer = dltbgxLayer;
gPParamClass.OutFeatureClassPath = TempfilePath + "\\" + "DLTBGXGC";
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.EraseAnalysis(gPParamClass, ref dltbTempNMKLayer);
IFeatureClassAPI fcAPI = new FeatureClassAPI(dltbgxLayer.FeatureClass);
fcAPI.FcToFc(dltbTempNMKLayer.FeatureClass, null, false);
//fcAPI = new FeatureClassAPI(dltbhrLayer.FeatureClass);
//fcAPI.FcToFc(dltbTempNMKLayer.FeatureClass, null, false);
fcAPI = new FeatureClassAPI(dltbTempNMKLayer.FeatureClass);
string dbPath = $"{(MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ProjDir}\\BGTJ.sqlite";
IRDBHelper rdbHelper = RDBFactory.CreateDbHelper("Data Source=" + dbPath, DatabaseType.SQLite);
//DataTable bhxxbDt = new DataTable();
//Dictionary<string, DataRow> bhxxDic = new Dictionary<string, DataRow>();
//List<string> hcbghbsmList = new List<string>();
//Dictionary<string, DataRow> zlbgList = new Dictionary<string, DataRow>();
//if (rdbHelper.TableIsExist("BHXXB"))
//{
// bhxxbDt = rdbHelper.ExecuteDatatable("BHXXB", "select * from BHXXB", true);
// if (bhxxbDt != null)
// {
// foreach (DataRow row in bhxxbDt.Rows)
// {
// if (row["BZ"].ToTrim() == "ZLBG" && row["BGXW"].ToTrim() == "1")
// {
// string bgqbsm = row["BGQTBBSM"].ToTrim();
// if (!zlbgList.ContainsKey(bgqbsm))
// zlbgList.Add(bgqbsm, row);
// continue;
// }
// string bghBSM = row["BGHTBBSM"].ToTrim();
// if (row["BGXW"].ToTrim() == "1")
// {
// bghBSM = row["BGQTBBSM"].ToTrim();
// }
// if (bhxxDic.ContainsKey(bghBSM))
// continue;
// bhxxDic.Add(bghBSM, row);
// string xzqtzlx = row["XZQTZLX"].ToTrim();
// if (xzqtzlx == "2" && !string.IsNullOrWhiteSpace(bghBSM))
// {
// hcbghbsmList.Add(bghBSM);
// }
// }
// }
//}
ITable nmkTable = dltbTempNMKLayer.FeatureClass as ITable;
IQueryFilter filter = new QueryFilterClass();
//foreach (string bsm in hcbghbsmList)
//{
// filter.WhereClause = string.Format("BSM ='{0}' ", bsm);
// nmkTable.DeleteSearchedRows(filter);
//}
ICursor nmkCur = nmkTable.Update(null, true);
IRow nmkRow = null;
int bsmIdx = nmkTable.FindField("BSM");
int zldwIdx = nmkTable.FindField("ZLDWDM");
int zldwmcIdx = nmkTable.FindField("ZLDWMC");
int sjnfIdx = nmkTable.FindField("SJNF");//数据年份
int num1 = 0;
while ((nmkRow = nmkCur.NextRow()) != null)
{
num1++;
string bsm = nmkRow.Value[bsmIdx].ToTrim();
//if (zlbgList.ContainsKey(bsm))
//{
// DataRow dr = zlbgList[bsm];
// nmkRow.Value[zldwIdx] = dr["BGHZLDWDM"];
// nmkRow.Value[zldwmcIdx] = dr["BGHZLDWMC"];
//}
//else
//if (bhxxDic.ContainsKey(bsm))
//{
// for (int i = 0; i < nmkRow.Fields.FieldCount; i++)
// {
// IField field = nmkRow.Fields.Field[i];
// string bghField = "BGH" + field.Name;
// DataRow dr = bhxxDic[bsm];
// if (!dr.Table.Columns.Contains(bghField))
// bghField = "BGHTB" + field.Name;
// if (dr.Table.Columns.Contains(bghField))
// {
// if (field.Type == esriFieldType.esriFieldTypeString)
// nmkRow.Value[i] = dr[bghField];
// else if (field.Type == esriFieldType.esriFieldTypeDouble)
// nmkRow.Value[i] = dr[bghField].ToDouble();
// }
// }
//}
if (sjnfIdx != -1)
{
nmkRow.Value[sjnfIdx] = "2020";
}
nmkCur.UpdateRow(nmkRow);
if (num1 % 1000 == 0)
{
nmkCur.Flush();
}
}
nmkCur.Flush();
#endregion
IWorkspaceFactory TempWorkspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass();
Tempworkspace = new WorkspaceAPI(TempWorkspaceFactory.OpenFromFile(TempfilePath, 0));
dltbFeatureClassAPI = Tempworkspace.OpenFeatureClass("DLTBGXGC");
#endregion
czcdydFeatureClassAPI = new FeatureClassAPI(MapsManager.Instance.MapService.GetFeatureClassByName("CZCDYDGX"));// Schemeworkspace.OpenFeatureClass("CZCDYDGX");
if (dltbFeatureClassAPI.FeatureClass.FeatureCount(null) <= 0)
{
return;
}
if (Tempworkspace.ExistFeatureClass("LSczcdyd"))
{
Tempworkspace.DeleteFeatureClass("LSczcdyd");
}
// 创建临时层
LSczcdydFeatureclassAPI = Tempworkspace.CreateFeatureClass("LSczcdyd", (czcdydFeatureClassAPI.FeatureClass as IGeoDataset).SpatialReference, null);
LSczcdydFeatureclassAPI.AddField("CZCLX", esriFieldType.esriFieldTypeString, "CZCLX");
LSczcdydFeatureclassAPI.AddField("CZCDM", esriFieldType.esriFieldTypeString, "CZCDM");
LSczcdydFeatureclassAPI.AddField("CZCMC", esriFieldType.esriFieldTypeString, "CZCMC");
LSczcdydFeatureclassAPI.AddField("ZLDWDM", esriFieldType.esriFieldTypeString, "ZLDWDM");
LSczcdydFeatureclassAPI.AddField("ZLDWMC", esriFieldType.esriFieldTypeString, "ZLDWMC");
if (czcdydFeatureClassAPI == null || czcdydFeatureClassAPI.FeatureClass == null)
{
MessageHelper.ShowTips("当前工程数据库未找到城镇村等用地(CZCDYDGX)图层!");
return;
}
int czcsxmIndex = dltbFeatureClassAPI.FeatureClass.Fields.FindField("CZCSXM");
int czclxIndex = czcdydFeatureClassAPI.FeatureClass.Fields.FindField("CZCLX");
int LSczclxIndex = LSczcdydFeatureclassAPI.FeatureClass.Fields.FindField("CZCLX");
IQueryFilter queryFilter = new QueryFilter()
{
WhereClause = "CZCSXM IN('201','202','203','204','205','201A','202A','203A')"
};
int count = dltbFeatureClassAPI.FeatureClass.FeatureCount(queryFilter);
if (czcdydFeatureClassAPI.FeatureClass.FeatureCount(null) > 0)
{
ITable pTable = czcdydFeatureClassAPI.FeatureClass as ITable;
if (pTable != null)
{
pTable.DeleteSearchedRows(null);
}
else
{
MessageHelper.ShowTips("城镇村等用地数据清除失败!");
return;
}
}
pFeatureCursor = dltbFeatureClassAPI.FeatureClass.Search(queryFilter, false);
pInsertCursor = LSczcdydFeatureclassAPI.FeatureClass.Insert(true);
IFeatureBuffer featureAdd = LSczcdydFeatureclassAPI.FeatureClass.CreateFeatureBuffer();
IFeature pFeature = null;
int num = 0;
int zldwdm = dltbFeatureClassAPI.FeatureClass.Fields.FindField("ZLDWDM");
int zldwmc = dltbFeatureClassAPI.FeatureClass.Fields.FindField("ZLDWMC");
int lszldwdm = LSczcdydFeatureclassAPI.FeatureClass.Fields.FindField("ZLDWDM");
int lszldwmc = LSczcdydFeatureclassAPI.FeatureClass.Fields.FindField("ZLDWMC");
while ((pFeature = pFeatureCursor.NextFeature()) != null)
{
featureAdd.Shape = pFeature.Shape;
if (LSczclxIndex > -1)
{
featureAdd.set_Value(LSczclxIndex, pFeature.get_Value(czcsxmIndex));
}
if (lszldwdm != -1)
{
featureAdd.set_Value(lszldwdm, pFeature.get_Value(zldwdm));
}
if (lszldwmc != -1)
{
featureAdd.set_Value(lszldwmc, pFeature.get_Value(zldwmc));
}
pInsertCursor.InsertFeature(featureAdd);
}
pInsertCursor.Flush();
this.UpdateMsg("正在对城镇村等用地更新进行赋值......");
pOutWorkFactory = new SqlWorkspaceFactoryClass();
IWorkspace pOutWork = pOutWorkFactory.OpenFromFile(dbPath, 0);
//IFeatureWorkspace SchemeWork = GetGdbWorkspace(schemeDBPath);
IFeatureWorkspace TempWork2 = GetGdbWorkspace(TempfilePath);
rdbHelper.ExecuteSQL(" drop table LSczcdyd ");
TableToTable(TempWork2, pOutWork, "LSczcdyd");
string updateSQL = string.Format("update {0} set CZCDM = substr(ZLDWDM,1,6) || '0000000000000' where CZCLX in ('201','201A')", "LSczcdyd");
rdbHelper.ExecuteSQL(updateSQL);
updateSQL = string.Format("update {0} set CZCDM = substr(ZLDWDM,1,9) || '0000000000' where CZCLX in ('202','202A')", "LSczcdyd");
rdbHelper.ExecuteSQL(updateSQL);
updateSQL = string.Format("update {0} set CZCDM = [ZLDWDM],CZCMC = [ZLDWMC] where CZCLX in ('203','203A','204','205')", "LSczcdyd");
var x = rdbHelper.ExecuteSQL(updateSQL);
DataTable dtLSczcdyd = rdbHelper.ExecuteDatatable("LSczcdyd", "select OBJECTID,CZCDM,CZCMC from LSczcdyd", true);
pFeatureCursor = Tempworkspace.OpenFeatureClass("LSczcdyd").FeatureClass.Update(null, false);
while ((pFeature = pFeatureCursor.NextFeature()) != null)
{
object czcdm = dtLSczcdyd.Select(string.Format(" OBJECTID={0} ", pFeature.get_Value(pFeature.Fields.FindField("OBJECTID"))))[0]["CZCDM"];
pFeature.set_Value(pFeature.Fields.FindField("CZCDM"), czcdm);
object czcmc = dtLSczcdyd.Select(string.Format(" OBJECTID={0} ", pFeature.get_Value(pFeature.Fields.FindField("OBJECTID"))))[0]["CZCMC"];
pFeature.set_Value(pFeature.Fields.FindField("CZCMC"), czcmc);
pFeatureCursor.UpdateFeature(pFeature);
}
pFeatureCursor.Flush();
LSczcdydFeatureclassAPI = Tempworkspace.OpenFeatureClass("LSczcdyd");
// 根据城镇村等用地类型及城镇村代码相同且相邻进行融合
List<string> columns = new List<string>() { "CZCLX", "CZCDM", "CZCMC" };
var beforeCount = LSczcdydFeatureclassAPI.FeatureClass.FeatureCount(null);
var outFilePath = TempfilePath + "\\CZCDYD_Dissolve";
gPParamClass = new GPParamClass()
{
FirstFeatureClass = LSczcdydFeatureclassAPI.FeatureClass,
OutFeatureClassPath = outFilePath,
IsGetOutPutFeature = true,
ListDissolveFiledName = columns,
//Statistics_Fields = "BSM COUNT",
};
if (Tempworkspace.ExistFeatureClass("CZCDYD_Dissolve"))
{
Tempworkspace.DeleteFeatureClass("CZCDYD_Dissolve");
}
if (Tempworkspace.ExistFeatureClass("CZCDYD_Dissolve_Result"))
{
Tempworkspace.DeleteFeatureClass("CZCDYD_Dissolve_Result");
}
string path = gPParamClass.OutFeatureClassPath;
IFeatureLayer featureLayer = null;
GeoprocessorHelper.DissolveAnalysis(gPParamClass, ref featureLayer);
GeoprocessorHelper.FeatureClassToFeatureClass(featureLayer, TempfilePath, "CZCDYD_Dissolve_Result");
pFeatureClass_4 = Tempworkspace.OpenFeatureClass("CZCDYD_Dissolve_Result");
int countt = pFeatureClass_4.FeatureClass.FeatureCount(null);
if (countt <= 0)
{
//MessageHelper.ShowTips("融合之后出错!,数据异常");
return;
}
pFeatureCursor = pFeatureClass_4.FeatureClass.Search(null, false);
//网格大小引发的错误
if (czcdydFeatureClassAPI.FeatureClass is IFeatureClassLoad pFclsLoad)
pFclsLoad.LoadOnlyMode = true;
pInsertCursor = czcdydFeatureClassAPI.FeatureClass.Insert(true);
int cxmIndex = pFeatureClass_4.FeatureClass.FindField("CZCLX");
int Index_1 = pFeatureClass_4.FeatureClass.FindField("CZCDM");
int Index_2 = czcdydFeatureClassAPI.FeatureClass.FindField("CZCDM");
int Index_3 = pFeatureClass_4.FeatureClass.FindField("CZCMC");
int Index_4 = czcdydFeatureClassAPI.FeatureClass.FindField("CZCMC");
featureAdd = czcdydFeatureClassAPI.FeatureClass.CreateFeatureBuffer();
pFeature = null;
string substr = "";
num = 0;
while ((pFeature = pFeatureCursor.NextFeature()) != null)
{
featureAdd.Shape = pFeature.Shape;
if (czclxIndex > -1)
{
featureAdd.set_Value(czclxIndex, pFeature.get_Value(cxmIndex));
featureAdd.set_Value(Index_2, pFeature.get_Value(Index_1));
featureAdd.set_Value(Index_4, pFeature.get_Value(Index_3));
if (string.IsNullOrEmpty(substr))
{
substr = pFeature.get_Value(Index_1).ToString();
}
}
pInsertCursor.InsertFeature(featureAdd);
}
pInsertCursor.Flush();
if (czcdydFeatureClassAPI.FeatureClass is IFeatureClassLoad pFclsLoads)
pFclsLoads.LoadOnlyMode = false;
path = GetTempPath();
rdbHelper.ExecuteSQL(" drop table CZCDYDGX ");
IFeatureWorkspace TempfeatureWorkspace = (MapsManager.Instance.MapService.GetFeatureClassByName("CZCDYDGX") as FeatureClass).Workspace as IFeatureWorkspace;
TableToTable(TempfeatureWorkspace, pOutWork, "CZCDYDGX");
#region 201 201A
{
var result = GetXZQDic().Find(p => p.CODE == substr.Substring(0, 6));
if (result != null)
{
string SQLstr = $"UPDATE CZCDYDGX SET CZCMC = '{result.NAME}' WHERE CZCLX in ('201','201A')";
rdbHelper.ExecuteSQL(SQLstr);
}
}
#endregion
rdbHelper.ExecuteSQL(" drop table CJDCQGX ");
TableToTable(TempfeatureWorkspace, pOutWork, "CJDCQGX");
DataTable dt = rdbHelper.ExecuteDatatable("CZCDYDGX", "select objectid,CZCMC from CZCDYDGX", true);
string time = new DateTime(DateTime.Now.Year - 1, 12, 31).ToShortDateString();
// 标识码赋值
int maxValue = 1;
string MaxBSM = GetMaxBSM(new List<IFeatureClass>() { jc_Fc });
string BSM = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + "2931";
IFeatureCursor pFeatureCursor_LS = czcdydFeatureClassAPI.FeatureClass.Update(null, true);
count = czcdydFeatureClassAPI.FeatureClass.FeatureCount(null);
this.UpdateMsg("正在对城镇村等用地更新进行赋值......");
int bsmIndexof = czcdydFeatureClassAPI.FeatureClass.FindField("BSM");
int czclxindex = czcdydFeatureClassAPI.FeatureClass.FindField("CZCLX");
num = 0;
IFeature pfeaFeature = pFeatureCursor_LS.NextFeature();
int czcdmIdx = czcdydFeatureClassAPI.FeatureClass.Fields.FindField("CZCDM");
while (pfeaFeature != null)
{
string bsmPrefix = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + MaxBSM.Substring(6, 4);
int MaxNum = Convert.ToInt32(MaxBSM.Substring(10));
MaxBSM = bsmPrefix + (MaxNum + 1).ToString().PadLeft(8, '0');
pfeaFeature.set_Value(bsmIndexof, MaxBSM);
pfeaFeature.set_Value(czcdydFeatureClassAPI.FeatureClass.FindField("YSDM"), "2099030100");
string czclx = pfeaFeature.Value[czclxindex].ToTrim();
string czcmc = string.Empty;
DataRow[] drSet = dt.Select(string.Format(" objectid='{0}' ", pfeaFeature.Value[0]));
if (drSet != null && drSet.Length > 0)
{
czcmc = drSet[0]["CZCMC"].ToString();
}
if (string.IsNullOrWhiteSpace(czcmc) && czcdmIdx != -1 && qsDic != null)
{
string strCZCDM = pfeaFeature.Value[czcdmIdx].ToTrim();
if (czclx == "203" || czclx == "203A" || czclx == "204" || czclx == "205")
{
if (strCZCDM.Length > 12)
{
DataDicTionary dic = qsDic.FirstOrDefault(qs => qs.CODE == strCZCDM || qs.CODE.StartsWith(strCZCDM.Substring(0, 12)));
if (dic != null)
czcmc = dic.NAME;
}
}
else if (czclx == "202" || czclx == "202A")
{
if (strCZCDM.Length > 9)
{
DataDicTionary dic = qsDic.FirstOrDefault(qs => qs.CODE == strCZCDM || qs.CODE.StartsWith(strCZCDM.Substring(0, 9)));
if (dic != null)
czcmc = dic.NAME;
}
}
else if (czclx == "201" || czclx == "201A")
{
if (strCZCDM.Length > 6)
{
DataDicTionary dic = qsDic.FirstOrDefault(qs => qs.CODE == strCZCDM || qs.CODE.StartsWith(strCZCDM.Substring(0, 6)));
if (dic != null)
czcmc = dic.NAME;
}
}
}
pfeaFeature.set_Value(czcdydFeatureClassAPI.FeatureClass.FindField("CZCMC"), czcmc);
pfeaFeature.set_Value(czcdydFeatureClassAPI.FeatureClass.FindField("GXSJ"), time);
pfeaFeature.set_Value(czcdydFeatureClassAPI.FeatureClass.FindField("CZCMJ"), pfeaFeature.ShapeCopy.GetEllipseArea());
pFeatureCursor_LS.UpdateFeature(pfeaFeature);
maxValue++;
pfeaFeature = pFeatureCursor_LS.NextFeature();
}
pFeatureCursor_LS.Flush();
//设置基础城镇村选中
gPParamClass = new GPParamClass();
IFeatureWorkspace inWs = GetGdbWorkspace(path);
IFeatureLayer czcUnionLayer = null;
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("城镇村等用地");
gPParamClass.SecondFeatureClass = czcdydFeatureClassAPI.FeatureClass;
gPParamClass.OutFeatureClassPath = path + "\\" + "TempCZCGXGC2";
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.UnionAnalysis(gPParamClass, ref czcUnionLayer);
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureClass = czcUnionLayer.FeatureClass;
gPParamClass.OutFeatureClassPath = path + "\\" + "TempCZCGXGC";
gPParamClass.IsGetOutPutFeature = true;
GeoprocessorHelper.MultipartToSinglePath(gPParamClass, ref czcUnionLayer);
rdbHelper.ExecuteSQL(" drop table TempCZCGXGC ");
TableToTable(inWs, pOutWork, "TempCZCGXGC");
string FID_JC_CZCDYD = $"FID_{featureClass.Name}";
string strSql = $"select objectid,fid_czcdydgx from (select count(1) num,objectid,{FID_JC_CZCDYD},czclx bgqczclx,czclx_1 bghczclx,czcdm bgqczcdm,czcdm_1 bghczcdm,czcmc bgqczcmc,czcmc_1 bghczcmc,FID_CZCDYDGX from TempCZCGXGC group by {FID_JC_CZCDYD}) where num=1 and bgqczclx=bghczclx and bgqczcdm=bghczcdm and bgqczcmc=bghczcmc";
DataTable gxgcDT = rdbHelper.ExecuteDatatable("tempgxgc", strSql, true);
strSql = $"select objectid,fid_czcdydgx from (select count(1) num,objectid,{FID_JC_CZCDYD},czclx bgqczclx,czclx_1 bghczclx,czcdm bgqczcdm,czcdm_1 bghczcdm,czcmc bgqczcmc,czcmc_1 bghczcmc,FID_CZCDYDGX from TempCZCGXGC group by FID_CZCDYDGX) where num=1 and bgqczclx=bghczclx and bgqczcdm=bghczcdm and bgqczcmc=bghczcmc";
DataTable gxgcDT2 = rdbHelper.ExecuteDatatable("tempgxgc", strSql, true);
List<int> AllowDelOIDs = new List<int>();
if (gxgcDT2 != null)
{
foreach (DataRow row in gxgcDT2.Rows)
{
AllowDelOIDs.Add(Convert.ToInt32(row["FID_CZCDYDGX"]));
}
}
if (gxgcDT != null)
{
List<string> SqlList = new List<string>();
foreach (DataRow row in gxgcDT.Rows)
{
int oid = Convert.ToInt32(row["OBJECTID"]);
int gxOID = Convert.ToInt32(row["FID_CZCDYDGX"]);
if (!AllowDelOIDs.Contains(gxOID))
continue;
IFeature gx = czcdydFeatureClassAPI.FeatureClass.GetFeature(gxOID);
gx.Delete();
IFeature gcF = czcUnionLayer.FeatureClass.GetFeature(oid);
gcF.Delete();
SqlList.Add(string.Format("Delete from TempCZCGXGC where OBJECTID={0}", oid));
}
rdbHelper.BeginTransaction();
try
{
foreach (var item in SqlList)
{
rdbHelper.ExecuteSQL(item);
}
rdbHelper.Commit();
}
catch (Exception ex)
{
rdbHelper.Rollback();
}
}
IFeatureClassAPI czcGXGCAPI = new FeatureClassAPI(MapsManager.Instance.MapService.GetFeatureClassByName("CZCDYDGXGC"));// .OpenFeatureClass("CZCDYDGXGC");
(czcGXGCAPI.FeatureClass as ITable).DeleteSearchedRows(null);
IFeatureCursor cursor = czcUnionLayer.FeatureClass.Search(null, true);
IFeature f = null;
int idxBGQBSM = czcUnionLayer.FeatureClass.FindField("BSM");
int idxBGHBSM = czcUnionLayer.FeatureClass.FindField("BSM_1");
int idxBGQCZCLX = czcUnionLayer.FeatureClass.FindField("CZCLX");
int idxBGHCZCLX = czcUnionLayer.FeatureClass.FindField("CZCLX_1");
int idxBGQCZCDM = czcUnionLayer.FeatureClass.FindField("CZCDM");
int idxBGHCZCDM = czcUnionLayer.FeatureClass.FindField("CZCDM_1");
int idxBGQCZCMC = czcUnionLayer.FeatureClass.FindField("CZCMC");
int idxBGHCZCMC = czcUnionLayer.FeatureClass.FindField("CZCMC_1");
int idxCZCMJ = czcUnionLayer.FeatureClass.FindField("CZCMJ");
int t_idxBGQBSM = czcGXGCAPI.FeatureClass.FindField("BGQBSM");
int t_idxBGHBSM = czcGXGCAPI.FeatureClass.FindField("BGHBSM");
int t_idxBGQCZCLX = czcGXGCAPI.FeatureClass.FindField("BGQCZCLX");
int t_idxBGHCZCLX = czcGXGCAPI.FeatureClass.FindField("BGHCZCLX");
int t_idxBGQCZCDM = czcGXGCAPI.FeatureClass.FindField("BGQCZCDM");
int t_idxBGHCZCDM = czcGXGCAPI.FeatureClass.FindField("BGHCZCDM");
int t_idxBGQCZCMC = czcGXGCAPI.FeatureClass.FindField("BGQCZCMC");
int t_idxBGHCZCMC = czcGXGCAPI.FeatureClass.FindField("BGHCZCMC");
int t_idxBGMJ = czcGXGCAPI.FeatureClass.FindField("BGMJ");
int t_idxBGXW = czcGXGCAPI.FeatureClass.FindField("BGXW");
int t_idxGXSJ = czcGXGCAPI.FeatureClass.FindField("GXSJ");
//网格大小报错
if (czcGXGCAPI.FeatureClass is IFeatureClassLoad pFclsLoad2)
pFclsLoad2.LoadOnlyMode = true;
IFeatureCursor insertCur = czcGXGCAPI.FeatureClass.Insert(true);
IFeatureBuffer buf = null;
List<PCModel> pcList = new List<PCModel>();
IFeatureLayer czcgxLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("CZCDYDGX");
IFeatureLayer jcczcLayer = MapsManager.Instance.MapService.GetFeatureLayerByLayerName("城镇村等用地");
int idxGXBSM = czcgxLayer.FeatureClass.FindField("BSM");
strSql = $"select BSM from (select count(1) num,objectid,bsm from TempCZCGXGC group by {FID_JC_CZCDYD}) where num>1";
DataTable txbgData = rdbHelper.ExecuteDatatable("tempgxgc", strSql, true);
count = czcUnionLayer.FeatureClass.FeatureCount(null);
num = 0;
while ((f = cursor.NextFeature()) != null)
{
string s_bgqbsm = f.Value[idxBGQBSM].ToTrim();
string s_bghbsm = f.Value[idxBGHBSM].ToTrim();
string s_bgqczclx = f.Value[idxBGQCZCLX].ToTrim();
string s_bghczclx = f.Value[idxBGHCZCLX].ToTrim();
string s_bgqczcdm = f.Value[idxBGQCZCDM].ToTrim();
string s_bghczcdm = f.Value[idxBGHCZCDM].ToTrim();
string s_bgqczcmc = f.Value[idxBGQCZCMC].ToTrim();
string s_bghczcmc = f.Value[idxBGHCZCMC].ToTrim();
double czcmj = f.Value[idxCZCMJ].ToDouble(2);
if ((s_bgqczclx == "203" || s_bgqczclx == "203A") && s_bgqczclx == s_bghczclx && s_bgqczcdm.Substring(0, 12) == s_bghczcdm.Substring(0, 12))
{
List<IFeature> jcList = FeatureAPI.Identify2(f.ShapeCopy, jcczcLayer);
if (jcList.Count == 1 && FeatureAPI.GetEqual(jcList[0].ShapeCopy, f.ShapeCopy))
{
List<IFeature> gxList = FeatureAPI.Identify2(f.ShapeCopy, czcgxLayer);
foreach (var tempGxItem in gxList)
{
tempGxItem.Shape = FeatureAPI.Difference(tempGxItem.ShapeCopy, f.ShapeCopy);
tempGxItem.Store();
if (tempGxItem.Shape.IsEmpty)
{
tempGxItem.Delete();
}
}
continue;
}
else
{
}
}
string bgxw = string.Empty;
if (string.IsNullOrWhiteSpace(s_bghczclx))
{
bgxw = "0";
List<IFeature> gxList = FeatureAPI.Identify2(f.ShapeCopy, dltbgxLayer);
if (gxList.Count == 0)
{
continue;
}
}
else if (string.IsNullOrWhiteSpace(s_bgqczcdm))
{
bgxw = "3";
List<IFeature> gxList = FeatureAPI.Identify2(f.ShapeCopy, dltbgxLayer);
if (gxList.Count == 0)
{
List<IFeature> gxList2 = FeatureAPI.Identify2(f.ShapeCopy, czcgxLayer);
foreach (var item in gxList2)
{
if (FeatureAPI.GetEqual(item.ShapeCopy, f.ShapeCopy))
item.Delete();
else
{
item.Shape = FeatureAPI.Difference(item.ShapeCopy, f.ShapeCopy);
item.Store();
}
}
continue;
}
}
else
{
bgxw = "2";
if (txbgData.Select(string.Format("BSM='{0}'", s_bgqbsm)).Length == 0)
{
List<IFeature> gxList = FeatureAPI.Identify2(f.ShapeCopy, czcgxLayer);
if (gxList.Count == 1)
{
if (FeatureAPI.GetEqual(f.ShapeCopy, gxList[0].ShapeCopy))
{
bgxw = "1";
s_bghbsm = s_bgqbsm;
gxList[0].Value[idxGXBSM] = s_bghbsm;
gxList[0].Store();
}
}
}
}
buf = czcGXGCAPI.FeatureClass.CreateFeatureBuffer();
buf.Shape = f.ShapeCopy;
buf.Value[t_idxBGQCZCLX] = s_bgqczclx;
buf.Value[t_idxBGQCZCDM] = s_bgqczcdm;
buf.Value[t_idxBGQCZCMC] = s_bgqczcmc;
buf.Value[t_idxBGQBSM] = s_bgqbsm;
buf.Value[t_idxGXSJ] = time;
buf.Value[t_idxBGXW] = bgxw;
buf.Value[t_idxBGHCZCLX] = s_bghczclx;
buf.Value[t_idxBGHCZCDM] = s_bghczcdm;
buf.Value[t_idxBGHCZCMC] = s_bghczcmc;
buf.Value[t_idxBGHBSM] = s_bghbsm;
double bgmj = f.ShapeCopy.GetEllipseArea().ToDouble(2);
if (bgxw == "3")
{
buf.Value[t_idxBGMJ] = bgmj;
}
object oid = insertCur.InsertFeature(buf);
if (bgxw != "3")
{
PCModel pc = pcList.FirstOrDefault(e => e.BGQBSM == s_bgqbsm);
if (pc == null)
{
pc = new PCModel();
pc.BGQBSM = s_bgqbsm;
pc.KZMJ = czcmj;
pc.GCData.Add(new GCMJModel() { OID = Convert.ToInt32(oid), MJ = bgmj });
pcList.Add(pc);
}
else
{
pc.GCData.Add(new GCMJModel() { OID = Convert.ToInt32(oid), MJ = bgmj });
}
}
}
insertCur.Flush();
if (czcGXGCAPI.FeatureClass is IFeatureClassLoad pFclsLoad3)
pFclsLoad3.LoadOnlyMode = false;
foreach (var item in pcList)
{
item.PC2();
foreach (var gcOID in item.GCData)
{
IFeature gc = czcGXGCAPI.FeatureClass.GetFeature(gcOID.OID);
gc.Value[t_idxBGMJ] = gcOID.MJ;
gc.Store();
}
}
DataTable tempGcDt = new DataTable();
IQueryFilter filt = new QueryFilterClass();
filt.SubFields = "BGMJ,BGHBSM,BGQBSM,OBJECTID,BGXW";
filt.WhereClause = "BGXW <> '0'";
czcGXGCAPI.QueryFeaturesToDataTable(filt, -1, out tempGcDt);
Dictionary<string, double> bghMJs = new Dictionary<string, double>();
foreach (DataRow item in tempGcDt.Rows)
{
string bsm = item["BGHBSM"].ToTrim();
if (string.IsNullOrWhiteSpace(bsm)) continue;
if (bghMJs.ContainsKey(bsm))
{
bghMJs[bsm] += item["BGMJ"].ToDouble(2);
}
else
{
bghMJs.Add(bsm, item["BGMJ"].ToDouble(2));
}
}
//List<string> upMJSqlList = new List<string>();
//foreach (var item in bghMJs.Keys)
//{
// upMJSqlList.Add(string.Format("Update CZCDYDGX set CZCMJ={0} where BSM='{1}'", bghMJs[item].ToDouble(2), item));
//}
ICursor upCursor = (czcgxLayer.FeatureClass as ITable).Update(null, true);
IRow upRow = null;
int idxGXCZCMJ = czcgxLayer.FeatureClass.FindField("CZCMJ");
while ((upRow = upCursor.NextRow()) != null)
{
string bsm = upRow.Value[idxGXBSM].ToTrim();
if (bghMJs.ContainsKey(bsm))
{
upRow.Value[idxGXCZCMJ] = bghMJs[bsm].ToDouble(2);
upCursor.UpdateRow(upRow);
}
}
IFeatureLayer JC_CZC203Layer_Mem = GeoDBAPI.CreateFeatureLayerInmemeory("JCCZC203", "城镇村203范围", (jc_Fc as IGeoDataset).SpatialReference, jc_Fc.ShapeType, jc_Fc.Fields);
filter.WhereClause = "CZCLX='203' or CZCLX='203A'";
InsertDataToMemeoryLayer(tempJCCZCLayer.FeatureClass, JC_CZC203Layer_Mem.FeatureClass, filter);
cursor = czcgxLayer.FeatureClass.Search(new QueryFilterClass() { WhereClause = " CZCLX='203' or CZCLX='203A'" }, true);
f = null;
List<int> feaOIDs = new List<int>();
//int czclxIdx = czcgxLayer.FeatureClass.FindField("CZCLX");
int jcczclxIdx = JC_CZC203Layer_Mem.FeatureClass.FindField("CZCLX");
while ((f = cursor.NextFeature()) != null)
{
List<IFeature> jc203Features = FeatureAPI.Identify(f.ShapeCopy, JC_CZC203Layer_Mem);
if (jc203Features != null)
{
foreach (var jcF in jc203Features)
{
if (FeatureAPI.GetEqual(jcF.ShapeCopy, f.ShapeCopy)) break;
if (!FeatureAPI.IsInterSect(jcF.ShapeCopy, f.ShapeCopy)) continue;
IGeometry InterGeo = FeatureAPI.InterSect(jcF.ShapeCopy, f.ShapeCopy);
IGeometry DiffGeo = FeatureAPI.Difference(f.ShapeCopy, jcF.ShapeCopy);
if (DiffGeo.IsEmpty)
{
break;
}
if (!feaOIDs.Contains(f.OID))
feaOIDs.Add(f.OID);
}
}
}
idxGXBSM = czcgxLayer.FeatureClass.FindField("BSM");
int idxGXCZCLX = czcgxLayer.FeatureClass.FindField("CZCLX");
int idxGXCZCDM = czcgxLayer.FeatureClass.FindField("CZCDM");
int idxGXCZCMC = czcgxLayer.FeatureClass.FindField("CZCMC");
idxGXCZCMJ = czcgxLayer.FeatureClass.FindField("CZCMJ");
int idxGXGXSJ = czcgxLayer.FeatureClass.FindField("GXSJ");
IFeatureLayer czcgxgcLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("CZCDYDGXGC");
int idxGXGCBGMJ = czcgxgcLayer.FeatureClass.FindField("BGMJ");
int idxGXGCBGHBSM = czcgxgcLayer.FeatureClass.FindField("BGHBSM");
insertCur = czcgxLayer.FeatureClass.Insert(true);
foreach (int oid in feaOIDs)
{
f = czcgxLayer.FeatureClass.GetFeature(oid);
string bsm = f.Value[idxGXBSM].ToTrim();
string czclx = f.Value[idxGXCZCLX].ToTrim();
List<IFeature> jc203Features = FeatureAPI.Identify(f.ShapeCopy, JC_CZC203Layer_Mem);
if (jc203Features != null)
{
IGeometry jc203 = null;
foreach (var jcF in jc203Features)
{
bool IsEquql = FeatureAPI.GetEqual(jcF.ShapeCopy, f.ShapeCopy);
string jcczclx = jcF.Value[jcczclxIdx].ToTrim();
if (IsEquql && jcczclx == czclx)
{
f.Delete();
break;
}
if (!FeatureAPI.IsInterSect(jcF.ShapeCopy, f.ShapeCopy)) continue;
if (FeatureAPI.IsContains(jcF.ShapeCopy, f.ShapeCopy))
{
break;
}
if (jc203 == null)
jc203 = jcF.ShapeCopy;
else
jc203 = FeatureAPI.Union(jc203, jcF.ShapeCopy);
}
IGeometry InterGeo = FeatureAPI.InterSect(jc203, f.ShapeCopy);
IGeometry DiffGeo = FeatureAPI.Difference(f.ShapeCopy, jc203);
if (DiffGeo.IsEmpty)
{
continue;
}
List<IGeometry> Rings = FeatureAPI.DissolveGeometryByRing(InterGeo);
List<IGeometry> Rings2 = FeatureAPI.DissolveGeometryByRing(DiffGeo);
if (Rings == null) Rings = new List<IGeometry>();
if (Rings2 == null) Rings2 = new List<IGeometry>();
if (Rings2.Count == 0)
{
f.Delete();
}
else
{
IGeometry tempGeo = f.ShapeCopy;
if (Rings2.Count > 0)
tempGeo = Rings2[0];
ITopologicalOperator topo = tempGeo as ITopologicalOperator;
IGeometry tempGeo2 = topo.Buffer(-0.00015);
List<IFeature> gcList = FeatureAPI.Identify(tempGeo2, czcgxgcLayer);
Marshal.ReleaseComObject(tempGeo2);
double czcmj = 0;
foreach (IFeature gcF in gcList)
{
if (!FeatureAPI.IsInterSect(tempGeo, gcF.ShapeCopy)) continue;
czcmj += gcF.Value[idxGXGCBGMJ].ToDouble(2);
gcF.Store();
}
f.Value[idxGXCZCMJ] = czcmj.ToDouble(2);
f.Shape = tempGeo;
f.Store();
if (Rings2.Count > 0)
Rings2.RemoveAt(0);
}
if (Rings2.Count > 0)
Rings.AddRange(Rings2);
foreach (var GeoRing in Rings)
{
string bsmPrefix = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + MaxBSM.Substring(6, 4);
int MaxNum = Convert.ToInt32(MaxBSM.Substring(10));
MaxBSM = bsmPrefix + (MaxNum + 1).ToString().PadLeft(8, '0');
IFeatureBuffer buff = czcgxLayer.FeatureClass.CreateFeatureBuffer();
buff.Value[idxGXBSM] = MaxBSM;
buff.Value[idxGXCZCLX] = f.Value[idxGXCZCLX];
buff.Value[idxGXCZCDM] = f.Value[idxGXCZCDM];
buff.Value[idxGXCZCMC] = f.Value[idxGXCZCMC];
buff.Value[idxGXGXSJ] = time;
buff.Shape = GeoRing;
List<IFeature> gcList = FeatureAPI.Identify2(GeoRing, czcgxgcLayer);
double czcmj = 0;
foreach (IFeature gcF in gcList)
{
if (!FeatureAPI.IsInterSect(GeoRing, gcF.ShapeCopy)) continue;
czcmj += gcF.Value[idxGXGCBGMJ].ToDouble(2);
gcF.Value[idxGXGCBGHBSM] = MaxBSM;
gcF.Store();
}
buff.Value[idxGXCZCMJ] = czcmj.ToDouble(2);
object addOid = insertCur.InsertFeature(buff);
}
}
insertCur.Flush();
Marshal.ReleaseComObject(f);
}
cursor = czcgxLayer.FeatureClass.Search(null, true);
f = null;
List<int> oids = new List<int>();
while ((f = cursor.NextFeature()) != null)
{
List<IGeometry> Rings = FeatureAPI.DissolveGeometryByRing(f.ShapeCopy);
if (Rings == null) continue;
if (Rings.Count == 1) continue;
oids.Add(f.OID);
}
foreach (int oid in oids)
{
IFeature gxFeature = czcgxLayer.FeatureClass.GetFeature(oid);
List<IGeometry> Rings = FeatureAPI.DissolveGeometryByRing(gxFeature.ShapeCopy);
if (Rings.Count > 1)
{
gxFeature.Shape = Rings[0];
{
List<IFeature> gcList = FeatureAPI.Identify2(Rings[0], czcgxgcLayer);
double czcmj = 0;
foreach (IFeature gcF in gcList)
{
czcmj += gcF.Value[idxGXGCBGMJ].ToDouble(2);
gcF.Store();
}
gxFeature.Value[idxGXCZCMJ] = czcmj.ToDouble(2);
}
gxFeature.Store();
for (int i = 1; i < Rings.Count; i++)
{
if (Rings[i].IsEmpty) continue;
string bsmPrefix = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + MaxBSM.Substring(6, 4);
int MaxNum = Convert.ToInt32(MaxBSM.Substring(10));
MaxBSM = bsmPrefix + (MaxNum + 1).ToString().PadLeft(8, '0');
IFeatureBuffer buff = czcgxLayer.FeatureClass.CreateFeatureBuffer();
buff.Value[idxGXBSM] = MaxBSM;
buff.Value[idxGXCZCLX] = gxFeature.Value[idxGXCZCLX];
buff.Value[idxGXCZCDM] = gxFeature.Value[idxGXCZCDM];
buff.Value[idxGXCZCMC] = gxFeature.Value[idxGXCZCMC];
buff.Value[idxGXGXSJ] = time;
buff.Shape = Rings[i];
List<IFeature> gcList = FeatureAPI.Identify2(Rings[i], czcgxgcLayer);
double czcmj = 0;
foreach (IFeature gcF in gcList)
{
czcmj += gcF.Value[idxGXGCBGMJ].ToDouble(2);
gcF.Value[idxGXGCBGHBSM] = MaxBSM;
gcF.Store();
}
buff.Value[idxGXCZCMJ] = czcmj.ToDouble(2);
insertCur.InsertFeature(buff);
}
}
}
insertCur.Flush();
if (IsDelMinMJ)
{
(czcgxLayer.FeatureClass as FeatureClass).Workspace.ExecuteSQL("Delete from CZCDYDGX where CZCMJ=0");
(czcgxgcLayer.FeatureClass as FeatureClass).Workspace.ExecuteSQL("Delete from CZCDYDGXGC where BGMJ=0");
}
rdbHelper.ExecuteSQL(" drop table CZCDYDGXGC ");
TableToTable(TempfeatureWorkspace, pOutWork, "CZCDYDGXGC");
rdbHelper.ExecuteSQL(" drop table CZCDYDGX ");
TableToTable(TempfeatureWorkspace, pOutWork, "CZCDYDGX");
string sql = "select a.bsm aoid,b.objectid boid from czcdydgx a left join czcdydgxgc b on a.bsm=b.bghbsm where boid is null";
DataTable gxDT = rdbHelper.ExecuteDatatable("tempgx", sql, true);
if (gxDT != null && gxDT.Rows.Count > 0)
{
foreach (DataRow dr in gxDT.Rows)
{
(czcgxLayer.FeatureClass as FeatureClass).Workspace.ExecuteSQL(string.Format("Delete from CZCDYDGX where bsm='{0}'", dr["AOID"]));
}
}
rdbHelper.ExecuteSQL(" drop table CZCDYDGXGC ");
TableToTable(TempfeatureWorkspace, pOutWork, "CZCDYDGXGC");
rdbHelper.ExecuteSQL(" drop table CZCDYDGX ");
TableToTable(TempfeatureWorkspace, pOutWork, "CZCDYDGX");
sql = "select bgqbsm from czcdydgxgc where bgqbsm in (select bgqbsm from czcdydgxgc where bghbsm in (select bghbsm from czcdydgxgc group by bghbsm HAVING count(1)=1) and bgqbsm!='')group by bgqbsm HAVING count(1)=1";
gxgcDT = rdbHelper.ExecuteDatatable("tempgxgc", sql, true);
string PrjCode = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE;
sql = "select * from CZCDYDGXGC";
DataTable gxgcDTAll = rdbHelper.ExecuteDatatable("CZCDYDGXGC", sql, true);
if (gxgcDT != null && gxgcDT.Rows.Count > 0)
{
foreach (DataRow dr in gxgcDT.Rows)
{
DataRow[] drs = gxgcDTAll.Select(string.Format("BGQBSM='{0}'", dr["BGQBSM"]));
if (drs != null && drs.Length == 1)
{
if (drs[0]["BGQCZCLX"].ToTrim() == drs[0]["BGHCZCLX"].ToTrim() &&
drs[0]["BGQCZCDM"].ToTrim() == drs[0]["BGHCZCDM"].ToTrim() &&
drs[0]["BGQCZCMC"].ToTrim() == drs[0]["BGHCZCMC"].ToTrim())
{
(czcgxLayer.FeatureClass as FeatureClass).Workspace.ExecuteSQL(string.Format("Delete from CZCDYDGX where bsm='{0}'", drs[0]["BGHBSM"]));
(czcgxgcLayer.FeatureClass as FeatureClass).Workspace.ExecuteSQL(string.Format("Delete from CZCDYDGXGC where BGQBSM='{0}'", drs[0]["BGQBSM"]));
}
else
{
string bsm = dr["BGQBSM"].ToTrim();
if (!dr["BGQBSM"].ToTrim().StartsWith(PrjCode))
bsm = bsm.Replace(bsm.Substring(0, 6), PrjCode);
(czcgxLayer.FeatureClass as FeatureClass).Workspace.ExecuteSQL(string.Format("Update CZCDYDGX set BSM='{0}' where bsm='{1}'", bsm, drs[0]["BGHBSM"]));
(czcgxgcLayer.FeatureClass as FeatureClass).Workspace.ExecuteSQL(string.Format("Update CZCDYDGXGC set BGXW='1',BGHBSM='{0}' where BGQBSM='{1}' and BGHBSM='{2}'", bsm, dr["BGQBSM"], drs[0]["BGHBSM"]));
}
}
}
}
}
catch (Exception ex)
{
LogAPI.Debug("提取城镇村等用地数据异常:" + ex.Message);
throw ex;
}
finally
{
if (LSczcdydFeatureclassAPI != null)
LSczcdydFeatureclassAPI.CloseFeatureClass();
if (CZCDLTBFeatureClass != null)
CZCDLTBFeatureClass.CloseFeatureClass();
if (pFeatureClass_4 != null)
pFeatureClass_4.CloseFeatureClass();
if (pFeatureCursor != null)
Marshal.ReleaseComObject(pFeatureCursor);
if (pInsertCursor != null)
Marshal.ReleaseComObject(pInsertCursor);
if (czcdydFeatureClassAPI != null)
czcdydFeatureClassAPI.CloseFeatureClass();
if (dltbFeatureClassAPI != null)
dltbFeatureClassAPI.CloseFeatureClass();
if (Schemeworkspace != null)
Schemeworkspace.CloseWorkspace();
if (Tempworkspace != null)
Tempworkspace.CloseWorkspace();
if (pOutWorkFactory != null)
Marshal.ReleaseComObject(pOutWorkFactory);
if (outputWorkspaceFactory != null)
Marshal.ReleaseComObject(outputWorkspaceFactory);
}
}
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($"获取城镇村范围数据失败:{ex.Message}");
LogAPI.Debug(ex);
}
}
private string GetMaxBSM(List<IFeatureClass> fcList)
{
int xh = 0;
string MaxBSM = "";
string leftStr = string.Empty;
foreach (var fc in fcList)
{
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;
}
if (BSM.Length != 18)
continue;
int xh2 = Convert.ToInt32(BSM.Substring(10));
leftStr = BSM.Substring(0, 10);
if (xh < xh2)
{
xh = xh2;
MaxBSM = BSM;
}
}
return MaxBSM;
}
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 ex)
{
LogAPI.Debug(ex + ":删除错误不处理");
}
}
private string GetTempPath()
{
string path = string.Empty;
try
{
string gdbFolder = Directory.GetCurrentDirectory() + "\\Temp\\城镇村用地变更临时数据";
if (!System.IO.Directory.Exists(gdbFolder))
{
System.IO.Directory.CreateDirectory(gdbFolder);
}
try
{
DelectDir(gdbFolder);//能删除就删除 删除报错不处理
}
catch
{ }
string mdbFileName = Guid.NewGuid().ToString() + ".gdb";
path = System.IO.Path.Combine(gdbFolder, mdbFileName);
var TempGDBPath = SysAppPath.GetTemplatePath() + "TempGDB.gdb";//模板
if (Directory.Exists(path))
{
Directory.Delete(path, true);
}
Directory.CreateDirectory(path);
CopyDirectInfo(TempGDBPath, path);
return path;
}
catch (Exception ex)
{
LogAPI.Debug("提取城镇村等用地错误:" + ex.Message.ToString());
return path;
}
}
private void CopyDirectInfo(string sourceDir, string toDir)
{
if (!Directory.Exists(sourceDir))
{
throw new ApplicationException("未找到文件:" + sourceDir);
}
if (!Directory.Exists(toDir))
{
Directory.CreateDirectory(toDir);
}
DirectoryInfo directInfo = new DirectoryInfo(sourceDir);
FileInfo[] filesInfos = directInfo.GetFiles();
foreach (FileInfo fileinfo in filesInfos)
{
string fileName = fileinfo.Name;
File.Copy(fileinfo.FullName, toDir + @"/" + fileName, true);
}
}
/// <summary>
/// 获取全国区县代码字典
/// </summary>
/// <returns></returns>
public static List<DataDicTionary> GetXZQDic()
{
List<DataDicTionary> result = new List<DataDicTionary>();
IRDBHelper rdbHelper = null;
try
{
string systemPath = SysAppPath.GetDataBasePath() + "System.mdb";
if (System.IO.File.Exists(systemPath))
{
string connStr = SysConfigsOprator.GetDBConnectionByName("MDBOledbConnection");
connStr = string.Format(connStr, systemPath);
rdbHelper = RDBFactory.CreateDbHelper(connStr, DatabaseType.MSAccess);
string strSQL = "select OBJECTID AS ID, XZQ AS CODE,XZQMC AS NAME from XZQ";
DataTable dt = rdbHelper.ExecuteDatatable("Dic", strSQL, true);
if (dt != null)
{
result = TBToList.ToList<DataDicTionary>(dt);
foreach (var item in result)
{
item.DisplayName = item.CODE + "-" + item.NAME;
}
}
}
}
catch (Exception ex)
{
LogAPI.Debug(ex);
}
finally
{
if (rdbHelper != null)
{
rdbHelper.DisConnect();
}
}
return result;
}
public IFeatureWorkspace GetGdbWorkspace(string pPathName)
{
try
{
IWorkspaceFactory factoryGdb = new FileGDBWorkspaceFactoryClass();
IWorkspace worckspace = factoryGdb.OpenFromFile(pPathName, 0);
return worckspace as IFeatureWorkspace;
}
catch (Exception ex)
{
LogAPI.Debug(ex);
return null;
}
}
public bool TableToTable(IFeatureWorkspace pInWork, IWorkspace pOutWork, string tableName, IQueryFilter queryFilter = null)
{
try
{
if (pInWork == null || pOutWork == null || string.IsNullOrEmpty(tableName)) return false;
IWorkspace2 workspace2 = pInWork as IWorkspace2;
if (workspace2 != null)
{
if (!workspace2.get_NameExists(esriDatasetType.esriDTFeatureClass, tableName))
{
return false;
}
}
ITable pInTable = pInWork.OpenTable(tableName);
if (pInTable == null) return false;
IDataset pIndataset = (IDataset)pInTable;
IDatasetName pInDatasetName = (IDatasetName)pIndataset.FullName;
IEnumDataset enumDataset = pOutWork.get_Datasets(esriDatasetType.esriDTTable);
IDataset dataset;
enumDataset.Reset();
while ((dataset = enumDataset.Next()) != null)
{
string[] names = dataset.Name.Split('.');
if (string.Equals(names[names.Length - 1], tableName, StringComparison.CurrentCultureIgnoreCase))
{
dataset.Delete();
break;
}
}
IDataset pOutDataset = (IDataset)pOutWork;
IDatasetName pOutDatasetName = new TableNameClass();
pOutDatasetName.WorkspaceName = (IWorkspaceName)pOutDataset.FullName;
pOutDatasetName.Name = tableName;
//Validate the field names because you are converting between different workspace types.
IFieldChecker fieldChecker = new FieldCheckerClass();
IFields targetFeatureClassFields = pInTable.Fields;
IFields sourceFeatureClassFields = pInTable.Fields;
IEnumFieldError enumFieldError;
// Most importantly set the input and validate workspaces!
fieldChecker.InputWorkspace = pInWork as IWorkspace;
fieldChecker.ValidateWorkspace = pOutWork;
fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);
IFeatureDataConverter one2another = new FeatureDataConverterClass();
try
{
one2another.ConvertTable(pInDatasetName, queryFilter, pOutDatasetName, targetFeatureClassFields, "", 1000, 0);
}
finally
{
Marshal.ReleaseComObject(one2another);
}
return true;
}
catch (Exception ex)
{
LogAPI.Debug(ex);
throw ex;
}
}
}
public class PCModel
{
public string BGQBSM { get; set; }
public string BGHBSM { get; set; }
public double BGHMJ { get; set; }
public double KZMJ { get; set; }
//public Dictionary<int, double> GCData { get; set; }
public List<GCMJModel> GCData { get; set; }
public PCModel()
{
GCData = new List<GCMJModel>();
}
public void PC()
{
//IOrderedEnumerable<KeyValuePair<int, double>> GCData2 = GCData.OrderByDescending(e => e.Value);
//GCData = new Dictionary<int, double>();
//foreach (var item in GCData2)
//{
// GCData.Add(item.Key, item.Value);
//}
GCData = GCData.OrderByDescending(o => o.MJ).ToList();
double zmj = GCData.Sum(s => s.MJ);
double mjc = Math.Round(zmj - KZMJ, 2);
if (Math.Round(mjc, 2) == 0)
{
return;
}
int num = 0;
//平差
while (Math.Round(mjc, 2) != 0 && num < 10)
{
num++;
int tpsm = (int)(Math.Abs(Math.Round(mjc, 2)) / 0.01);
int e = tpsm / GCData.Count;
int f = tpsm % GCData.Count;
if (f != 0)
{
double tpmj = (e + 1) * 0.01;
for (int j = 0; j < f; j++)
{
if (mjc < 0)
GCData[j].MJ = GCData[j].MJ + tpmj;
else if (GCData[j].MJ > 0.01)
GCData[j].MJ = GCData[j].MJ - tpmj;
}
}
if (e != 0)
{
double tpmj = e * 0.01;
for (int j = f; j < GCData.Count; j++)
{
if (mjc < 0)
GCData[j].MJ = GCData[j].MJ + tpmj;
else if (GCData[j].MJ > 0.01)
GCData[j].MJ = GCData[j].MJ - tpmj;
}
}
zmj = GCData.Sum(s => s.MJ);
mjc = Math.Round(zmj - KZMJ, 2);
//mjc = Math.Round(shpList.Sum(s => s.MJ), 2) - pKZMJ;
}
}
public void PC2()
{
//IOrderedEnumerable<KeyValuePair<int, double>> GCData2 = GCData.OrderByDescending(e => e.Value);
//GCData = new Dictionary<int, double>();
//foreach (var item in GCData2)
//{
// GCData.Add(item.Key, item.Value);
//}
GCData = GCData.OrderByDescending(o => o.MJ).ToList();
double zmj = GCData.Sum(s => s.MJ);
double mjc = Math.Round(zmj - KZMJ, 2);
if (Math.Round(mjc, 2) == 0 || (zmj == 0 && mjc < 0))
{
return;
}
int num = 0;
while (Math.Round(mjc, 2) != 0 && num < 10)
{
foreach (var item in GCData)
{
if (Math.Round(mjc, 2) == 0) break;
if (mjc < 0)
{
if (item.MJ <= 0.01) continue;
item.MJ = Math.Round(item.MJ + 0.01, 2);
zmj = GCData.Sum(s => s.MJ);
mjc = Math.Round(zmj - KZMJ, 2);
}
else
{
if (item.MJ <= 0.01) continue;
item.MJ = Math.Round(item.MJ - 0.01, 2);
zmj = GCData.Sum(s => s.MJ);
mjc = Math.Round(zmj - KZMJ, 2);
}
}
}
////平差
//while (Math.Round(mjc, 2) != 0 && num < 10)
//{
// num++;
// int tpsm = (int)(Math.Abs(Math.Round(mjc, 2)) / (double)0.01);
// int e = tpsm / GCData.Count;
// int f = tpsm % GCData.Count;
// if (f != 0)
// {
// double tpmj = (e + 1) * 0.01;
// for (int j = 0; j < f; j++)
// {
// if (mjc < 0)
// GCData[j].MJ = GCData[j].MJ + tpmj;
// else if (GCData[j].MJ > 0.01)
// GCData[j].MJ = GCData[j].MJ - tpmj;
// }
// }
// if (e != 0)
// {
// double tpmj = e * 0.01;
// for (int j = f; j < GCData.Count; j++)
// {
// if (mjc < 0)
// GCData[j].MJ = GCData[j].MJ + tpmj;
// else if (GCData[j].MJ > 0.01)
// GCData[j].MJ = GCData[j].MJ - tpmj;
// }
// }
// zmj = GCData.Sum(s => s.MJ);
// mjc = Math.Round(zmj - KZMJ, 2);
// //mjc = Math.Round(shpList.Sum(s => s.MJ), 2) - pKZMJ;
//}
}
}
public class GCMJModel
{
public int OID { get; set; }
public double MJ { get; set; }
}
}