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

278 lines
13 KiB

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using KGIS.Framework.AE;
using KGIS.Framework.AE.Enum;
using KGIS.Framework.AE.GPHelper;
using KGIS.Framework.ThreadManager;
using KGIS.Framework.Utils;
using Kingo.PluginServiceInterface;
using Kingo.PluginServiceInterface.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace IDGForNDBG
{
public class GDDB_IDGHelper : BaseIDG
{
public string IDataChangeName { get => "XZQ_IDGHelper"; }
private string TempGDBPath = string.Empty;
private string TempDBPath = string.Empty;
//IDGParameter IDGParm = null;
public void Execute(IDGParameter pParm)
{
ThreadManager.Instance.QueueUserWorkItem(new System.Threading.WaitCallback(ExeGDDB), pParm, new System.Threading.WaitCallback(ComplateCallBack));
}
public void ComplateCallBack(object obj)
{
Console.WriteLine($"Log:耕地等别数据提取完成。耗时:{GetTime(StartTime)}");
}
DateTime StartTime = DateTime.Now;
public void ExeGDDB(object obj)
{
StartTime = DateTime.Now;
//IDGParameter Parm = obj as IDGParameter;
Kingo.ThreadManager.TaskParameter taskParm = obj as Kingo.ThreadManager.TaskParameter;
IDGParameter Parm = taskParm.Data as IDGParameter;
//IFeatureLayer GDDBGXLayer = null;
//IFeatureLayer JCGDDBLayer = JCGDDBLayer;
//IFeatureLayer NMKLayer = null;
IFeatureLayer featureLayer = null;
IFeatureCursor sourCursor = null;
IFeatureCursor gddbCursor = null;
try
{
OpenDB(Parm);
string TempDir = CreateTempDB("GDDB");
TempGDBPath = System.IO.Path.Combine(TempDir, "TempGDB.gdb");
TempDBPath = TempDir + @"\Temp" + ".sqlite";
//this.UpdateMsg("正在进行耕地等别数据提取,请稍候...");
//jcgddblayer = Parm.JC_GDDBLay as IFeatureLayer;
Console.WriteLine("Log:正在进行耕地等别数据提取,请稍候...");
if (JcGddbLayer != null)
{
//GeoprocessorHelper gpHelper = new GeoprocessorHelper();
//string gdbFolder = Directory.GetCurrentDirectory() + "\\Temp\\NMK";
//if (!System.IO.Directory.Exists(gdbFolder))
//{
// System.IO.Directory.CreateDirectory(gdbFolder);
//}
//try
//{ DelectDir(gdbFolder); }
//catch
//{ }
//string gdbFileName = Guid.NewGuid().ToString() + ".gdb";
//string path = System.IO.Path.Combine(gdbFolder, gdbFileName);
//IWorkspaceFactory pFtWsFct = new FileGDBWorkspaceFactory();
//IWorkspaceName workspaceName = pFtWsFct.Create(gdbFolder, gdbFileName, null, 0);
(GxGddbLayer.FeatureClass as ITable).DeleteSearchedRows(null);//删除耕地等别更新层所有数据
//空间联合赋值
GPParamClass gPParamClass = new GPParamClass();
IFeatureLayer TempNMKLayer = null;
if (NmTbLayer == null)
{
Console.WriteLine("Log:正在进行耕地等别数据提取_数据准备...");
gPParamClass = new GPParamClass();
gPParamClass.FirstFeatureLayer = JcTbLayer;
gPParamClass.SecondFeatureLayer = GxTbLayer;
gPParamClass.OutFeatureClassPath = Parm.PrjInfo.NMDatabase + "\\" + "DLTB_NMK";//要添加的要素类图层
gPParamClass.IsGetOutPutFeature = true;
gPParamClass.GPType = EnumGPType.Erase;
GPHelper.Instance.ExeGPForProces(gPParamClass, ref TempNMKLayer);//擦除/相减分析
}
else
TempNMKLayer = NmTbLayer;
//GPParamClass gPParamClass = new GPParamClass();
gPParamClass = new GPParamClass();
//gPParamClass.FirstFeatureLayer = NmTbLayer;
gPParamClass.FirstFeatureLayer = TempNMKLayer;
gPParamClass.SecondFeatureLayer = JcGddbLayer;
gPParamClass.OutFeatureClassPath = TempGDBPath + "\\" + "DLTB_GDDB";
gPParamClass.Match_option = "ARE_IDENTICAL_TO";
gPParamClass.IsGetOutPutFeature = true;
gPParamClass.GPType = EnumGPType.SpatialJoin;
gPParamClass.Join_type = "KEEP_ALL";
gPParamClass.JOIN_ONE_TO_ONE = true;
GPHelper.Instance.ExeGPForProces(gPParamClass, ref featureLayer);
if (featureLayer != null)
{
Console.WriteLine("Log:正在进行耕地等别数据提取_数据生成中...");
sourCursor = featureLayer.FeatureClass.Search(new QueryFilterClass() { WhereClause = " DLBM LIKE '01%'" }, true);
IFeature f = null;
gddbCursor = GxGddbLayer.FeatureClass.Insert(true);
int gddbIdx = featureLayer.FeatureClass.FindField("GDDB");
int kcmjIdx = featureLayer.FeatureClass.FindField("KCMJ");
int tbmjIdx = featureLayer.FeatureClass.FindField("TBMJ");
DateTime dateTime = DateTime.Now.Month >= 10 ? new DateTime(DateTime.Now.Year, 12, 31) : new DateTime(DateTime.Now.Year - 1, 12, 31);
while ((f = sourCursor.NextFeature()) != null)
{
IFeatureBuffer buff = GxGddbLayer.FeatureClass.CreateFeatureBuffer();
buff.Shape = f.ShapeCopy;
int sIndex = -1;
for (int i = 0; i < buff.Fields.FieldCount; i++)
{
IField field = buff.Fields.Field[i];
if (field.Name.Contains(featureLayer.FeatureClass.ShapeFieldName) || !field.Editable) continue;
sIndex = f.Fields.FindField(field.Name);
if (field.Name.ToUpper() == "GXSJ")
{
buff.Value[i] = dateTime;
}
if (sIndex == -1) continue;
else if (field.Name.ToUpper() == "KCDLMJ")
{
buff.Value[i] = f.Value[kcmjIdx];
}
else if (field.Name.ToUpper() == "TBDLMJ")
{
buff.Value[i] = f.Value[tbmjIdx];
}
else if (field.Name.ToUpper() == "LYD")
{
buff.Value[i] = f.Value[gddbIdx];
}
else
{
string value = f.Value[sIndex].ToTrim();
if (string.IsNullOrWhiteSpace(value))
buff.Value[i] = DBNull.Value;
else
buff.Value[i] = f.Value[sIndex];
}
}
gddbCursor.InsertFeature(buff);
if (buff != null)
Marshal.ReleaseComObject(buff);
}
gddbCursor.Flush();
}
Console.WriteLine($"Log:耕地等别数据提取完成。耗时:{GetTime(StartTime)}");
}
else
{
Console.WriteLine($"Log:耕地等别数据获取失败。");
}
}
catch (Exception ex)
{
LogAPI.Debug("耕地等别数据提取错误:" + ex.Message.ToString());
LogAPI.Debug(ex);
Console.WriteLine("Err:耕地等别数据提取错误:" + ex.Message.ToString());
//throw ex;
}
finally
{
if (sourCursor != null)
Marshal.ReleaseComObject(sourCursor);
if (gddbCursor != null)
Marshal.ReleaseComObject(gddbCursor);
}
}
public void OpenDB(IDGParameter parm)
{
try
{
LayerCfg layerInfo = null;
if (parm.PrjInfo != null && !string.IsNullOrWhiteSpace(parm.PrjInfo.TempData))
{
byte[] contentArray = Convert.FromBase64String(parm.PrjInfo.TempData);
string LayerCfg = System.Text.Encoding.Default.GetString(contentArray);
layerInfo = KGIS.Framework.Utils.SerializeAPI.DeserializeToObject<LayerCfg>(LayerCfg);
}
if (layerInfo == null)
{
throw new Exception("Err:获取图层信息失败!");
}
//FileInfo fileInfo = new FileInfo(parm.PrjInfo.BGDatabase);
string PathBgDB = parm.PrjInfo.BGDatabase;// Path.Combine(, "BGDB" + ".gdb");
string PathZLDB = parm.PrjInfo.ZLDatabase;
string PathNMDB = parm.PrjInfo.NMDatabase;// System.IO.Path.Combine(fileInfo.DirectoryName, "NMDB" + ".gdb");
BgWsAPI = new WorkspaceAPI(PathBgDB, WorkspaceTypeEnum.GDBFile);
if (BgWsAPI.CurrentWorkspace == null)
{
Console.WriteLine("打开变更数据库失败!");
return;
}
ZlWsAPI = new WorkspaceAPI(PathZLDB, WorkspaceTypeEnum.GDBFile);
if (ZlWsAPI.CurrentWorkspace == null)
{
Console.WriteLine("打开增量数据库失败!");
return;
}
NmWsAPI = new WorkspaceAPI(PathNMDB, WorkspaceTypeEnum.GDBFile);
LayerCfg JCLayerInfo = layerInfo.Layers.FirstOrDefault(f => f.LayerName == "年初数据");
if (JCLayerInfo == null)
{
throw new Exception("Err:获取年初数据库失败!");
}
List<LayerCfg> NcList = JCLayerInfo.GetAllItem();
//if (parm.ExeDLTB)
//{
LayerCfg _JcTBLayerInfo = NcList.FirstOrDefault(f => f.LayerName == "地类图斑");
_jcTbLayer = OpenLayer(_JcTBLayerInfo);
if (_jcTbLayer == null)
{
throw new Exception("Err:获取年初地类图斑数据失败!");
}
//}
LayerCfg _JcGddbLayerInfo = NcList.FirstOrDefault(f => f.LayerName == "耕地等别");
_jcGddbLayer = OpenLayer(_JcGddbLayerInfo);
if (_JcGddbLayerInfo == null)
throw new Exception("Err:获取年初耕地等别数据失败!");
if (_nmTbLayer == null)
_nmTbLayer = OpenLayer(NmWsAPI, "DLTB_NMK");
_gxGddbLayer = OpenLayer(ZlWsAPI, "GDDBGX");
_gxTbLayer = OpenLayer(ZlWsAPI, "DLTBGX");
}
catch (Exception ex)
{
throw ex;
}
}
#region DelectDir
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 + ":删除错误不处理");
}
}
#endregion
}
}