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

403 lines
18 KiB

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using KGIS.Framework.Commands;
using KGIS.Framework.Maps;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.Plugin.DTBJK.View;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Kingo.Plugin.DTBJK.Commands
{
public class CmdExportDBG : BaseMenuCommand
{
private IEngineEditor m_pEditor { get; set; }
private UCExportDTBJKData uCExportDTBJK { get; set; }
private ProjectInfo projectInfo { get; set; }
public override void OnClick()
{
if (uCExportDTBJK == null)
{
uCExportDTBJK = new UCExportDTBJKData(m_pEditor, projectInfo)
{
Title = "矢量成果导出",
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen
};
uCExportDTBJK.Closed += ExportDTBJK_CloseViewHandler;
}
uCExportDTBJK.ShowInMainWindow(true);
}
private void ExportDBGG()
{
try
{
IFeatureLayer featureClassBG = MapsManager.Instance.MapService.GetFeatureLayerByName("DLTBBG");
if (featureClassBG == null) return;
IFeatureLayer pFeatureLayer = featureClassBG as IFeatureLayer;
KGIS.Framework.Utils.Dialog.FolderBrowserDialog pBrowser = new KGIS.Framework.Utils.Dialog.FolderBrowserDialog
{
ShowNewFolderButton = true
};
if (pBrowser.ShowDialog() == DialogResult.OK)
{
string savepath = pBrowser.SelectedPath;
string fileName = projectInfo.ProWorkDir + "变更范围" + DateTime.Now.ToString("yyyyMMddHHmmssfff");//pFeatureLayer.Name;
IQueryFilter queryfilter = new QueryFilterClass();
//fileName = pFeatureLayer.FeatureClass.AliasName;
if (System.IO.File.Exists(System.IO.Path.Combine(savepath, fileName + ".shp")) || System.IO.File.Exists(System.IO.Path.Combine(savepath, fileName + ".SHP")))
{
if (MessageHelper.ShowYesNoAndTips("同名文件已存在是否替换?") != DialogResult.Yes)
{
return;
}
DeleteShp(pFeatureLayer.FeatureClass.AliasName, savepath);
}
ExportFeaturesToShp(pFeatureLayer.FeatureClass, queryfilter, savepath, fileName);
MessageHelper.ShowTips("导出成功!");
}
}
catch (Exception ex)
{
MessageHelper.ShowTips("数据导出失败!");
LogAPI.Debug("数据导出异常:" + ex);
}
}
/// <summary>
/// 导出SHP数据
/// </summary>
/// <param name="pSourceFeatureClass"></param>
/// <param name="pQueryFilter"></param>
/// <param name="forlder"></param>
/// <param name="fileName"></param>
public static void ExportFeaturesToShp(IFeatureClass pSourceFeatureClass, IQueryFilter pQueryFilter, string forlder, string fileName)
{
IWorkspaceFactory workFactory = null;
IFeatureWorkspace pFeatureWorkspace = null;
IFeatureCursor pFeatureCursor = null;
IFeatureClass pFeatureClass = null;
IFeatureCursor pInsertFeatureCursor = null;
int count = 0;
try
{
if (pSourceFeatureClass == null)
{
MessageHelper.ShowTips("未获取到要素集!");
return;
}
if (!System.IO.Directory.Exists(forlder))
{
System.IO.Directory.CreateDirectory(forlder);
}
count = pSourceFeatureClass.FeatureCount(pQueryFilter);
pFeatureCursor = pSourceFeatureClass.Search(pQueryFilter, true);
//ProgressHelper.ShowProcessBar("正在导出,请稍候...");
workFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
pFeatureWorkspace = workFactory.OpenFromFile(forlder, 0) as IFeatureWorkspace;
//创建字段信息
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
List<string> fieldNames = new List<string>();
for (int i = 0; i < pSourceFeatureClass.Fields.FieldCount; i++)
{
IField field = pSourceFeatureClass.Fields.get_Field(i);
if ("TBYBH,QSXZ,QSDWDM,QSDWMC,ZLDWDM,ZLDWMC,YSDM,KCDLBM,KCXS,KCMJ,TBDLMJ,GDPDJB,XZDWKD,FRDBS,MSSM,HDMC,BZ,XZQTZLX,BGZT,JCZT,TZ,BGDL,BGFW,WBGLX,SJLY,SFJCTB,NYJY,NYYPDL,SFJZ,ONLYZLBG".Contains(field.Name))
continue;
if ((field.Type == esriFieldType.esriFieldTypeBlob || field.Type == esriFieldType.esriFieldTypeRaster) && field.Type != esriFieldType.esriFieldTypeGeometry)
continue;
if (field.Type == esriFieldType.esriFieldTypeGeometry)
{
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "Shape";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef pGeometryDef = new GeometryDef();
IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = pSourceFeatureClass.ShapeType;//pFeature.Shape.GeometryType;
if ((pSourceFeatureClass as IGeoDataset) != null)
{
pGeometryDefEdit.SpatialReference_2 = (pSourceFeatureClass as IGeoDataset).SpatialReference;//pFeature.Shape.SpatialReference;
}
pFieldEdit.GeometryDef_2 = pGeometryDef;
pFieldsEdit.AddField(pField);
}
else if (field.Name == "SHAPE_Area" || field.Name == "SHAPE_Length")
{
continue;
}
else
{
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = field.Length;
if (field.AliasName == "种植属性名称")
{
pFieldEdit.Length_2 = field.Length + 10;
}
if (field.Name.Length > 10)
{
string fname = field.Name.Substring(0, 10);
int n = 1;
while (fieldNames.Contains(fname))
{
string end = n.ToString();
fname = fname.Remove(10 - end.Length) + end;
n++;
}
pFieldEdit.Name_2 = fname;
}
else
{
pFieldEdit.Name_2 = field.Name;
}
fieldNames.Add(pFieldEdit.Name);
pFieldEdit.AliasName_2 = field.AliasName;
pFieldEdit.Type_2 = field.Type;
pFieldsEdit.AddField(pField);
}
}
if (pSourceFeatureClass.FindField("SHAPE_Length") != -1)
{
IField field = pSourceFeatureClass.Fields.get_Field(pSourceFeatureClass.FindField("SHAPE_Length"));
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = field.Length;
if (field.Name.Length > 10)
{
string fname = field.Name.Substring(0, 10);
int n = 1;
while (fieldNames.Contains(fname))
{
string end = n.ToString();
fname = fname.Remove(10 - end.Length) + end;
n++;
}
pFieldEdit.Name_2 = fname;
}
else
{
pFieldEdit.Name_2 = field.Name;
}
fieldNames.Add(pFieldEdit.Name);
pFieldEdit.AliasName_2 = field.AliasName;
pFieldEdit.Type_2 = field.Type;
pFieldsEdit.AddField(pField);
}
if (pSourceFeatureClass.FindField("SHAPE_Area") != -1)
{
IField field = pSourceFeatureClass.Fields.get_Field(pSourceFeatureClass.FindField("SHAPE_Area"));
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Length_2 = field.Length;
pFieldEdit.Name_2 = field.Name;
fieldNames.Add(pFieldEdit.Name);
pFieldEdit.AliasName_2 = field.AliasName;
pFieldEdit.Type_2 = field.Type;
pFieldsEdit.AddField(pField);
}
pFeatureClass = pFeatureWorkspace.CreateFeatureClass(fileName, pFields, null, null, pSourceFeatureClass.FeatureType, "SHAPE", "");
pInsertFeatureCursor = pFeatureClass.Insert(true);
int num = 0;
//ProgressHelper.CountProgress = count;
Dictionary<int, int> dicMatchFields = new Dictionary<int, int>();
GetMatchFieldsDirectory(pSourceFeatureClass, pFeatureClass, ref dicMatchFields, true);
IFeatureBuffer featureAdd = pFeatureClass.CreateFeatureBuffer();
IFeature pFeature = null;
while ((pFeature = pFeatureCursor.NextFeature()) != null)
{
featureAdd.Shape = pFeature.Shape;
foreach (KeyValuePair<int, int> kvpMatchField in dicMatchFields)
{
if (pFeature.Value[kvpMatchField.Value] != DBNull.Value)
{
featureAdd.set_Value(kvpMatchField.Key, pFeature.get_Value(kvpMatchField.Value));
}
else
{
featureAdd.set_Value(kvpMatchField.Key, null);
}
}
pInsertFeatureCursor.InsertFeature(featureAdd);
num++;
if (num % 100 == 0 || num == count)
{
//ProgressHelper.CurrentProgress = num;
}
}
pInsertFeatureCursor.Flush();
//关闭lock
IWorkspaceFactoryLockControl ipWsFactoryLock = (IWorkspaceFactoryLockControl)workFactory;
if (ipWsFactoryLock.SchemaLockingEnabled)
{
ipWsFactoryLock.DisableSchemaLocking();
}
}
catch (Exception ex)
{
MessageHelper.ShowError("导出SHAPE文件出错:" + ex.Message);
LogAPI.Debug("导出SHAPE文件出错,可能的原因是:" + ex.Message);
}
finally
{
if (pFeatureCursor != null)
{
Marshal.ReleaseComObject(pFeatureCursor);
}
if (pFeatureClass != null)
{
Marshal.ReleaseComObject(pFeatureClass);
}
if (pInsertFeatureCursor != null)
{
Marshal.ReleaseComObject(pInsertFeatureCursor);
}
if (pFeatureWorkspace != null)
{
Marshal.ReleaseComObject(pFeatureWorkspace);
}
if (workFactory != null)
{
Marshal.ReleaseComObject(workFactory);
}
}
}
/// <summary>
/// 删除同名SHP
/// </summary>
/// <param name=""></param>
public static void DeleteShp(string fileName, string filePath)
{
IWorkspace pWorkspace = null;
IWorkspaceFactory pWorkspaceFactory = null;
IFeatureClass pFeatureClass = null;
try
{
//打开shp工作空间
pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactory();
pWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0);
IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
IDataset pFeaDataset = pFeatureClass as IDataset;
pFeaDataset.Delete();
}
catch (Exception ex)
{
LogAPI.Debug("删除shp失败,报错原因是:" + ex.Message);
}
finally
{
if (pWorkspaceFactory != null)
{
Marshal.ReleaseComObject(pWorkspaceFactory);
}
if (pWorkspace != null)
{
Marshal.ReleaseComObject(pWorkspace);
}
if (pFeatureClass != null)
{
Marshal.ReleaseComObject(pFeatureClass);
}
}
}
/// <summary>
/// 获得匹配的字段索引集合
/// </summary>
/// <param name="pSourceFeatureClass">源要素类</param>
/// <param name="pTargetClass">目标要素类</param>
/// <param name="dicMatchFields">匹配字段集合-KEY为目标图层字段,VALUE为源图层字段</param>
/// <param name="isGetRequired">是否获取必须字段</param>
public static void GetMatchFieldsDirectory(IClass pSourceClass, IClass pTargetClass, ref Dictionary<int, int> dicMatchFields, bool isGetRequired = false)
{
for (int i = 0; i < pTargetClass.Fields.FieldCount; i++)
{
IField pTargetField = pTargetClass.Fields.get_Field(i);
//目标图层的字段必须为可编辑并且不是必须字段
if (pTargetField.Required == false && pTargetField.Editable == true)
{
int iSourceFeatureClassIndex = pSourceClass.Fields.FindField(pTargetField.Name);
if (pTargetField.Name == "SHAPE_Leng")
{
iSourceFeatureClassIndex = pSourceClass.Fields.FindField("SHAPE_Length");
}
//源要素类中该字段存在
if (iSourceFeatureClassIndex > -1)
{
IField pSourceField = pSourceClass.Fields.get_Field(iSourceFeatureClassIndex);
if ("XZQTZLX,BGZT,JCZT,TZ,BGDL,BGFW,WBGLX,SJLY,SFJCTB,NYJY,NYYPDL,SFJZ,ONLYZLBG".Contains(pSourceField.Name))
continue;
//目标图层的字段也必须为可编辑并且不是必须字段
if (pSourceField.Required == false && pSourceField.Editable == true)
{
//添加到字段匹配集合中
dicMatchFields.Add(i, iSourceFeatureClassIndex);
}
else if (isGetRequired)
{
dicMatchFields.Add(i, iSourceFeatureClassIndex);
}
}
}
//此处为了保留原有要素ObjectID字段值
if (pTargetField.Name.Equals("OIDCOPY", StringComparison.CurrentCultureIgnoreCase))
{
dicMatchFields.Add(i, pSourceClass.FindField(pSourceClass.OIDFieldName));
}
}
}
public override void OnCreate(object Hook)
{
m_pEditor = new EngineEditorClass();
}
private void ExportDTBJK_CloseViewHandler(object sender, EventArgs e)
{
try
{
if (uCExportDTBJK != null)
{
uCExportDTBJK.DataContext = null;
uCExportDTBJK = null;
}
}
catch (Exception ex)
{
LogAPI.Debug(ex);
}
}
public override bool Enabled => JudgeIsHaveTargetPath();
private bool JudgeIsHaveTargetPath()
{
try
{
projectInfo = MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo;
if (projectInfo == null)
{
return false;
}
return true;
}
catch (Exception ex)
{
LogAPI.Debug("判定 检测数据加载 按钮是否有效时失败,异常原因: " + ex + " ; ");
return false;
}
}
}
}