-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackControl.cs
1376 lines (1262 loc) · 54 KB
/
TrackControl.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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace GPX
{
public partial class TrackControl : UserControl
{
#region Variables and Properties
private GMap.NET.Internals.Core _Core;
private DateTime _LastDrawTime;
private bool _DrawPending;
private Timer timer1;
bool _UseOffset;
private int _LeftOffset;
private int _TopOffset;
//for stupid Windows 7 (flickering of tooltips)
private int _LastMouseX;
private int _LastMouseY;
private List<Track> _Tracks;
public List<Track> Tracks
{
get { return _Tracks; }
set
{
_Tracks = value;
CreateWaypointsList();
}
}
private List<WayPointOnMap> _PointsOfInterest;
public void SetPointsOfInterest(List<WayPoint> inputList)
{
if (inputList == null)
{
_PointsOfInterest = null;
}
else
{
_PointsOfInterest = new List<WayPointOnMap>();
foreach (WayPoint wp in inputList)
{
_PointsOfInterest.Add(new WayPointOnMap(wp));
}
}
}
private DrawCategory _XCategory;
public DrawCategory XCategory
{
get { return _XCategory; }
set { _XCategory = value; }
}
private DrawCategory _YCategory;
public DrawCategory YCategory
{
get { return _YCategory; }
set { _YCategory = value; }
}
private bool _UseEqualScale;
public bool UseEqualScale
{
get { return _UseEqualScale; }
set { _UseEqualScale = value; }
}
private float _Zoom;
public float Zoom
{
get { return _Zoom; }
set { _Zoom = value; }
}
private bool _DrawWaypoints;
public bool DrawWaypoints
{
get { return _DrawWaypoints; }
set { _DrawWaypoints = value; }
}
private bool _DrawTrackLine;
public bool DrawTrackLine
{
get { return _DrawTrackLine; }
set { _DrawTrackLine = value; }
}
private WaypointsStyle _StyleForWaypoints;
public WaypointsStyle StyleForWaypoints
{
get { return _StyleForWaypoints; }
set { _StyleForWaypoints = value; }
}
private WaypointsStyle _StyleForPoI;
public WaypointsStyle StyleForPoI
{
get { return _StyleForPoI; }
set { _StyleForPoI = value; }
}
private bool _ShowAllPointsOfSegment;
public bool ShowAllPointsOfSegment
{
get { return _ShowAllPointsOfSegment; }
set { _ShowAllPointsOfSegment = value; }
}
private int _IndexOfFirstPointShown;
public int IndexOfFirstPointShown
{
get { return _IndexOfFirstPointShown; }
set { _IndexOfFirstPointShown = value; }
}
private int _IndexOfLastPointShown;
public int IndexOfLastPointShown
{
get { return _IndexOfLastPointShown; }
set { _IndexOfLastPointShown = value; }
}
private bool _ShowMap;
public bool ShowMap
{
get { return _ShowMap; }
set { _ShowMap = value; }
}
private GMap.NET.MapType _MapType;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public GMap.NET.MapType MapType
{
get { return _MapType; }
set { _MapType = value; }
}
private Color _WayPointsColor = Color.BlanchedAlmond;
public Color WayPointsColor
{
get { return _WayPointsColor; }
set { _WayPointsColor = value; }
}
private Color _WayPointsHighlight = Color.Gold;
public Color WayPointsHighlight
{
get { return _WayPointsHighlight; }
set { _WayPointsHighlight = value; }
}
private Color _PoIColor = Color.DarkGreen;
public Color PoIColor
{
get { return _PoIColor; }
set { _PoIColor = value; }
}
private Color _TrackColor = Color.Red;
public Color TrackColor
{
get { return _TrackColor; }
set { _TrackColor = value; }
}
private Color _TrackColorHighlight = Color.Goldenrod;
public Color TrackColorHighlight
{
get { return _TrackColorHighlight; }
set { _TrackColorHighlight = value; }
}
private float _TrackThickness = 1.0f;
public float TrackThickness
{
get { return _TrackThickness; }
set { _TrackThickness = value; }
}
private WayPointOnMap _SelectedWayPointOnMap;
private List<WayPointOnMap> _SelectedWayPointsOnMap;
private static Color _PhotoFrameColor = Color.Aqua;
public static Color PhotoFrameColor
{
get { return _PhotoFrameColor; }
set { _PhotoFrameColor = value; }
}
private static Color _PhotoFrameColorHighLight = Color.OrangeRed;
public static Color PhotoFrameColorHighLight
{
get { return _PhotoFrameColorHighLight; }
set { _PhotoFrameColorHighLight = value; }
}
private DateTime _CacheDate;
public DateTime CacheDate
{
get { return _CacheDate; }
set { _CacheDate = value; }
}
private List<WayPointOnMap> _WayPointsOnMap;
private int _LeftMargin = 40;
private int _LowerMargin = 12;
private int _TopMargin = 5;
private Bitmap _Bitmap;
private Point _SelectionStart;
private Point _SelectionEnd;
private bool _Selecting;
private double _XMax;
private double _YMax;
private double _XMin;
private double _YMin;
private double _XFactor;
private double _YFactor;
private bool _DrawPointsOfInterest;
public bool DrawPointsOfInterest
{
get { return _DrawPointsOfInterest; }
set { _DrawPointsOfInterest = value; }
}
#endregion Variables and Properties
#region Events
public delegate void WayPointSelectedEventHandler(object sender, WayPointSelectedEventArgs wpArgs);
public event WayPointSelectedEventHandler WayPointSelected;
#endregion Events
private System.Resources.ResourceManager ResourceManager
{
get { return GPXAppResourceManager.ResourceManager; }
}
public TrackControl()
{
InitializeComponent();
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
return;
_Bitmap = null;
_Zoom = 1;
_XCategory = DrawCategory.LongitudeKm;
_YCategory = DrawCategory.LatitudeKm;
_UseEqualScale = false;
_Selecting = false;
_DrawWaypoints = false;
_DrawTrackLine = false;
_ShowAllPointsOfSegment = true;
_IndexOfFirstPointShown = 1;
_IndexOfLastPointShown = 100000;
//For maps
{
timer1 = new Timer(this.components);
timer1.Interval = 2500;
timer1.Tick += new System.EventHandler(this.timer1_Tick);
GMap.NET.CacheProviders.FilePureImageCache imageCache = new GMap.NET.CacheProviders.FilePureImageCache();
string cacheLocation = Defaults.GMapsFileCacheLocation;
imageCache.CacheLocation = cacheLocation;
GMap.NET.Internals.Cache.Instance.ImageCache = imageCache;
GMap.NET.GMaps.Instance.ImageProxy = new GMap.NET.WindowsForms.WindowsFormsImageProxy();
GMap.NET.GMaps.Instance.UseGeocoderCache = true;
GMap.NET.GMaps.Instance.UseMemoryCache = true;
GMap.NET.GMaps.Instance.UsePlacemarkCache = true;
_Core = GMap.NET.Internals.Core.Instance;
_Core.MapType = GMap.NET.MapType.GoogleSatellite;
_Core.Projection = new GMap.NET.Projections.MercatorProjection();
_Core.OnNeedInvalidation += new GMap.NET.NeedInvalidation(Core_OnNeedInvalidation);
_Core.StartSystem();
}
}
private List<TrackOnMap> _TracksOnMap;
private void CreateWaypointsList()
{
if (_Tracks == null)
{
_WayPointsOnMap = null;
_TracksOnMap = null;
}
else
{
_WayPointsOnMap = new List<WayPointOnMap>();
_TracksOnMap = new List<TrackOnMap>();
foreach (Track track in _Tracks)
{
TrackOnMap tom = new TrackOnMap();
_TracksOnMap.Add(tom);
foreach (TrackSegment seg in track.TrackSegments)
{
foreach (WayPoint wp in seg.WayPoints)
{
WayPointOnMap wop = new WayPointOnMap(wp);
_WayPointsOnMap.Add(wop);
tom.WayPoints.Add(wop);
}
}
}
}
}
public void Draw()
{
if ((_Tracks == null) || (_Tracks.Count == 0))
{
//GPX_TrackControl_Draw0=The TrackSegment to be drawn was either not set or is empty.
throw new ApplicationException(ResourceManager.GetString("GPX_TrackControl_Draw0"));
}
if ((_WayPointsOnMap == null) || (_TracksOnMap.Count != _Tracks.Count))
CreateWaypointsList();
//determine applicable values
//get maximum and minimum for x and y values each
_XMax = double.MinValue;
_YMax = double.MinValue;
_XMin = double.MaxValue;
_YMin = double.MaxValue;
foreach (WayPointOnMap wpm in _WayPointsOnMap)
{
//Selected value: latitude, longitude, elevation, time, what ever....
wpm.XValue = GetSelectedValueFromWaypoint(wpm.WayPoint, _XCategory);
wpm.YValue = GetSelectedValueFromWaypoint(wpm.WayPoint, _YCategory);
_XMax = (_XMax < wpm.XValue) ? wpm.XValue : _XMax;
_YMax = (_YMax < wpm.YValue) ? wpm.YValue : _YMax;
_XMin = (_XMin > wpm.XValue) ? wpm.XValue : _XMin;
_YMin = (_YMin > wpm.YValue) ? wpm.YValue : _YMin;
}
//get the size of control for drawing, reserve space for axes
pnlImage.Width = Convert.ToInt32(this.ClientSize.Width * _Zoom);
pnlImage.Height = Convert.ToInt32(this.ClientSize.Height * _Zoom);
int drawWidth = pnlImage.Width - _LeftMargin - _TopMargin;
int drawHeight = pnlImage.Height - _LowerMargin - _TopMargin;
//now differentiate between external maps and internal graphs
if (_ShowMap)
{
_UseOffset = true;
_Core.BeginUpdate();
GMap.NET.CacheProviders.FilePureImageCache.CacheDate = _CacheDate;
//center: that's the center point for the _Core object
GMap.NET.PointLatLng center = new GMap.NET.PointLatLng((_YMax + _YMin) / 2,
(_XMax + _XMin) / 2);
_Core.CurrentPosition = center;
_Core.MapType = _MapType;
//top left and bottom right: for calculating zoom
GMap.NET.PointLatLng topLeft = new GMap.NET.PointLatLng(_YMax, _XMin);
GMap.NET.PointLatLng bottomRight = new GMap.NET.PointLatLng(_YMin, _XMax);
//number of tiles fitting into the image
int tilesX = drawWidth / _Core.Projection.TileSize.Width;
int tilesY = drawHeight / _Core.Projection.TileSize.Height;
//calculating zoom: when more tiles are required in one direction than fitting into the image
//the zoom has been exceeded by 1.
for (int zoomTmp = 0; zoomTmp <= 25; zoomTmp++)
{
GMap.NET.Point pixelTopLeft = _Core.Projection.FromLatLngToPixel(topLeft, zoomTmp);
GMap.NET.Point tileTopLeft = _Core.Projection.FromPixelToTileXY(pixelTopLeft);
GMap.NET.Point pixelBottomRight = _Core.Projection.FromLatLngToPixel(bottomRight, zoomTmp);
GMap.NET.Point tileBottomRight = _Core.Projection.FromPixelToTileXY(pixelBottomRight);
if ((tileBottomRight.X - tileTopLeft.X > tilesX)
|| (tileBottomRight.Y - tileTopLeft.Y > tilesY))
{
_Core.Zoom = zoomTmp - 1;
break;
}
}
//set size
_Core.OnMapSizeChanged(drawWidth - _Core.Projection.TileSize.Width,
drawHeight - _Core.Projection.TileSize.Height);
//get the MAP images
_Core.EndUpdate();
}
else
{
_UseOffset = false;
DetermineOffset(); //sets offset values to 0
//scale for internal representation
_XFactor = (_XMax - _XMin) / drawWidth;
_YFactor = (_YMax - _YMin) / drawHeight;
if (_UseEqualScale)
{
_XFactor = (_XFactor > _YFactor) ? _XFactor : _YFactor;
_YFactor = _XFactor;
}
}
//calculate position for each point
foreach (WayPointOnMap wpm in _WayPointsOnMap)
{
if (!wpm.IsHidden)
{
int xPixel, yPixel;
ValueToPixel((double)wpm.XValue, (double)wpm.YValue, out xPixel, out yPixel);
wpm.XPixels = xPixel;
wpm.YPixels = yPixel;
}
else
{
wpm.XPixels = -1;
wpm.YPixels = -1;
}
}
//Points of Interest
foreach (WayPointOnMap poi in _PointsOfInterest)
{
//Selected value: latitude, longitude, elevation, time, what ever....
poi.XValue = GetSelectedValueFromWaypoint(poi.WayPoint, _XCategory);
poi.YValue = GetSelectedValueFromWaypoint(poi.WayPoint, _YCategory);
int xPixel, yPixel;
ValueToPixel((double)poi.XValue, (double)poi.YValue, out xPixel, out yPixel);
poi.XPixels = xPixel;
poi.YPixels = yPixel;
}
//and now draw it!
_Bitmap = new Bitmap(pnlImage.Width, pnlImage.Height);
DrawTracks();
if (_ShowMap)
{
DrawGridForMap();
}
else
{
DrawGrid();
}
DrawPoI();
_Bitmap.Save(Defaults.TrackControl_BitmapPath, System.Drawing.Imaging.ImageFormat.Bmp); //@"C:\temp\bitmap.bmp"
this.Refresh();
}
private void DrawPoI()
{
if (_DrawPointsOfInterest)
{
Graphics graphics = Graphics.FromImage(_Bitmap);
Pen pointsPen = new Pen(_PoIColor, _TrackThickness);
foreach (WayPointOnMap wpm in _PointsOfInterest)
{
Point point = new Point(wpm.XPixels, wpm.YPixels);
point.Offset(-_LeftOffset, -_TopOffset);
switch (_StyleForPoI)
{
case WaypointsStyle.SmallPlus:
graphics.DrawLine(pointsPen, point.X - 2, point.Y, point.X + 2, point.Y);
graphics.DrawLine(pointsPen, point.X, point.Y - 2, point.X, point.Y + 2);
break;
case WaypointsStyle.BigPlus:
graphics.DrawLine(pointsPen, point.X - 4, point.Y, point.X + 4, point.Y);
graphics.DrawLine(pointsPen, point.X, point.Y - 4, point.X, point.Y + 4);
break;
case WaypointsStyle.SmallCircle:
graphics.DrawEllipse(pointsPen, point.X - 2, point.Y - 2, 4, 4);
break;
case WaypointsStyle.BigCircle:
graphics.DrawEllipse(pointsPen, point.X - 4, point.Y - 4, 8, 8);
break;
case WaypointsStyle.SmallX:
graphics.DrawLine(pointsPen, point.X - 2, point.Y - 2, point.X + 2, point.Y + 2);
graphics.DrawLine(pointsPen, point.X - 2, point.Y + 2, point.X + 2, point.Y - 2);
break;
case WaypointsStyle.BigX:
graphics.DrawLine(pointsPen, point.X - 4, point.Y - 4, point.X + 4, point.Y + 4);
graphics.DrawLine(pointsPen, point.X - 4, point.Y + 4, point.X + 4, point.Y - 4);
break;
case WaypointsStyle.SmallSquare:
graphics.DrawRectangle(pointsPen, point.X - 2, point.Y - 2, 4, 4);
break;
case WaypointsStyle.BigSquare:
graphics.DrawRectangle(pointsPen, point.X - 4, point.Y - 4, 8, 8);
break;
}
}
}
}
private void DrawTracks()
{
Graphics graphics = Graphics.FromImage(_Bitmap);
foreach (TrackOnMap tom in _TracksOnMap)
{
DrawTrack(graphics, tom);
}
graphics.Dispose();
}
private void DrawTrack(Graphics graphics, TrackOnMap track)
{
Point previous = new Point();
Point nullPoint = new Point();
foreach (WayPointOnMap wpm in track.WayPoints)
{
if (wpm.IsHidden)
{
previous = nullPoint;
}
else
{
Point p1;
if (previous != nullPoint)
p1 = previous;
else
{
p1 = new Point(wpm.XPixels, wpm.YPixels);
p1.Offset(-_LeftOffset, -_TopOffset);
}
Point p2 = new Point(wpm.XPixels, wpm.YPixels);
p2.Offset(-_LeftOffset, -_TopOffset);
DrawWaypoint(graphics, p2, p1, wpm.IsHighlighted);
previous = p2;
}
}
}
private void DrawWaypoint(Graphics graphics, Point point, Point previousPoint, bool highLighted)
{
Pen pen;
Pen penStandard = new Pen(_TrackColor, _TrackThickness);
Pen penHighlight = new Pen(_TrackColorHighlight, _TrackThickness);
Pen pointsPen;
Pen pointsPenStandard = new Pen(_WayPointsColor, _TrackThickness);
Pen pointsPenHighlight = new Pen(_WayPointsHighlight, _TrackThickness);
if (_DrawTrackLine)
{
pen = (highLighted) ? penHighlight : penStandard;
graphics.DrawLine(pen, previousPoint, point);
}
if (_DrawWaypoints)
{
pointsPen = (highLighted) ? pointsPenHighlight : pointsPenStandard;
switch (_StyleForWaypoints)
{
case WaypointsStyle.SmallPlus:
graphics.DrawLine(pointsPen, point.X - 2, point.Y, point.X + 2, point.Y);
graphics.DrawLine(pointsPen, point.X, point.Y - 2, point.X, point.Y + 2);
break;
case WaypointsStyle.BigPlus:
graphics.DrawLine(pointsPen, point.X - 4, point.Y, point.X + 4, point.Y);
graphics.DrawLine(pointsPen, point.X, point.Y - 4, point.X, point.Y + 4);
break;
case WaypointsStyle.SmallCircle:
graphics.DrawEllipse(pointsPen, point.X - 2, point.Y - 2, 4, 4);
break;
case WaypointsStyle.BigCircle:
graphics.DrawEllipse(pointsPen, point.X - 4, point.Y - 4, 8, 8);
break;
case WaypointsStyle.SmallX:
graphics.DrawLine(pointsPen, point.X - 2, point.Y - 2, point.X + 2, point.Y + 2);
graphics.DrawLine(pointsPen, point.X - 2, point.Y + 2, point.X + 2, point.Y - 2);
break;
case WaypointsStyle.BigX:
graphics.DrawLine(pointsPen, point.X - 4, point.Y - 4, point.X + 4, point.Y + 4);
graphics.DrawLine(pointsPen, point.X - 4, point.Y + 4, point.X + 4, point.Y - 4);
break;
case WaypointsStyle.SmallSquare:
graphics.DrawRectangle(pointsPen, point.X - 2, point.Y - 2, 4, 4);
break;
case WaypointsStyle.BigSquare:
graphics.DrawRectangle(pointsPen, point.X - 4, point.Y - 4, 8, 8);
break;
}
}
}
public List<WayPoint> GetSelection()
{
List<WayPoint> retVal = new List<WayPoint>();
if (_SelectedWayPointsOnMap != null)
{
foreach (WayPointOnMap wpm in _SelectedWayPointsOnMap)
retVal.Add(wpm.WayPoint);
}
return retVal;
}
private void DrawGrid()
{
Pen penLine = new Pen(Color.Wheat);
Brush brushValue = Brushes.White;
Font font = new Font("Arial", 8.0f);
Graphics g = Graphics.FromImage(_Bitmap);
//Y Axis
double pot = 0.0;
double dY = (_YMax - _YMin) / 10;
int decimals = 0;
int log = Convert.ToInt32(Math.Floor(Math.Log10(dY)));
if (log >= 0)
{
pot = Convert.ToInt32(Math.Pow(10, log));
decimals = 0;
}
else
{
pot = 1.0 / Math.Pow(10, -log);
decimals = -log;
}
dY = pot * Math.Round(dY / pot, 0);
double yStart = 0.0;
if (Math.Abs(_YMin) > 0.0001)
{
yStart = Convert.ToInt32(_YMin / dY) * dY;
}
while (yStart <= _YMax)
{
int yPos = _TopMargin + Convert.ToInt32((_YMax - yStart) / _YFactor);
g.DrawLine(penLine, 0, yPos, _Bitmap.Width, yPos);
string str = yStart.ToString("F" + decimals.ToString());
g.DrawString(str, font, brushValue, 0.0f, (float)yPos);
yStart = Math.Round(yStart + dY, decimals);
}
//x axis
double dX = (_XMax - _XMin) / 10;
log = Convert.ToInt32(Math.Floor(Math.Log10(dX)));
if (log >= 0)
{
pot = Convert.ToInt32(Math.Pow(10, log));
decimals = 0;
}
else
{
pot = 1.0 / Math.Pow(10, -log);
decimals = -log;
}
dX = pot * Math.Round(dX / pot, 0);
double xStart = 0.0;
if (Math.Abs(_XMin) > 0.0001)
{
xStart = Convert.ToInt32(_XMin / dX) * dX;
}
while (xStart <= _XMax)
{
int xPos = _LeftMargin + Convert.ToInt32((xStart - _XMin) / _XFactor);
g.DrawLine(penLine, xPos, 0, xPos, _Bitmap.Height);
string str = xStart.ToString("F" + decimals.ToString());
g.DrawString(str, font, brushValue, (float)xPos, (float)(_Bitmap.Height - _LowerMargin));
xStart = Math.Round(xStart + dX, decimals);
}
penLine.Dispose();
g.Dispose();
}
private void DrawGridForMap()
{
System.Drawing.Point topLeftReceived = new System.Drawing.Point();
System.Drawing.Point bottomRightReceived = new System.Drawing.Point();
if (_Core.tileDrawingList.GetExtension(ref topLeftReceived, ref bottomRightReceived))
{
topLeftReceived = new System.Drawing.Point(topLeftReceived.X * _Core.Projection.TileSize.Width, topLeftReceived.Y * _Core.Projection.TileSize.Height);
bottomRightReceived = new System.Drawing.Point((bottomRightReceived.X + 1) * _Core.Projection.TileSize.Width, (bottomRightReceived.Y + 1) * _Core.Projection.TileSize.Height);
int zoom = _Core.Zoom;
GMap.NET.PointLatLng topLeft = _Core.Projection.FromPixelToLatLng(topLeftReceived.X, topLeftReceived.Y, zoom);
GMap.NET.PointLatLng bottomRight = _Core.Projection.FromPixelToLatLng(bottomRightReceived.X, bottomRightReceived.Y, zoom);
//center: das wird als Mittelpunkt übergeben
GMap.NET.PointLatLng center = new GMap.NET.PointLatLng((_YMax + _YMin) / 2,
(_XMax + _XMin) / 2);
Pen penLine = new Pen(Color.Wheat);
Brush brushValue = Brushes.White;
Font font = new Font("Arial", 8.0f);
Graphics g = Graphics.FromImage(_Bitmap);
try
{
//Y Axis
double pot = 0.0;
double dY = (topLeft.Lat - bottomRight.Lat) / 10;
int decimals = 0;
int log = Convert.ToInt32(Math.Floor(Math.Log10(dY)));
if (log >= 0)
{
pot = Convert.ToInt32(Math.Pow(10, log));
decimals = 0;
}
else
{
pot = 1.0 / Math.Pow(10, -log);
decimals = -log;
}
dY = pot * Math.Round(dY / pot, 0);
double yStart = 0.0;
if (Math.Abs(bottomRight.Lat) > 0.0001)
{
yStart = Convert.ToInt32(bottomRight.Lat / dY) * dY;
}
while (yStart <= topLeft.Lat)
{
GMap.NET.Point yPoint = _Core.Projection.FromLatLngToPixel(new GMap.NET.PointLatLng(yStart, center.Lng), zoom);
int yPos = yPoint.Y - topLeftReceived.Y - _TopOffset + _TopMargin;
//int yPos = Convert.ToInt32((topLeft.Lat - yStart) / _YFactor) - _TopOffset;
g.DrawLine(penLine, 0, yPos, _Bitmap.Width, yPos);
string str = yStart.ToString("F" + decimals.ToString());
g.DrawString(str, font, brushValue, 0.0f, (float)yPos);
yStart = Math.Round(yStart + dY, decimals);
}
//x axis
double dX = (bottomRight.Lng - topLeft.Lng) / 10;
log = Convert.ToInt32(Math.Floor(Math.Log10(dX)));
if (log >= 0)
{
pot = Convert.ToInt32(Math.Pow(10, log));
decimals = 0;
}
else
{
pot = 1.0 / Math.Pow(10, -log);
decimals = -log;
}
dX = pot * Math.Round(dX / pot, 0);
double xStart = 0.0;
if (Math.Abs(topLeft.Lng) > 0.0001)
{
xStart = Convert.ToInt32(topLeft.Lng / dX) * dX;
}
while (xStart <= bottomRight.Lng)
{
GMap.NET.Point xPoint = _Core.Projection.FromLatLngToPixel(new GMap.NET.PointLatLng(center.Lat, xStart), zoom);
int xPos = xPoint.X - topLeftReceived.X - _LeftOffset + _LeftMargin;
//int xPos = Convert.ToInt32((xStart - topLeft.Lng) / _XFactor) - _TopOffset;
g.DrawLine(penLine, xPos, 0, xPos, _Bitmap.Height);
string str = xStart.ToString("F" + decimals.ToString());
g.DrawString(str, font, brushValue, (float)xPos, (float)pnlImage.ClientSize.Height - 12);
xStart = Math.Round(xStart + dX, decimals);
}
}
catch
{
}
finally
{
penLine.Dispose();
g.Dispose();
}
}
}
private double GetSelectedValueFromWaypoint(WayPoint wp, DrawCategory drawCategory)
{
double retVal = 0;
switch (drawCategory)
{
case DrawCategory.DistanceFromStart:
retVal = wp.GetDistanceFromStart().GetLength(wp.Latitude);
break;
case DrawCategory.Elevation:
retVal = Convert.ToDouble(wp.Elevation);
break;
case DrawCategory.Latitude:
retVal = Convert.ToDouble(wp.Latitude);
break;
case DrawCategory.LatitudeKm:
retVal = wp.LatitudeKm;
break;
case DrawCategory.Longitude:
retVal = Convert.ToDouble(wp.Longitude);
break;
case DrawCategory.LongitudeKm:
retVal = wp.LongitudeKm;
break;
case DrawCategory.Speed:
retVal = wp.GetSpeed();
break;
case DrawCategory.SpeedvectorEast:
retVal = wp.GetSpeedVector().DeltaLongitude;
break;
case DrawCategory.SpeedvectorElevation:
retVal = wp.GetSpeedVector().DeltaElevation;
break;
case DrawCategory.SpeedvectorNorth:
retVal = wp.GetSpeedVector().DeltaLatitude;
break;
case DrawCategory.Time:
retVal = wp.GetTimeSinceStartSeconds();
break;
case DrawCategory.TrackDistanceFromStart:
retVal = wp.GetTrackDistanceFromStart();
break;
case DrawCategory.DirectionEast:
retVal = wp.GetDirectionVector().DeltaLongitude;
break;
case DrawCategory.DirectionNorth:
retVal = wp.GetDirectionVector().DeltaLatitude;
break;
case DrawCategory.DirectionVertical:
retVal = wp.GetDirectionVector().DeltaElevation;
break;
case DrawCategory.PercrentAscent:
retVal = wp.GetAscent();
break;
case DrawCategory.Heartrate:
retVal = wp.Heartreate;
break;
case DrawCategory.Cadence:
retVal = wp.Cadence;
break;
case DrawCategory.Depth:
retVal = wp.Depth;
break;
case DrawCategory.Temperature:
retVal = wp.Temperature;
break;
case DrawCategory.Watertemperature:
retVal = wp.WaterTemperature;
break;
default:
//GPX_TrackControl_GetSelectedValueFromWaypoint0=Unknown category for drawing
throw new ArgumentException(ResourceManager.GetString("GPX_TrackControl_GetSelectedValueFromWaypoint0"));
}
return retVal;
}
private void SetWaypointValueForCoordinates(WayPoint wp, int x, int y)
{
if (_ShowMap)
{
GMap.NET.PointLatLng pos = _Core.FromLocalToLatLng(x, y);
wp.Latitude = Convert.ToDecimal(pos.Lat);
wp.Longitude = Convert.ToDecimal(pos.Lng);
}
else
{
switch (_XCategory)
{
case DrawCategory.Elevation:
wp.Elevation = Convert.ToDecimal(_XMin + _XFactor * x); //_XMin + _XFactor * (x - _LeftMargin)
break;
case DrawCategory.Latitude:
wp.Latitude = Convert.ToDecimal(_XMin + _XFactor * x);
break;
case DrawCategory.LatitudeKm:
double latitudeKm = _XMin + _XFactor * x;
wp.Latitude = Convert.ToDecimal(latitudeKm / GPXUtils.KmPerDegree);
break;
case DrawCategory.Longitude:
wp.Longitude = Convert.ToDecimal(_XMin + _XFactor * x);
break;
case DrawCategory.LongitudeKm:
double longitudeKm = _XMin + _XFactor * x;
wp.Longitude = Convert.ToDecimal(longitudeKm / GPXUtils.KmPerDegreeAtLatitude(wp.Latitude));
break;
default:
//GPX_TrackControl_SetWaypointValueForCoordinates0=X Category not valid for editing
throw new ArgumentException(ResourceManager.GetString("GPX_TrackControl_SetWaypointValueForCoordinates0"));
}
switch (_YCategory)
{
case DrawCategory.Elevation:
wp.Elevation = Convert.ToDecimal(_YMax - _YFactor * y); //_YMax - _YFactor * (y - _GeneralMargin)
break;
case DrawCategory.Latitude:
wp.Latitude = Convert.ToDecimal(_YMax - _YFactor * y);
break;
case DrawCategory.LatitudeKm:
double latitudeKm = _YMax - _YFactor * y;
wp.Latitude = Convert.ToDecimal(latitudeKm / GPXUtils.KmPerDegree);
break;
case DrawCategory.Longitude:
wp.Longitude = Convert.ToDecimal(_YMax - _YFactor * y);
break;
case DrawCategory.LongitudeKm:
double longitudeKm = _YMax - _YFactor * y;
wp.Longitude = Convert.ToDecimal(longitudeKm / GPXUtils.KmPerDegreeAtLatitude(wp.Latitude));
break;
default:
//GPX_TrackControl_SetWaypointValueForCoordinates1=Y Category not valid for editing
throw new ArgumentException(ResourceManager.GetString("GPX_TrackControl_SetWaypointValueForCoordinates1"));
}
}
}
private void ResetHighlight()
{
//Utils.DebugStackTrace();
if (_WayPointsOnMap != null)
{
foreach (WayPointOnMap wpm in _WayPointsOnMap)
wpm.IsHighlighted = false;
}
}
private void pnlImage_Paint(object sender, PaintEventArgs e)
{
try
{
if (_Bitmap != null)
{
e.Graphics.DrawImageUnscaled(_Bitmap, 0, 0);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("pnlImage_Paint: " + ex.ToString());
}
}
private void pnlImage_MouseDown(object sender, MouseEventArgs e)
{
try
{
if (e != null)
{
if (e.Button == MouseButtons.Left)
{
_Selecting = true;
_SelectionStart = e.Location;
_SelectedWayPointsOnMap = null;
ResetHighlight();
}
else if (e.Button == MouseButtons.Right)
{
//is it a waypoint?
_SelectedWayPointOnMap = GetWaypointFromPosition(e.X, e.Y);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("pnlImage_MouseDown: " + ex.ToString());
}
}
private void pnlImage_MouseMove(object sender, MouseEventArgs e)
{
try
{
if (e != null)
{
//prevent flickering in Windows 7
if (e.X == _LastMouseX && e.Y == _LastMouseY)
{
return;
}
_LastMouseX = e.X;
_LastMouseY = e.Y;
if (_Selecting)
{
this.Refresh();
Graphics g = pnlImage.CreateGraphics();
Pen pen = new Pen(Color.Blue);
int top = Math.Min(_SelectionStart.Y, e.Y);
int left = Math.Min(_SelectionStart.X, e.X);
int width = Math.Max(_SelectionStart.X, e.X) - left;
int height = Math.Max(_SelectionStart.Y, e.Y) - top;
g.DrawRectangle(pen, new Rectangle(left, top, width, height));
g.Dispose();
}
else if (_SelectedWayPointOnMap != null)
{
if (_ShowMap)
{
_SelectedWayPointOnMap.XPixels = e.X + _LeftOffset;
_SelectedWayPointOnMap.YPixels = e.Y + _TopOffset;
}
else
{
_SelectedWayPointOnMap.XPixels = e.X;
_SelectedWayPointOnMap.YPixels = e.Y;
}