-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadKML.cs
401 lines (328 loc) · 14.7 KB
/
UploadKML.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using SharpKml.Dom;
using SharpKml.Engine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Spatial;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Bus_Application
{
public partial class UploadKML : Form
{
Form parentForm;
Teste_OnibusContext context;
int count;
public UploadKML(Form parent)
{
InitializeComponent();
this.parentForm = parent;
btnBrowse.Enabled = false;
}
#region Basic Events
private void rdbStations_CheckedChanged(object sender, EventArgs e)
{
lblMessage.Text = String.Empty;
btnBrowse.Enabled = true;
}
private void rdbReferences_CheckedChanged(object sender, EventArgs e)
{
lblMessage.Text = String.Empty;
btnBrowse.Enabled = true;
}
private void btnClearStationReferences_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete all the Landmarks?", "Confirmation", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
{
try
{
using (context = new Teste_OnibusContext())
{
DBLandmark db = new DBLandmark(context);
db.DeleteAll();
Methods.DisplayMessage(lblMessage, "All records deleted successfully!", Color.Green);
}
}
catch (Exception ex)
{
Methods.DisplayMessage(lblMessage, "It was not possible to delete the records!", Color.Red);
}
}
}
private void chbBus_CheckedChanged(object sender, EventArgs e)
{
lblMessage.Text = String.Empty;
btnBrowse.Enabled = true;
}
private void btnClearSelection_Click(object sender, EventArgs e)
{
rdbReferences.Checked = false;
rdbStations.Checked = false;
chbBus.Checked = false;
}
private void UploadKMLStations_FormClosed(object sender, FormClosedEventArgs e)
{
parentForm.Enabled = true;
parentForm.Activate();
}
private void btnClearAll_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure you want to delete all records?", "Confirmation", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
{
try
{
using (context = new Teste_OnibusContext())
{
DBLandmark dbLandmark = new DBLandmark(context);
dbLandmark.DeleteAll();
DBBus dbBus = new DBBus(context);
dbBus.DeleteAll();
DBStation dbStation = new DBStation(context);
dbStation.DeleteAll();
Methods.DisplayMessage(lblMessage, "All records deleted successfully!", Color.Green);
}
}
catch (Exception ex)
{
Methods.DisplayMessage(lblMessage, "It was not possible to delete the records!", Color.Red);
}
}
}
#endregion
private void btnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "KML|*.kml";
lblMessage.Text = "";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (context = new Teste_OnibusContext())
{
using (var transaction = context.Database.BeginTransaction())
{
try
{
KmlFile kml = Methods.OpenFile(openFileDialog1.FileName, lblMessage);
bool done = false;
if (chbBus.Checked)
{
done = addBusRoutes(kml); // cadastro o ônibus, a rota e faço o join com as estações em que ele passa
}
else if (rdbStations.Checked || rdbReferences.Checked) // cadastro as estações ou os pontos de referência
{
count = 0;
string fileName = "";
if (rdbStations.Checked)
fileName = "stations_coordinates.txt";
else if (rdbReferences.Checked)
fileName = "reference_coordinates.txt";
using (StreamWriter writetext = new StreamWriter(fileName))
{
if (kml.Root.Flatten().OfType<Folder>().Any())
done = parseFolder(kml, done, writetext);
else
done = parsePlacemark(kml.Root.Flatten().OfType<Placemark>(), done, writetext);
}
}
transaction.Commit();
if (done)
Methods.DisplayMessage(lblMessage, "Load completed successfully!", Color.Green);
else
Methods.DisplayMessage(lblMessage, "THe operation was not done!", Color.Red);
}
catch (Exception ex)
{
transaction.Rollback();
Methods.DisplayMessage(lblMessage, ex.Message, Color.Red);
}
}
}
}
}
#region Add BusRoutes
/* Código referente ao cadastro dos Ônibus juntamente com suas Rotas.
É feito um join da rota do ônibus com as estações em que ele passa. Portanto, DEVE ser feito o
cadastro das estações de ônibus ANTES de adicionar a rota.
É realizado um insert nas tabelas BUSES, ROUTES e STATION_BUSES */
private bool addBusRoutes(KmlFile kml)
{
BUS bus = new BUS();
ROUTE route = new ROUTE();
DBRoute dbRoute = new DBRoute(context);
DBStation dbStation = new DBStation(context);
route.BUS = bus;
StringBuilder sb = new StringBuilder("LINESTRING (");
bus.Bus_Description = Methods.getBusDescription(kml);
bool hasStation = false;
foreach (var folder in kml.Root.Flatten().OfType<Folder>())
{
if (folder.Flatten().OfType<SharpKml.Dom.LineString>().Any())
{
DBStation_Bus dbStationBus = new DBStation_Bus(context);
foreach (var placemark in folder.Flatten().OfType<SharpKml.Dom.Placemark>())
{
parseLineString(placemark, sb); // grava a rota do onibus
hasStation = parsePoint(placemark, dbStationBus, dbStation, bus, route, hasStation); // grava as estacoes que o onibus passa
}
}
}
if (!hasStation)
{
string message = "The stations this bus pass by are not in the database. Please insert the stations first, then the bus route";
Methods.DisplayMessage(lblMessage, message, Color.Red);
throw new Exception(message);
}
route.Route_Coordinates = DbGeography.LineFromText(sb.Replace(',', ')', sb.Length - 1, 1).ToString(), 4326);
dbRoute.Add(route);
return true;
}
private void parseLineString(Placemark placemark, StringBuilder sb)
{
if (chbBus.Checked)
{
StringBuilder textString = new StringBuilder();
bool flag = false;
textString.Append("Route_Coordinates: ");
foreach (var lineString in placemark.Flatten().OfType<SharpKml.Dom.LineString>())
{
flag = true;
sb.Append(string.Join(" ", lineString.Coordinates.Select(x => x.Latitude.ToString() + " " + x.Longitude.ToString() + ",")));
textString.Append(string.Join(" ", lineString.Coordinates.Select(x => x.Latitude.ToString() + "," + x.Longitude.ToString() + "; ")));
}
if (flag)
{
using (StreamWriter writetext = new StreamWriter("write.txt"))
{
writetext.Write(textString.ToString());
}
}
}
}
private bool parsePoint(Placemark placemark, DBStation_Bus dbStationBus, DBStation dbStation, BUS bus, ROUTE route, bool hasStation)
{
foreach (var lineString in placemark.Flatten().OfType<SharpKml.Dom.Point>())
{
IList<STATION> stationsNear = dbStation.SelectStationsNear(50,
Methods.ConvertLatLonToDbGeography(lineString.Coordinate.Longitude, lineString.Coordinate.Latitude));
foreach (var station in stationsNear)
{
STATION_BUSES stationBus = new STATION_BUSES();
stationBus.STATION = station;
stationBus.BUS = bus;
dbStationBus.AddStation_Buses_Route(route, stationBus);
hasStation = true;
}
}
return hasStation;
}
#endregion Add BusRoutes
#region Add References or Stations
// Código referente ao cadastro dos Pontos de Referência OU das Estações de Ônibus
// É realizado um insert nas tabelas STATIONS OU LANDMARKs e LANDMARK_KNOWN_AS
private bool parseFolder(KmlFile kml, bool done, StreamWriter writetext)
{
foreach (var folder in kml.Root.Flatten().OfType<Folder>())
{
IEnumerable<Placemark> enumerablePlacemark = null;
if (folder.Flatten().OfType<Placemark>().Any())
{
if (folder.Flatten().OfType<LineString>().Any())
{
enumerablePlacemark = folder.Flatten().OfType<Placemark>().Take(1);
}
else
{
enumerablePlacemark = folder.Flatten().OfType<Placemark>();
}
}
done = parsePlacemark(enumerablePlacemark, done, writetext, kml);
}
return done;
}
private bool parsePlacemark(IEnumerable<Placemark> enumerablePlacemark, bool done, StreamWriter writetext, KmlFile kml = null)
{
foreach (var placemark in enumerablePlacemark)
{
done = parsePoint(placemark, done, writetext);
if (placemark.Flatten().OfType<SharpKml.Dom.LineString>().Any())
{
BUS bus = new BUS();
bus.Bus_Description = Methods.getBusDescription(kml);
}
}
return done;
}
private bool parsePoint(Placemark placemark, bool done, StreamWriter writetext)
{
StringBuilder textString = new StringBuilder();
textString.Append("{");
if (rdbStations.Checked)
{
textString.Append("\"Station_Description\": \"" + placemark.Name + "\"," + Environment.NewLine);
textString.Append("\"Station_Coordinates\": \"");
}
else if (rdbReferences.Checked)
{
textString.Append("\"Known_As_Description\": \"" + placemark.Name + "\"," + Environment.NewLine);
}
foreach (var point in placemark.Geometry.Flatten().OfType<SharpKml.Dom.Point>())
{
DbGeography coordinates = DbGeography.PointFromText("POINT (" + point.Coordinate.Longitude
+ " " + point.Coordinate.Latitude + ")", 4326);
textString.Append(point.Coordinate.Longitude + "," + point.Coordinate.Latitude + "\"");
if (rdbStations.Checked)
{
addStation(placemark, point, coordinates);
done = true;
}
else if (rdbReferences.Checked)
{
addReference(placemark, point, coordinates);
done = true;
}
}
textString.Append("}, " + Environment.NewLine);
writetext.WriteLine(textString.ToString());
return done;
}
private void addStation(Placemark placemark, SharpKml.Dom.Point point, DbGeography coordinates)
{
STATION station = new STATION();
station.Station_Description = placemark.Name;
station.Station_Coordinates = coordinates;
DBStation db = new DBStation(context);
db.Add(station);
}
private void addReference(Placemark placemark, SharpKml.Dom.Point point, DbGeography coordinates)
{
LANDMARK landmark = new LANDMARK();
LANDMARK_KNOWN_AS knowAsLandmark;
knowAsLandmark = new LANDMARK_KNOWN_AS();
knowAsLandmark.Known_As_Description = placemark.Name;
landmark.Landmark_Coordinates = coordinates;
knowAsLandmark.LANDMARK = landmark;
knowAsLandmark.Known_As_ID = count = count + 1;
DBLandmarkKnownAs db = new DBLandmarkKnownAs(context);
db.Add(knowAsLandmark);
if (placemark.Description != null)
{
string[] description = placemark.Description.Text.Split(';');
for (int i = 0; i < description.Count(); i++)
{
knowAsLandmark = new LANDMARK_KNOWN_AS();
knowAsLandmark.Known_As_Description = description[i];
landmark.Landmark_Coordinates = coordinates;
knowAsLandmark.Known_As_ID = count = count + 1;
knowAsLandmark.LANDMARK = landmark;
db = new DBLandmarkKnownAs(context);
db.Add(knowAsLandmark);
}
}
}
#endregion Add References or Stations
}
}