using System.Collections.ObjectModel; using System.Diagnostics; using System.Drawing; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; using Azaion.Common; using Azaion.Common.Database; using Azaion.Common.DTO; using Azaion.Common.DTO.Config; using Azaion.Common.Extensions; using GMap.NET; using GMap.NET.MapProviders; using GMap.NET.WindowsPresentation; using Microsoft.WindowsAPICodePack.Dialogs; namespace Azaion.Annotator.Controls; public partial class MapMatcher : UserControl { private AppConfig _appConfig; List _allMediaFiles; private Dictionary _annotations; private string _currentDir; public MapMatcher() { InitializeComponent(); } public void Init(AppConfig appConfig) { _appConfig = appConfig; GoogleMapProvider.Instance.ApiKey = appConfig.MapConfig.ApiKey; SatelliteMap.MapProvider = GMapProviders.GoogleSatelliteMap; SatelliteMap.Position = new PointLatLng(48.295985271707664, 37.14477539062501); SatelliteMap.MultiTouchEnabled = true; GpsFiles.MouseDoubleClick += async (sender, args) => { var media = (GpsFiles.SelectedItem as MediaFileInfo)!; var ann = _annotations.GetValueOrDefault(Path.GetFileNameWithoutExtension(media.Name)); GpsImageEditor.Background = new ImageBrush { ImageSource = await Path.Combine(_currentDir, ann.Name).OpenImage() }; SatelliteMap.Position = new PointLatLng(ann.Lat, ann.Lon); }; } private void GpsFilesContextOpening(object sender, ContextMenuEventArgs e) { var listItem = sender as ListViewItem; GpsFilesContextMenu.DataContext = listItem!.DataContext; } private void OpenContainingFolder(object sender, RoutedEventArgs e) { var mediaFileInfo = (sender as MenuItem)?.DataContext as MediaFileInfo; if (mediaFileInfo == null) return; Process.Start("explorer.exe", "/select,\"" + mediaFileInfo.Path +"\""); } private async void OpenGpsTilesFolderClick(object sender, RoutedEventArgs e) { var dlg = new CommonOpenFileDialog { Title = "Open Video folder", IsFolderPicker = true, InitialDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory) }; var dialogResult = dlg.ShowDialog(); if (dialogResult != CommonFileDialogResult.Ok || string.IsNullOrEmpty(dlg.FileName)) return; TbGpsMapFolder.Text = dlg.FileName; _currentDir = dlg.FileName; var dir = new DirectoryInfo(dlg.FileName); var mediaFiles = dir.GetFiles(_appConfig.AnnotationConfig.ImageFormats.ToArray()) .Select(x => new MediaFileInfo { Name = x.Name, Path = x.FullName, MediaType = MediaTypes.Image }).ToList(); // var allFiles = videoFiles.Concat(imageFiles).ToList(); // // var labelsDict = await _dbFactory.Run(async db => await db.Annotations // .GroupBy(x => x.Name.Substring(0, x.Name.Length - 7)) // .Where(x => allFileNames.Contains(x.Key)) // .ToDictionaryAsync(x => x.Key, x => x.Key)); // // foreach (var mediaFile in allFiles) // mediaFile.HasAnnotations = labelsDict.ContainsKey(mediaFile.FName); // // AllMediaFiles = new ObservableCollection(allFiles); _allMediaFiles = mediaFiles; GpsFiles.ItemsSource = new ObservableCollection(_allMediaFiles); var annotations = SetFromCsv(mediaFiles); await Task.Delay(TimeSpan.FromSeconds(10)); SetMarkers(annotations); } private Dictionary SetFromCsv(List mediaFiles) { _annotations = mediaFiles.Select(x => new Annotation { Name = x.Name, OriginalMediaName = x.Name }).ToDictionary(x => Path.GetFileNameWithoutExtension(x.OriginalMediaName)); var csvResults = GpsCsvResult.ReadFromCsv(Constants.CSV_PATH); var csvDict = csvResults.ToDictionary(x => x.Image); foreach (var ann in _annotations) { var csvRes = csvDict.GetValueOrDefault(ann.Key); if (csvRes == null) continue; ann.Value.Lat = csvRes.Latitude; ann.Value.Lon = csvRes.Longitude; } return _annotations; } private void SetMarkers(Dictionary annotations) { if (!annotations.Any()) return; var firstAnnotation = annotations.FirstOrDefault(); SatelliteMap.Position = new PointLatLng(firstAnnotation.Value.Lat, firstAnnotation.Value.Lon); foreach (var ann in annotations) { var marker = new GMapMarker(new PointLatLng(ann.Value.Lat, ann.Value.Lon)); var circle = new CircleVisual(marker, System.Windows.Media.Brushes.Blue) { Text = " " }; marker.Shape = circle; SatelliteMap.Markers.Add(marker); } } }