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