/
GeoCoordinateForm.cs
265 lines (252 loc) · 8.6 KB
/
GeoCoordinateForm.cs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using De.AHoerstemeier.Geo;
namespace De.AHoerstemeier.Tambon
{
public partial class GeoCoordinateForm : Form
{
private Boolean _Changing = false;
private GeoPoint _Point = null;
public GeoCoordinateForm()
{
InitializeComponent();
}
private String ZoneForThailandMgrs(String value)
{
Int32 zone = 0;
Char eastingChar = value[0];
if ( (eastingChar >= 'A') && (eastingChar <= 'H') )
{
zone = 49;
}
else if ( (eastingChar >= 'J') && (eastingChar <= 'R') )
{
zone = 47;
}
else if ( (eastingChar >= 'S') && (eastingChar <= 'Z') )
{
zone = 48;
}
Char northingChar = value[1];
String northingCharacters = UtmPoint.MgrsNorthingChars(zone);
Int32 northingCount = northingCharacters.IndexOf(northingChar);
Char zoneChar;
if ( (northingCount > 17) | (northingCount == 0) )
{
zoneChar = 'Q';
}
else if ( northingCount > 8 )
{
zoneChar = 'P';
}
else
{
zoneChar = 'N';
}
String result = zone.ToString() + zoneChar;
return result;
}
private void GeoCoordinateForm_Load(object sender, EventArgs e)
{
cbx_datum.Items.Add(GeoDatum.DatumWGS84());
cbx_datum.SelectedIndex = 0;
cbx_datum.Items.Add(GeoDatum.DatumIndian1975());
cbx_datum.Items.Add(GeoDatum.DatumIndian1954());
}
private void SetValues(GeoPoint geoPoint, UtmPoint utmPoint, object sender)
{
if ( sender != edt_LatLong )
{
if ( geoPoint == null )
{
edt_LatLong.Text = String.Empty;
}
else
{
edt_LatLong.Text = geoPoint.ToString();
}
}
if ( sender != edt_geohash )
{
if ( geoPoint == null )
{
edt_geohash.Text = String.Empty;
}
else
{
edt_geohash.Text = geoPoint.GeoHash;
}
}
if ( sender != edt_UTM )
{
if ( utmPoint == null )
{
edt_UTM.Text = String.Empty;
}
else
{
edt_UTM.Text = utmPoint.ToString();
}
}
if ( sender != edt_MGRS )
{
if ( utmPoint == null )
{
edt_MGRS.Text = String.Empty;
}
else
{
edt_MGRS.Text = utmPoint.ToMgrsString(6);
}
}
_Point = geoPoint;
btnFlyTo.Enabled = (_Point != null);
lbl_L7018Value.Text = "Not available";
if ( geoPoint != null )
{
try
{
var sheet = RtsdMapIndex.IndexL7018(geoPoint);
if ( sheet != null )
{
lbl_L7018Value.Text = sheet.Name;
}
}
catch ( ArgumentOutOfRangeException )
{
}
}
}
private void edit_MGRS_TextChanged(object sender, EventArgs e)
{
if ( !_Changing )
{
String value = TambonHelper.ReplaceThaiNumerals(edt_MGRS.Text.ToUpper()).Trim();
GeoPoint geoPoint = null;
UtmPoint utmPoint = null;
try
{
_Changing = true;
if ( !TambonHelper.IsNumeric(value.Substring(0, 2)) )
{
value = ZoneForThailandMgrs(value) + value;
}
utmPoint = UtmPoint.ParseMgrsString(value);
geoPoint = new GeoPoint(utmPoint, (GeoDatum)cbx_datum.SelectedItem);
geoPoint.Datum = GeoDatum.DatumWGS84();
}
catch
{
// invalid string
utmPoint = null;
geoPoint = null;
}
SetValues(geoPoint, utmPoint, sender);
_Changing = false;
}
}
private void edit_UTM_TextChanged(object sender, EventArgs e)
{
if ( !_Changing )
{
String value = TambonHelper.ReplaceThaiNumerals(edt_UTM.Text.ToUpper()).Replace(",", "").Trim();
GeoPoint geoPoint = null;
UtmPoint utmPoint = null;
try
{
_Changing = true;
utmPoint = UtmPoint.ParseUtmString(value);
geoPoint = new GeoPoint(utmPoint, (GeoDatum)cbx_datum.SelectedItem);
geoPoint.Datum = GeoDatum.DatumWGS84();
}
catch
{
// invalid string
utmPoint = null;
geoPoint = null;
}
SetValues(geoPoint, utmPoint, sender);
_Changing = false;
}
}
private void edt_geohash_TextChanged(object sender, EventArgs e)
{
if ( !_Changing )
{
GeoPoint geoPoint = null;
UtmPoint utmPoint = null;
try
{
_Changing = true;
geoPoint = new GeoPoint();
geoPoint.GeoHash = edt_geohash.Text;
GeoPoint lGeoOtherDatum = new GeoPoint(geoPoint);
lGeoOtherDatum.Datum = (GeoDatum)cbx_datum.SelectedItem;
utmPoint = lGeoOtherDatum.CalcUTM();
}
catch
{
// invalid string
utmPoint = null;
geoPoint = null;
}
SetValues(geoPoint, utmPoint, sender);
_Changing = false;
}
}
private void edt_LatLong_TextChanged(object sender, EventArgs e)
{
if ( !_Changing )
{
GeoPoint geoPoint = null;
UtmPoint utmPoint = null;
try
{
_Changing = true;
geoPoint = new GeoPoint(edt_LatLong.Text);
geoPoint.Datum = (GeoDatum)cbx_datum.SelectedItem;
GeoPoint lGeoOtherDatum = new GeoPoint(geoPoint);
lGeoOtherDatum.Datum = (GeoDatum)cbx_datum.SelectedItem;
utmPoint = lGeoOtherDatum.CalcUTM();
}
catch
{
// invalid string
geoPoint = null;
utmPoint = null;
}
SetValues(geoPoint, utmPoint, sender);
_Changing = false;
}
}
private void btnFlyTo_Click(object sender, EventArgs e)
{
//try
//{
// var googleEarth = new ApplicationGEClass();
// String tempKmlFile = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".kml";
// KmlHelper kmlWriter = new KmlHelper();
// kmlWriter.AddPoint(_Point.Latitude, _Point.Longitude, "Temporary location", "", "", "");
// kmlWriter.SaveToFile(tempKmlFile);
// while ( googleEarth.IsInitialized() == 0 )
// {
// Thread.Sleep(500);
// }
// googleEarth.OpenKmlFile(tempKmlFile, 0);
//}
//catch ( Exception ex )
//{
// MessageBox.Show(ex.ToString());
//}
}
}
}