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

720 lines
30 KiB

using DevExpress.XtraBars;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using KGIS.Framework.AE;
using KGIS.Framework.AE.Enum;
using KGIS.Framework.Maps;
using KGIS.Framework.OpenData.Control;
using KGIS.Framework.OpenData.Filter;
using KGIS.Framework.OpenData.InterFace;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.Plugin.BHTB_Extract.Model;
using Kingo.PluginServiceInterface;
using KUI.Windows;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Rectangle = Kingo.Plugin.BHTB_Extract.Model.Rectangle;
using ESRI.ArcGIS.ADF;
using UIShell.OSGi;
namespace Kingo.Plugin.BHTB_Extract.View
{
/// <summary>
/// 分幅 的交互逻辑
/// </summary>
public partial class ExtractDataView : BaseWindow
{
public double FramingSize { get; set; }
//public IFeatureClass featureClass { get; set; }
//public IFeatureClass featureClassDLTB { get; set; }
private IEnvelope CurrentEnvelope { get; set; }
ILayer m_featureLayer = null;
IFeatureClass m_featureClass = null;
public ExtractDataView()
{
InitializeComponent();
Init();
}
private void Init()
{
List<ILayer> MapLayerList = MapsManager.Instance.MapService.GetAllLayerInMap<ILayer>();
List<ILayer> SelectLayerList = new List<ILayer>();
List<IGeometry> geometries = new List<IGeometry>();
try
{
if (CurrentEnvelope != null)
CurrentEnvelope.SetEmpty();
foreach (ILayer item in MapLayerList)
{
if ((item as RasterLayer) != null)
{
txtFcPath.Text = (item as IRasterLayer).FilePath;
SelectLayerList.Add(item);
//确保最大范围
if (CurrentEnvelope == null)
CurrentEnvelope = item.AreaOfInterest;
else
CurrentEnvelope.Union(item.AreaOfInterest);
m_featureLayer = item as RasterLayer;
}
else if ((item as KOTilesLayer) != null)
{
txtFcPath.Text = (item as KOTilesLayer).DBPath;
SelectLayerList.Add(item);
//确保最大范围
if (CurrentEnvelope == null)
CurrentEnvelope = item.AreaOfInterest;
else
CurrentEnvelope.Union(item.AreaOfInterest);
m_featureLayer = item as KOTilesLayer;
}
}
if (SelectLayerList.Count == 0)
{
foreach (ILayer item in MapLayerList)
{
if ((item as IFeatureLayer) != null)
{
txtFcPath.Text = ((item as IFeatureLayer).FeatureClass as FeatureClass).Workspace.PathName;
if ((item as IFeatureLayer).FeatureClass.FeatureCount(null) == 0) continue;
SelectLayerList.Add(item);
//确保最大范围
if (CurrentEnvelope == null)
CurrentEnvelope = item.AreaOfInterest;
else
{
IEnvelope env = item.AreaOfInterest;
//if (env != null)
// CurrentEnvelope.Union(env);
if (env.XMin < 0 || env.YMin < 0) continue;
if (env.XMax > CurrentEnvelope.XMax)
{
CurrentEnvelope.XMax = env.XMax;
}
if (env.YMax > CurrentEnvelope.YMax)
{
CurrentEnvelope.YMax = env.YMax;
}
if (env.XMin < CurrentEnvelope.XMin)
{
CurrentEnvelope.XMin = env.XMin;
}
if (env.YMin < CurrentEnvelope.YMin)
{
CurrentEnvelope.YMin = env.YMin;
}
}
m_featureLayer = item;
}
}
}
IEnvelope envelope = CurrentEnvelope;
if (envelope == null)
{
tbXmin.Text = "0";
tbXmax.Text = "0";
tbYmin.Text = "0";
tbYmax.Text = "0";
return;
}
tbXmin.Text = (envelope.XMin < 0 ? 0 : envelope.XMin).ToString();
tbXmax.Text = envelope.XMax.ToString();
tbYmin.Text = (envelope.YMin < 0 ? 0 : envelope.YMin).ToString();
tbYmax.Text = envelope.YMax.ToString();
}
catch (Exception ex)
{
LogAPI.Debug("初始化作业区生成页面值失败:" + ex);
return;
}
}
//固定分幅大小
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
if (radioBtDistance.IsChecked == true)
{
if (!string.IsNullOrEmpty(tbFramingSize.Text))
{
bool CanGetFramingSizeValue = double.TryParse(tbFramingSize.Text, out double size);
if (!CanGetFramingSizeValue)
{
MessageHelper.ShowTips("请输入合理的固定距离值!");
return;
}
if (size <= 0)
{
MessageHelper.ShowTips("输入值请大于0!");
return;
}
}
else
{
MessageHelper.ShowTips("请输入合理的固定距离值!");
return;
}
}
if (string.IsNullOrEmpty(txtFcPath.Text))
{
MessageHelper.ShowTips("请选择作业区范围!");
return;
}
(sender as Button).IsEnabled = false;
ExtensionShowWindow.MainWinForm.Enabled = false;
try
{
this.ShowLoading("正在进行图斑分幅...", 0, 0);
bool isSucceed = false;
if (radioBtDistance.IsChecked == true)
{
FramingSize = double.Parse(tbFramingSize.Text);
isSucceed = GetEnvelopeBorder();
}
else
{
isSucceed = GrtGraphBorder();
}
this.CloseLoading();
if (isSucceed)
{
this.UpdateMsg("正在添加作业区图斑标注...");
MarkLayer();
//刷新作业区管理列表
if (UcZYQMagr.Instance.IsLoaded)
{
this.UpdateMsg("正在刷新作业区管理列表...");
UcZYQMagr.Instance.InitTreeView();
}
IUcMulitMapControlHelper ucMulitMapControlHelper = BundleRuntime.Instance.GetFirstOrDefaultService<IUcMulitMapControlHelper>();
if (ucMulitMapControlHelper != null)
{
ucMulitMapControlHelper.ClearData();
ucMulitMapControlHelper.SetLayer();
ucMulitMapControlHelper.MarkLabelLayer();
}
MessageHelper.ShowTips("作业区生成完成。");
}
this.Close();
}
catch (Exception ex)
{
LogAPI.Debug(ex);
return;
}
finally
{
this.CloseLoading();
(sender as Button).IsEnabled = true;
GC.Collect();
ExtensionShowWindow.MainWinForm.Enabled = true;
this.Close();
}
}
private bool GrtGraphBorder()
{
IFeatureCursor pCursor = null;
IFeature feature = null;
IFeatureCursor insertCur = null;
IFeatureBuffer newFeature = null;
try
{
if (m_featureClass == null) return false;
IFeatureClass featureClass = MapsManager.Instance.MapService.GetFeatureClassByName("ZYQ");
if (featureClass == null)
{
MessageHelper.ShowError("创建分幅GDB文件失败!");
return false;
}
(featureClass as ITable).DeleteSearchedRows(null);
ISpatialReference spr = (featureClass as IGeoDataset).SpatialReference;
insertCur = featureClass.Insert(true);
int k = 0;
int num = 0;
pCursor = m_featureClass.Search(null, true);
while ((feature = pCursor.NextFeature()) != null)
{
k++;
num++;
newFeature = featureClass.CreateFeatureBuffer();
newFeature.Shape = feature.Shape;//同时转换坐标系统
newFeature.set_Value(newFeature.Fields.FindField("ZYQBH"), "ZYQ" + num.ToString("000000"));
newFeature.set_Value(newFeature.Fields.FindField("ZYQMC"), "作业区_" + num.ToString("000000"));
newFeature.set_Value(newFeature.Fields.FindField("ZYQZT"), 0);
newFeature.set_Value(newFeature.Fields.FindField("HH"), null);
newFeature.set_Value(newFeature.Fields.FindField("LH"), null);
insertCur.InsertFeature(newFeature);
if (k % 500 == 0)
{
k = 0;
insertCur.Flush();
}
}
if (k > 0)
{
insertCur.Flush();
}
return true;
}
catch (Exception ex)
{
MessageHelper.ShowError("图斑分幅发生异常,详情请查看系统日志!");
LogAPI.Debug("图斑分幅时发生异常,异常信息如下:");
LogAPI.Debug(ex);
return false;
}
finally
{
if (pCursor != null)
Marshal.ReleaseComObject(pCursor);
if (feature != null)
Marshal.ReleaseComObject(feature);
if (insertCur != null)
Marshal.ReleaseComObject(insertCur);
if (newFeature != null)
Marshal.ReleaseComObject(newFeature);
GC.Collect();
}
}
private bool GetEnvelopeBorder()
{
IEnvelope envelope = CurrentEnvelope;
if (envelope == null)
{
MessageHelper.Show("未获取到作业区范围数据,请重新选择作业区范围!");
return false;
}
IFeatureCursor insertCur = null;
IFeatureBuffer newFeature = null;
try
{
Rectangle wjfw = new Rectangle();
wjfw.MinX = envelope.XMin;
wjfw.MaxX = envelope.XMax;
wjfw.MaxY = envelope.YMax;
wjfw.MinY = envelope.YMin;
List<BHTBFramingDatas> bwtfList = GetBWTFH(wjfw);
if (bwtfList.Count == 0) return false;
IFeatureClass featureClass = MapsManager.Instance.MapService.GetFeatureClassByName("ZYQ");
if (featureClass == null)
{
MessageHelper.ShowError("创建分幅GDB文件失败!");
return false;
}
(featureClass as ITable).DeleteSearchedRows(null);
ISpatialReference spr = (featureClass as IGeoDataset).SpatialReference;
insertCur = featureClass.Insert(true);
int k = 0;
foreach (var item in bwtfList)
{
k++;
newFeature = featureClass.CreateFeatureBuffer();
newFeature.Shape = GetGeoByWJFW(item.WJFW, spr);//同时转换坐标系统
newFeature.set_Value(newFeature.Fields.FindField("ZYQBH"), item.TFH);
newFeature.set_Value(newFeature.Fields.FindField("ZYQMC"), item.TBName);
newFeature.set_Value(newFeature.Fields.FindField("ZYQZT"), 0);
newFeature.set_Value(newFeature.Fields.FindField("HH"), item.HH);
newFeature.set_Value(newFeature.Fields.FindField("LH"), item.LH);
insertCur.InsertFeature(newFeature);
if (k % 500 == 0)
{
k = 0;
insertCur.Flush();
}
}
if (k > 0)
{
insertCur.Flush();
}
return true;
}
catch (Exception ex)
{
MessageHelper.ShowError("图斑分幅发生异常,详情请查看系统日志!");
LogAPI.Debug("图斑分幅时发生异常,异常信息如下:");
LogAPI.Debug(ex);
return false;
}
finally
{
if (insertCur != null)
Marshal.ReleaseComObject(insertCur);
if (newFeature != null)
Marshal.ReleaseComObject(newFeature);
GC.Collect();
}
}
/// <summary>
/// 添加作业区图斑标注
/// </summary>
private void MarkLayer()
{
try
{
IFeatureLayer pFeatureLayer = MapsManager.Instance.MapService.GetFeatureLayerByName("ZYQ");
if (pFeatureLayer == null) return;
IGeoFeatureLayer pGeoLayer = pFeatureLayer as IGeoFeatureLayer;
IRgbColor pColor = new RgbColorClass() { Red = 85, Blue = 105, Green = 255 };
stdole.IFontDisp pFont = new stdole.StdFont()
{
Name = "宋体",
//Bold = true
} as stdole.IFontDisp;
ITextSymbol pTextSymbol = new TextSymbolClass()
{
Color = pColor,
Font = pFont,
Size = 8
};
IBasicOverposterLayerProperties pBasicOverLayerPro = new BasicOverposterLayerPropertiesClass();
pBasicOverLayerPro.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon;
pBasicOverLayerPro.GenerateUnplacedLabels = true;
pBasicOverLayerPro.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerShape;
ILabelEngineLayerProperties pLableEngine = new LabelEngineLayerPropertiesClass()
{
Symbol = pTextSymbol,
IsExpressionSimple = true
};
IOverposterLayerProperties2 pOverLayerProper = pBasicOverLayerPro as IOverposterLayerProperties2;
pOverLayerProper.TagUnplaced = true;//设置该属性,目的是不被其他element遮挡
pLableEngine.BasicOverposterLayerProperties = pBasicOverLayerPro;
IAnnotateLayerProperties pAnnoPros = pLableEngine as IAnnotateLayerProperties;
pGeoLayer.AnnotationProperties.Add(pAnnoPros);
pGeoLayer.DisplayAnnotation = true;
AxMapControl axMapControl1 = MapsManager.Instance.MapService.getAxMapControl();
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, axMapControl1.Extent);
}
catch (Exception ex)
{
LogAPI.Debug("设置标注时发生异常,异常信息如下:" + ex);
return;
}
}
private List<BHTBFramingDatas> GetBWTFH(Rectangle wjfw)
{
if (wjfw == null) return null;
List<BHTBFramingDatas> bwtfList = new List<BHTBFramingDatas>();
IIdentify indentify = null;
IGeometry pGeometry = null;
try
{
if (m_featureLayer != null)
indentify = m_featureLayer as IIdentify;
double xlength = wjfw.MaxX - wjfw.MinX;
double ylength = wjfw.MaxY - wjfw.MinY;
int xCount = (int)Math.Ceiling(xlength / FramingSize);
int yCount = (int)Math.Ceiling(ylength / FramingSize);
int num = 0;
for (int y = 0; y < yCount; y++)
{
for (int x = 0; x < xCount; x++)
{
Rectangle rectangle = new Rectangle()
{
MinX = wjfw.MinX + x * FramingSize,
MinY = wjfw.MinY + y * FramingSize,
MaxX = wjfw.MinX + (x + 1) * FramingSize,
MaxY = wjfw.MinY + (y + 1) * FramingSize
};
if (rectangle.MaxX > wjfw.MaxX)
{
rectangle.MaxX = wjfw.MaxX;
}
if (rectangle.MaxY > wjfw.MaxY)
{
rectangle.MaxY = wjfw.MaxY;
}
if (indentify != null)
{
IEnvelope envelope = new EnvelopeClass();
envelope.XMax = rectangle.MaxX;
envelope.XMin = rectangle.MinX;
envelope.YMax = rectangle.MaxY;
envelope.YMin = rectangle.MinY;
pGeometry = envelope as IGeometry;
if(pGeometry != null)
{
IArray pIDs = indentify.Identify(pGeometry);
if (pIDs == null || pIDs.Count == 0) continue;
}
}
num++;
BHTBFramingDatas bwtf = new BHTBFramingDatas();
bwtf.TFH = "ZYQ" + num.ToString("000000");
bwtf.TBName = "作业区_" + num.ToString("000000");
bwtf.IsModify = "否";
bwtf.HH = x;
bwtf.LH = y;
bwtf.WJFW = rectangle;
bwtfList.Add(bwtf);
}
}
return bwtfList;
}
catch (Exception ex)
{
LogAPI.Debug("图斑分幅错误:" + ex);
return bwtfList;
}
}
public void SetField(ref IFields fields, List<DataTypeEntity> lstEntity)
{
try
{
if (lstEntity == null)
{
return;
}
IField pField = new FieldClass();
IFieldEdit pFieldEdit = (IFieldEdit)pField;
IFieldsEdit pFieldsEdit = (IFieldsEdit)fields;
foreach (var item in lstEntity)
{
pField = new FieldClass();
pFieldEdit = (IFieldEdit)pField;
if (!string.IsNullOrWhiteSpace(item.FieldAliasName))
{
pFieldEdit.AliasName_2 = item.FieldAliasName;
}
pFieldEdit.Name_2 = item.DataName;
pFieldEdit.Type_2 = item.DataType;
if (!string.IsNullOrEmpty(item.DefaultValue))
{
pFieldEdit.DefaultValue_2 = item.DefaultValue;
}
else
{
pFieldEdit.DefaultValue_2 = DBNull.Value;
}
pFieldEdit.IsNullable_2 = item.AllowNull;
pFieldEdit.Length_2 = item.Length;
pFieldEdit.Precision_2 = item.Precision;
pFieldsEdit.AddField(pField);
}
}
catch (Exception ex)
{
LogAPI.Debug("创建图层字段异常:" + ex);
throw ex;
}
}
private IGeometry GetGeoByWJFW(Rectangle reg, ISpatialReference spr)
{
ArrayList result = new ArrayList();
try
{
result.Add((byte)1);//顺序
result.AddRange(BitConverter.GetBytes(3));//类型 面
result.AddRange(BitConverter.GetBytes(1));//环数
ArrayList jmdArray = new ArrayList();
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinX));
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinY));
//上边
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinX));
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxY));
//右边
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxX));
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxY));
//下边
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MaxX));
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinY));
//首尾相连
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinX));
jmdArray.AddRange(BitConverter.GetBytes((double)reg.MinY));
result.AddRange(BitConverter.GetBytes(jmdArray.Count / 16));
result.AddRange(jmdArray);
byte[] wkb = (byte[])result.ToArray(typeof(byte));
IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3;
IGeometry geom;
int countin = wkb.GetLength(0);
factory.CreateGeometryFromWkbVariant(wkb, out geom, out countin);
if ((geom as IArea).Area < 0)
{
IPolygon pPolygon = geom as IPolygon;
pPolygon.ReverseOrientation();
}
if (geom == null) return null;
//ISpatialReference spatialReference = (featureClassDLTB as IGeoDataset).SpatialReference;
//if (spatialReference is IProjectedCoordinateSystem)
//{
// IProjectedCoordinateSystem pcsSys = spatialReference as IProjectedCoordinateSystem;
// geom.SpatialReference = pcsSys;
//}
//geom.Project(spr);//投影到对应的坐标系上
return geom;
}
catch (Exception ex)
{
LogAPI.Debug("获取分幅后的图斑形状异常:" + ex);
return null;
}
}
public bool DirectoryCopy(string sourceDir, string targetDirPath)
{
try
{
if (!Directory.Exists(sourceDir)) return false;
string targetDir = targetDirPath + "\\" + System.IO.Path.GetFileName(sourceDir);
if (!Directory.Exists(targetDir)) Directory.CreateDirectory(targetDir);
// 文件及文件夹名称数组
string[] dirColl = Directory.GetDirectories(sourceDir);
string[] fileColl = Directory.GetFiles(sourceDir);
// 便利所有文件
if (fileColl.Length > 0)
{
string fileName;
foreach (string fileDir in fileColl)
{
fileName = System.IO.Path.GetFileName(fileDir);
File.Copy(sourceDir + "\\" + fileName, targetDir + "\\" + fileName, true);
}
}
// 遍历所有文件夹
if (dirColl.Length > 0)
{
string folderName;
foreach (string dir in dirColl)
{
folderName = System.IO.Path.GetFileName(dir);
// 递归调用
Directory.CreateDirectory(targetDir + "\\" + folderName);
DirectoryCopy(dir, targetDir + "\\" + folderName);
}
}
return true;
}
catch (Exception ex)
{
LogAPI.Debug("新建工程页面中,文件夹复制时失败,异常原因: " + ex + " ; ");
return false;
throw;
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void btnSelectLayer_Click(object sender, RoutedEventArgs e)
{
try
{
OpenDataDialog pDialog = new OpenDataDialog();
ISpatialDataObjectFilter pOFilter;
pOFilter = new FilterDatasetsAndLayers();
pDialog.AddFilter(pOFilter, true);
pDialog.Title = "选择导入的数据";
pDialog.AllowMultiSelect = false;
pDialog.RestoreLocation = true;
pDialog.StartLocation = pDialog.FinalLocation;
System.Windows.Forms.DialogResult dialogResult = pDialog.ShowDialog();
if (dialogResult == System.Windows.Forms.DialogResult.OK && pDialog.Selection.Count != 0)
{
foreach (ISpatialDataObject distObj in pDialog.Selection)
{
if (distObj.DatasetType == esriDatasetType.esriDTFeatureClass)
{
txtFcPath.Text = pDialog.FinalLocation;
IFeatureClass SourceFeatureClass = (distObj.DatasetName as IName).Open() as IFeatureClass;
m_featureClass = SourceFeatureClass;
if ((SourceFeatureClass as FeatureClass).Workspace.Type != esriWorkspaceType.esriRemoteDatabaseWorkspace)
{
IFeatureClassManage fcManage = SourceFeatureClass as IFeatureClassManage;
fcManage.UpdateExtent();
}
CurrentEnvelope = (SourceFeatureClass as IGeoDataset).Extent;
if (CurrentEnvelope != null)
{
tbXmin.Text = CurrentEnvelope.XMin.ToString();
tbXmax.Text = CurrentEnvelope.XMax.ToString();
tbYmin.Text = CurrentEnvelope.YMin.ToString();
tbYmax.Text = CurrentEnvelope.YMax.ToString();
}
m_featureLayer = new FeatureLayer() { Name = SourceFeatureClass.AliasName, FeatureClass = SourceFeatureClass };
}
else if (distObj.DatasetType == esriDatasetType.esriDTRasterDataset) //影像数据
{
txtFcPath.Text = pDialog.FinalLocation;
IRasterLayer templayer = new RasterLayerClass();
templayer.CreateFromFilePath(pDialog.FinalLocation);
templayer.Name = pDialog.Selection[0].Name;
m_featureClass = null;
CurrentEnvelope = templayer.AreaOfInterest;
if (CurrentEnvelope != null)
{
tbXmin.Text = CurrentEnvelope.XMin.ToString();
tbXmax.Text = CurrentEnvelope.XMax.ToString();
tbYmin.Text = CurrentEnvelope.YMin.ToString();
tbYmax.Text = CurrentEnvelope.YMax.ToString();
}
m_featureLayer = templayer;
}
}
}
}
catch (Exception ex)
{
txtFcPath.Text = "";
MessageHelper.Show("获取作业区范围失败!");
LogAPI.Debug("获取作业区范围时发生异常,异常信息如下:");
LogAPI.Debug(ex);
}
}
private void radioBtGraph_Checked(object sender, RoutedEventArgs e)
{
if (radioBtGraph.IsChecked == true)
{
txtFcPath.Text = null;
}
}
}
}