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.
521 lines
24 KiB
521 lines
24 KiB
using ESRI.ArcGIS.Carto; |
|
using ESRI.ArcGIS.Geodatabase; |
|
using ESRI.ArcGIS.Geometry; |
|
using KGIS.Framework.DBOperator; |
|
using KGIS.Framework.Maps; |
|
using KGIS.Framework.Platform.Helper; |
|
using KGIS.Framework.Utils; |
|
using KGIS.Framework.Utils.Helper; |
|
using NetTopologySuite.IO; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Runtime.InteropServices; |
|
using System.Windows; |
|
using KGIS.Framework.AE.ExtensionMethod; |
|
using Kingo.PluginServiceInterface; |
|
using KGIS.Framework.Platform; |
|
|
|
namespace Kingo.Plugin.BGResultManager.View |
|
{ |
|
/// <summary> |
|
/// FrmGCDataToSHPGTY.xaml 的交互逻辑 |
|
/// </summary> |
|
public partial class FrmGCDataToSHPGTY : BaseWindow, IBGResultFormHelper |
|
{ |
|
public static IGeometryFactory3 GeometryFactory3 { get; set; } |
|
IFeatureLayer featureClassJCtTB = null; |
|
public FrmGCDataToSHPGTY(IFeatureLayer featurelayer) |
|
{ |
|
InitializeComponent(); |
|
InitViewData(featurelayer); |
|
} |
|
|
|
public void InitView(IFeatureLayer layer) |
|
{ |
|
InitViewData(layer); |
|
} |
|
|
|
private void InitViewData(IFeatureLayer layer) |
|
{ |
|
if (Platform.Instance.SystemType == SystemTypeEnum.BGFWCG) |
|
{ |
|
this.Title = "变化图斑成果导出"; |
|
//txtTargetLayer.Text = "变化图斑"; |
|
txtTargetLayer.ItemsSource = new List<string>() { "监测图斑" }; |
|
panelGLZD.Visibility = Visibility.Collapsed; |
|
Height = 220; |
|
} |
|
else |
|
{ |
|
this.Title = "预变更成果导出"; |
|
//txtTargetLayer.Text = "监测图斑"; |
|
txtTargetLayer.ItemsSource = new List<string>() { "监测图斑" }; |
|
panelGLZD.Visibility = Visibility.Visible; |
|
Height = 280; |
|
} |
|
txtTargetLayer.SelectedIndex = 0; |
|
featureClassJCtTB = layer; |
|
} |
|
|
|
/// <summary> |
|
/// 导出 |
|
/// </summary> |
|
/// <param name="sender"></param> |
|
/// <param name="e"></param> |
|
private void BtnExport_Click(object sender, RoutedEventArgs e) |
|
{ |
|
try |
|
{ |
|
btnExport.IsEnabled = false; |
|
btnCanel.IsEnabled = false; |
|
if (!System.IO.Directory.Exists(btnImportDataPath.Text)) |
|
{ |
|
MessageHelper.ShowTips("不存在保存路径下的该文件夹!"); |
|
return; |
|
} |
|
if (featureClassJCtTB == null) |
|
{ |
|
MessageHelper.ShowTips("不存在需导出的图层!"); |
|
return; |
|
} |
|
//IFeatureLayer featureClassJCtTB = MapsManager.Instance.MapService.GetFeatureLayerByName("JCTB"); |
|
string fileName = featureClassJCtTB.Name; |
|
IQueryFilter queryfilter = new QueryFilterClass(); |
|
if (txtTarget.SelectedIndex != 2)//举证条件过滤 |
|
{ |
|
queryfilter.WhereClause = string.Format("SFJZ='{0}'", txtTarget.SelectedIndex.ToString()); |
|
} |
|
if (featureClassJCtTB.FeatureClass.FeatureCount(queryfilter) == 0) |
|
{ |
|
MessageHelper.ShowTips("当前条件下并无数据!"); |
|
return; |
|
} |
|
fileName = featureClassJCtTB.FeatureClass.AliasName; |
|
if (rdGJJC.IsChecked == true)//GTY格式导出 |
|
{ |
|
string dbPath = System.IO.Path.Combine(SysAppPath.GetCurrentAppPath() + "工作空间\\模板\\GTY模板文件", "610000NDBG2021.gty.db"); |
|
if (!System.IO.File.Exists(dbPath)) |
|
{ |
|
MessageHelper.ShowTips("不存在GTY文件的DB模板!路径:" + dbPath); |
|
return; |
|
} |
|
//string DBFields = "TBBSM,XZQDM,XMC,JCBH,JCMJ,XZDM,XZMC,CSDM,CSMC,YSTBBH,TBLX,QSX,HSX,BZ,TZ,SHAPEAREA,SJDRLY,XZB,YZB,YZBLONGITUDE,LATITUDE,SHAPE"; |
|
string DBFields = ""; |
|
if (Platform.Instance.SystemType == SystemTypeEnum.BGFWCG) |
|
DBFields = "XZQDM,XMC,JCBH,TBLX,TZ,HSX,DDTC,XZB,YZB,JCMJ,BGDL,BGFW,WBGLX,BZ,SHAPEAREA,SHAPE"; |
|
else |
|
DBFields = "TBBSM,XZQDM,XMC,JCBH,JCMJ,YSTBBH,TBLX,HSX,BZ,TZ,SHAPEAREA,XZB,YZB,SHAPE"; |
|
List<string> sqls = new List<string>(); |
|
SQLiteDBOperate.Instance.ExecuteNonQuery(dbPath, "delete from NDBG2021", null); |
|
IFeatureCursor featureCursor = featureClassJCtTB.FeatureClass.Search(queryfilter, true); |
|
IFeature feature = null; |
|
IFields fields = featureClassJCtTB.FeatureClass.Fields; |
|
while ((feature = featureCursor.NextFeature()) != null) |
|
{ |
|
string DBValue = ""; |
|
string[] fieldArr = DBFields.Split(','); |
|
foreach (string dbField in fieldArr) |
|
{ |
|
if (dbField == "SHAPE") |
|
{ |
|
object jObject = null; |
|
ISpatialReferenceFactory NewSpatialReference = new SpatialReferenceEnvironmentClass(); |
|
ISpatialReference s = NewSpatialReference.CreateGeographicCoordinateSystem(4490); |
|
IGeometry geo = feature.ShapeCopy; |
|
geo.Project(s); |
|
jObject = geo.ToJson(); |
|
DBValue += string.Format("'{0}',", jObject.ToString()); //每个位置必存在对应值 |
|
continue; |
|
} |
|
if (fields.FindField(dbField) != -1) |
|
{ |
|
if ("XZB,YZB".Contains(dbField) && string.IsNullOrWhiteSpace(feature.Value[fields.FindField(dbField)].ToString())) |
|
DBValue += string.Format("{0},", 0); |
|
else if ("XZB,YZB".Contains(dbField)) |
|
DBValue += string.Format("{0},", feature.Value[fields.FindField(dbField)]); |
|
else |
|
DBValue += string.Format("'{0}',", feature.Value[fields.FindField(dbField)]); |
|
} |
|
else |
|
{ |
|
if (dbField == "TBBSM") |
|
{ |
|
if (string.IsNullOrWhiteSpace(feature.Value[fields.FindField("BSM")].ToString())) |
|
{ |
|
DBValue += Guid.NewGuid() + ","; |
|
} |
|
else |
|
{ |
|
DBValue += string.Format("'{0}',", feature.Value[fields.FindField("BSM")]); |
|
} |
|
} |
|
else if (dbField == "SHAPEAREA") |
|
{ |
|
DBValue += string.Format("'{0}',", feature.Value[fields.FindField("SHAPE_Area")]); |
|
} |
|
else if (dbField == "YSTBBH") |
|
{ |
|
DBValue += string.Format("'{0}',", feature.Value[fields.FindField("TBBH")]); |
|
} |
|
else |
|
{ |
|
if ("YZBLONGITUDE,LATITUDE".Contains(dbField) && string.IsNullOrWhiteSpace(feature.Value[fields.FindField(dbField)].ToString())) |
|
DBValue += string.Format("{0},", 0); |
|
else if ("YZBLONGITUDE,LATITUDE".Contains(dbField)) |
|
DBValue += string.Format("{0},", feature.Value[fields.FindField(dbField)]); |
|
else |
|
DBValue += string.Format("'{0}',", "");//每个位置必存在对应值 |
|
} |
|
} |
|
} |
|
sqls.Add(string.Format("insert into NDBG2021 ({0})" + " VALUES ({1})", DBFields, DBValue.Trim(','))); |
|
int asd = DBValue.Trim(',').Split(',').Length; |
|
} |
|
SQLiteDBOperate.Instance.ExecuteNoQueryList(dbPath, sqls); |
|
string NewGTYName = Guid.NewGuid() + "NDBG2022.gty"; |
|
string NewdbPath = System.IO.Path.Combine(btnImportDataPath.Text, NewGTYName); |
|
System.IO.File.Copy(dbPath, NewdbPath); |
|
MessageHelper.ShowTips("GTY文件导出成功!"); |
|
} |
|
else//SHP格式导出 |
|
{ |
|
if (System.IO.File.Exists(System.IO.Path.Combine(btnImportDataPath.Text, fileName + ".shp")) || System.IO.File.Exists(System.IO.Path.Combine(btnImportDataPath.Text, fileName + ".SHP"))) |
|
{ |
|
if (MessageHelper.ShowYesNoAndTips("同名文件已存在,是否替换?") != System.Windows.Forms.DialogResult.Yes) |
|
{ |
|
return; |
|
} |
|
DeleteShp(featureClassJCtTB.FeatureClass.AliasName, btnImportDataPath.Text); |
|
} |
|
ExportFeaturesToShp(featureClassJCtTB.FeatureClass, queryfilter, btnImportDataPath.Text, fileName); |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
LogAPI.Debug(ex.Message); |
|
} |
|
finally |
|
{ |
|
btnExport.IsEnabled = true; |
|
btnCanel.IsEnabled = true; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 导出SHP数据 |
|
/// </summary> |
|
/// <param name="pSourceFeatureClass"></param> |
|
/// <param name="pQueryFilter"></param> |
|
/// <param name="forlder"></param> |
|
/// <param name="fileName"></param> |
|
public 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 ("XZQTZLX,BGZT,JCZT,TZ,BGDL,BGFW,WBGLX,SJLY,SFJCTB,NYJY,NYYPDL,SFJZ,ONLYZLBG".Contains(field.Name)) |
|
// continue; |
|
if (Platform.Instance.SystemType == SystemTypeEnum.BGFWCG) |
|
{ |
|
if (!"XZQDM,XMC,JCBH,TBLX,TZ,HSX,DDTC,XZB,YZB,JCMJ,BGDL,BGFW,WBGLX,BZ,SHAPEAREA,SHAPE".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 |
|
{ |
|
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); |
|
} |
|
} |
|
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(); |
|
ProgressHelper.CloseProcessBar(); |
|
MessageHelper.ShowTips("SHP文件导出成功!"); |
|
//关闭lock |
|
IWorkspaceFactoryLockControl ipWsFactoryLock = (IWorkspaceFactoryLockControl)workFactory; |
|
if (ipWsFactoryLock.SchemaLockingEnabled) |
|
{ |
|
ipWsFactoryLock.DisableSchemaLocking(); |
|
} |
|
} |
|
catch (Exception ex) |
|
{ |
|
ProgressHelper.CloseProcessBar(); |
|
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> |
|
/// 获得匹配的字段索引集合 |
|
/// </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)); |
|
} |
|
} |
|
} |
|
|
|
/// <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="sender"></param> |
|
/// <param name="e"></param> |
|
private void BtnSelectedImportDataPath_Click(object sender, RoutedEventArgs e) |
|
{ |
|
KGIS.Framework.Utils.Dialog.FolderBrowserDialog pBrowser = new KGIS.Framework.Utils.Dialog.FolderBrowserDialog |
|
{ |
|
ShowNewFolderButton = true |
|
}; |
|
if (pBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK) |
|
{ |
|
btnImportDataPath.Text = pBrowser.SelectedPath; |
|
} |
|
} |
|
|
|
public static string ConvertIGeoemtryToWKT(IGeometry geometry) |
|
{ |
|
return ConvertWKBToWKT(ConvertGeometryToWKB(geometry)); |
|
} |
|
/// <summary> |
|
/// IGeometry转WKB |
|
/// </summary> |
|
/// <param name="geometry"></param> |
|
/// <returns></returns> |
|
public static byte[] ConvertGeometryToWKB(IGeometry geometry) |
|
{ |
|
//IWkb wkb = geometry as IWkb; |
|
ITopologicalOperator oper = geometry as ITopologicalOperator; |
|
oper.Simplify(); |
|
if (GeometryFactory3 == null) |
|
{ |
|
GeometryFactory3 = new GeometryEnvironment() as IGeometryFactory3; |
|
} |
|
byte[] b = GeometryFactory3.CreateWkbVariantFromGeometry(geometry) as byte[]; |
|
return b; |
|
} |
|
/// <summary> |
|
/// WKB转WKT |
|
/// </summary> |
|
/// <param name="wkb"></param> |
|
/// <returns></returns> |
|
public static string ConvertWKBToWKT(byte[] wkb) |
|
{ |
|
try |
|
{ |
|
WKTWriter writer = new WKTWriter(); |
|
WKBReader reader = new WKBReader(); |
|
return writer.Write(reader.Read(wkb)); |
|
} |
|
catch (Exception ex) |
|
{ |
|
return ex.Message; |
|
} |
|
} |
|
private void BtnCanel_Click(object sender, RoutedEventArgs e) |
|
{ |
|
this.Close(); |
|
} |
|
|
|
} |
|
}
|
|
|