GeoidPanel.cs 6.22 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/**
 * \file NETGeographicLib\GeoidPanel.cs
 * \brief NETGeographicLib.Geoid example
 *
 * NETGeographicLib is copyright (c) Scott Heiman (2013)
 * GeographicLib is Copyright (c) Charles Karney (2010-2012)
 * <charles@karney.com> and licensed under the MIT/X11 License.
 * For more information, see
 * https://geographiclib.sourceforge.io/
 **********************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NETGeographicLib;

using System.IO;

namespace Projections
{
    public partial class GeoidPanel : UserControl
    {
        Geoid m_geoid = null;
        string m_path;
        string m_fileName;

        public GeoidPanel()
        {
            InitializeComponent();
            m_threadSafeCheckBox.Checked = true;
        }

        private void OnSelectFile(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Geoid File (*.pgm)|*.pgm";
            dlg.DefaultExt = "pgm";
            dlg.Title = "Open Geoid File";

            if (dlg.ShowDialog() == DialogResult.Cancel) return;

            m_path = dlg.FileName.Substring(0, dlg.FileName.LastIndexOf('\\')).Replace('\\', '/');
            int length = dlg.FileName.LastIndexOf('.') - dlg.FileName.LastIndexOf('\\') - 1;
            m_fileName = dlg.FileName.Substring(dlg.FileName.LastIndexOf('\\') + 1, length);

            try
            {
                m_geoid = new Geoid(m_fileName, m_path, true, m_threadSafeCheckBox.Checked);
            }
            catch (Exception xcpt)
            {
                MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            m_convertEllipsodButton.Enabled = m_convertGeoidButton.Enabled = m_heightButton.Enabled = m_validateButton.Enabled = true;
            m_geoidFileNameTextBox.Text = dlg.FileName;
            m_dateTimeTextBox.Text = m_geoid.DateTime;
            m_descriptionTextBox.Text = m_geoid.Description;
            m_majorRadiusTextBox.Text = m_geoid.EquatorialRadius.ToString();
            m_flatteningTtextBox.Text = m_geoid.Flattening.ToString();
        }

        private void OnThreadSafe(object sender, EventArgs e)
        {
            if (m_geoidFileNameTextBox.Text.Length > 0)
            {
                try
                {
                    m_geoid = new Geoid(m_fileName, m_path, true, m_threadSafeCheckBox.Checked);
                }
                catch (Exception xcpt)
                {
                    MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            m_cacheButton.Enabled = !m_threadSafeCheckBox.Checked;
            m_northTextBox.ReadOnly = m_southTextBox.ReadOnly = m_eastTextBox.ReadOnly = m_westTextBox.ReadOnly = m_threadSafeCheckBox.Checked;
        }

        private void OnCache(object sender, EventArgs e)
        {
            if (m_geoid == null) return;

            try
            {
                double south = Double.Parse(m_southTextBox.Text);
                double north = Double.Parse(m_northTextBox.Text);
                double west = Double.Parse(m_westTextBox.Text);
                double east = Double.Parse(m_eastTextBox.Text);
                m_geoid.CacheArea(south, west, north, east);
            }
            catch (Exception xcpt)
            {
                MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OnConvertEllipsod(object sender, EventArgs e)
        {
            try
            {
                double lat = Double.Parse(m_latitudeTextBox.Text);
                double lon = Double.Parse(m_longitudeTextBox.Text);
                double h = Double.Parse(m_ellipsoidTextBox.Text);
                m_geoidTextBox.Text = m_geoid.ConvertHeight(lat, lon, h, Geoid.ConvertFlag.ELLIPSOIDTOGEOID).ToString();
            }
            catch (Exception xcpt)
            {
                MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OnConvertGeoid(object sender, EventArgs e)
        {
            try
            {
                double lat = Double.Parse(m_latitudeTextBox.Text);
                double lon = Double.Parse(m_longitudeTextBox.Text);
                double h = Double.Parse(m_geoidTextBox.Text);
                m_ellipsoidTextBox.Text = m_geoid.ConvertHeight(lat, lon, h, Geoid.ConvertFlag.GEOIDTOELLIPSOID).ToString();
            }
            catch (Exception xcpt)
            {
                MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OnHeight(object sender, EventArgs e)
        {
            try
            {
                double lat = Double.Parse(m_latitudeTextBox.Text);
                double lon = Double.Parse(m_longitudeTextBox.Text);
                m_ellipsoidTextBox.Text = m_geoid.Height(lat, lon).ToString();
                m_geoidTextBox.Text = "0";
            }
            catch (Exception xcpt)
            {
                MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OnValidate(object sender, EventArgs e)
        {
            try
            {
                Geoid g = new Geoid(m_fileName, m_path, false, false);
                g.CacheArea(20.0, -30.0, 30.0, -20.0);
                g.CacheAll();
                double h2 = g.Height(32.0, -60.0);
                g.ConvertHeight(32.0, -60.0, 100.0, Geoid.ConvertFlag.ELLIPSOIDTOGEOID);
                MessageBox.Show("No errors detected", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception xcpt)
            {
                MessageBox.Show(xcpt.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}