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

353 lines
15 KiB

4 months ago
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.Core.Services;
using KGIS.Framework.Maps;
using KGIS.Framework.Utils;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Path = System.IO.Path;
namespace Kingo.Plugin.YJJK.ModelEntity
{
public class ExportBGMDB
{
private string GX_VCTPath;
private string GXGC_VCTPath;
public ExportBGMDB(string pGX_VCTPath, string pGXGC_VCTPath)
{
GX_VCTPath = pGX_VCTPath;
GXGC_VCTPath = pGXGC_VCTPath;
}
public string LayerNameFilter { get; set; } = null;
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;
}
CreateMdb(Path.GetDirectoryName(SavedMDBPath), Path.GetFileNameWithoutExtension(SavedMDBPath));
t_WsAPI = new WorkspaceAPI(SavedMDBPath, WorkspaceTypeEnum.MDBFile);
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();
}
}
}
public void ExportGDB(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);
ISpatialReference pSR = null;
IFeatureClassAPI fcSourceAPI = s_WsAPI.OpenFeatureClass("PDTGX");
IFeatureClass fcSource = fcSourceAPI.FeatureClass;
if (pSR == null)
{
pSR = (fcSource as IGeoDataset).SpatialReference;
}
GeoDBAPI.CreateFields(fcSource.Fields, out IFields fields);
t_WsAPI.DeleteFeatureClass("PDTGX");
IFeatureClassAPI fcTargetAPI = t_WsAPI.CreateFeatureClass("PDTGX", pSR, fields);
fcSourceAPI.FcToFc(fcTargetAPI.FeatureClass, null, false);
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();
}
}
}
/// <summary>
/// 创建数据库
/// </summary>
/// <param name="mdbFolder"></param>
/// <param name="mdbName"></param>
/// <returns></returns>
public void CreateMdb(string mdbFolder, string mdbName)
{
try
{
IWorkspaceFactory pFtWsFct = new AccessWorkspaceFactory();
pFtWsFct.Create(mdbFolder, mdbName, null, 0);
}
catch (Exception ex)
{
LogAPI.Debug("创建mdb数据库失败:" + ex);
}
}
public void CreateGDB(string mdbFolder, string mdbName)
{
try
{
IWorkspaceFactory pFtWsFct = new FileGDBWorkspaceFactory();
pFtWsFct.Create(mdbFolder, mdbName, null, 0);
//IWorkspaceName workspaceName = pFtWsFct.Create(mdbFolder, mdbName, 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);
}
}
/// <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;
}
}
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;
}
}
}