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

819 lines
29 KiB

6 months ago
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using System.IO;
using System.Data.SQLite;
using ESRI.ArcGIS.Carto;
using System.Xml.Serialization;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using KGIS.Framework.OpenData.Control;
using KGIS.Framework.OpenData.Filter;
using KGIS.Framework.OpenData.InterFace;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using Kingo.Plugin.MakeTaskPackage.Entity;
using Raster2KOTiles;
using OSGeo.OGR;
using OSGeo.GDAL;
namespace Kingo.Plugin.MakeTaskPackage.View
{
public delegate void CutReport(int cutCount, int allCount);
public delegate void CutDown(DateTime dt, double cutTime);
public delegate void SetCutFileName(string fileName);
public partial class UCRasterMultCut : UserControl
{
public UCRasterMultCut()
{
InitializeComponent();
this.compressListBox.SelectedIndex = 0;
}
private MultRaster2MBTiles pMergeRaster = null;
private double distance = 100;
private EnumSaveType saveType = EnumSaveType.Level;
public event CutReport ReportMess = null;
public event CutDown DownTime = null;
public event SetCutFileName SetFileName = null;
private void btnOk_Click(object sender, EventArgs e)
{
}
private void UnionCallBack()
{
this.Invoke(new MethodInvoker(() =>
{
pMergeRaster = new MultRaster2MBTiles();
List<string> files = new List<string>();
int nThread = 1;
try
{
foreach (var item in this.listBoxFileList.Items)
{
files.Add(item.ToString());
}
if (files.Count == 0)
throw new Exception("请选择文件");
System.Drawing.Imaging.ImageFormat imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
if (this.cboxTileFormat.Text.ToLower() == "png")
{
imgFormat = System.Drawing.Imaging.ImageFormat.Png;
}
pMergeRaster.ImgFormat = imgFormat;
pMergeRaster.Level = Convert.ToInt32(this.txtLevel.Text);
pMergeRaster.TiledSize = Convert.ToInt32(this.txtPicSize.Text); ;
pMergeRaster.MBTilesPath = this.txtSaveFilePath.Text;
pMergeRaster.TopLayer = toplayer;
pMergeRaster.SaveType = saveType;
switch (this.compressListBox.Text)
{
case "高":
pMergeRaster.CompressRate = 25;
break;
case "中":
pMergeRaster.CompressRate = 40;
break;
case "低":
pMergeRaster.CompressRate = 60;
break;
default:
pMergeRaster.CompressRate = 25;
break;
}
if (string.IsNullOrEmpty(this.txtSaveFilePath.Text))
throw new Exception("请设置保存位置");
nThread = Convert.ToInt32(this.txtThreadNo.Text);
nThread = nThread <= 0 ? 1 : nThread;
}
catch (Exception ex)
{
MessageHelper.Show( "参数设置异常"+ ex);
return;
}
DateTime dt1 = DateTime.Now;
pMergeRaster.CreatDown += (s, e1) =>
{
DateTime dt3 = DateTime.Now;
if (!this.IsDisposed && !this.Disposing)
{
this.Invoke(new MethodInvoker(() =>
{
if (DownTime != null)
DownTime(dt3, dt3.Subtract(dt1).TotalSeconds);
SetState(false, dt3.Subtract(dt1).TotalSeconds.ToString("0.0"));
GC.Collect();
}));
}
};
pMergeRaster.Report += (s, c) =>
{
if (!this.IsDisposed && !this.Disposing)
{
this.Invoke(new MethodInvoker(() =>
{
this.lbTips.Text = string.Format("{0}/{1}", c, s);
if (ReportMess != null)
{
//进度条控制
ReportMess(c, s);
}
}));
}
};
try
{
pMergeRaster.CreateMulFilesKOTiles(files, nThread);
SetState(true);
}
catch (Exception ex)
{
if (DownTime != null)
DownTime(DateTime.Now, 0);
MessageBox.Show(ex.Message, "提示");
}
}));
}
private void Test()
{ }
private void SetState(bool isRunning, string msg = null)
{
this.groupBox1.Enabled = !isRunning;
this.groupBox2.Enabled = !isRunning;
this.btnOk.Enabled = !isRunning;
this.progressBar1.Visible = isRunning;
this.groupBox3.Enabled = !isRunning;
if (isRunning)
{
this.lbTips.Text = "处理中……";
}
else
{
if (msg == null)
{
this.lbTips.Text = "";
}
else
{
this.lbTips.Text = "耗时" + msg + "秒";
}
}
}
private void txtSaveFilePath_Click(object sender, EventArgs e)
{
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.listBoxFileList.Items.AddRange(this.openFileDialog1.FileNames);
if (SetFileName != null)
{
string fileName = System.IO.Path.GetFileNameWithoutExtension(this.openFileDialog1.FileNames[0]);
if (fileName.Length > 6)
SetFileName(fileName.Substring(0, 6));
}
GetBestLevelSize();
}
}
catch (Exception ex)
{
MessageHelper.Show("增加影像文件报错:" + ex);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (this.listBoxFileList.SelectedItem != null)
{
int index = this.listBoxFileList.SelectedIndex;
this.listBoxFileList.Items.Remove(this.listBoxFileList.SelectedItem);
if (this.listBoxFileList.Items.Count > index)
{
this.listBoxFileList.SelectedIndex = index;
}
else if (index != 0)
{
this.listBoxFileList.SelectedIndex = index - 1;
}
GetBestLevelSize();
}
}
catch (Exception ex)
{
MessageHelper.Show("删除影像文件报错:" + ex);
}
}
public void SetParams(int level, int threadNum, int tileSize, string tileType)
{
this.txtLevel.Text = level.ToString();
this.txtPicSize.Text = tileSize.ToString();
this.txtThreadNo.Text = threadNum.ToString();
this.cboxTileFormat.SelectedIndex = tileType == "png" ? 0 : 1;
}
private void GetBestLevelSize()
{
List<string> files = new List<string>();
foreach (var item in this.listBoxFileList.Items)
{
if (System.IO.Path.GetExtension(item.ToString()) != ".kotiles")
{
files.Add(item.ToString());
}
}
if (files.Count == 0)
return;
//// 注册所有的驱动
//Ogr.RegisterAll();
//Gdal.AllRegister();
int level, tileSize;
double resolution = MultRaster2MBTiles.GetBestLevelAndSize(files, out level, out tileSize);
if (level != 0 && tileSize != 0)
{
this.txtLevel.Text = level.ToString();
this.txtPicSize.Text = tileSize.ToString();
this.txtResolution.Text = resolution.ToString("0.00");
}
}
private void UCRasterMultCut_Load(object sender, EventArgs e)
{
this.cboxTileFormat.SelectedIndex = 0;
}
private void btnBrowser_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtSaveFilePath.Text))
{
string filename =System.IO.Path.GetFileName(this.txtSaveFilePath.Text);
if (!string.IsNullOrEmpty(filename))
this.saveFileDialog1.FileName = filename;
}
if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.txtSaveFilePath.Text = this.saveFileDialog1.FileName;
}
}
private void btnUp_Click(object sender, EventArgs e)
{
if (this.listBoxFileList.SelectedItem != null && this.listBoxFileList.SelectedIndex != 0)
{
object obj = this.listBoxFileList.SelectedItem;
int index = this.listBoxFileList.SelectedIndex;
this.listBoxFileList.Items.Remove(obj);
this.listBoxFileList.Items.Insert(index - 1, obj);
this.listBoxFileList.SelectedItem = obj;
}
}
private void btnDown_Click(object sender, EventArgs e)
{
if (this.listBoxFileList.SelectedItem != null && this.listBoxFileList.SelectedIndex != this.listBoxFileList.Items.Count - 1)
{
object obj = this.listBoxFileList.SelectedItem;
int index = this.listBoxFileList.SelectedIndex;
this.listBoxFileList.Items.Remove(obj);
this.listBoxFileList.Items.Insert(index + 1, obj);
this.listBoxFileList.SelectedItem = obj;
}
}
private void btnGetResolution_Click(object sender, EventArgs e)
{
List<string> files = new List<string>();
foreach (var item in this.listBoxFileList.Items)
{
files.Add(item.ToString());
}
if (files.Count == 0)
return;
try
{
double resolution = MultRaster2MBTiles.GetResolution(files, Convert.ToInt32(this.txtLevel.Text), Convert.ToInt32(this.txtPicSize.Text));
this.txtResolution.Text = resolution.ToString("0.00");
}
catch (Exception ex)
{
throw ex;
//MessageBox.Show(ex.Message);
}
}
public void SetRasterFiles(List<string> files)
{
this.listBoxFileList.Items.Clear();
this.listBoxFileList.Items.AddRange(files.ToArray());
GetBestLevelSize();
}
public void SetSaveFilePath(string filepath)
{
if (!string.IsNullOrEmpty(filepath) && filepath.EndsWith(".kotiles"))
{
this.txtSaveFilePath.Text = filepath;
}
}
public void SetLayerInfos(List<LayerInfo> layerInfos)
{
this.listBoxVector.Items.Clear();
this.listBoxFileList.Items.AddRange(layerInfos.ToArray());
this.selectMdbLayer.Clear();
this.selectMdbLayer.AddRange(layerInfos);
}
public void OnFormClosing(object sender, FormClosingEventArgs e)
{
if (this.progressBar1.Visible)
{
if (MessageBox.Show("正在切片中,确定退出?", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true;
}
else
{
if (pMergeRaster != null)
{
pMergeRaster.StopCreateMulFilesKOTiles();
pMergeRaster = null;
}
}
}
}
public bool IsProcessing
{
get { return this.progressBar1.Visible; }
}
private List<LayerInfo> selectMdbLayer = null;
private void btnAddVector_Click(object sender, EventArgs e)
{
if (this.openFileDialog2.ShowDialog() == DialogResult.OK)
{
string[] files = this.openFileDialog2.FileNames;
List<string> mdbPaths = new List<string>();
for (int i = 0; i < files.Length; i++)
{
if (System.IO.Path.GetExtension(files[i]).ToLower() == ".mdb")
{
mdbPaths.Add(files[i]);
}
else
{
this.listBoxVector.Items.Add(files[i]);
}
}
if (mdbPaths.Count > 0)
{
//FrmSelectLayer frmSelectLayer = new FrmSelectLayer();
//frmSelectLayer.VectorPaths = mdbPaths;
//frmSelectLayer.Owner = this.Parent as Form;
//selectMdbLayer = new List<LayerInfo>();
//if (frmSelectLayer.ShowDialog() == DialogResult.OK)
//{
// selectMdbLayer = frmSelectLayer.SelectVectorNames;
// foreach (LayerInfo item in frmSelectLayer.SelectVectorNames)
// {
// this.listBoxVector.Items.Add(item.LayerSourcePath + "/" + item.LayerName);
// }
//}
}
}
}
private void btnDelVector_Click(object sender, EventArgs e)
{
if (this.listBoxVector.SelectedItem != null)
{
int index = this.listBoxVector.SelectedIndex;
this.listBoxVector.Items.Remove(this.listBoxVector.SelectedItem);
if (this.listBoxVector.Items.Count > index)
{
this.listBoxVector.SelectedIndex = index;
}
else if (index != 0)
{
this.listBoxVector.SelectedIndex = index - 1;
}
}
}
private OSGeo.OGR.Layer toplayer = null;
private void UnionLayers(object obj)
{
toplayer = UnionVector.UnionVectorLayers(obj as List<LayerInfo>, distance);
UnionCallBack();
}
private TaskDescribeEntity taskParamsEntity = null;
private void CreateLayer(object obj)
{
List<OSGeo.OGR.Geometry> geo= obj as List<OSGeo.OGR.Geometry>;
toplayer = UnionVector.CreateLayer(geo, distance);
OSGeo.OGR.Geometry rstGeometry = null;
foreach (OSGeo.OGR.Geometry item in geo)
{
rstGeometry = UnionVector.UnionGeometry(rstGeometry, item);
}
OSGeo.OGR.Envelope envelope=new OSGeo.OGR.Envelope();
rstGeometry.GetEnvelope(envelope);
CreateKotiles(taskParamsEntity, new Raster2KOTiles.Envelope()
{
XMax = envelope.MaxX,
XMin = envelope.MinX,
YMax = envelope.MaxY,
YMin = envelope.MinY
});
}
private void CreateKotiles(TaskDescribeEntity paramsEntity, Raster2KOTiles.Envelope envelopeCut =null)
{
pMergeRaster = new MultRaster2MBTiles();
List<string> files = new List<string>();
int nThread = 1;
try
{
foreach (var item in paramsEntity.RasterPaths)
{
files.Add(item);
}
if (files.Count == 0)
throw new Exception("请选择文件");
int level, tileSize;
double resolution = MultRaster2MBTiles.GetBestLevelAndSize(files, out level, out tileSize);
System.Drawing.Imaging.ImageFormat imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
if (paramsEntity.TileType== "png")
{
imgFormat = System.Drawing.Imaging.ImageFormat.Png;
}
pMergeRaster.ImgFormat = imgFormat;
pMergeRaster.Level = paramsEntity.Level > level ? level : paramsEntity.Level;
pMergeRaster.TiledSize = paramsEntity.TileSize;
pMergeRaster.MBTilesPath = paramsEntity.TaskPath;
pMergeRaster.TopLayer = toplayer;
pMergeRaster.SaveType = EnumSaveType.Level;
if (string.IsNullOrEmpty(paramsEntity.TaskPath))
throw new Exception("请设置保存位置");
nThread = paramsEntity.ThreadNum;
nThread = nThread <= 0 ? 1 : nThread;
}
catch (Exception ex)
{
throw ex;
}
DateTime dt1 = DateTime.Now;
pMergeRaster.CreatDown += (s, e1) =>
{
DateTime dt3 = DateTime.Now;
if (DownTime != null)
DownTime(dt3, dt3.Subtract(dt1).TotalSeconds);
GC.Collect();
};
pMergeRaster.Report += (s, c) =>
{
if (ReportMess != null)
{
ReportMess(c, s);
}
};
try
{
pMergeRaster.CreateMulFilesKOTiles(files, nThread, envelopeCut);
}
catch (Exception ex)
{
if (DownTime != null)
DownTime(DateTime.Now, 0);
throw ex;
}
}
#region 裁取图斑最高分辨率
public void btnCutOk_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtSaveFilePath.Text))
{
MessageHelper.Show("请选择输出路径!");
return;
}
saveType = EnumSaveType.Level;
SetState(true);
List<LayerInfo> lstVectorPaths = new List<LayerInfo>();
foreach (var item in this.listBoxVector.Items)
{
if (item.ToString().Contains("/"))
{
LayerInfo layerInfoMdb = selectMdbLayer.Find(p => p.LayerSourcePath + "/" + p.LayerName == item.ToString());
if (layerInfoMdb != null)
lstVectorPaths.Add(new LayerInfo() { LayerSourcePath = layerInfoMdb.LayerSourcePath, LayerIndex = 0, LayerName = layerInfoMdb.LayerName });
}
else
{
lstVectorPaths.Add(new LayerInfo() { LayerSourcePath = item.ToString(), LayerIndex = 0, LayerName = null });
}
}
if (lstVectorPaths == null || lstVectorPaths.Count <= 0)
{
UnionCallBack();
}
else
{
double.TryParse(this.txtBuffer.Text, out distance);
distance = distance == 0 ? 100 : distance;
Thread threadUnion = new Thread(new ParameterizedThreadStart(UnionLayers));
threadUnion.Start(lstVectorPaths);
}
}
public void CutRasterToKotiles(List<OSGeo.OGR.Geometry> geometrys,TaskDescribeEntity paramsEntity)
{
try
{
this.taskParamsEntity = paramsEntity;
saveType = EnumSaveType.Level;
if (geometrys == null || geometrys.Count <= 0)
{
CreateKotiles(taskParamsEntity);
}
else
{
distance = paramsEntity.Distance == 0 ? 100 : distance;
Thread threadUnion = new Thread(new ParameterizedThreadStart(CreateLayer));
threadUnion.Start(geometrys);
}
}
catch (Exception ex)
{
throw ex;
}
}
private void UnionLayersMul(object obj)
{
TaskDescribeEntity taskEntity = obj as TaskDescribeEntity;
toplayer = UnionVector.UnionVectorLayers(taskEntity.LayerInfos, distance);
UnionCallBackMul(taskEntity);
}
public void CutRasterMul(TaskDescribeEntity taskEnity)
{
saveType = EnumSaveType.Level;
if (taskEnity.LayerInfos == null || taskEnity.LayerInfos.Count <= 0)
{
UnionCallBackMul(taskEnity);
}
else
{
distance = taskEnity.Distance;
Thread threadUnion = new Thread(new ParameterizedThreadStart(UnionLayersMul));
threadUnion.Start(taskEnity);
}
}
private void UnionCallBackMul(TaskDescribeEntity taskEnity)
{
pMergeRaster = new MultRaster2MBTiles();
int nThread = 1;
try
{
if (taskEnity.RasterPaths.Count == 0)
throw new Exception("请选择文件");
System.Drawing.Imaging.ImageFormat imgFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
if (taskEnity.TileType == "0")
{
imgFormat = System.Drawing.Imaging.ImageFormat.Png;
}
pMergeRaster.ImgFormat = imgFormat;
pMergeRaster.Level = taskEnity.Level;
pMergeRaster.TiledSize = taskEnity.TileSize;
pMergeRaster.MBTilesPath = taskEnity.TaskPath;
pMergeRaster.TopLayer = toplayer;
pMergeRaster.SaveType = saveType;
if (string.IsNullOrEmpty(taskEnity.TaskPath))
throw new Exception("请设置保存位置");
nThread = taskEnity.ThreadNum;
nThread = nThread <= 0 ? 1 : nThread;
}
catch (Exception ex)
{
throw new Exception("参数设置异常");
}
DateTime dt1 = DateTime.Now;
pMergeRaster.CreatDown += (s, e1) =>
{
DateTime dt3 = DateTime.Now;
//this.Text = dt3.Subtract(dt1).TotalSeconds.ToString();
if (DownTime != null)
DownTime(dt3, dt3.Subtract(dt1).TotalSeconds);
GC.Collect();
};
pMergeRaster.Report += (s, c) =>
{
if (ReportMess != null)
{
ReportMess(c, s);
}
};
try
{
pMergeRaster.CreateMulFilesKOTiles(taskEnity.RasterPaths, nThread);
//SetState(true);
}
catch (Exception ex)
{
if (DownTime != null)
DownTime(DateTime.Now, 0);
throw new Exception(ex.Message);
}
}
#endregion
#region 批量切片相关信息
public void VisbleControls()
{
this.label4.Visible = false;
this.txtSaveFilePath.Visible = false;
this.btnBrowser.Visible = false;
this.panel2.Visible = false;
}
/// <summary>
/// 获取栅格影像
/// </summary>
private List<string> GetRasters
{
get
{
if (this.listBoxFileList.Items == null || this.listBoxFileList.Items.Count <= 0) return null;
List<string> rasterLst = new List<string>();
for (int i = 0; i < this.listBoxFileList.Items.Count; i++)
{
rasterLst.Add(this.listBoxFileList.Items[i].ToString());
}
return rasterLst;
}
}
/// <summary>
/// 获取矢量
/// </summary>
private List<LayerInfo> GetVector
{
get
{
List<LayerInfo> lstVectorPaths = new List<LayerInfo>();
foreach (var item in this.listBoxVector.Items)
{
if (item.ToString().Contains("/"))
{
LayerInfo layerInfoMdb = selectMdbLayer.Find(p => p.LayerSourcePath + "/" + p.LayerName == item.ToString());
if (layerInfoMdb != null)
lstVectorPaths.Add(new LayerInfo() { LayerSourcePath = layerInfoMdb.LayerSourcePath, LayerIndex = 0, LayerName = layerInfoMdb.LayerName });
}
else
{
lstVectorPaths.Add(new LayerInfo() { LayerSourcePath = item.ToString(), LayerIndex = 0, LayerName = null });
}
}
return lstVectorPaths;
}
}
/// <summary>
/// 获取切片参数
/// </summary>
public TaskDescribeEntity TaskParams
{
get
{
try
{
TaskDescribeEntity taskParams = new TaskDescribeEntity();
double dis = 0;
double.TryParse(this.txtBuffer.Text, out dis);
taskParams.Distance = dis;
taskParams.LayerInfos = this.GetVector;
taskParams.Level = Convert.ToInt32(this.txtLevel.Text);
taskParams.RasterPaths = GetRasters;
taskParams.ThreadNum = Convert.ToInt32(this.txtThreadNo.Text);
taskParams.TileSize = Convert.ToInt32(this.txtPicSize.Text);
taskParams.TileType = this.cboxTileFormat.SelectedIndex.ToString();
return taskParams;
}
catch
{
return null;
}
}
}
public void ClearTaskParams()
{
this.listBoxFileList.Items.Clear();
this.listBoxVector.Items.Clear();
//this.selectMdbLayer.Clear();
}
public void SetParams(TaskDescribeEntity taskEntity)
{
this.listBoxFileList.Items.Clear();
this.listBoxVector.Items.Clear();
if (this.selectMdbLayer != null)
this.selectMdbLayer.Clear();
else
this.selectMdbLayer = new List<LayerInfo>();
if (taskEntity.LayerInfos != null && taskEntity.LayerInfos.Count > 0)
{
this.listBoxFileList.Items.AddRange(taskEntity.LayerInfos.ToArray());
this.selectMdbLayer.AddRange(taskEntity.LayerInfos);
}
if (taskEntity.RasterPaths != null && taskEntity.RasterPaths.Count > 0)
{
this.listBoxFileList.Items.AddRange(taskEntity.RasterPaths.ToArray());
GetBestLevelSize();
}
if (!string.IsNullOrEmpty(taskEntity.TaskPath) && taskEntity.TaskPath.EndsWith(".kotiles"))
{
this.txtSaveFilePath.Text = taskEntity.TaskPath;
}
this.txtLevel.Text = taskEntity.Level.ToString();
this.txtPicSize.Text = taskEntity.TileSize.ToString();
this.txtThreadNo.Text = taskEntity.ThreadNum.ToString();
this.cboxTileFormat.SelectedIndex = taskEntity.TileType == "0" ? 0 : 1;
}
#endregion
public void Dispose()
{
if (pMergeRaster != null)
{
pMergeRaster.Dispose();
}
if (this.toplayer != null)
{
toplayer.Dispose();
toplayer = null;
}
}
}
}