Newer
Older
EMS_REFACTOR / FrmBaseLineProfillAnalysis.cs
nn-203 on 26 Jul 2017 15 KB first commit
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using GeoScene.Data;
using GeoScene.Globe;
using ZedGraph;

namespace Cyberpipe
{
    public partial class FrmBaseLineProfillAnalysis : Office2007Form
    {

        public GSOGeoPolyline3D m_geopolyline;
        public GSOGlobe m_globe;

        private GSOPoint3ds m_pnt3ds;
        private GSOPoint3d m_pntMax;
        private GSOPoint3d m_pntMin;
        private GSOPoint3d m_pntStart;
        private GSOPoint3d m_pntEnd;
        private double m_dXTotalLength;
        private double m_dSpaceLength;
        private double m_dSphereLength;
        private double m_dGroundLength;
        private double m_dBaseAlt;
        private Boolean m_bXYSameScale;
        private Boolean m_bSetMinX;
        private Boolean m_bSetMinY;
        public FrmBaseLineProfillAnalysis(GSOGlobe _globe,GSOGeoPolyline3D _geopolyline)
        {
            InitializeComponent();

            m_globe = _globe;
            m_geopolyline = _geopolyline;
        }
        /// <summary>
        /// 窗体初始化事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BaseLineProfillAnalysis_Load(object sender, EventArgs e)
        {
            zedGraphControl1.ContextMenuStrip.Enabled = false;
            GraphPane myPane = zedGraphControl1.GraphPane;

            myPane.Title.Text = "基线剖面分析";
            myPane.Title.FontSpec.Family = "黑体";
            myPane.Title.FontSpec.IsBold = false;
            myPane.Title.FontSpec.Size = 18.0f;
            
            myPane.XAxis.Title.Text = "长度";
            myPane.XAxis.Title.FontSpec.Family = "黑体";
            myPane.XAxis.Title.FontSpec.IsBold = false;
            myPane.XAxis.Title.FontSpec.Size = 18.0f;

            myPane.YAxis.Title.Text = "高程";
            myPane.YAxis.Title.FontSpec.Family = "黑体";
            myPane.YAxis.Title.FontSpec.IsBold = false;
            myPane.YAxis.Title.FontSpec.Size = 18.0f;

            myPane.Chart.Fill = new Fill(Color.White, Color.White, 45.0f);
        }

