Files
annotations/Azaion.Annotator/Controls/MapMatcher.xaml.cs
T
Alex Bezdieniezhnykh b21f8e320f fix bug with annotation result gradient stops
add tensorrt engine
2025-04-02 00:29:21 +03:00

160 lines
5.6 KiB
C#

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<MediaFileInfo> _allMediaFiles;
private Dictionary<string, Annotation> _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) => { await OpenGpsLocation(GpsFiles.SelectedIndex); };
}
private async Task OpenGpsLocation(int gpsFilesIndex)
{
var media = GpsFiles.Items[gpsFilesIndex] as MediaFileInfo;
var ann = _annotations.GetValueOrDefault(Path.GetFileNameWithoutExtension(media.Name));
GpsImageEditor.Background = new ImageBrush
{
ImageSource = await Path.Combine(_currentDir, ann.Name).OpenImage()
};
if (ann.Lat != 0 && ann.Lon != 0)
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<MediaFileInfo>(allFiles);
_allMediaFiles = mediaFiles;
GpsFiles.ItemsSource = new ObservableCollection<MediaFileInfo>(_allMediaFiles);
var annotations = SetFromCsv(mediaFiles);
Cursor = Cursors.Wait;
await Task.Delay(TimeSpan.FromSeconds(10));
SetMarkers(annotations);
Cursor = Cursors.Arrow;
await OpenGpsLocation(0);
}
private Dictionary<string, Annotation> SetFromCsv(List<MediaFileInfo> 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
.Where(x => x.MatchType == "stitched")
.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<string, Annotation> annotations)
{
if (!annotations.Any())
return;
var firstAnnotation = annotations.FirstOrDefault();
SatelliteMap.Position = new PointLatLng(firstAnnotation.Value.Lat, firstAnnotation.Value.Lon);
foreach (var ann in annotations.Where(x => x.Value.Lat != 0 && x.Value.Lon != 0))
{
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);
}
SatelliteMap.ZoomAndCenterMarkers(null);
}
}