-
Notifications
You must be signed in to change notification settings - Fork 3
/
GPIReader.cs
4243 lines (3958 loc) · 189 KB
/
GPIReader.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************/
/* */
/* C# Garmin POI File Reader */
/* (by [email protected]) */
/* & */
/* C# Garmin POI File Writer */
/* (by [email protected]) */
/* */
/* GPIReader by [email protected] */
/* Part of KMZRebuilder & KMZViewer Project */
/* GPIWrited by [email protected] */
/* Part of KMZRebuilder Project */
/* */
/* by reverse engineering */
/* */
/***************************************************/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
namespace KMZRebuilder
{
#region RECTYPES
/// <summary>
/// GPI File Record Types
/// </summary>
public enum RecType: ushort
{
Header0 = 0,
Header1 = 1,
Waypoint = 2,
Alert = 3,
BitmapReference = 4,
Bitmap = 5,
CategoryReference = 6,
Category = 7,
Area = 8,
POIGroup = 9,
Comment = 10,
Address = 11,
Contact = 12,
Image = 13,
Description = 14,
ProductInfo = 15,
AlertCircle = 16,
Copyright = 17,
Media = 18,
SpeedCamera = 19,
AlertTriggerOptions = 27,
End = 0xFFFF
}
/// <summary>
/// Type Utils
/// </summary>
/// <typeparam name="T"></typeparam>
public static class RecEnum<T>
{
public static bool IsDefined(string name)
{
return Enum.IsDefined(typeof(T), name);
}
public static bool IsDefined(T value)
{
return Enum.IsDefined(typeof(T), value);
}
}
/// <summary>
/// GPI File Record Block
/// </summary>
public class Record
{
/// <summary>
/// Parent Record
/// </summary>
public Record Parent = null;
/// <summary>
/// Child Records
/// </summary>
public List<Record> Childs = new List<Record>();
/// <summary>
/// Record is on top
/// </summary>
public bool RootIsTop { get { return Parent == null; } }
/// <summary>
/// Record Nesting Index
/// </summary>
public int RootLevel { get { return Parent == null ? 0 : Parent.RootLevel + 1; } }
/// <summary>
/// Record Nesting Ierarchy
/// </summary>
internal string RootIerarchy { get { return Parent == null ? @"\Root" : Parent.RootIerarchy + @"\" + RecordType.ToString(); } }
/// <summary>
/// Has Extra Block Data
/// </summary>
public bool RecHasExtra { get { return (RecFlags & 0x08) == 0x08; } }
/// <summary>
/// GPI Record Type
/// </summary>
public RecType RecordType { get { return (RecType)RecType; } }
/// <summary>
/// GPI Record Type
/// </summary>
internal ushort RecType = 0;
/// <summary>
/// GPI Record Flags
/// </summary>
internal ushort RecFlags = 0;
/// <summary>
/// Offset of Record Block
/// </summary>
internal uint OffsetBlock = 0;
/// <summary>
/// Offset of Record Main Data Block
/// </summary>
internal uint OffsetMain = 0;
/// <summary>
/// Offset of Record Extra Data Block
/// </summary>
internal uint OffsetExtra = 0;
/// <summary>
/// Record Block Length
/// </summary>
internal uint LengthBlock = 0;
/// <summary>
/// Record Block Main Data Length
/// </summary>
internal uint LengthMain = 0;
/// <summary>
/// Record Block Extra Data Length
/// </summary>
internal uint LengthExtra = 0;
/// <summary>
/// Record Block Main & Extra Data Length
/// </summary>
internal uint LengthTotal = 0;
/// <summary>
/// Source Block Without Any Offsets
/// </summary>
internal byte[] DataBlock;
/// <summary>
/// Record Main Data
/// </summary>
public byte[] DataMain
{
get
{
if (DataBlock == null) return null;
byte[] res = new byte[LengthMain];
Array.Copy(DataBlock, OffsetMain, res, 0, LengthMain);
return res;
}
}
/// <summary>
/// Record Extra Data
/// </summary>
public byte[] DataExtra
{
get
{
if (DataBlock == null) return null;
byte[] res = new byte[LengthExtra];
Array.Copy(DataBlock, OffsetExtra, res, 0, LengthExtra);
return res;
}
}
/// <summary>
/// Record Main & Extra Data
/// </summary>
public byte[] DataTotal
{
get
{
if (DataBlock == null) return null;
byte[] res = new byte[LengthTotal];
Array.Copy(DataBlock, OffsetMain, res, 0, LengthTotal);
return res;
}
}
/// <summary>
/// Last Read Record Block Error
/// </summary>
internal Exception ReadError = null;
/// <summary>
/// Create with Parent
/// </summary>
/// <param name="parent"></param>
protected Record(Record parent)
{
this.Parent = parent;
if (parent != null) parent.Childs.Add(this);
}
/// <summary>
/// Create No Parent (File Root)
/// </summary>
public static Record ROOT
{
get
{
return new Record(null);
}
}
public static Record Create(Record parent, uint offset, ref byte[] sourceData, ushort RecordType)
{
Record res = null;
if (RecordType == 0) res = new RecHeader0(parent);
if (RecordType == 1) res = new RecHeader1(parent);
if (RecordType == 2) res = new RecWaypoint(parent);
if (RecordType == 3) res = new RecAlert(parent);
if (RecordType == 4) res = new RecBitmapReference(parent);
if (RecordType == 5) res = new RecBitmap(parent);
if (RecordType == 6) res = new RecCategoryReference(parent);
if (RecordType == 7) res = new RecCategory(parent);
if (RecordType == 8) res = new RecArea(parent);
if (RecordType == 9) res = new RecPOIGroup(parent);
if (RecordType == 10) res = new RecComment(parent);
if (RecordType == 11) res = new RecAddress(parent);
if (RecordType == 12) res = new RecContact(parent);
if (RecordType == 13) res = new RecImage(parent);
if (RecordType == 14) res = new RecDescription(parent);
if (RecordType == 15) res = new RecProductInfo(parent);
if (RecordType == 16) res = new RecAlertCircle(parent);
if (RecordType == 17) res = new RecCopyright(parent);
if (RecordType == 18) res = new RecMedia(parent);
if (RecordType == 19) res = new RecSpeedCamera(parent);
if (RecordType == 27) res = new RecAlertTriggerOptions(parent);
if (RecordType == 0xFFFF) res = new RecEnd(parent);
if (res == null) res = new Record(parent);
res.RecType = RecordType;
res.OffsetBlock = offset;
res.DataBlock = sourceData;
return res;
}
public override string ToString()
{
return String.Format("{1}[{2}]{3}", RecordType, RecType, RootLevel, RootIerarchy);
}
}
// 0
public sealed class RecHeader0 : Record
{
internal RecHeader0(Record parent) : base(parent) { }
public string Header = null;
public string Version = null;
public DateTime Created = DateTime.MinValue;
public string Name = null;
}
// 1
public sealed class RecHeader1 : Record
{
internal RecHeader1(Record parent) : base(parent) { }
public string Content = null;
public ushort CodePage = 0xFDE9;
public Encoding Encoding
{
get
{
try { return Encoding.GetEncoding(CodePage); } catch { };
return Encoding.Unicode;
}
}
}
// 2
public sealed class RecWaypoint : Record
{
internal RecWaypoint(Record parent) : base(parent) { }
internal int cLat;
internal int cLon;
public double Lat { get { return (double)cLat * 360.0 / Math.Pow(2, 32); } }
public double Lon { get { return (double)cLon * 360.0 / Math.Pow(2, 32); } }
public List<KeyValuePair<string, string>> ShortName = new List<KeyValuePair<string, string>>();
public string Name
{
get
{
foreach (KeyValuePair<string, string> kvp in ShortName)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in ShortName)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in ShortName)
return kvp.Value;
return null;
}
}
public RecAlert Alert;
public RecBitmap Bitmap;
public RecImage Image;
public RecDescription Description;
public RecComment Comment;
public RecContact Contact;
public RecAddress Address;
}
// 3
public sealed class RecAlert : Record
{
internal RecAlert(Record parent) : base(parent) { }
public ushort Proximity;
internal ushort cSpeed;
public int Speed { get { return (int)Math.Round((double)cSpeed / 100.0 * 3.6); } }
public byte Alert;
public byte AlertType;
public byte SoundNumber;
public byte AudioAlert;
public bool IsOn { get { return Alert == 1; } }
public string IsType { get {
if (AlertType == 0) return "proximity";
if (AlertType == 1) return "along_road";
if (AlertType == 2) return "toure_guide";
return AlertType.ToString();
} }
public RecAlertCircle AlertCircles;
public RecAlertTriggerOptions AlertTriggerOptions;
}
// 4
public sealed class RecBitmapReference : Record
{
internal RecBitmapReference(Record parent) : base(parent) { }
public ushort BitmapID;
}
// 5
public sealed class RecBitmap : Record
{
internal RecBitmap(Record parent) : base(parent) { }
public ushort BitmapID;
public ushort Height;
public ushort Width;
public ushort LineSize;
public ushort BitsPerPixel;
public ushort Reserved9;
public uint ImageSize; // LineSize * Height
public uint Reserved10;
public uint Palette;
public uint TransparentColor;
public uint Flags;
public uint Reserved11;
public byte[] Pixels;
public uint[] Colors;
}
// 6
public sealed class RecCategoryReference : Record
{
internal RecCategoryReference(Record parent) : base(parent) { }
public ushort CategoryID;
}
// 7
public sealed class RecCategory : Record
{
internal RecCategory(Record parent) : base(parent) { }
public ushort CategoryID;
public List<KeyValuePair<string, string>> Category = new List<KeyValuePair<string, string>>();
public string Name
{
get
{
foreach (KeyValuePair<string, string> kvp in Category)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in Category)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in Category)
return kvp.Value;
return null;
}
}
public List<RecWaypoint> Waypoints = new List<RecWaypoint>();
public RecBitmap Bitmap = null;
public RecDescription Description;
public RecComment Comment;
public RecContact Contact;
}
// 8
public sealed class RecArea : Record
{
internal RecArea(Record parent) : base(parent) { }
internal int cMaxLat;
internal int cMaxLon;
internal int cMinLat;
internal int cMinLon;
public double MaxLat { get { return (double)cMaxLat * 360.0 / Math.Pow(2, 32); } }
public double MaxLon { get { return (double)cMaxLon * 360.0 / Math.Pow(2, 32); } }
public double MinLat { get { return (double)cMinLat * 360.0 / Math.Pow(2, 32); } }
public double MinLon { get { return (double)cMinLon * 360.0 / Math.Pow(2, 32); } }
}
// 9
public sealed class RecPOIGroup : Record
{
internal RecPOIGroup(Record parent) : base(parent) { }
public List<KeyValuePair<string, string>> DataSource = new List<KeyValuePair<string, string>>();
public string Name
{
get
{
foreach (KeyValuePair<string, string> kvp in DataSource)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in DataSource)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in DataSource)
return kvp.Value;
return null;
}
}
}
// 10
public sealed class RecComment : Record
{
internal RecComment(Record parent) : base(parent) { }
public List<KeyValuePair<string, string>> Comment = new List<KeyValuePair<string, string>>();
public string Text
{
get
{
foreach (KeyValuePair<string, string> kvp in Comment)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in Comment)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in Comment)
return kvp.Value;
return null;
}
}
}
// 11
public sealed class RecAddress : Record
{
internal RecAddress(Record parent) : base(parent) { }
public ushort Flags;
public List<KeyValuePair<string, string>> aCity = new List<KeyValuePair<string, string>>();
public List<KeyValuePair<string, string>> aCountry = new List<KeyValuePair<string, string>>();
public List<KeyValuePair<string, string>> aState = new List<KeyValuePair<string, string>>();
public string Postal;
public List<KeyValuePair<string, string>> aStreet = new List<KeyValuePair<string, string>>();
public string House;
public string City
{
get
{
foreach (KeyValuePair<string, string> kvp in aCity)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aCity)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aCity)
return kvp.Value;
return null;
}
}
public string Country
{
get
{
foreach (KeyValuePair<string, string> kvp in aCountry)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aCountry)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aCountry)
return kvp.Value;
return null;
}
}
public string State
{
get
{
foreach (KeyValuePair<string, string> kvp in aState)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aState)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aState)
return kvp.Value;
return null;
}
}
public string Street
{
get
{
foreach (KeyValuePair<string, string> kvp in aStreet)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aStreet)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in aStreet)
return kvp.Value;
return null;
}
}
}
// 12
public sealed class RecContact : Record
{
internal RecContact(Record parent) : base(parent) { }
public ushort Flags;
public string Phone;
public string Phone2;
public string Fax;
public string Email;
public string Web;
}
// 13
public sealed class RecImage : Record
{
internal RecImage(Record parent) : base(parent) { }
public uint Length;
public byte[] ImageData;
}
// 14
public sealed class RecDescription : Record
{
internal RecDescription(Record parent) : base(parent) { }
public List<KeyValuePair<string, string>> Description = new List<KeyValuePair<string, string>>();
public string Text
{
get
{
foreach (KeyValuePair<string, string> kvp in Description)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in Description)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in Description)
return kvp.Value;
return null;
}
}
}
// 15
public sealed class RecProductInfo : Record
{
internal RecProductInfo(Record parent) : base(parent) { }
public ushort FactoryID;
public byte ProductID;
public byte RegionID;
public byte VendorID;
}
// 16
public sealed class RecAlertCircle : Record
{
internal RecAlertCircle(Record parent) : base(parent) { }
public ushort Count;
public double[] lat;
public double[] lon;
public uint[] radius;
}
// 17
public sealed class RecCopyright : Record
{
internal RecCopyright(Record parent) : base(parent) { }
public ushort Flags1 = 0;
public ushort Flags2 = 0;
public List<KeyValuePair<string, string>> cDataSource = new List<KeyValuePair<string, string>>();
public List<KeyValuePair<string, string>> cCopyrights = new List<KeyValuePair<string, string>>();
public string DeviceModel = null;
public string DataSource
{
get
{
foreach (KeyValuePair<string, string> kvp in cDataSource)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cDataSource)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cDataSource)
return kvp.Value;
return null;
}
}
public string Copyrights
{
get
{
foreach (KeyValuePair<string, string> kvp in cCopyrights)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cCopyrights)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cCopyrights)
return kvp.Value;
return null;
}
}
}
// 18
public sealed class RecMedia : Record
{
internal RecMedia(Record parent) : base(parent) { }
public ushort MediaID;
public byte Format;
public bool IsWav { get { return Format == 0; } }
public bool IsMP3 { get { return Format == 1; } }
public List<KeyValuePair<string, byte[]>> Content = new List<KeyValuePair<string, byte[]>>();
public byte[] Media
{
get
{
foreach (KeyValuePair<string, byte[]> kvp in Content)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, byte[]> kvp in Content)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
return null;
}
}
}
// 19
public sealed class RecSpeedCamera : Record
{
internal RecSpeedCamera(Record parent) : base(parent) { }
internal int cMaxLat;
internal int cMaxLon;
internal int cMinLat;
internal int cMinLon;
public double MaxLat { get { return (double)cMaxLat * 360.0 / Math.Pow(2, 24); } }
public double MaxLon { get { return (double)cMaxLon * 360.0 / Math.Pow(2, 24); } }
public double MinLat { get { return (double)cMinLat * 360.0 / Math.Pow(2, 24); } }
public double MinLon { get { return (double)cMinLon * 360.0 / Math.Pow(2, 24); } }
public byte Flags;
internal int cLat;
internal int cLon;
public double Lat { get { return (double)cLat * 360.0 / Math.Pow(2, 24); } }
public double Lon { get { return (double)cLon * 360.0 / Math.Pow(2, 24); } }
}
// 27
public sealed class RecAlertTriggerOptions : Record
{
internal RecAlertTriggerOptions(Record parent) : base(parent) { }
public byte BearingCount = 0;
public ushort[] BearingAngle;
public ushort[] BearingWide;
public bool[] BearingBiDir;
public byte[] DateTimeBlock;
public List<string> DateTimeList = new List<string>();
}
[Serializable]
public class MarkerBlock
{
public MarkerBlock() { }
public static MarkerBlock FromBytes(byte[] data)
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MarkerBlock));
MemoryStream ms = new MemoryStream(data);
System.IO.StreamReader reader = new System.IO.StreamReader(ms, System.Text.Encoding.UTF8);
MarkerBlock c = (MarkerBlock)xs.Deserialize(reader);
ms.Close();
return c;
}
public byte[] ToBytes()
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
this.Creator = "KMZRebuilder v" + fvi.FileVersion;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = false;
settings.OmitXmlDeclaration = true;
settings.NewLineHandling = NewLineHandling.None;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(MarkerBlock));
System.IO.MemoryStream ms = new MemoryStream();
XmlWriter writer = XmlWriter.Create(ms, settings);
xs.Serialize(writer, this, ns);
writer.Flush();
ms.Position = 0;
byte[] bb = new byte[ms.Length];
ms.Read(bb, 0, bb.Length);
writer.Close();
ms.Close();
return bb;
}
[XmlArray("Bounds"),XmlArrayItem("B")]
public double[] Bounds = null;
public string Creator = "KMZRebuilder";
[XmlElement("DT")]
public DateTime Created = DateTime.MinValue;
public uint AlertZones = 0;
public string Description = null;
}
// 0xFFFF
public sealed class RecEnd : Record
{
internal RecEnd(Record parent) : base(parent) { }
}
#endregion RECTYPES
/// <summary>
/// GPI Reader
/// </summary>
public class GPIReader
{
public delegate void Add2LogProc(string text);
/// <summary>
/// Current Locale Language ISO-639
/// </summary>
public static string LOCALE_LANGUAGE = "EN"; // 2-SYMBOLS
/// <summary>
/// Default Language ISO-639
/// </summary>
public static string DEFAULT_LANGUAGE = "EN"; // 2-SYMBOLS
/// <summary>
/// Save Media Content to disk
/// </summary>
public static bool SAVE_MEDIA = false;
/// <summary>
/// Create Images for categories without images
/// </summary>
public static bool CREATE_CATEGORY_IMAGES_IFNO = false;
/// <summary>
/// Set kmz poi image from jpeg (not bitmap); false - from bitmap; true - from image (if specified)
/// </summary>
public static bool POI_IMAGE_FROM_JPEG = false; // bitmap o
/// <summary>
/// Save Multilanguage Names in Description
/// </summary>
public static bool SAVE_MULTINAMES = true;
/// <summary>
/// Source File Name
/// </summary>
public string FileName { get { return fileName; } }
private string fileName;
/// <summary>
/// Public GPI Root Element
/// </summary>
public Record RootElement = Record.ROOT;
/// <summary>
/// GPI File Document Name
/// </summary>
public string Content = null;
/// <summary>
/// GPI Text CodePage
/// </summary>
public ushort CodePage = 0xFDE9;
/// <summary>
/// GPI Text Encoding
/// </summary>
public Encoding Encoding = Encoding.Unicode;
/// <summary>
/// GPI File Header Text
/// </summary>
public string Header = null;
/// <summary>
/// GPI File Version
/// </summary>
public string Version = null;
/// <summary>
/// GPI File DateTime Created
/// </summary>
public DateTime Created = DateTime.MinValue;
/// <summary>
/// GPI File Name
/// </summary>
public string Name = null;
/// <summary>
/// Multilang Content Data Sources
/// </summary>
public List<KeyValuePair<string, string>> cDataSource = new List<KeyValuePair<string, string>>();
/// <summary>
/// Multilang Content Copyrights
/// </summary>
public List<KeyValuePair<string, string>> cCopyrights = new List<KeyValuePair<string, string>>();
/// <summary>
/// List Of POI Categories in file
/// </summary>
public Dictionary<ushort, RecCategory> Categories = new Dictionary<ushort, RecCategory>();
/// <summary>
/// Default Category
/// </summary>
private RecCategory DefaultCategory = new RecCategory(null);
/// <summary>
/// List of Bitmaps in file
/// </summary>
public Dictionary<ushort, RecBitmap> Bitmaps = new Dictionary<ushort, RecBitmap>();
/// <summary>
/// List of Media in file
/// </summary>
public Dictionary<ushort, RecMedia> Medias = new Dictionary<ushort, RecMedia>();
private Add2LogProc Add2Log;
private uint readNotifier;
/// <summary>
/// File Content Data Source (local language)
/// </summary>
public string DataSource
{
get
{
foreach (KeyValuePair<string, string> kvp in cDataSource)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cDataSource)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cDataSource)
return kvp.Value;
return null;
}
}
/// <summary>
/// File Content Copyrights (local language)
/// </summary>
public string Copyrights
{
get
{
foreach (KeyValuePair<string, string> kvp in cCopyrights)
if (kvp.Key == GPIReader.LOCALE_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cCopyrights)
if (kvp.Key == GPIReader.DEFAULT_LANGUAGE)
return kvp.Value;
foreach (KeyValuePair<string, string> kvp in cCopyrights)
return kvp.Value;
return null;
}
}
/// <summary>
/// Marker Block
/// </summary>
public MarkerBlock MarkerData = null;
/// <summary>
/// Constructor (GPI File Reader)
/// </summary>
/// <param name="fileName"></param>
public GPIReader(string fileName)
{
this.fileName = fileName;
this.Read();
this.LoopRecords(this.RootElement.Childs);
this.IfNoCats();
}
/// <summary>
/// Constructor (GPI File Reader)
/// </summary>
/// <param name="fileName"></param>
public GPIReader(string fileName, Add2LogProc Add2Log)
{
this.Add2Log = Add2Log;
this.fileName = fileName;
this.Read();
if (Add2Log != null) Add2Log(String.Format("POI File, version {0}",this.Version));
if (Add2Log != null) Add2Log("Reading References...");
this.LoopRecords(this.RootElement.Childs);
this.IfNoCats();
if (Add2Log != null) Add2Log("Reading Done");
}
private void IfNoCats()
{
if (this.Categories.Count > 0) return;
this.DefaultCategory.Category.Add(new KeyValuePair<string, string>("EN", "No Category"));
this.Categories.Add(0, this.DefaultCategory);
}
/// <summary>
/// Save File Content to KML file
/// </summary>
/// <param name="fileName"></param>
public void SaveToKML(string fileName)
{
SaveToKML(fileName, null);
}
/// <summary>
/// Save File Content to KML file
/// </summary>
/// <param name="fileName"></param>
public void SaveToKML(string fileName, Add2LogProc Add2Log)
{
if(Add2Log != null) this.Add2Log = Add2Log;
string images_file_dir = Path.GetDirectoryName(fileName) + @"\images\";
Directory.CreateDirectory(images_file_dir);
if (this.Add2Log != null) this.Add2Log("Saving to kml...");
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
sw.WriteLine("<kml><Document>");
string caption = (String.IsNullOrEmpty(this.Name) ? "GPI Has No Name" : this.Name);
if (!String.IsNullOrEmpty(this.DataSource)) caption = this.DataSource;
sw.WriteLine("<name><![CDATA[" + caption + "]]></name><createdby>KMZ Rebuilder GPI Reader</createdby>");
string desc = "Created: " + this.Created.ToString() + "\r\n";
foreach (KeyValuePair<string, string> langval in this.cDataSource)
desc += String.Format("data_source:{0}={1}\r\n", langval.Key.ToLower(), langval.Value);
foreach (KeyValuePair<string, string> langval in this.cCopyrights)
desc += String.Format("copyrights:{0}={1}\r\n", langval.Key.ToLower(), langval.Value);
sw.WriteLine("<description><![CDATA[" + desc + "]]></description>");
List<string> simstyles = new List<string>();
int ccount = 0;
foreach (KeyValuePair<ushort, RecCategory> kCat in this.Categories)
{
ccount++;
if (kCat.Value.Waypoints.Count == 0) continue;
if (this.Add2Log != null) this.Add2Log(String.Format("Saving {2} POIs of {0}/{1} Category...", ccount, this.Categories.Count, kCat.Value.Waypoints.Count));
string style = "catid" + kCat.Value.CategoryID.ToString();
if (kCat.Value.Bitmap != null) style = "imgid" + kCat.Value.Bitmap.BitmapID.ToString();
sw.WriteLine("<Folder><name><![CDATA[" + kCat.Value.Name + "]]></name>");
desc = "CategoryID: " + kCat.Value.CategoryID.ToString() + "\r\n";
desc += "Objects: " + kCat.Value.Waypoints.Count.ToString() + "\r\n";
if(GPIReader.SAVE_MULTINAMES)
foreach (KeyValuePair<string, string> langval in kCat.Value.Category)
desc += String.Format("name:{0}={1}\r\n", langval.Key.ToLower(), langval.Value);
if (kCat.Value.Comment != null)
foreach (KeyValuePair<string, string> langval in kCat.Value.Comment.Comment)
desc += String.Format("comm:{0}={1}\r\n", langval.Key.ToLower(), langval.Value);
if (kCat.Value.Contact != null)
{
if(!String.IsNullOrEmpty(kCat.Value.Contact.Phone))