        /// <summary>
        /// 分析按钮事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAnalyse_Click(object sender, EventArgs e)
        {
            if(m_geopolyline!=null && m_globe!=null)
            {
                GSOPoint3d pntMax, pntMin, pntStart, pntEnd;
                GSOPoint3ds pnt3ds;
                double dLineLength;

                m_globe.Analysis3D.ProfileAnalyse(m_geopolyline, 100, out pnt3ds, out dLineLength, out pntMax, out pntMin, out pntStart, out pntEnd);
                
                m_pnt3ds = pnt3ds;
                m_pntMax = pntMax;
                m_pntMin = pntMin;
                m_pntStart = pntStart;
                m_pntEnd = pntEnd;
                m_dXTotalLength = dLineLength;
                m_dSphereLength = m_geopolyline.GetSphereLength(6378137.0);
                m_dSpaceLength = m_geopolyline.GetSpaceLength(false, 6378137.0);
                m_dGroundLength = m_globe.Analysis3D.GetGroundLength(m_geopolyline, false, 0);
                checkBoxYMin.Enabled = true;
                checkBoxXmin.Enabled = true;
                checkBoxSameScale.Enabled = true;
                SetLableText();
                DrawCurveGraph();
            }
        }
        /// <summary>
        /// 设置标签的显示的文字
        /// </summary>
        private void SetLableText()
        {
            labelStartLon.Text = "起点经度: " + m_pntStart.X.ToString("f6");
            labelStartLat.Text = "起点纬度: " + m_pntStart.Y.ToString("f6");
            labelEndLon.Text = "终点经度: " + m_pntEnd.X.ToString("f6");
            labelEndLat.Text = "终点纬度: " + m_pntEnd.Y.ToString("f6");
            labelStartAlt.Text = "起点高程: " + m_pntStart.Z.ToString("f2");
            labelEndAlt.Text = "终点高程: " + m_pntEnd.Z.ToString("f2");
            labelMaxAlt.Text = "最大高程: " + m_pntMax.Z.ToString("f2");
            labelMinAlt.Text = "最小高程: " + m_pntMin.Z.ToString("f2");
            labelSphereLenth.Text = "投影距离: " + m_dSphereLength.ToString("f2");
            labelGroundLenth.Text = "地表距离: " + m_dGroundLength.ToString("f2");
            labelSpaceLenth.Text = "直线距离: " + m_dSpaceLength.ToString("f2");
            labelPointNum.Text = "采样点数: " + m_pnt3ds.Count;
        }
        /// <summary>
        /// 重新绘制图形
        /// </summary>
        private void DrawCurveGraph()
        {

                GraphPane myPane = zedGraphControl1.GraphPane;
                myPane.CurveList.Clear();
                myPane.GraphObjList.Clear();
                zedGraphControl1.RestoreScale(zedGraphControl1.GraphPane);

                try {
                    m_dBaseAlt = Convert.ToDouble(textBoxBaseAlt.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + "请输入正确的基线高程信息!", "提示");
                    return;
                }

                myPane.Legend.IsVisible = false;
                PointPairList listHLAbove = new PointPairList();
                PointPairList listAbove = new PointPairList();
                PointPairList listHLBelow = new PointPairList();
                PointPairList listBelow = new PointPairList();
                PointPairList listEqual = new PointPairList();
                PointPairList listBase = new PointPairList();
                ArrayList arryHLAbove = new ArrayList();
                ArrayList arryHLBelow = new ArrayList();
                ArrayList arryAbove = new ArrayList();
                ArrayList arryBelow = new ArrayList();
                int nPointCount = m_pnt3ds.Count;
                double dOneSegLen = m_dXTotalLength / (nPointCount - 1);
                int nSegmentType = -1; //0=m_dBaseAlt,1=above,2=below

                for (int i = 0; i < m_pnt3ds.Count; i++)
                {
                    double x = i * dOneSegLen;
                    double y = (int)(Math.Round(m_pnt3ds[i].Z * 100)) / 100.0; // 精确到厘米就行了

                    if (y > m_dBaseAlt)
                    {
                        // 如果当前段是基准线下面的段
                        if (nSegmentType == 2)
                        {
                            arryHLBelow.Add(listHLBelow);
                            arryBelow.Add(listBelow);
                            listHLBelow = new PointPairList();
                            listBelow = new PointPairList();
                        }
                        nSegmentType = 1;
                        listAbove.Add(x, y);
                        listHLAbove.Add(x, y, m_dBaseAlt);
                    }
                    else if (y < m_dBaseAlt)
                    {
                        if (nSegmentType == 1)
                        {
                            arryHLAbove.Add(listHLAbove);
                            arryAbove.Add(listAbove);
                            listHLAbove = new PointPairList();
                            listAbove = new PointPairList();
                        }
                        nSegmentType = 2;
                        listBelow.Add(x, y);
                        listHLBelow.Add(x, m_dBaseAlt, y);
                    }
                    else
                    {
                        if (nSegmentType == 2)
                        {
                            arryHLBelow.Add(listHLBelow);
                            arryBelow.Add(listBelow);
                            listHLBelow = new PointPairList();
                            listBelow = new PointPairList();
                        }
                        else if (nSegmentType == 1)
                        {
                            arryHLAbove.Add(listHLAbove);
                            arryAbove.Add(listAbove);
                            listHLAbove = new PointPairList();
                            listAbove = new PointPairList();
                        }
                        nSegmentType = 0;
                        listEqual.Add(x, y, m_dBaseAlt);
                    }
                }

                if (nSegmentType == 2)
                {
                    arryHLBelow.Add(listHLBelow);
                    arryBelow.Add(listBelow);
                }
                else if (nSegmentType == 1)
                {
                    arryHLAbove.Add(listHLAbove);
                    arryAbove.Add(listAbove);
                }

                listBase.Add(0, m_dBaseAlt);
                listBase.Add(m_dXTotalLength, m_dBaseAlt);

                LineItem myCurveBase = myPane.AddCurve("基线剖面", listBase, Color.Blue, SymbolType.None);
                myCurveBase.Line.IsAntiAlias = true;
                myCurveBase.Line.Width = 2;

                int k = 0;
                for (k = 0; k < arryHLAbove.Count; k++)
                {
                    LineItem myCurveAbove = myPane.AddCurve("高于基线剖面", (PointPairList)arryAbove[k], Color.Red, SymbolType.None);
                    myCurveAbove.Line.IsAntiAlias = true;
                    myCurveAbove.Line.Width = 2;
                    myCurveAbove.Line.IsSmooth = true;


                    HiLowBarItem hlAboveItem = myPane.AddHiLowBar("高于基线", (PointPairList)arryHLAbove[k], Color.Red);
                    hlAboveItem.Bar.Border.Color = Color.Red;
                }
                for (k = 0; k < arryHLBelow.Count; k++)
                {
                    LineItem myCurveBelow = myPane.AddCurve("低于基线剖面", (PointPairList)arryBelow[k], Color.Green, SymbolType.None);
                    myCurveBelow.Line.IsAntiAlias = true;
                    myCurveBelow.Line.Width = 2;
                    myCurveBelow.Line.IsSmooth = true;

                    HiLowBarItem hlBolowItem = myPane.AddHiLowBar("低于基线", (PointPairList)arryHLBelow[k], Color.Green);
                    hlBolowItem.Bar.Border.Color = Color.Green;
                }

                // Show the x axis grid
                myPane.XAxis.MajorGrid.IsVisible = true;
                myPane.XAxis.IsAxisSegmentVisible = true;

                if (m_bSetMinX)
                {
                    myPane.XAxis.Scale.Min = 0;
                }

                if (m_bSetMinY)
                {
                    myPane.YAxis.Scale.Min = m_pntMin.Z;
                }

                myPane.YAxis.MajorGrid.IsVisible = true;
                // Don't display the Y zero line
                myPane.YAxis.MajorGrid.IsZeroLine = false;
                // Align the Y axis labels so they are flush to the axis
                myPane.YAxis.Scale.Align = AlignP.Inside;
                // Manually set the axis range
                //myPane.YAxis.Scale.Min = -1000;
                // myPane.YAxis.Scale.Max = 1000;

                // Fill the axis background with a gradient
                myPane.Chart.Fill = new Fill(Color.White, Color.LightGray, 45.0f);

                // Enable scrollbars if needed
                //zedGraphControl1.IsShowHScrollBar = true;
                //zedGraphControl1.IsShowVScrollBar = true;
                zedGraphControl1.IsAutoScrollRange = true;

                // OPTIONAL: Show tooltips when the mouse hovers over a point
                zedGraphControl1.IsShowPointValues = true;
                zedGraphControl1.PointValueEvent += MyPointValueHandler;

                // OPTIONAL: Handle the Zoom Event
                zedGraphControl1.ZoomEvent += MyZoomEvent;

                zedGraphControl1.AxisChange();

                if (m_bXYSameScale)
                {
                    graphPane_AxisChangeEvent();
                }
                // Make sure the Graph gets redrawn
                zedGraphControl1.Invalidate();

           
        }

