-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackManager.cs
167 lines (131 loc) · 4.57 KB
/
TrackManager.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using UCNLDrivers;
using UCNLKML;
using UCNLNav;
namespace UCNLUI
{
public class TrackManager
{
#region Properties
public string Descrption { get; set; }
Dictionary<string, List<GeoPoint3DTm>> tracks;
public bool IsEmpty
{
get { return tracks.Count == 0; }
}
bool changed = false;
public bool Changed
{
get => changed;
set
{
if (value != changed)
{
changed = value;
ChangedChanged.Rise(this, EventArgs.Empty);
}
}
}
#endregion
#region Constructor
public TrackManager() : this("www.docs.unavlab.com")
{
}
public TrackManager(string generator_Description)
{
Descrption = generator_Description;
tracks = new Dictionary<string, List<GeoPoint3DTm>>();
}
#endregion
#region Methods
public void Clear()
{
tracks.Clear();
IsEmptyChanged.Rise(this, new EventArgs());
}
public void AddPoint(string trackID, double lat, double lon, double dpt, DateTime timeStamp)
{
bool prevIsEmpty = IsEmpty;
if (!tracks.ContainsKey(trackID))
tracks.Add(trackID, new List<GeoPoint3DTm>());
tracks[trackID].Add(new GeoPoint3DTm(lat, lon, dpt, timeStamp));
if (prevIsEmpty != IsEmpty)
IsEmptyChanged.Rise(this, EventArgs.Empty);
Changed = true;
}
public void ExportToKML(string fileName)
{
KMLData data = new KMLData(fileName, Descrption);
List<KMLLocation> kmlTrack;
foreach (var item in tracks)
{
kmlTrack = new List<KMLLocation>();
foreach (var point in item.Value)
kmlTrack.Add(new KMLLocation(point.Longitude, point.Latitude, -point.Depth));
data.Add(new KMLPlacemark(string.Format("{0} track", item.Key), "", kmlTrack.ToArray()));
}
TinyKML.Write(data, fileName);
Changed = false;
}
public void ExportToCSV(string fileName)
{
StringBuilder sb = new StringBuilder();
foreach (var track in tracks)
{
sb.AppendFormat("#{0}\r\n", Descrption);
sb.AppendFormat("#\r\nTrack name: {0}\r\n", track.Key);
sb.Append("HH:MM:SS;LAT;LON;DPT;\r\n");
foreach (var point in track.Value)
{
sb.AppendFormat(CultureInfo.InvariantCulture,
"{0:00};{1:00};{2:00};{3:F06};{4:F06};{5:F03}\r\n",
point.TimeStamp.Hour,
point.TimeStamp.Minute,
point.TimeStamp.Second,
point.Latitude,
point.Longitude,
point.Depth);
}
}
File.WriteAllText(fileName, sb.ToString());
Changed = false;
}
public void ImportFromKML(string fileName)
{
KMLData data = TinyKML.Read(fileName);
foreach (var placemark in data)
{
foreach (var kmllocation in placemark.PlacemarkItem)
{
AddPoint(placemark.Name, kmllocation.Latitude, kmllocation.Longitude, kmllocation.Altitude, DateTime.Now);
}
}
Changed = true;
}
public bool IsTrackPresent(string trackID)
{
return tracks.ContainsKey(trackID);
}
public List<GeoPoint> GetTrack2D(string trackID)
{
List<GeoPoint> result = new List<GeoPoint>();
if (tracks.ContainsKey(trackID))
{
foreach (var item in tracks[trackID])
{
result.Add(new GeoPoint(item.Latitude, item.Longitude));
}
}
return result;
}
#endregion
#region Events
public EventHandler IsEmptyChanged;
public EventHandler ChangedChanged;
#endregion
}
}