-
Notifications
You must be signed in to change notification settings - Fork 3
/
MapViewerForm.cs
7791 lines (6959 loc) · 361 KB
/
MapViewerForm.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
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows;
namespace KMZRebuilder
{
public partial class ContentViewer : Form
{
private GetRouter groute = null;
private WaitingBoxForm wbf = null;
public NaviMapNet.MapLayer mapContent = null;
public NaviMapNet.MapLayer mapSelect = null;
public ToolTip mapTootTip = new ToolTip();
private KMZRebuilederForm parent = null;
private bool firstboot = true;
public NaviMapNet.MapLayer mapRoute = null;
public NaviMapNet.MapPoint mapRStart = null;
public NaviMapNet.MapPoint mapRFinish = null;
public NaviMapNet.MapPolyLine mapRVector = null;
private MultiPointRouteForm mapRMulti = null;
private string SASPlanetCacheDir = @"C:\Program Files\SASPlanet\cache\osmmapMapnik";
private string UserDefindedUrl = @"http://tile.openstreetmap.org/{z}/{x}/{y}.png";
private string UserDefindedFile = @"C:\nofile.mbtiles";
MruList mru1;
State state;
public ContentViewer(KMZRebuilederForm parent)
{
this.parent = parent;
Init();
PastInit();
LoadXUN();
}
public ContentViewer(KMZRebuilederForm parent, WaitingBoxForm waitBox)
{
this.parent = parent;
this.wbf = waitBox;
Init();
PastInit();
LoadXUN();
}
private void PastInit()
{
ToolStripMenuItem mi = new ToolStripMenuItem("Select None");
mi.Click += new EventHandler(selectNoneToolStripMenuItem_Click);
mi.ShortcutKeys = Keys.N | Keys.Control;
MapViewer.AddItemToDefaultMenu(mi);
MapViewer.AddItemToDefaultMenu(new ToolStripSeparator());
mi = new ToolStripMenuItem("Switch to Constructor Mode");
mi.Click += new EventHandler(mcme_Click);
mi.ShortcutKeys = Keys.F3;
MapViewer.AddItemToDefaultMenu(mi);
}
private List<XUN> xuns = new List<XUN>();
private void LoadXUN()
{
string fn = KMZRebuilederForm.CurrentDirectory() + @"\Map_Places.txt";
if (!File.Exists(fn)) return;
FileStream fs = new FileStream(fn, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(1251));
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
if (line.StartsWith("#")) continue;
if (line.StartsWith("@")) continue;
if (line.Length < 5) continue;
string[] xyn = line.Split(new char[] { ' ' }, 3);
try
{
double la = double.Parse(xyn[0], System.Globalization.CultureInfo.InvariantCulture);
double lo = double.Parse(xyn[1], System.Globalization.CultureInfo.InvariantCulture);
xuns.Add(new XUN(xyn[2], la, lo));
}
catch { };
};
sr.Close();
fs.Close();
}
public class XUN
{
public double lat;
public double lon;
public string nam;
public XUN(string nam, double lat, double lon)
{
this.lat = lat;
this.lon = lon;
this.nam = nam;
}
public override string ToString()
{
return String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} ({1:0.000000} {2:0.000000})", nam, lat, lon);
}
}
[Serializable]
public class MapStore
{
public string Name;
public string Url;
public string CacheDir;
public NaviMapNet.NaviMapNetViewer.MapServices Service = NaviMapNet.NaviMapNetViewer.MapServices.Custom_UserDefined;
public NaviMapNet.NaviMapNetViewer.ImageSourceTypes Source = NaviMapNet.NaviMapNetViewer.ImageSourceTypes.tiles;
public NaviMapNet.NaviMapNetViewer.ImageSourceProjections Projection = NaviMapNet.NaviMapNetViewer.ImageSourceProjections.EPSG3857;
public override string ToString()
{
return Name;
}
public MapStore() { }
public MapStore(string Name) { this.Name = Name; }
public MapStore(string Name, string Url, string Cache)
{
this.Name = Name;
this.Url = Url;
this.CacheDir = Cache;
}
public MapStore(string Name, string Url, NaviMapNet.NaviMapNetViewer.MapServices Service)
{
this.Name = Name;
this.Url = Url;
this.Service = Service;
}
}
private void Init()
{
InitializeComponent();
string fn = KMZRebuilederForm.CurrentDirectory() + @"\KMZRebuilder.stt";
if (File.Exists(fn)) state = State.Load(fn);
mru1 = new MruList(KMZRebuilederForm.CurrentDirectory()+@"\KMZRebuilder.drs", spcl, 10);
mru1.FileSelected += new MruList.FileSelectedEventHandler(mru1_FileSelected);
mapTootTip.ShowAlways = true;
mapRoute = new NaviMapNet.MapLayer("mapRoute");
MapViewer.MapLayers.Add(mapRoute);
mapSelect = new NaviMapNet.MapLayer("mapSelect");
MapViewer.MapLayers.Add(mapSelect);
mapContent = new NaviMapNet.MapLayer("mapContent");
MapViewer.MapLayers.Add(mapContent);
// LOAD NO MAP
iStorages.Items.Add(new MapStore("[[*** No Map ***]]", "", null));
// LOAD MAPS FROM FILE
string mf = KMZRebuilederForm.CurrentDirectory() + @"\KMZRebuilder.maps";
if (File.Exists(mf))
{
MapStore[] mss = XMLSaved<MapStore[]>.Load(mf);
if ((mss != null) && (mss.Length > 0))
iStorages.Items.AddRange(mss);
};
//iStorages.Items.Add("OSM Mapnik Render Tiles");
//iStorages.Items.Add("OSM OpenVkarte Render Tiles");
//iStorages.Items.Add("Wikimapia");
//iStorages.Items.Add("OpenTopoMaps");
//iStorages.Items.Add("Sputnik.ru");
//iStorages.Items.Add("RUMAP");
//iStorages.Items.Add("2GIS");
//iStorages.Items.Add("ArcGIS ESRI");
//iStorages.Items.Add("Nokia-Ovi");
//iStorages.Items.Add("OviMap");
//iStorages.Items.Add("OviMap Sputnik");
//iStorages.Items.Add("OviMap Relief");
//iStorages.Items.Add("OviMap Hybrid");
//iStorages.Items.Add("Kosmosnimki.ru ScanEx 1");
//iStorages.Items.Add("Kosmosnimki.ru ScanEx 2");
//iStorages.Items.Add("Kosmosnimki.ru IRS Sat");
//iStorages.Items.Add("Google Map");
//iStorages.Items.Add("Google Sat");
// LOAD USER-DEFINED MAPS
iStorages.Items.Add(new MapStore("[[*** MBTiles file ***]]", "", NaviMapNet.NaviMapNetViewer.MapServices.Custom_MBTiles));
iStorages.Items.Add(new MapStore("[[*** User-Defined Url ***]]", "", "URLDefined"));
iStorages.Items.Add(new MapStore("[[*** SAS Planet Cache ***]]", "", "SASPlanet"));
MapViewer.NotFoundTileColor = Color.LightYellow;
MapViewer.ImageSourceService = NaviMapNet.NaviMapNetViewer.MapServices.Custom_LocalFiles;
MapViewer.ImageSourceUrl = @"C:\Program Files\SASPlanet\cache\osmmapMapnik\";
MapViewer.WebRequestTimeout = 10000;
MapViewer.ZoomID = 10;
MapViewer.OnMapUpdate = new NaviMapNet.NaviMapNetViewer.MapEvent(MapUpdate);
MapViewer.UserDefinedGetTileUrl = new NaviMapNet.NaviMapNetViewer.GetTilePathCall(UserDefinedGetTileUrl);
//MapViewer.DrawMap = true;
//MapViewer.ReloadMap();
//iStorages.SelectedIndex = iStorages.Items.Count - 2;
if (state != null)
{
SASPlanetCacheDir = state.SASDir;
UserDefindedUrl = state.URL;
UserDefindedFile = state.FILE;
if (state.MapID < iStorages.Items.Count)
iStorages.SelectedIndex = state.MapID;
};
}
private void mru1_FileSelected(string file_name)
{
SASPlanetCacheDir = ClearLastSlash(file_name);
mru1.AddFile(SASPlanetCacheDir);
if (iStorages.SelectedIndex == (iStorages.Items.Count - 1))
iStorages_SelectedIndexChanged(this, null);
else
iStorages.SelectedIndex = iStorages.Items.Count - 1;
}
private string UserDefinedGetTileUrl(int x, int y, int z)
{
if (iStorages.SelectedIndex == (iStorages.Items.Count - 1))
return SASPlanetCache(x, y, z + 1);
return "";
}
private void iStorages_SelectedIndexChanged(object sender, EventArgs e)
{
MapStore iS = (MapStore)iStorages.SelectedItem;
MapViewer.ImageSourceService = iS.Service;
MapViewer.ImageSourceType = iS.Source;
MapViewer.ImageSourceProjection = iS.Projection;
if (iStorages.SelectedIndex < (iStorages.Items.Count - 1))
{
MapViewer.UseDiskCache = true;
MapViewer.UserDefinedMapName = iS.CacheDir;
if (iStorages.SelectedIndex == (iStorages.Items.Count - 2))
MapViewer.ImageSourceUrl = UserDefindedUrl;
else if (iStorages.SelectedIndex == (iStorages.Items.Count - 3))
{
MapViewer.UseDiskCache = false;
MapViewer.ImageSourceUrl = UserDefindedFile;
}
else
MapViewer.ImageSourceUrl = iS.Url;
};
if (iStorages.SelectedIndex == (iStorages.Items.Count - 1))
{
MapViewer.UseDiskCache = false;
MapViewer.UserDefinedMapName = iS.CacheDir = @"LOCAL\" + SASPlanetCacheDir.Substring(SASPlanetCacheDir.LastIndexOf(@"\") + 1);
MapViewer.ImageSourceUrl = SASPlanetCacheDir;
};
iStorages.Refresh();
MapViewer.ReloadMap();
}
private void MapUpdate()
{
string lreq = MapViewer.LastRequestedFile;
if (lreq.Length > 70) lreq = "... " + lreq.Substring(lreq.Length - 70);
toolStripStatusLabel1.Text = "Last Requested File: " + lreq;
toolStripStatusLabel2.Text = MapViewer.CenterDegreesLat.ToString().Replace(",", ".");
toolStripStatusLabel3.Text = MapViewer.CenterDegreesLon.ToString().Replace(",", ".");
string regNm = "...";
if (MapViewer.ZoomID > 7)
{
int regNo = KMZRebuilederForm.PIRU.PointInRegion(MapViewer.CenterDegreesY, MapViewer.CenterDegreesX);
regNm = regNo > 0 ? KMZRebuilederForm.PIRU.GetRegionName(regNo) : "...";
};
RegName.Text = regNm;
}
private Timer mmTimer = null;
private bool locate = false;
private void MapViewer_MouseMove(object sender, MouseEventArgs e)
{
locate = false;
if (e.Button != MouseButtons.None) mapTootTip.Hide(this);
if (mmTimer != null)
mmTimer.Enabled = false;
else
{
mmTimer = new Timer();
mmTimer.Interval = 800;
mmTimer.Tick += new EventHandler(mmTimer_Tick);
};
mmTimer.Start();
PointF m = MapViewer.MousePositionDegrees;
toolStripStatusLabel4.Text = m.Y.ToString().Replace(",", ".");
toolStripStatusLabel5.Text = m.X.ToString().Replace(",", ".");
}
private void mmTimer_Tick(object sender, EventArgs e)
{
mmTimer.Enabled = false;
if (mapContent.ObjectsCount == 0) return;
try
{
Point f = this.PointToScreen(new Point(0, 0));
Point p = Cursor.Position;
Point s = new Point(p.X - f.X, p.Y - f.Y);
Point current = MapViewer.MousePositionPixels;
PointF sCenter = MapViewer.PixelsToDegrees(current);
PointF sFrom = MapViewer.PixelsToDegrees(new Point(current.X - 5, current.Y + 5));
PointF sTo = MapViewer.PixelsToDegrees(new Point(current.X + 5, current.Y - 5));
NaviMapNet.MapObject[] objs = mapContent.Select(new RectangleF(sFrom, new SizeF(sTo.X - sFrom.X, sTo.Y - sTo.X)));
if ((objs != null) && (objs.Length > 0))
{
uint len = uint.MaxValue;
int ind = 0;
for (int i = 0; i < objs.Length; i++)
{
uint tl = GetLengthMetersC(sCenter.Y, sCenter.X, objs[i].Center.Y, objs[i].Center.X, false);
if (tl < len) { len = tl; ind = i; };
};
mapTootTip.Show(objs[ind].Name, this, s.X, s.Y, 5000);
}
else
mapTootTip.Hide(this);
}
catch { };
}
private void objects_DoubleClick(object sender, EventArgs e)
{
if (objects.SelectedItems.Count == 0) return;
NaviMapNet.MapObject mo = mapContent[objects.SelectedIndices[0]];
if ((mo is NaviMapNet.MapPolyLine) || (mo is NaviMapNet.MapPolygon))
{
if (mo is NaviMapNet.MapPolyLine)
{
if ((mo.Bounds.Width > MapViewer.MapBoundsRectDegrees.Width) || (mo.Bounds.Height > MapViewer.MapBoundsRectDegrees.Height))
MapViewer.CenterDegrees = mo.Points[0];
else
MapViewer.ZoomByArea((mo as NaviMapNet.MapPolyLine).Bounds, MapViewer.ZoomID);
}
else
{
byte nextZoom = MapViewer.ZoomID;
if ((mo.Bounds.Width > MapViewer.MapBoundsRectDegrees.Width) || (mo.Bounds.Height > MapViewer.MapBoundsRectDegrees.Height))
{
int pow = (int)Math.Round(Math.Max(mo.Bounds.Width / MapViewer.MapBoundsRectDegrees.Width, mo.Bounds.Height / MapViewer.MapBoundsRectDegrees.Height));
nextZoom = (byte)(nextZoom - pow);
if (nextZoom < 2) nextZoom = 2;
if (nextZoom > 20) nextZoom = 2;
};
MapViewer.ZoomByArea((mo as NaviMapNet.MapPolygon).Bounds, nextZoom);
};
}
else
{
double[] b = MapViewer.MapBoundsMinMaxDegrees;
if((mo.Points[0].X < b[0]) || (mo.Points[0].Y < b[1]) || (mo.Points[0].X > b[2]) || (mo.Points[0].Y > b[3]))
MapViewer.CenterDegrees = mo.Points[0];
};
SelectOnMap(objects.SelectedIndices[0]);
}
Dictionary<string, string> style2image = new Dictionary<string, string>();
private int prev_selected = -1;
private void laySelect_SelectedIndexChanged(object sender, EventArgs e)
{
List<int> selected_to_del = new List<int>();
if(prev_selected == laySelect.SelectedIndex)
{
if (objects.Items.Count > 0)
for (int i = 0; i < objects.Items.Count; i++)
if (objects.Items[i].SubItems[6].Text == "Yes")
selected_to_del.Add(i);
}
else
mapSelect.Clear();
prev_selected = laySelect.SelectedIndex;
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
System.Globalization.NumberFormatInfo ni = (System.Globalization.NumberFormatInfo)ci.NumberFormat.Clone();
ni.NumberDecimalSeparator = ".";
images.Images.Clear();
objects.Items.Clear();
mapContent.Clear();
Hashtable imList = new Hashtable();
if (true)
{
KMLayer l = (KMLayer)parent.kmzLayers.Items[laySelect.SelectedIndex];
XmlNode xn = l.file.kmlDoc.SelectNodes("kml/Document/Folder")[l.id];
int el_line = 0;
int el_polygon = 0;
int el_point = 0;
XmlNodeList xnf = xn.SelectNodes("Placemark");
if (xnf.Count > 0)
for (int el = 0; el < xnf.Count; el++)
{
if (el % 100 == 0)
{
toolStripStatusLabel1.Text = String.Format("Loading {0} of {1} placemarks", el, xnf.Count);
statusStrip2.Refresh();
};
if ((wbf != null) && (el % 100 == 0)) wbf.Text = String.Format("Loading {0} of {1} placemarks", el, xnf.Count);
if (xnf[el].ChildNodes.Count == 0) continue;
if (xnf[el].SelectNodes("LineString").Count > 0) // ++LINE
{
XmlNode xnn = xnf[el].SelectNodes("LineString/coordinates")[0];
string[] llza = xnn.ChildNodes[0].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string name = "NoName";
try { name = xnn.ParentNode.ParentNode.SelectSingleNode("name").ChildNodes[0].Value; }
catch { };
string description = "";
try { description = xnn.ParentNode.ParentNode.SelectSingleNode("description").ChildNodes[0].Value; }
catch { };
string styleUrl = "";
if (xnn.ParentNode.ParentNode.SelectSingleNode("styleUrl") != null) styleUrl = xnn.ParentNode.ParentNode.SelectSingleNode("styleUrl").ChildNodes[0].Value;
if (styleUrl.IndexOf("#") == 0) styleUrl = styleUrl.Remove(0, 1);
Color lineColor = Color.FromArgb(255, Color.Blue);
int lineWidth = 3;
XmlNode sn = null;
if (styleUrl != "")
{
string firstsid = styleUrl;
XmlNodeList pk = l.file.kmlDoc.SelectNodes("kml/Document/StyleMap[@id='" + styleUrl + "']/Pair/key");
if (pk.Count > 0)
for (int n = 0; n < pk.Count; n++)
{
XmlNode cn = pk[n];
if ((cn.ChildNodes[0].Value != "normal") && (n > 0)) continue;
if (cn.ParentNode.SelectSingleNode("styleUrl") == null) continue;
firstsid = cn.ParentNode.SelectSingleNode("styleUrl").ChildNodes[0].Value;
if (firstsid.IndexOf("#") == 0) firstsid = firstsid.Remove(0, 1);
};
try
{
sn = l.file.kmlDoc.SelectSingleNode("kml/Document/Style[@id='" + firstsid + "']/LineStyle");
}
catch { };
}
else
sn = xnn.ParentNode.ParentNode.SelectSingleNode("Style/LineStyle");
if (sn != null)
{
string colval = sn.SelectSingleNode("color").ChildNodes[0].Value;
try
{
lineColor = Color.FromName(colval);
if (colval.Length == 8)
{
lineColor = Color.FromArgb(
Convert.ToInt32(colval.Substring(0, 2), 16),
Convert.ToInt32(colval.Substring(6, 2), 16),
Convert.ToInt32(colval.Substring(4, 2), 16),
Convert.ToInt32(colval.Substring(2, 2), 16)
);
};
}
catch { };
string widval = sn.SelectSingleNode("width").ChildNodes[0].Value;
try
{
lineWidth = (int)double.Parse(widval, ni);
if (lineWidth < 3) lineWidth = 3;
}
catch { };
};
List<PointF> xy = new List<PointF>();
foreach (string llzix in llza)
{
string llzi = llzix.Trim('\r').Trim('\n');
if (String.IsNullOrEmpty(llzi)) continue;
string[] llz = llzi.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
xy.Add(new PointF(float.Parse(llz[0], ni), float.Parse(llz[1], ni)));
};
NaviMapNet.MapPolyLine ml = new NaviMapNet.MapPolyLine(xy.ToArray());
ml.Name = name;
ml.UserData = description;
ml.Color = lineColor;
ml.Width = lineWidth;
Image im = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(im);
g.FillRectangle(new SolidBrush(lineColor), 0, 0, 16, 16);
g.DrawString("L", new Font("Terminal", 11, FontStyle.Bold), new SolidBrush(Color.FromArgb(255 - lineColor.R, 255 - lineColor.G, 255 - lineColor.B)), 1, -1);
g.Dispose();
images.Images.Add(im);
if (l.file.DrawEvenSizeIsTooSmall) ml.DrawEvenSizeIsTooSmall = true;
mapContent.Add(ml);
ListViewItem lvi = objects.Items.Add(ml.Name, images.Images.Count - 1);
lvi.SubItems.Add("Line (" + ml.PointsCount.ToString() + " points)");
lvi.SubItems.Add(ml.Points[0].Y.ToString(System.Globalization.CultureInfo.InvariantCulture));
lvi.SubItems.Add(ml.Points[0].X.ToString(System.Globalization.CultureInfo.InvariantCulture));
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("Placemark/LineString/coordinates[" + el_line.ToString() + "]");
if (((el_point + el_polygon + el_line) == 0) && firstboot) MapViewer.CenterDegrees = ml.Points[0];
if (selected_to_del.IndexOf(lvi.Index) >= 0)
{
lvi.SubItems[6].Text = "Yes";
lvi.Font = new Font(lvi.Font, FontStyle.Strikeout);
mapContent[lvi.Index].Visible = false;
};
el_line++;
}; // --LINE
if (xnf[el].SelectNodes("Polygon").Count > 0) // ++Polygon
{
XmlNode xnn = xnf[el].SelectNodes("Polygon/outerBoundaryIs/LinearRing/coordinates")[0];
string[] llza = xnn.ChildNodes[0].Value.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
string name = "NoName";
try { name = xnn.ParentNode.ParentNode.ParentNode.ParentNode.SelectSingleNode("name").ChildNodes[0].Value; }
catch { };
string description = "";
try { description = xnn.ParentNode.ParentNode.ParentNode.ParentNode.SelectSingleNode("description").ChildNodes[0].Value; }
catch { };
string styleUrl = "";
if (xnn.ParentNode.ParentNode.ParentNode.ParentNode.SelectSingleNode("styleUrl") != null) styleUrl = xnn.ParentNode.ParentNode.ParentNode.ParentNode.SelectSingleNode("styleUrl").ChildNodes[0].Value;
if (styleUrl.IndexOf("#") == 0) styleUrl = styleUrl.Remove(0, 1);
Color lineColor = Color.FromArgb(255, Color.Blue);
int lineWidth = 3;
Color fillColor = Color.FromArgb(255, Color.Blue);
int fill = 1;
XmlNode sl = null;
XmlNode sf = null;
if (styleUrl != "")
{
string firstsid = styleUrl;
XmlNodeList pk = l.file.kmlDoc.SelectNodes("kml/Document/StyleMap[@id='" + styleUrl + "']/Pair/key");
if (pk.Count > 0)
for (int n = 0; n < pk.Count; n++)
{
XmlNode cn = pk[n];
if ((cn.ChildNodes[0].Value != "normal") && (n > 0)) continue;
if (cn.ParentNode.SelectSingleNode("styleUrl") == null) continue;
firstsid = cn.ParentNode.SelectSingleNode("styleUrl").ChildNodes[0].Value;
if (firstsid.IndexOf("#") == 0) firstsid = firstsid.Remove(0, 1);
};
try
{
sl = l.file.kmlDoc.SelectSingleNode("kml/Document/Style[@id='" + firstsid + "']/LineStyle");
}
catch { };
try
{
sf = l.file.kmlDoc.SelectSingleNode("kml/Document/Style[@id='" + firstsid + "']/PolyStyle");
}
catch { };
}
else
{
sl = xnn.ParentNode.ParentNode.SelectSingleNode("Style/LineStyle");
sf = xnn.ParentNode.ParentNode.SelectSingleNode("Style/PolyStyle");
};
if (sl != null)
{
string colval = sl.SelectSingleNode("color").ChildNodes[0].Value;
try
{
lineColor = Color.FromName(colval);
if (colval.Length == 8)
{
lineColor = Color.FromArgb(
Convert.ToInt32(colval.Substring(0, 2), 16),
Convert.ToInt32(colval.Substring(6, 2), 16),
Convert.ToInt32(colval.Substring(4, 2), 16),
Convert.ToInt32(colval.Substring(2, 2), 16)
);
};
}
catch { };
string widval = sl.SelectSingleNode("width").ChildNodes[0].Value;
try
{
lineWidth = (int)double.Parse(widval, ni);
if (lineWidth < 2)
lineWidth = 2;
}
catch { };
};
if (sf != null)
{
string colval = sf.SelectSingleNode("color").ChildNodes[0].Value;
try
{
fillColor = Color.FromName(colval);
if (colval.Length == 8)
{
fillColor = Color.FromArgb(
Convert.ToInt32(colval.Substring(0, 2), 16),
Convert.ToInt32(colval.Substring(6, 2), 16),
Convert.ToInt32(colval.Substring(4, 2), 16),
Convert.ToInt32(colval.Substring(2, 2), 16)
);
};
}
catch { };
string fillval = sf.SelectSingleNode("fill").ChildNodes[0].Value;
try
{
fill = int.Parse(fillval, ni);
}
catch { };
};
List<PointF> xy = new List<PointF>();
foreach (string llzix in llza)
{
string llzi = llzix.Trim('\r').Trim('\n');
if (String.IsNullOrEmpty(llzi)) continue;
string[] llz = llzi.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
xy.Add(new PointF(float.Parse(llz[0], ni), float.Parse(llz[1], ni)));
};
NaviMapNet.MapPolygon mp = new NaviMapNet.MapPolygon(xy.ToArray());
mp.Name = name;
mp.UserData = description;
mp.BorderColor = lineColor;
mp.Width = lineWidth;
mp.BodyColor = Color.FromArgb(0, fillColor);
if (fill != 0)
mp.BodyColor = fillColor;
Image im = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(im);
g.FillRectangle(new SolidBrush(fillColor), 0, 0, 16, 16);
g.DrawRectangle(new Pen(new SolidBrush(lineColor), 2), 0, 0, 16, 16);
g.DrawString("A", new Font("Terminal", 11, FontStyle.Bold), new SolidBrush(Color.FromArgb(255 - fillColor.R, 255 - fillColor.G, 255 - fillColor.B)), 1, -1);
g.Dispose();
images.Images.Add(im);
if (l.file.DrawEvenSizeIsTooSmall) mp.DrawEvenSizeIsTooSmall = true;
mapContent.Add(mp);
ListViewItem lvi = objects.Items.Add(mp.Name, images.Images.Count - 1);
lvi.SubItems.Add("Polygon (" + mp.PointsCount.ToString() + " points)");
lvi.SubItems.Add(mp.Center.Y.ToString(System.Globalization.CultureInfo.InvariantCulture));
lvi.SubItems.Add(mp.Center.X.ToString(System.Globalization.CultureInfo.InvariantCulture));
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("Placemark/Polygon/outerBoundaryIs/LinearRing/coordinates[" + el_polygon.ToString() + "]");
if (((el_point + el_polygon + el_line) == 0) && firstboot) MapViewer.CenterDegrees = mp.Center;
if (selected_to_del.IndexOf(lvi.Index) >= 0)
{
lvi.SubItems[6].Text = "Yes";
lvi.Font = new Font(lvi.Font, FontStyle.Strikeout);
mapContent[lvi.Index].Visible = false;
};
el_polygon++;
}; // --Polygon
if (xnf[el].SelectNodes("Point").Count > 0) // ++Point
{
XmlNode xnn = xnf[el].SelectNodes("Point/coordinates")[0];
string[] llz = xnn.ChildNodes[0].Value.Replace("\r", "").Replace("\n", "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
string name = "NoName";
try { name = xnn.ParentNode.ParentNode.SelectSingleNode("name").ChildNodes[0].Value; }
catch { };
string description = "";
try { description = xnn.ParentNode.ParentNode.SelectSingleNode("description").ChildNodes[0].Value; }
catch { };
string styleUrl = "";
string href = "";
try
{
if (xnn.ParentNode.ParentNode.SelectSingleNode("styleUrl") != null) styleUrl = xnn.ParentNode.ParentNode.SelectSingleNode("styleUrl").ChildNodes[0].Value;
if (styleUrl.IndexOf("#") == 0) styleUrl = styleUrl.Remove(0, 1);
}
catch { };
if (styleUrl != "")
{
string firstsid = styleUrl;
XmlNodeList pk = l.file.kmlDoc.SelectNodes("kml/Document/StyleMap[@id='" + styleUrl + "']/Pair/key");
if (pk.Count > 0)
for (int n = 0; n < pk.Count; n++)
{
XmlNode cn = pk[n];
if ((cn.ChildNodes[0].Value != "normal") && (n > 0)) continue;
if (cn.ParentNode.SelectSingleNode("styleUrl") == null) continue;
firstsid = cn.ParentNode.SelectSingleNode("styleUrl").ChildNodes[0].Value;
if (firstsid.IndexOf("#") == 0) firstsid = firstsid.Remove(0, 1);
};
try
{
XmlNode nts = l.file.kmlDoc.SelectSingleNode("kml/Document/Style[@id='" + firstsid + "']/IconStyle/Icon/href");
href = nts.ChildNodes[0].Value;
if (!style2image.ContainsKey("#" + firstsid))
style2image.Add("#" + firstsid, href);
}
catch { };
};
NaviMapNet.MapPoint mp = new NaviMapNet.MapPoint(double.Parse(llz[1], ni), double.Parse(llz[0], ni));
mp.Name = name;
mp.UserData = description;
mp.SizePixels = new Size(16, 16);
int ii = -1;
if (imList.ContainsKey(href))
ii = (int)imList[href];
else
{
if (href == "")
imList.Add(href, -1);
else
{
Image im = null;
if (Uri.IsWellFormedUriString(href, UriKind.Absolute))
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(href);
try
{
using (System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
im = Bitmap.FromStream(stream);
}
catch
{ im = null; };
}
else
{
try { im = Image.FromFile(l.file.tmp_file_dir + href); }
catch { im = null; };
};
if (im != null)
{
images.Images.Add(href, (Image)new Bitmap(im));
im.Dispose();
imList.Add(href, ii = images.Images.Count - 1);
}
else
imList.Add(href, ii = -1);
};
};
if (ii >= 0)
{
mp.Color = Color.Transparent;
mp.Squared = true;
mp.Img = images.Images[ii];
}
else
{
mp.Color = Color.Purple;
mp.Squared = false;
};
mapContent.Add(mp);
ListViewItem lvi = objects.Items.Add(String.Format("{0}", mp.Name, mp.Center.Y.ToString(System.Globalization.CultureInfo.InvariantCulture), mp.Center.X.ToString(System.Globalization.CultureInfo.InvariantCulture)), ii);
lvi.SubItems.Add("Point");
lvi.SubItems.Add(mp.Center.Y.ToString(System.Globalization.CultureInfo.InvariantCulture));
lvi.SubItems.Add(mp.Center.X.ToString(System.Globalization.CultureInfo.InvariantCulture));
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("Placemark/Point/coordinates[" + el_point.ToString() + "]");
if (((el_point + el_polygon + el_line) == 0) && firstboot) MapViewer.CenterDegrees = mp.Center;
if (selected_to_del.IndexOf(lvi.Index) >= 0)
{
lvi.SubItems[6].Text = "Yes";
lvi.Font = new Font(lvi.Font, FontStyle.Strikeout);
mapContent[lvi.Index].Visible = false;
};
el_point++;
}; // --Point
};
};
toolStripStatusLabel1.Text = "All placemarks loaded";
statusStrip2.Refresh();
NPB.Enabled = false;
NNB.Enabled = false;
laySelect.Enabled = selected_to_del.Count == 0;
MapViewer.DrawOnMapData();
firstboot = false;
UpdateCheckedAndMarked(true);
}
private void FindCopies(int toIndex, bool xy4, bool nm5, Single distanceInMeters)
{
if (objects.Items.Count == 0) return;
Color[] colors = new Color[] {
Color.LightBlue, Color.LightCoral, Color.LightCyan, Color.LightGray, Color.LightGreen,
Color.LightPink, Color.LightSalmon, Color.LightSeaGreen, Color.LightSkyBlue, Color.LightSteelBlue,
Color.LightYellow, Color.Lime, Color.LimeGreen, Color.Orange, Color.OrangeRed,
Color.Pink, Color.RoyalBlue, Color.SeaGreen, Color.SeaShell, Color.SkyBlue,
Color.Tan, Color.YellowGreen};
int simIndex = 0;
Dictionary<string,int[]> copies = new Dictionary<string,int[]>(); // all combinations store
for (int i = 0; i < objects.Items.Count; i++)
{
objects.Items[i].BackColor = objects.BackColor;
objects.Items[i].SubItems[4].Text = "";
objects.Items[i].SubItems[5].Text = "";
};
int iFrom = 0;
int iTo = mapContent.ObjectsCount - 1;
if (toIndex >= 0) { iFrom = toIndex; iTo = toIndex; };
for (int i = iFrom; i <= iTo; i++)
for(int j=0;j<mapContent.ObjectsCount;j++)
if (i != j)
{
NaviMapNet.MapObject a = mapContent[i];
NaviMapNet.MapObject b = mapContent[j];
bool same = (!nm5) || (a.Name.Trim().ToLower() == b.Name.Trim().ToLower());
if (xy4 && same)
{
same = false;
if (a.PointsCount == b.PointsCount)
{
same = true;
for (int n = 0; n < a.PointsCount; n++)
{
if (distanceInMeters <= 0)
{
if (a.Points[n].X != b.Points[n].X) { same = false; break; };
if (a.Points[n].Y != b.Points[n].Y) { same = false; break; }
}
else
{
float dist = Utils.GetLengthMeters(a.Points[n].Y, a.Points[n].X, b.Points[n].Y, b.Points[n].X, false);
if (dist > distanceInMeters)
{ same = false; break; };
};
};
};
};
string key = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0},{1}", a.Center.X, a.Center.Y);
if (nm5) key = a.Name.Trim().ToLower();
if (same)
{
int fex = -1;
int sex = -1;
if (copies.Count > 0)
foreach (KeyValuePair<string, int[]> kpv in copies)
{
if (Array.IndexOf<int>(kpv.Value, i) >= 0) fex = kpv.Value[0];
if (Array.IndexOf<int>(kpv.Value, j) >= 0) sex = kpv.Value[0];
};
if ((fex >= 0) && (sex >= 0) && (fex == sex))
continue; // combination exists
if (!copies.ContainsKey(key)) copies.Add(key, new int[] { simIndex++ }); // create new combination or add to existing
List<int> val = new List<int>();
val.AddRange(copies[key]);
if (val.IndexOf(i, 1) < 0) val.Add(i);
if (val.IndexOf(j, 1) < 0) val.Add(j);
copies[key] = val.ToArray();
int colIndex = val[0] % colors.Length;
objects.Items[i].BackColor = colors[colIndex];
objects.Items[j].BackColor = colors[colIndex];
if (nm5)
{
objects.Items[i].SubItems[5].Text = val[0].ToString();
objects.Items[j].SubItems[5].Text = val[0].ToString();
};
if (xy4)
{
if(objects.Items[i].SubItems[4].Text == "")
objects.Items[i].SubItems[4].Text = val[0].ToString();
if(objects.Items[j].SubItems[4].Text == "")
objects.Items[j].SubItems[4].Text = val[0].ToString();
};
};
};
status.Text = "";
int ttl = 0;
if (copies.Count > 0)
{
NPB.Enabled = xy4;
NNB.Enabled = nm5;
foreach (KeyValuePair<string, int[]> kpv in copies)
ttl += (kpv.Value.Length - 2);
if (xy4 && nm5)
status.Text += "Found " + ttl.ToString() + " combinations for " + copies.Count.ToString() + " placemarks\r\n";
else if (xy4)
status.Text += "Found " + ttl.ToString() + " combinations for " + copies.Count.ToString() + " placemarks by coordinates\r\n";
else if (nm5)
status.Text += "Found " + ttl.ToString() + " combinations for " + copies.Count.ToString() + " placemarks by name\r\n";
if (ttl > objects.Items.Count)
status.Text += "Too many combinations! You must use less search radius!\r\n";
}
else
{
status.Text = "No copies found";
NPB.Enabled = false;
NNB.Enabled = false;
};
status.SelectionStart = status.TextLength;
status.ScrollToCaret();
}
private void MapViewer_MouseClick(object sender, MouseEventArgs e)
{
if (!locate)
return;
Point clicked = MapViewer.MousePositionPixels;
PointF sCenter = MapViewer.PixelsToDegrees(clicked);
if (mapContent.ObjectsCount == 0)
{
SubClick(sCenter, null);
return;
};
PointF sFrom = MapViewer.PixelsToDegrees(new Point(clicked.X - 5, clicked.Y + 5));
PointF sTo = MapViewer.PixelsToDegrees(new Point(clicked.X + 5, clicked.Y - 5));
NaviMapNet.MapObject[] objs = mapContent.Select(new RectangleF(sFrom, new SizeF(sTo.X - sFrom.X, sTo.Y - sFrom.Y)), NaviMapNet.MapObjectType.mEllipse | NaviMapNet.MapObjectType.mLine | NaviMapNet.MapObjectType.mPoint | NaviMapNet.MapObjectType.mPolygon | NaviMapNet.MapObjectType.mPolyline, true, false);
if ((objs != null) && (objs.Length > 0))
{
uint len = uint.MaxValue;
int ind = 0;
for (int i = 0; i < objs.Length; i++)
{
uint tl = GetLengthMetersC(sCenter.Y, sCenter.X, objs[i].Center.Y, objs[i].Center.X, false);
if (tl < len) { len = tl; ind = i; };
};
if ((objects.SelectedIndices.Count == 0) || (objects.SelectedIndices[0] != objs[ind].Index))
{
objects.Items[objs[ind].Index].Selected = true;
objects.Items[objs[ind].Index].Focused = true;
};
SelectOnMap(objs[ind].Index);
if(objs[ind].PointsCount == 1)
SubClick(new PointF(objs[ind].Center.X, objs[ind].Center.Y),objs[ind].Name);
else
SubClick(sCenter, null);
}