        /// <summary>
        /// Display customized tooltips when the mouse hovers over a point
        /// </summary>
        private string MyPointValueHandler(ZedGraphControl control, GraphPane pane,
                        CurveItem curve, int iPt)
        {
            // Get the PointPair that is under the mouse
            PointPair pt = curve[iPt];

            return "(" + pt.X.ToString("f2") + "," + pt.Y.ToString("f2") + ")";
        }


        // Respond to a Zoom Event
        private void MyZoomEvent(ZedGraphControl control, ZoomState oldState,
                    ZoomState newState)
        {
            // Here we get notification everytime the user zooms
        }
        private void graphPane_AxisChangeEvent()
        {
            GraphPane graphPane = zedGraphControl1.GraphPane;
            // Correct the scale so that the two axes are 1:1 aspect ratio
            double scalex2 = (graphPane.XAxis.Scale.Max - graphPane.XAxis.Scale.Min) / graphPane.Rect.Width;
            double scaley2 = (graphPane.YAxis.Scale.Max - graphPane.YAxis.Scale.Min) / graphPane.Rect.Height;
            if (scalex2 > scaley2)
            {
                double diff = graphPane.YAxis.Scale.Max - graphPane.YAxis.Scale.Min;
                double new_diff = graphPane.Rect.Height * scalex2;

                graphPane.YAxis.Scale.Max = graphPane.YAxis.Scale.Min + new_diff;
            }
            else if (scaley2 > scalex2)
            {
                double diff = graphPane.XAxis.Scale.Max - graphPane.XAxis.Scale.Min;
                double new_diff = graphPane.Rect.Width * scaley2;
                // graphPane.XAxis.Scale.Min -= (new_diff - diff) / 2.0;
                //graphPane.XAxis.Scale.Max += (new_diff - diff) / 2.0;
                graphPane.XAxis.Scale.Max = graphPane.XAxis.Scale.Min + new_diff;
            }
            // Recompute the grid lines
            float scaleFactor = graphPane.CalcScaleFactor();
            Graphics g = zedGraphControl1.CreateGraphics();
            graphPane.XAxis.Scale.PickScale(graphPane, g, scaleFactor);
            graphPane.YAxis.Scale.PickScale(graphPane, g, scaleFactor);
        }
        /// <summary>
        /// 固定图形不变复选框选中状态改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxSameScale_CheckedChanged(object sender, EventArgs e)
        {
            //if (m_pnt3ds == null)
            //{
            //    MessageBox.Show("请先进行分析!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            m_bXYSameScale = checkBoxSameScale.Checked;
            DrawCurveGraph();
        }
        /// <summary>
        /// 固定最小X值复选框选中状态改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxXmin_CheckedChanged(object sender, EventArgs e)
        {
            //if (m_pnt3ds == null)
            //{
            //    MessageBox.Show("请先进行分析!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            m_bSetMinX = checkBoxXmin.Checked;
            DrawCurveGraph();
        }
        /// <summary>
        /// 固定最小Y值复选框选中状态改变事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxYMin_CheckedChanged(object sender, EventArgs e)
        {
            //if (m_pnt3ds == null)
            //{
            //    MessageBox.Show("请先进行分析!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            m_bSetMinY = checkBoxYMin.Checked;
            DrawCurveGraph();
        }

        private void FrmBaseLineProfillAnalysis_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_globe.MemoryLayer.RemoveAllFeature();
        }
    }
}