| | | 1 | | using GMap.NET; |
| | | 2 | | using GMap.NET.MapProviders; |
| | | 3 | | using GMap.NET.WindowsPresentation; |
| | | 4 | | using RouteEditorCS.Properties; |
| | | 5 | | using Ookii.Dialogs.Wpf; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Net.NetworkInformation; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Windows.Input; |
| | | 11 | | using System.Windows.Media; |
| | | 12 | | |
| | | 13 | | namespace RouteEditorCS |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// MainViewModel implements the UI functions joins them with data and files. |
| | | 17 | | /// </summary> |
| | | 18 | | public class MainViewModel : INotifyPropertyChanged |
| | | 19 | | { |
| | | 20 | | |
| | | 21 | | private string _windowTitle; |
| | | 22 | | private double _guiZoomFactor; |
| | | 23 | | private string _statusLine; |
| | | 24 | | private string _selectedMap; |
| | | 25 | | |
| | | 26 | | private PointLatLng _mouseDownPos; |
| | | 27 | | private List<PointLatLng> _routePoints; |
| | | 28 | | private GMapRoute _route; |
| | | 29 | | |
| | | 30 | | private readonly IGMapControl _gmapControl; |
| | | 31 | | |
| | | 32 | | public event PropertyChangedEventHandler PropertyChanged; |
| | 0 | 33 | | public ICommand GuiZoomInCommand => new RelayCommand(GuiZoomIn); |
| | 0 | 34 | | public ICommand GuiZoomOutCommand => new RelayCommand(GuiZoomOut); |
| | 0 | 35 | | public ICommand BeforeClosingCommand => new RelayCommand(BeforeClosing); |
| | 0 | 36 | | public ICommand RemoveRouteCommand => new RelayCommand(RemoveRoute); |
| | 0 | 37 | | public ICommand LoadRouteCommand => new RelayCommand(LoadRoute); |
| | 0 | 38 | | public ICommand SaveRouteCommand => new RelayCommand(SaveRoute); |
| | | 39 | | |
| | 4 | 40 | | public ProviderCollection Items { get; } |
| | | 41 | | |
| | | 42 | | public string WindowTitle |
| | | 43 | | { |
| | 4 | 44 | | get => _windowTitle; |
| | | 45 | | set |
| | | 46 | | { |
| | 10 | 47 | | if (value != null) |
| | | 48 | | { |
| | 8 | 49 | | _windowTitle = value; |
| | 8 | 50 | | OnPropertyChanged(nameof(WindowTitle)); |
| | | 51 | | } |
| | 10 | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public string StatusLine |
| | | 56 | | { |
| | 0 | 57 | | get => _statusLine; |
| | | 58 | | set |
| | | 59 | | { |
| | 4 | 60 | | if (value != null) |
| | | 61 | | { |
| | 4 | 62 | | _statusLine = value; |
| | 4 | 63 | | OnPropertyChanged(nameof(StatusLine)); |
| | | 64 | | } |
| | 4 | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public double GuiZoomFactor |
| | | 69 | | { |
| | 0 | 70 | | get { return _guiZoomFactor; } |
| | | 71 | | set |
| | | 72 | | { |
| | 4 | 73 | | if (_guiZoomFactor != value) |
| | | 74 | | { |
| | 4 | 75 | | _guiZoomFactor = value; |
| | 4 | 76 | | OnPropertyChanged(nameof(GuiZoomFactor)); |
| | | 77 | | } |
| | 4 | 78 | | } |
| | | 79 | | } |
| | | 80 | | public string SelectedMap |
| | | 81 | | { |
| | 0 | 82 | | get => _selectedMap; |
| | | 83 | | set |
| | | 84 | | { |
| | 4 | 85 | | _selectedMap = value; |
| | 4 | 86 | | OnPropertyChanged(nameof(SelectedMap)); |
| | 4 | 87 | | HandleMapChanges(); |
| | 4 | 88 | | } |
| | | 89 | | } |
| | | 90 | | protected void OnPropertyChanged (string propertyName) |
| | | 91 | | { |
| | 20 | 92 | | PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| | 2 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// The Model Constructor needs the map control object. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="gmapControl">most functions of the UI are focused on the MapControl</param> |
| | 4 | 99 | | public MainViewModel (IGMapControl gmapControl) { |
| | | 100 | | |
| | | 101 | | // @todo make this init more configurable |
| | | 102 | | |
| | | 103 | | // for testing |
| | | 104 | | //Thread.CurrentThread.CurrentUICulture = new CultureInfo("de"); |
| | | 105 | | |
| | 4 | 106 | | WindowTitle = GetType().Namespace; |
| | 4 | 107 | | _gmapControl = gmapControl; |
| | | 108 | | |
| | 4 | 109 | | GuiZoomFactor = Settings.Default.guiSize; |
| | | 110 | | |
| | 4 | 111 | | Items = new ProviderCollection(new[] |
| | 4 | 112 | | { |
| | 4 | 113 | | new ProviderItem( |
| | 4 | 114 | | "OSM", |
| | 4 | 115 | | "OpenStreetMap", |
| | 4 | 116 | | OpenStreetMapProvider.Instance |
| | 4 | 117 | | ), |
| | 4 | 118 | | new ProviderItem( |
| | 4 | 119 | | "googlemaps", |
| | 4 | 120 | | "Google Maps", |
| | 4 | 121 | | GMapProviders.GoogleMap |
| | 4 | 122 | | ), |
| | 4 | 123 | | new ProviderItem( |
| | 4 | 124 | | "opencyclemap", |
| | 4 | 125 | | "Cycle Maps", |
| | 4 | 126 | | GMapProviders.OpenCycleMap |
| | 4 | 127 | | ), |
| | 4 | 128 | | new ProviderItem( |
| | 4 | 129 | | "BingHybrid", |
| | 4 | 130 | | "Bing Hybrid", |
| | 4 | 131 | | GMapProviders.BingHybridMap |
| | 4 | 132 | | ) |
| | 4 | 133 | | }); |
| | 4 | 134 | | SelectedMap = Settings.Default.lastMap; |
| | 4 | 135 | | _gmapControl.Zoom = Settings.Default.lastZoom; |
| | 4 | 136 | | _gmapControl.ShowCenter = false; |
| | 4 | 137 | | _gmapControl.CanDragMap = true; |
| | 4 | 138 | | _gmapControl.DragButton = MouseButton.Left; |
| | | 139 | | |
| | 4 | 140 | | _gmapControl.Position = new PointLatLng( |
| | 4 | 141 | | Settings.Default.lastLatitude, |
| | 4 | 142 | | Settings.Default.lastLongitude |
| | 4 | 143 | | ); |
| | | 144 | | |
| | 4 | 145 | | OnPositionChanged(_gmapControl.Position); |
| | 4 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// UI Button function to make the UI bigger. |
| | | 150 | | /// </summary> |
| | | 151 | | public void GuiZoomIn () |
| | | 152 | | { |
| | 0 | 153 | | GuiZoomFactor *= 1.1; |
| | 0 | 154 | | Settings.Default.guiSize = _guiZoomFactor; |
| | 0 | 155 | | Settings.Default.Save(); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// UI Button function to make the UI smaller. |
| | | 160 | | /// </summary> |
| | | 161 | | public void GuiZoomOut () |
| | | 162 | | { |
| | 0 | 163 | | GuiZoomFactor /= 1.1; |
| | 0 | 164 | | Settings.Default.guiSize = _guiZoomFactor; |
| | 0 | 165 | | Settings.Default.Save(); |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// This method sets map provider and cache based on the _selectedMap and settings. |
| | | 170 | | /// </summary> |
| | | 171 | | private void HandleMapChanges () |
| | | 172 | | { |
| | 4 | 173 | | if (NetworkInterface.GetIsNetworkAvailable()) |
| | | 174 | | { |
| | 4 | 175 | | _gmapControl.CacheMode = AccessMode.ServerAndCache; |
| | | 176 | | } |
| | | 177 | | else |
| | | 178 | | { |
| | 0 | 179 | | _gmapControl.CacheMode = AccessMode.CacheOnly; |
| | | 180 | | } |
| | | 181 | | |
| | 4 | 182 | | if (Items.TryGet(_selectedMap, out var item)) |
| | | 183 | | { |
| | 4 | 184 | | _gmapControl.MapProvider = item.Provider; |
| | | 185 | | } |
| | 4 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Store position, map zoom and map provider before closing the application. |
| | | 190 | | /// This method is called by the UI in the OnClose Event. |
| | | 191 | | /// </summary> |
| | | 192 | | private void BeforeClosing () |
| | | 193 | | { |
| | 0 | 194 | | Settings.Default.lastLatitude = _gmapControl.Position.Lat; |
| | 0 | 195 | | Settings.Default.lastLongitude = _gmapControl.Position.Lng; |
| | 0 | 196 | | Settings.Default.lastZoom = (int)_gmapControl.Zoom; |
| | 0 | 197 | | Settings.Default.lastMap = _selectedMap; |
| | 0 | 198 | | Settings.Default.Save(); |
| | 0 | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// This method refreshs the statusline with coordinates and route length. |
| | | 203 | | /// It is called by mouse events, which occurs on the mapControl object. |
| | | 204 | | /// </summary> |
| | | 205 | | /// <param name="point">the latitude and longitude calculated by the UI event.</param> |
| | | 206 | | private void OnPositionChanged (PointLatLng point) |
| | | 207 | | { |
| | 4 | 208 | | string formattedLat = point.Lat.ToString("F6", CultureInfo.InvariantCulture); |
| | 4 | 209 | | string formattedLng = point.Lng.ToString("F6", CultureInfo.InvariantCulture); |
| | 4 | 210 | | string zoom = _gmapControl.Zoom.ToString("F1", CultureInfo.InvariantCulture); |
| | | 211 | | |
| | 4 | 212 | | double distance = RouteLengthKm(); |
| | 4 | 213 | | if (distance > 0) |
| | | 214 | | { |
| | 0 | 215 | | string formattedDist = distance.ToString("F4", CultureInfo.InvariantCulture); |
| | 0 | 216 | | StatusLine = $"Lat Lng [Zoom] Route: {formattedLat} {formattedLng} [{zoom}] {formattedDist} km"; |
| | | 217 | | } |
| | | 218 | | else |
| | | 219 | | { |
| | 4 | 220 | | StatusLine = $"Lat Lng [Zoom]: {formattedLat} {formattedLng} [{zoom}]"; |
| | | 221 | | } |
| | 4 | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// UI Button function to delete the route from the map. |
| | | 226 | | /// </summary> |
| | | 227 | | private void RemoveRoute() |
| | | 228 | | { |
| | 0 | 229 | | if (_route != null) |
| | | 230 | | { |
| | 0 | 231 | | _gmapControl.Markers.Remove(_route); |
| | 0 | 232 | | _route = null; |
| | | 233 | | } |
| | 0 | 234 | | _routePoints = new List<PointLatLng>(); |
| | 0 | 235 | | StatusLine = ""; |
| | 0 | 236 | | } |
| | | 237 | | |
| | | 238 | | /// <summary> |
| | | 239 | | /// UI Button function to open a save dialog. |
| | | 240 | | /// </summary> |
| | | 241 | | private void SaveRoute () |
| | | 242 | | { |
| | 0 | 243 | | if (_routePoints == null || _routePoints.Count == 0) return; |
| | | 244 | | |
| | 0 | 245 | | var dlg = new VistaSaveFileDialog |
| | 0 | 246 | | { |
| | 0 | 247 | | Title = Strings.SaveDialog_Title, |
| | 0 | 248 | | Filter = Strings.Dialog_Filetype + " (*.txt)|*.txt", |
| | 0 | 249 | | DefaultExt = "txt", |
| | 0 | 250 | | FileName = "route.txt", |
| | 0 | 251 | | OverwritePrompt = true |
| | 0 | 252 | | }; |
| | | 253 | | |
| | 0 | 254 | | bool? result = dlg.ShowDialog(); |
| | 0 | 255 | | if (result != true) return; |
| | | 256 | | |
| | 0 | 257 | | var culture = CultureInfo.InvariantCulture; |
| | | 258 | | |
| | 0 | 259 | | var sb = new StringBuilder(); |
| | 0 | 260 | | double distance = 0.0; |
| | 0 | 261 | | for (int i = 0; i < _routePoints.Count; i++) |
| | | 262 | | { |
| | 0 | 263 | | double lat = _routePoints[i].Lat; |
| | 0 | 264 | | double lng = _routePoints[i].Lng; |
| | 0 | 265 | | if (i > 0) |
| | | 266 | | { |
| | 0 | 267 | | distance += DistanceKm(_routePoints[i - 1], _routePoints[i]); |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | sb.AppendLine(string.Format(culture, "{0:F6},{1:F6},{2:F4}km", lat, lng, distance)); |
| | | 271 | | } |
| | | 272 | | |
| | 0 | 273 | | System.IO.File.WriteAllText(dlg.FileName, sb.ToString(), Encoding.UTF8); |
| | 0 | 274 | | } |
| | | 275 | | |
| | | 276 | | /// <summary> |
| | | 277 | | /// UI Button function to open a file-open dialog to load a route. |
| | | 278 | | /// </summary> |
| | | 279 | | private void LoadRoute () |
| | | 280 | | { |
| | 0 | 281 | | var dialog = new VistaOpenFileDialog |
| | 0 | 282 | | { |
| | 0 | 283 | | Title = Strings.LoadDialog_Title, |
| | 0 | 284 | | Filter = Strings.Dialog_Filetype + " (*.txt)|*.txt", |
| | 0 | 285 | | DefaultExt = "txt", |
| | 0 | 286 | | Multiselect = false, |
| | 0 | 287 | | CheckFileExists = true, |
| | 0 | 288 | | CheckPathExists = true |
| | 0 | 289 | | }; |
| | | 290 | | |
| | 0 | 291 | | bool? result = dialog.ShowDialog(); |
| | | 292 | | |
| | 0 | 293 | | if (result == true) |
| | | 294 | | { |
| | 0 | 295 | | var culture = CultureInfo.InvariantCulture; |
| | | 296 | | |
| | 0 | 297 | | if (_route != null) |
| | | 298 | | { |
| | 0 | 299 | | _gmapControl.Markers.Remove(_route); |
| | | 300 | | } |
| | 0 | 301 | | _routePoints = new List<PointLatLng>(); |
| | | 302 | | |
| | 0 | 303 | | foreach (var line in System.IO.File.ReadLines(dialog.FileName)) |
| | | 304 | | { |
| | 0 | 305 | | if (string.IsNullOrWhiteSpace(line)) continue; |
| | 0 | 306 | | var parts = line.Split(new[] { ' ', ',', '\t' }, StringSplitOptions.RemoveEmptyEntries); |
| | 0 | 307 | | if (parts.Length < 2) |
| | | 308 | | { |
| | | 309 | | continue; |
| | | 310 | | } |
| | 0 | 311 | | if ( |
| | 0 | 312 | | double.TryParse(parts[0], NumberStyles.Float | NumberStyles.AllowLeadingSign, culture, out doubl |
| | 0 | 313 | | double.TryParse(parts[1], NumberStyles.Float | NumberStyles.AllowLeadingSign, culture, out doubl |
| | 0 | 314 | | ) |
| | | 315 | | { |
| | 0 | 316 | | _routePoints.Add(new PointLatLng(v1, v2)); |
| | | 317 | | } |
| | | 318 | | // @todo error handling like a log or StatusLine |
| | | 319 | | } |
| | 0 | 320 | | if (_routePoints.Count > 1) |
| | | 321 | | { |
| | 0 | 322 | | _gmapControl.Position = _routePoints.Last(); |
| | 0 | 323 | | ShowRoute(); |
| | | 324 | | } |
| | | 325 | | } |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// This method is called on load route or if a new point is added to the route or a point is removed. |
| | | 330 | | /// </summary> |
| | | 331 | | private void ShowRoute () |
| | | 332 | | { |
| | 0 | 333 | | if (_routePoints == null) |
| | | 334 | | { |
| | 0 | 335 | | _routePoints = new List<PointLatLng>(); |
| | | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | if (_route != null) |
| | | 339 | | { |
| | 0 | 340 | | _gmapControl.Markers.Remove(_route); |
| | | 341 | | } |
| | | 342 | | |
| | 0 | 343 | | if (_routePoints.Count > 0) |
| | | 344 | | { |
| | 0 | 345 | | _route = new GMapRoute(_routePoints); |
| | 0 | 346 | | _route.Shape = new System.Windows.Shapes.Path() |
| | 0 | 347 | | { |
| | 0 | 348 | | Stroke = new SolidColorBrush(Colors.Blue), |
| | 0 | 349 | | StrokeThickness = 2 |
| | 0 | 350 | | }; |
| | | 351 | | |
| | 0 | 352 | | _gmapControl.Markers.Add(_route); |
| | | 353 | | } |
| | 0 | 354 | | } |
| | | 355 | | |
| | | 356 | | /// <summary> |
| | | 357 | | /// Primitive methode to calculate the distance between to positions on the earth. |
| | | 358 | | /// </summary> |
| | | 359 | | /// <param name="p1">position 1</param> |
| | | 360 | | /// <param name="p2">position 2</param> |
| | | 361 | | /// <returns></returns> |
| | | 362 | | private double DistanceKm (PointLatLng p1, PointLatLng p2) |
| | | 363 | | { |
| | | 364 | | const double R = 6371.0; // Erdradius in km |
| | 0 | 365 | | double lat1 = DegreesToRadians(p1.Lat); |
| | 0 | 366 | | double lat2 = DegreesToRadians(p2.Lat); |
| | 0 | 367 | | double dLat = DegreesToRadians(p2.Lat - p1.Lat); |
| | 0 | 368 | | double dLon = DegreesToRadians(p2.Lng - p1.Lng); |
| | | 369 | | |
| | 0 | 370 | | double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + |
| | 0 | 371 | | Math.Cos(lat1) * Math.Cos(lat2) * |
| | 0 | 372 | | Math.Sin(dLon / 2) * Math.Sin(dLon / 2); |
| | 0 | 373 | | double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); |
| | 0 | 374 | | return R * c; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | /// <summary> |
| | | 378 | | /// converter used by DistanceKm() to get positions in radians and not degrees |
| | | 379 | | /// </summary> |
| | | 380 | | /// <param name="deg">angle by degrees</param> |
| | | 381 | | /// <returns>the angle in radians</returns> |
| | | 382 | | private double DegreesToRadians (double deg) |
| | | 383 | | { |
| | 0 | 384 | | return deg * (Math.PI / 180.0); |
| | | 385 | | } |
| | | 386 | | |
| | | 387 | | /// <summary> |
| | | 388 | | /// method loops through _routePoints and calculate + sum the route distance. |
| | | 389 | | /// </summary> |
| | | 390 | | /// <returns>the complete distance of the route stored in _routePoints</returns> |
| | | 391 | | private double RouteLengthKm () |
| | | 392 | | { |
| | 8 | 393 | | if (_routePoints == null || _routePoints.Count < 2) return 0.0; |
| | 0 | 394 | | double total = 0.0; |
| | 0 | 395 | | for (int i = 0; i < _routePoints.Count - 1; i++) |
| | | 396 | | { |
| | 0 | 397 | | total += DistanceKm(_routePoints[i], _routePoints[i + 1]); |
| | | 398 | | } |
| | 0 | 399 | | return total; |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | /// <summary> |
| | | 403 | | /// Used by UI events to store the map positon on earth in _mouseDownPos |
| | | 404 | | /// </summary> |
| | | 405 | | /// <param name="point"></param> |
| | | 406 | | public void UpdateMousePositionFrom (System.Windows.Point point) |
| | | 407 | | { |
| | 0 | 408 | | _mouseDownPos = _gmapControl.FromLocalToLatLng((int)point.X, (int)point.Y); |
| | 0 | 409 | | OnPositionChanged(_mouseDownPos); |
| | 0 | 410 | | } |
| | | 411 | | |
| | | 412 | | /// <summary> |
| | | 413 | | /// Used by UI event on mapControl object and add a point in _routePoints |
| | | 414 | | /// </summary> |
| | | 415 | | public void AddRoutePoint () |
| | | 416 | | { |
| | | 417 | | // to modern for mono csc |
| | | 418 | | //_routePoints ??= new List<PointLatLng>(); |
| | 0 | 419 | | if (_routePoints == null) _routePoints = new List<PointLatLng>(); |
| | 0 | 420 | | _routePoints.Add(_mouseDownPos); |
| | 0 | 421 | | ShowRoute(); |
| | 0 | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Used by UI event on mapControl object and remove a point in _routePoints |
| | | 426 | | /// </summary> |
| | | 427 | | public void RemoveLastRoutePoint () |
| | | 428 | | { |
| | 0 | 429 | | if (_routePoints != null && _routePoints.Count > 0) |
| | | 430 | | { |
| | 0 | 431 | | _routePoints.RemoveAt(_routePoints.Count - 1); |
| | 0 | 432 | | ShowRoute(); |
| | | 433 | | } |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | } |
| | | 437 | | } |