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

457 lines
20 KiB

4 months ago
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.esriSystem;
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.Core.Services;
using KGIS.Framework.Maps;
using KGIS.Framework.Platform;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.ExtensionMethod;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Path = System.IO.Path;
namespace Kingo.Plugin.YJJK.ModelEntity
{
public class ExportBGGDB
{
string sourcePath;
public string LayerNameFilter { get; set; } = null;
private string GX_VCTPath;
private string GXGC_VCTPath;
public ExportBGGDB(string pGX_VCTPath, string pGXGC_VCTPath)
{
GX_VCTPath = pGX_VCTPath;
GXGC_VCTPath = pGXGC_VCTPath;
}
public void Export(string SavedMDBPath)
{
WorkspaceAPI s_WsAPI = null;
WorkspaceAPI t_WsAPI = null;
try
{
bool IsDeleteFileFailed = false;
//创建要保存的MDB文件
try
{
if (System.IO.File.Exists(SavedMDBPath))
{
System.IO.File.Delete(SavedMDBPath);
}
}
catch
{
IsDeleteFileFailed = true;
}
CreateGdb(Path.GetDirectoryName(SavedMDBPath), Path.GetFileNameWithoutExtension(SavedMDBPath));
t_WsAPI = new WorkspaceAPI(SavedMDBPath, WorkspaceTypeEnum.GDBFile);
string currentGDBPath = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ZLDatabase;
s_WsAPI = new WorkspaceAPI(currentGDBPath, WorkspaceTypeEnum.GDBFile);
Dictionary<string, string> s_FcList = GetExportDic();
if (s_FcList.Count > 0)
{
IFeatureDataset pFD = null;
try
{
pFD = (t_WsAPI.CurrentWorkspace as IFeatureWorkspace).OpenFeatureDataset("变更数据");
}
catch
{ }
ISpatialReference pSR = null;
bool isVerifyMandate = false;
foreach (KeyValuePair<string, string> kvp in s_FcList)
{
if (!string.IsNullOrWhiteSpace(LayerNameFilter))
{
if (!kvp.Value.EndsWith(LayerNameFilter))
{
if (kvp.Value != "GXZJ")
{
continue;
}
else
{
if (LayerNameFilter != "GX")
{
continue;
}
}
}
}
IFeatureClassAPI fcSourceAPI = s_WsAPI.OpenFeatureClass(kvp.Value);
IFeatureClass fcSource = fcSourceAPI.FeatureClass;
if (fcSource.FeatureDataset != null && fcSource.FeatureDataset.Name == "增量更新库")
{
if (pSR == null)
{
pSR = (fcSource as IGeoDataset).SpatialReference;
}
if (pFD == null)
{
pFD = (t_WsAPI.CurrentWorkspace as IFeatureWorkspace).CreateFeatureDataset("变更数据", pSR);
}
GeoDBAPI.CreateFields(fcSource.Fields, out IFields fields);
if (IsDeleteFileFailed)
{
t_WsAPI.DeleteFeatureClass(kvp.Value);
}
IFeatureClassAPI fcTargetAPI = CreateFeatureClass(kvp.Value, pFD, pSR, fcSource.ShapeType, fields);
if (kvp.Value == "PDTGX")
{
//fcSourceAPI.FcToFc(fcTargetAPI.FeatureClass, new QueryFilter() { WhereClause = " 1=2 " }, false);
}
else
{
if (fcTargetAPI.FeatureClass.AliasName == "DLTBGX" || fcTargetAPI.FeatureClass.AliasName == "DLTBGXGC")
{
if (fcSourceAPI.FeatureClass.FeatureCount(null) > 0)
{
if (!isVerifyMandate)
isVerifyMandate = CommonHelper.VerifyMandate(fcSourceAPI.FeatureClass);
if (!isVerifyMandate)
{
LogAPI.Debug("验证授权失败,请先核对授权区域是否正确。");
throw new Exception("验证授权失败,请先核对授权区域是否正确。");
}
else
{
fcSourceAPI.FcToFc(fcTargetAPI.FeatureClass, new QueryFilter() { WhereClause = " ONLYZLBG is null OR ONLYZLBG = ''" }, false);
}
}
}
else
{
fcSourceAPI.FcToFc(fcTargetAPI.FeatureClass, null, false);
}
}
if (fcTargetAPI.FeatureClass.FindField("JCTBBSM") != -1)
fcTargetAPI.DeleteField("JCTBBSM");
if (fcTargetAPI.FeatureClass.FindField("BGTBOID") != -1)
fcTargetAPI.DeleteField("BGTBOID");//导出的原格式数据不需要BGTBOID字段
if (fcTargetAPI.FeatureClass.FindField("ONLYZLBG") != -1)
fcTargetAPI.DeleteField("ONLYZLBG");
if (kvp.Value == "DLTBGXGC")
{
if (fcTargetAPI.FeatureClass.FindField("BGTBBH") != -1)
fcTargetAPI.DeleteField("BGTBBH");
if (fcTargetAPI.FeatureClass.FindField("JCZT") != -1)
fcTargetAPI.DeleteField("JCZT");
if (fcTargetAPI.FeatureClass.FindField("JCJG") != -1)
fcTargetAPI.DeleteField("JCJG");
if (fcTargetAPI.FeatureClass.FindField("XMMC") != -1)
fcTargetAPI.DeleteField("XMMC");
if (fcTargetAPI.FeatureClass.FindField("XMBH") != -1)
fcTargetAPI.DeleteField("XMBH");
if (fcTargetAPI.FeatureClass.FindField("DKMC") != -1)
fcTargetAPI.DeleteField("DKMC");
}
if (kvp.Value == "DLTBGX")
{
if (fcTargetAPI.FeatureClass.FindField("XZQTZLX") != -1)
fcTargetAPI.DeleteField("XZQTZLX");
}
fcTargetAPI.CloseFeatureClass();
}
}
}
}
catch (Exception ex)
{
LogAPI.Debug(ex);
throw ex;
}
finally
{
if (s_WsAPI != null)
{
s_WsAPI.CloseWorkspace();
}
if (t_WsAPI != null)
{
t_WsAPI.CloseWorkspace();
}
}
}
private Dictionary<string, string> GetExportDic()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
//string BGVCTPath = Path.Combine(SysAppPath.GetCurrentAppPath(), "VCTTemplate3ForBG.VCT");
StreamReader sr = new StreamReader(GX_VCTPath, Encoding.GetEncoding("GB18030"));
string line = null;
bool BeginLoad = false;
while ((line = sr.ReadLine()) != null)
{
if (line == "FeatureCodeBegin")
{
BeginLoad = true;
}
if (BeginLoad && line != "FeatureCodeBegin")
{
string[] strs = line.Split(',');
if (strs.Length == 4)
{
dic.Add(strs[1], strs[3]);
}
}
if (line == "FeatureCodeEnd")
{
BeginLoad = false;
break;
}
}
sr.Close();
sr.Dispose();
//string BGVCT2Path = Path.Combine(SysAppPath.GetCurrentAppPath(), "VCTTemplate3ForBG2.VCT");
StreamReader sr2 = new StreamReader(GXGC_VCTPath, Encoding.GetEncoding("GB18030"));
line = null;
BeginLoad = false;
while ((line = sr2.ReadLine()) != null)
{
if (line == "FeatureCodeBegin")
{
BeginLoad = true;
}
if (BeginLoad && line != "FeatureCodeBegin")
{
string[] strs = line.Split(',');
if (strs.Length == 4)
{
dic.Add(strs[1], strs[3]);
}
}
if (line == "FeatureCodeEnd")
{
BeginLoad = false;
break;
}
}
sr2.Close();
sr2.Dispose();
return dic;
}
//public void Export(string SaveGDBPath)
//{
// IWorkspaceAPI s_WsAPI = null;
// IWorkspaceAPI t_WsAPI = null;
// try
// {
// if (System.IO.Directory.Exists(SaveGDBPath))
// {
// System.IO.Directory.Delete(SaveGDBPath);
// }
// //创建要保存的MDB文件
// IWorkspace workspace = CreateGdb(Path.GetDirectoryName(SaveGDBPath), Path.GetFileNameWithoutExtension(SaveGDBPath));
// if (workspace == null)
// {
// throw new Exception("无法创建GDB文件夹,导出操作已终止");
// }
// t_WsAPI = new WorkspaceAPI(workspace);
// string currentGDBPath = System.IO.Path.Combine(Env.Instance.Project.ProjDir, "工程数据.gdb");
// s_WsAPI = new WorkspaceAPI(currentGDBPath, AE.Enum.WorkspaceTypeEnum.GDBFile);
// Dictionary<string, string> s_FcList = s_WsAPI.GetFeatureClassName(esriDatasetType.esriDTFeatureDataset);
// if (s_FcList.Count > 0)
// {
// IFeatureDataset pFD = null;
// ISpatialReference pSR = null;
// foreach (KeyValuePair<string, string> kvp in s_FcList)
// {
// if (!string.IsNullOrWhiteSpace(LayerNameFilter))
// {
// if (!kvp.Value.EndsWith(LayerNameFilter))
// {
// if (kvp.Value != "GXZJ")
// {
// continue;
// }
// else
// {
// if (LayerNameFilter != "GX")
// {
// continue;
// }
// }
// }
// }
// IFeatureClassAPI fcSourceAPI = s_WsAPI.OpenFeatureClass(kvp.Value);
// IFeatureClass fcSource = fcSourceAPI.FeatureClass;
// if (fcSource.FeatureDataset != null && fcSource.FeatureDataset.Name == "变更成果")
// {
// if (pSR == null)
// {
// pSR = (fcSource as IGeoDataset).SpatialReference;
// }
// if (pFD == null)
// {
// pFD = (t_WsAPI.CurrentWorkspace as IFeatureWorkspace).CreateFeatureDataset("变更数据", pSR);
// }
// GeoDBAPI.CreateFields(fcSource.Fields, out IFields fields);
// IFeatureClass fcTarget = CreateFeatureClass(kvp.Value, pFD, pSR, fcSource.ShapeType, fields).FeatureClass;
// fcSourceAPI.FcToFc(fcTarget, null, false);
// }
// }
// }
// }
// catch (Exception ex)
// {
// LogAPI.Debug(ex);
// throw ex;
// }
// finally
// {
// if (s_WsAPI != null)
// {
// s_WsAPI.CloseWorkspace();
// }
// if (t_WsAPI != null)
// {
// t_WsAPI.CloseWorkspace();
// }
// }
// }
public void Export_PDT(string SaveGDBPath)
{
if (!CheckTemplateFile_PDTGX())
{
return;
}
IWorkspaceAPI s_WsAPI = null;
IWorkspaceAPI t_WsAPI = null;
try
{
if (System.IO.Directory.Exists(SaveGDBPath))
{
System.IO.Directory.Delete(SaveGDBPath);
}
//创建要保存的MDB文件
IWorkspace workspace = CreateGdb(Path.GetDirectoryName(SaveGDBPath), Path.GetFileNameWithoutExtension(SaveGDBPath));
if (workspace == null)
{
throw new Exception("无法创建GDB文件夹,导出操作已终止");
}
t_WsAPI = new WorkspaceAPI(workspace);
string currentGDBPath = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).GetProjFilePath();
s_WsAPI = new WorkspaceAPI(currentGDBPath, WorkspaceTypeEnum.GDBFile);
Dictionary<string, string> s_FcList = s_WsAPI.GetFeatureClassName(esriDatasetType.esriDTFeatureDataset);
if (s_FcList.Count > 0)
{
IFeatureDataset pFD = null;
ISpatialReference pSR = null;
foreach (KeyValuePair<string, string> kvp in s_FcList)
{
if (kvp.Value == "PDTGX")
{
IFeatureClassAPI fcSourceAPI = s_WsAPI.OpenFeatureClass(kvp.Value);
IFeatureClass fcSource = fcSourceAPI.FeatureClass;
if (fcSource.FeatureDataset != null && fcSource.FeatureDataset.Name == "变更成果")
{
pSR = (fcSource as IGeoDataset).SpatialReference;
GeoDBAPI.CreateFields(fcSource.Fields, out IFields fields);
IFeatureClass fc = t_WsAPI.CreateFeatureClass(kvp.Value, pSR, fields).FeatureClass;
fcSourceAPI.FcToFc(fc, null, false);
}
}
}
}
}
catch (Exception ex)
{
LogAPI.Debug(ex);
throw ex;
}
finally
{
if (s_WsAPI != null)
{
s_WsAPI.CloseWorkspace();
}
if (t_WsAPI != null)
{
t_WsAPI.CloseWorkspace();
}
}
}
/// <summary>
/// 检查坡度图更新层模板是否存在
/// </summary>
/// <returns></returns>
private bool CheckTemplateFile_PDTGX()
{
bool result = false; //工作空间\模板\临时文件
string str = SysAppPath.GetCurrentAppPath();
sourcePath = SysAppPath.GetCurrentAppPath() + "工作空间\\模板\\新建变更工程\\PDTGX.gdb";
if (Directory.Exists(sourcePath))
{
result = true;
}
return result;
}
/// <summary>
/// 创建数据库
/// </summary>
/// <param name="mdbFolder"></param>
/// <param name="mdbName"></param>
/// <returns></returns>
public IWorkspace CreateGdb(string gdbFolder, string gdbName)
{
try
{
IWorkspaceFactory pFtWsFct = new FileGDBWorkspaceFactoryClass();
IWorkspaceName workspaceName = pFtWsFct.Create(gdbFolder, gdbName, null, 0);
IFeatureWorkspace pFeatureWorkSpace = (workspaceName as IName).Open() as IFeatureWorkspace;
IWorkspace pWorkspace = (workspaceName as IName).Open() as IWorkspace;
return pWorkspace;
}
catch (Exception ex)
{
LogAPI.Debug("创建mdb数据库失败:" + ex);
return null;
}
}
/// <summary>
/// 在指定数据集中创建要素类
/// </summary>
/// <param name="pFeatureClassName">要素类名称</param>
/// <param name="pFields">字段集合</param>
/// <param name="pFeatureDataSet">要素数据集</param>
public IFeatureClassAPI CreateFeatureClass(string pFeatureClassName, IFeatureDataset pFeatureDataset, ISpatialReference pSpatialReference, esriGeometryType esriGeoType = esriGeometryType.esriGeometryPolygon, IFields pFields = null)
{
try
{
if (string.IsNullOrEmpty(pFeatureClassName) || pFeatureDataset == null || pSpatialReference == null)
{
return null;
}
if (pFields == null)
{
pFields = new FieldsClass();
GeoDBAPI.CreateMainField(pFields as IFieldsEdit, pSpatialReference, esriGeoType);
}
IFeatureClass fc = pFeatureDataset.CreateFeatureClass(
pFeatureClassName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
return new FeatureClassAPI(fc);
}
catch (Exception ex)
{
throw ex;
}
}
}
}