-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyKML.cs
598 lines (482 loc) · 17.2 KB
/
TinyKML.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml;
namespace UCNLKML
{
public enum BoolEnum : int
{
unused = 0,
used = 1
}
public enum AltitudeMode
{
absolute,
relativeToGround
}
public class KMLLocation
{
public double Longitude { get; set; }
public double Latitude { get; set; }
public double Altitude { get; set; }
public KMLLocation()
: this(0, 0, 0)
{
}
public KMLLocation(double longitude, double latitude, double altitude)
{
Longitude = longitude;
Latitude = latitude;
Altitude = altitude;
}
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0:F08},{1:F08},{2:F08}", Longitude, Latitude, Altitude);
}
public static List<KMLLocation> Parse(string src)
{
List<KMLLocation> result = new List<KMLLocation>();
var c_splits = src.Split(new string[] { " ", "\r\n", "\t", "\n" }, System.StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < c_splits.Length; i++)
{
var l_splits = c_splits[i].Split(",".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
double lon = double.NaN;
double lat = double.NaN;
double alt = 0;
if (l_splits.Length > 1)
{
lon = double.Parse(l_splits[0], CultureInfo.InvariantCulture);
lat = double.Parse(l_splits[1], CultureInfo.InvariantCulture);
if (l_splits.Length > 2)
alt = double.Parse(l_splits[2], CultureInfo.InvariantCulture);
}
else
{
throw new ArgumentException(string.Format("Error: missing parameters in string: {1}", c_splits[i]));
}
result.Add(new KMLLocation(lon, lat, alt));
}
return result;
}
}
public abstract class KMLPlacemarkItem : IList<KMLLocation>
{
public bool Extrude { get; set; }
public bool Tessellate { get; set; }
protected List<KMLLocation> coordinates;
public string CoordinatesToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
for (int i = 0; i < coordinates.Count - 1; i++)
sb.AppendFormat("{0} ", coordinates[i].ToString());
sb.AppendFormat("{0}", coordinates[coordinates.Count - 1].ToString());
sb.AppendLine();
return sb.ToString();
}
#region IList
public int IndexOf(KMLLocation item)
{
return coordinates.IndexOf(item);
}
public void Insert(int index, KMLLocation item)
{
coordinates.Insert(index, item);
}
public void RemoveAt(int index)
{
coordinates.RemoveAt(index);
}
public KMLLocation this[int index]
{
get
{
return coordinates[index];
}
set
{
coordinates[index] = value;
}
}
public void Add(KMLLocation item)
{
coordinates.Add(item);
}
public void Clear()
{
coordinates.Clear();
}
public bool Contains(KMLLocation item)
{
return coordinates.Contains(item);
}
public void CopyTo(KMLLocation[] array, int arrayIndex)
{
coordinates.CopyTo(array, arrayIndex);
}
public int Count
{
get { return coordinates.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(KMLLocation item)
{
return coordinates.Remove(item);
}
public IEnumerator<KMLLocation> GetEnumerator()
{
return coordinates.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (var item in coordinates)
yield return item;
}
#endregion
}
public class KMLPoint : KMLPlacemarkItem
{
#region Properties
public KMLLocation Coordinate
{
get
{
return base.coordinates[0];
}
set
{
base.coordinates[0] = value;
}
}
#endregion
#region Constructor
public KMLPoint(double lon, double lat, double alt, bool isExtrude, bool isTessellate)
: this(new KMLLocation(lon, lat, alt), isExtrude, isTessellate)
{
}
public KMLPoint(KMLLocation loc, bool isExtrude, bool isTessellate)
{
base.Extrude = isExtrude;
base.Tessellate = isTessellate;
base.coordinates = new List<KMLLocation>();
base.coordinates.Add(loc);
}
#endregion
}
public class KMLLineString : KMLPlacemarkItem
{
#region Constructor
public KMLLineString()
: this(false, true)
{
}
public KMLLineString(KMLLocation[] points)
: this(false, true, points)
{
}
public KMLLineString(bool extrude, bool tessellate)
{
base.coordinates = new List<KMLLocation>();
base.Extrude = extrude;
base.Tessellate = tessellate;
}
public KMLLineString(bool extrude, bool tessellate, KMLLocation[] points)
{
base.coordinates = new List<KMLLocation>(points);
base.Extrude = extrude;
base.Tessellate = tessellate;
}
#endregion
}
public class KMLPlacemark
{
#region Properties
public string Name { get; set; }
public string Description { get; set; }
public KMLPlacemarkItem PlacemarkItem { get; set; }
public KMLPlacemark(string name, string description, bool extrude, bool tessellate, KMLLocation point)
{
Name = name;
Description = description;
PlacemarkItem = new KMLPoint(point, extrude, tessellate);
}
public KMLPlacemark(string name, string description, KMLLocation[] points)
{
Name = name;
Description = description;
PlacemarkItem = new KMLLineString(points);
}
public KMLPlacemark(string name, string description, KMLPlacemarkItem item)
{
Name = name;
Description = description;
PlacemarkItem = item;
}
#endregion
}
public class KMLData : IList<KMLPlacemark>
{
#region Properties
public string Name { get; set; }
public string Description { get; set; }
List<KMLPlacemark> placemarks;
#endregion
#region Constructor
public KMLData()
: this("", "")
{
}
public KMLData(string name, string description)
{
Name = name;
Description = description;
placemarks = new List<KMLPlacemark>();
}
#endregion
#region IList
public int IndexOf(KMLPlacemark item)
{
return placemarks.IndexOf(item);
}
public void Insert(int index, KMLPlacemark item)
{
placemarks.Insert(index, item);
}
public void RemoveAt(int index)
{
placemarks.RemoveAt(index);
}
public KMLPlacemark this[int index]
{
get
{
return placemarks[index];
}
set
{
placemarks[index] = value;
}
}
public void Add(KMLPlacemark item)
{
placemarks.Add(item);
}
public void Clear()
{
placemarks.Clear();
}
public bool Contains(KMLPlacemark item)
{
return placemarks.Contains(item);
}
public void CopyTo(KMLPlacemark[] array, int arrayIndex)
{
placemarks.CopyTo(array, arrayIndex);
}
public int Count
{
get { return placemarks.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(KMLPlacemark item)
{
return placemarks.Remove(item);
}
public IEnumerator<KMLPlacemark> GetEnumerator()
{
return placemarks.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (var item in placemarks)
yield return item;
}
#endregion
}
public static class TinyKML
{
#region Methods
#region Public
public static KMLData Read(string fileName)
{
KMLData result = new KMLData();
using (XmlReader reader = XmlReader.Create(fileName))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name.ToUpper())
{
case "NAME":
{
result.Name = TinyKML.ReadString(reader);
break;
}
case "DESCRIPTION":
{
result.Name = TinyKML.ReadString(reader);
break;
}
case "PLACEMARK":
{
result.Add(TinyKML.ReadPlacemark(reader));
break;
}
}
}
}
}
return result;
}
public static void Write(KMLData data, string fileName)
{
using (XmlWriter writer = XmlWriter.Create(fileName, new XmlWriterSettings { CloseOutput = true, Indent = true }))
{
writer.WriteStartDocument(false);
writer.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
writer.WriteStartElement("Document");
if (!string.IsNullOrEmpty(data.Name)) writer.WriteElementString("name", data.Name);
if (!string.IsNullOrEmpty(data.Description)) writer.WriteElementString("description", data.Description);
foreach (var item in data)
Write("Placemark", item, writer);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
#endregion
#region Private
#region Write
private static void Write(string tagName, KMLPlacemark placemark, XmlWriter writer)
{
writer.WriteStartElement("Placemark");
if (!string.IsNullOrEmpty(placemark.Name)) writer.WriteElementString("name", placemark.Name);
if (!string.IsNullOrEmpty(placemark.Description)) writer.WriteElementString("description", placemark.Description);
if (placemark.PlacemarkItem is KMLPoint)
writer.WriteStartElement("Point");
else if (placemark.PlacemarkItem is KMLLineString)
writer.WriteStartElement("LineString");
writer.WriteElementString("extrude", (Convert.ToInt32(placemark.PlacemarkItem.Extrude)).ToString());
writer.WriteElementString("tessellate", (Convert.ToInt32(placemark.PlacemarkItem.Tessellate)).ToString());
writer.WriteElementString("coordinates", placemark.PlacemarkItem.CoordinatesToString());
writer.WriteEndElement();
writer.WriteEndElement();
}
#endregion
#region Read
private static string ReadString(XmlReader reader)
{
if (reader.IsEmptyElement) throw new FormatException(reader.Name);
string elementName = reader.Name;
string result = string.Empty;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text:
result = reader.Value;
break;
case XmlNodeType.EndElement:
return result;
case XmlNodeType.Element:
throw new FormatException(elementName);
}
}
throw new FormatException(elementName);
}
private static int ReadInt(XmlReader reader)
{
string value = ReadString(reader);
return int.Parse(value, CultureInfo.InvariantCulture);
}
private static KMLPlacemarkItem ReadPlacemarkItem(XmlReader reader)
{
bool isExtrude = false;
bool isTessallate = true;
List<KMLLocation> coordinates = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name.ToUpper())
{
case "EXTRUDE":
{
isExtrude = Convert.ToBoolean(TinyKML.ReadInt(reader));
break;
}
case "TESSALLATE":
{
isTessallate = Convert.ToBoolean(TinyKML.ReadInt(reader));
break;
}
case "COORDINATES":
{
coordinates = KMLLocation.Parse(TinyKML.ReadString(reader));
if (coordinates == null)
throw new FormatException();
if (coordinates.Count == 1)
return new KMLPoint(coordinates[0], isExtrude, isTessallate);
else
return new KMLLineString(isExtrude, isTessallate, coordinates.ToArray());
}
}
}
}
throw new FormatException();
}
private static KMLPlacemark ReadPlacemark(XmlReader reader)
{
string name = string.Empty;
string description = string.Empty;
KMLPlacemarkItem item = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name.ToUpper())
{
case "NAME":
{
name = TinyKML.ReadString(reader);
break;
}
case "DESCRIPTION":
{
description = TinyKML.ReadString(reader);
break;
}
case "POINT":
{
item = (KMLPoint)TinyKML.ReadPlacemarkItem(reader);
if (item != null)
return new KMLPlacemark(name, description, item);
else
throw new FormatException();
}
case "LINESTRING":
{
item = (KMLLineString)TinyKML.ReadPlacemarkItem(reader);
if (item != null)
return new KMLPlacemark(name, description, item);
else
throw new FormatException();
}
}
}
}
throw new FormatException();
}
#endregion
#endregion
#endregion
}
}