/
DialogKMLPath.cs
268 lines (224 loc) · 9.85 KB
/
DialogKMLPath.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
266
267
268
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using SharpKml.Engine;
using SharpKml.Dom;
using Waypoint_Path_Generator.Models;
namespace Waypoint_Path_Generator
{
public partial class DialogKMLPath : Form
{
double _lat;
double _lon;
private Waypoint_Path_Gen _wpg;
private GMAP _gmap;
private WayPoints _wp;
private Models.Path _path;
private TreeView _treeview;
int _current_path_index;
double[,] _kml_points = new double[1000, 4];
int _kml_point_count = 0;
string _kml_filename = null;
public DialogKMLPath(Waypoint_Path_Gen wpg, GMAP gmap,TreeView treeview, double lat, double lon)
{
_lat = lat;
_lon = lon;
_wpg = wpg;
_gmap = gmap;
_treeview = treeview;
_current_path_index = -1;
_path = new Models.Path();
_wp = new WayPoints();
InitializeComponent();
// Fill POI combobox
cmbKLMPOI.Items.Clear();
for (int i = 0; i < _wpg.POICount(); i++)
{
cmbKLMPOI.Items.Add(_wpg.POIPointAt(i).name);
}
// Generate EMpty Path
_path.Add_Empty_Path(_wpg, _gmap, "Empty Path", "KML");
_current_path_index = _wpg.PathCount() - 1;
}
private void chKLMPOI_CheckedChanged(object sender, EventArgs e)
{
BuildKMLPath();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void btnSelectKMLFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
/* Get kml filename */
string kml_file = openFileDialog1.FileName;
if (kml_file.Contains(".kml"))
{
Form1.Globals.kml_filename = kml_file;
rtbKMLRead.AppendText("KML File : " + kml_file + "\n");
rtbKMLRead.AppendText("\n");
/* Open KML File */
System.IO.TextReader stream = System.IO.File.OpenText(kml_file);
SharpKml.Engine.KmlFile file = KmlFile.Load(stream);
Kml _kml = file.Root as Kml;
SharpKml.Dom.Placemark[] tempPlaceMarks = new SharpKml.Dom.Placemark[1000];
SharpKml.Dom.Placemark tmp_placemark = new SharpKml.Dom.Placemark();
CoordinateCollection coordinates = new CoordinateCollection();
Form1.Globals.kml_point_count = 0;
//rtbKMLRead.AppendText("Waypoint Count : " + Convert.ToString(Globals.waypoint_count) + "\n");
int numOfPlaceMarks = 0;
if (_kml != null)
{
foreach (var placemark in _kml.Flatten().OfType<SharpKml.Dom.Placemark>())
{
string placename = placemark.Name;
rtbKMLRead.AppendText("Placemark Name : " + placename + "\n");
tmp_placemark.Name = placename;
numOfPlaceMarks++;
}
rtbKMLRead.AppendText("Number of Placemarks : " + Convert.ToString(numOfPlaceMarks) + "\n");
int num_linestring = 0;
SharpKml.Base.Vector vector;
double lat;
double lon;
double alt;
foreach (var linestring in _kml.Flatten().OfType<LineString>())
{
coordinates = linestring.Coordinates;
int num = coordinates.Count;
rtbKMLRead.AppendText("Num Coordinates : " + Convert.ToString(num) + "\n");
for (int i = 0; i < num; i++)
{
vector = coordinates.ElementAt(i);
lat = vector.Latitude;
lon = vector.Longitude;
alt = (double)vector.Altitude;
rtbKMLRead.AppendText("Lat/Lon : " + Convert.ToString(lat));
rtbKMLRead.AppendText(", " + Convert.ToString(lon) + "Altitude : " + Convert.ToString(alt) + "\n");
_kml_points[_kml_point_count, 0] = lat;
_kml_points[_kml_point_count, 1] = lon;
_kml_points[_kml_point_count, 2] = alt;
_kml_point_count++;
//dgvWaypoints.Rows.Add(Globals.waypoint_count, Convert.ToString(lat), Convert.ToString(lon), Convert.ToString(30));
}
num_linestring++;
}
rtbKMLRead.AppendText("Number of Linestrings : " + Convert.ToString(num_linestring) + "\n");
BuildKMLPath();
}
}
else { MessageBox.Show("Wrong file type!", "GPS Grid:", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); }
}
}
private void BuildKMLPath()
{
// Create Path
if (_current_path_index != -1)
{
_wpg.DeletePath(_wpg.PathAt(_current_path_index));
}
string path_name = txtKMLName.Text;
if (path_name == "") path_name = _kml_filename;
LinkedList<WayPoints> wp_list = new LinkedList<WayPoints>();
double lat, lon, alt;
int gimblemode = 0;
double gimblepitch = 0;
double curvesize = 0;
double rotdir = 0;
int[,] actions = new int[,] { { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 }, { -1, 0 } };
LinkedList<WayPoints> new_list = new LinkedList<WayPoints>();
/* Generate Waypoints for each coordinate */
for (int i = 0; i < _kml_point_count; i++)
{
lat = _kml_points[i, 0];
lon = _kml_points[i, 1];
alt = _kml_points[i, 2];
double heading = 0.0;
double poi_lat = 0;
double poi_lon = 0;
/* Calculate Heading for each Waypoint */
if (chKLMPOI.Checked)
{
int poi_index = cmbKLMPOI.SelectedIndex;
if (poi_index == -1)
{
poi_lat = _lat;
poi_lon = _lon;
}
else {
POIPoints tmp_point = _wpg.POIPointAt(poi_index);
poi_lat = tmp_point.lat;
poi_lon = tmp_point.lon;
}
heading = GPS.GPS_Bearing(lat, lon, poi_lat, poi_lon);
gimblemode = 2;
double distance = GPS.GPS_Distance(lat, lon, poi_lat, poi_lon, Form1.Globals.gps_radius);
gimblepitch = -GPS.RadiansToDegrees(Math.Atan(alt / distance));
gimblemode = 2;
}
else {
if (i != _kml_point_count - 1)
{
double lat_next = _kml_points[i + 1, 0];
double lon_next = _kml_points[i + 1, 1];
heading = GPS.GPS_Bearing(lat, lon, lat_next, lon_next);
}
}
_wp.Add_Waypoint_List(new_list, lat, lon, alt, heading, curvesize, rotdir, gimblemode, gimblepitch, actions);
//dgvWaypoints.Rows.Add(Globals.waypoint_count + i+1, Convert.ToString(lat), Convert.ToString(lon), Convert.ToString(alt));
}
_path.Add_Path(_wpg, _gmap, path_name, "KML", new_list);
int index = _wpg.PathCount() - 1;
_current_path_index = index;
Models.Path path = _wpg.PathAt(index);
string exist_type = path.type;
bool exist_select = path.selected;
bool exist_visible = path.visible;
if (exist_type == "KML")
{
_wpg.ChangePathWP(index, new_list);
string pathname = path.name;
int id = path.id;
string type = path.type;
_gmap.Delete_gMapPath(path);
Models.Path newpath = new Models.Path();
newpath.name = pathname;
newpath.id = id;
newpath.type = type;
newpath.selected = exist_select;
newpath.visible = exist_visible;
newpath.waypoints = new_list;
_gmap.Add_gMapPath(path, false);
}
_gmap.ReDrawgMap();
GMAPTree.Update_GMapTree(_wpg, _treeview);
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (_current_path_index != -1) _wpg.DeletePath(_wpg.PathAt(_current_path_index));
_gmap.ReDrawgMap();
GMAPTree.Update_GMapTree(_wpg, _treeview);
this.Close();
}
private void btnAddKML_Click(object sender, EventArgs e)
{
this.Close();
}
private void cmbKLMPOI_SelectedIndexChanged(object sender, EventArgs e)
{
BuildKMLPath();
}
private void txtKMLName_TextChanged(object sender, EventArgs e)
{
BuildKMLPath();
}
}
}