XAML-Map-Control/MapControl/Shared/GeoImage.cs

196 lines
6.7 KiB
C#
Raw Normal View History

2025-03-31 21:33:52 +02:00
using Microsoft.Extensions.Logging;
using System;
2022-01-18 18:07:19 +01:00
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
2024-09-02 15:49:53 +02:00
using System.Windows.Controls;
2024-05-22 11:25:32 +02:00
using System.Windows.Media;
using System.Windows.Media.Imaging;
2024-09-06 23:50:59 +02:00
using Shape = System.Windows.Shapes.Shape;
2022-01-18 18:07:19 +01:00
#elif UWP
using Windows.UI.Xaml;
2024-09-02 15:49:53 +02:00
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
2022-01-18 18:07:19 +01:00
using Windows.UI.Xaml.Media.Imaging;
2024-09-06 23:50:59 +02:00
using Shape = Windows.UI.Xaml.Shapes.Shape;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
2024-09-02 15:49:53 +02:00
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
using Microsoft.UI.Xaml.Media.Imaging;
2024-09-06 23:50:59 +02:00
using Shape = Microsoft.UI.Xaml.Shapes.Shape;
#elif AVALONIA
2025-08-19 19:43:02 +02:00
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
2024-09-06 23:50:59 +02:00
using Shape = Avalonia.Controls.Shapes.Shape;
2025-08-19 19:43:02 +02:00
using BitmapSource = Avalonia.Media.Imaging.Bitmap;
2022-01-18 18:07:19 +01:00
#endif
namespace MapControl
{
public static partial class GeoImage
2022-01-18 18:07:19 +01:00
{
private class GeoBitmap
{
2025-01-01 19:45:40 +01:00
public GeoBitmap(BitmapSource bitmap, Matrix transform, MapProjection projection)
{
var p1 = transform.Transform(new Point());
#if AVALONIA
2025-08-09 00:29:26 +02:00
var p2 = transform.Transform(new Point(bitmap.PixelSize.Width, bitmap.PixelSize.Height));
#else
2025-08-09 00:29:26 +02:00
var p2 = transform.Transform(new Point(bitmap.PixelWidth, bitmap.PixelHeight));
#endif
2025-01-01 19:45:40 +01:00
BitmapSource = bitmap;
LatLonBox = projection != null
? new LatLonBox(projection.MapToBoundingBox(new Rect(p1, p2)))
: new LatLonBox(p1.Y, p1.X, p2.Y, p2.X);
}
2024-09-13 23:52:17 +02:00
public BitmapSource BitmapSource { get; }
public LatLonBox LatLonBox { get; }
}
private const ushort ProjectedCRSGeoKey = 3072;
private const ushort GeoKeyDirectoryTag = 34735;
private const ushort ModelPixelScaleTag = 33550;
private const ushort ModelTiePointTag = 33922;
private const ushort ModelTransformationTag = 34264;
private const ushort NoDataTag = 42113;
private static string QueryString(ushort tag) => $"/ifd/{{ushort={tag}}}";
2022-01-18 18:07:19 +01:00
2025-08-22 11:06:37 +02:00
private static ILogger logger;
private static ILogger Logger => logger ?? (logger = ImageLoader.LoggerFactory?.CreateLogger(nameof(GeoImage)));
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty SourcePathProperty =
DependencyPropertyHelper.RegisterAttached<string>("SourcePath", typeof(GeoImage), null,
async (element, oldValue, newValue) => await LoadGeoImageAsync(element, newValue));
2024-09-02 15:49:53 +02:00
2024-09-06 23:50:59 +02:00
public static string GetSourcePath(FrameworkElement image)
2024-09-02 15:49:53 +02:00
{
return (string)image.GetValue(SourcePathProperty);
}
2024-09-06 23:50:59 +02:00
public static void SetSourcePath(FrameworkElement image, string value)
2024-09-02 15:49:53 +02:00
{
image.SetValue(SourcePathProperty, value);
}
2022-01-18 18:07:19 +01:00
public static async Task<Image> CreateAsync(string sourcePath)
{
var image = new Image();
2024-09-02 15:49:53 +02:00
await LoadGeoImageAsync(image, sourcePath);
2024-09-02 15:49:53 +02:00
return image;
}
public static async Task LoadGeoImageAsync(this FrameworkElement element, string sourcePath)
2022-01-18 18:07:19 +01:00
{
2024-09-02 19:41:44 +02:00
if (!string.IsNullOrEmpty(sourcePath))
2022-01-18 18:07:19 +01:00
{
2024-08-31 13:00:16 +02:00
try
{
2025-01-26 22:40:33 +01:00
var geoBitmap = await LoadGeoBitmap(sourcePath);
2024-09-03 08:29:51 +02:00
2024-09-06 23:50:59 +02:00
if (element is Image image)
{
image.Stretch = Stretch.Fill;
image.Source = geoBitmap.BitmapSource;
2024-09-06 23:50:59 +02:00
}
else if (element is Shape shape)
{
shape.Stretch = Stretch.Fill;
shape.Fill = new ImageBrush
{
Stretch = Stretch.Fill,
#if AVALONIA
Source = geoBitmap.BitmapSource
#else
ImageSource = geoBitmap.BitmapSource
#endif
};
2024-09-06 23:50:59 +02:00
}
2024-09-02 15:49:53 +02:00
MapPanel.SetBoundingBox(element, geoBitmap.LatLonBox);
2024-08-31 13:00:16 +02:00
}
catch (Exception ex)
{
2025-08-22 11:06:37 +02:00
Logger?.LogError(ex, "Failed loading from {path}", sourcePath);
2024-08-31 13:00:16 +02:00
}
}
}
2025-01-26 22:40:33 +01:00
private static async Task<GeoBitmap> LoadGeoBitmap(string sourcePath)
2024-08-31 13:00:16 +02:00
{
2025-08-19 19:43:02 +02:00
var ext = System.IO.Path.GetExtension(sourcePath);
2022-01-18 18:07:19 +01:00
2024-08-31 15:56:58 +02:00
if (ext.Length >= 4)
{
var dir = Path.GetDirectoryName(sourcePath);
var file = Path.GetFileNameWithoutExtension(sourcePath);
var worldFilePath = Path.Combine(dir, file + ext.Remove(2, 1) + "w");
2025-02-23 22:12:53 +01:00
if (File.Exists(worldFilePath))
2022-01-18 18:07:19 +01:00
{
2025-02-23 22:12:53 +01:00
return new GeoBitmap(
(BitmapSource)await ImageLoader.LoadImageAsync(sourcePath),
await ReadWorldFileMatrix(worldFilePath),
null);
2022-01-18 18:07:19 +01:00
}
2024-08-31 15:56:58 +02:00
}
2022-01-18 18:07:19 +01:00
2025-01-26 22:40:33 +01:00
return await LoadGeoTiff(sourcePath);
2022-01-18 18:07:19 +01:00
}
2025-01-26 22:40:33 +01:00
private static async Task<Matrix> ReadWorldFileMatrix(string worldFilePath)
2022-01-18 18:07:19 +01:00
{
using (var fileStream = File.OpenRead(worldFilePath))
using (var streamReader = new StreamReader(fileStream))
{
var parameters = new double[6];
var index = 0;
string line;
2024-09-02 15:49:53 +02:00
while (index < 6 &&
(line = await streamReader.ReadLineAsync()) != null &&
double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double parameter))
{
parameters[index++] = parameter;
}
if (index != 6)
{
throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}.");
}
2024-08-31 13:00:16 +02:00
return new Matrix(
parameters[0], // line 1: A or M11
parameters[1], // line 2: D or M12
parameters[2], // line 3: B or M21
parameters[3], // line 4: E or M22
parameters[4], // line 5: C or OffsetX
parameters[5]); // line 6: F or OffsetY
}
2024-08-31 13:00:16 +02:00
}
2024-09-01 23:57:47 +02:00
private static MapProjection GetProjection(short[] geoKeyDirectory)
2024-09-01 23:57:47 +02:00
{
for (var i = 4; i < geoKeyDirectory.Length - 3; i += 4)
{
if (geoKeyDirectory[i] == ProjectedCRSGeoKey && geoKeyDirectory[i + 1] == 0)
{
var epsgCode = geoKeyDirectory[i + 3];
return MapProjectionFactory.Instance.GetProjection($"EPSG:{epsgCode}");
2024-09-01 23:57:47 +02:00
}
}
return null;
2024-09-01 23:57:47 +02:00
}
2022-01-18 18:07:19 +01:00
}
}