2025-02-27 18:46:32 +01:00
|
|
|
|
using System;
|
2024-08-31 13:00:16 +02:00
|
|
|
|
using System.Diagnostics;
|
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
|
|
|
|
|
|
using Shape = Avalonia.Controls.Shapes.Shape;
|
2022-01-18 18:07:19 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2024-09-13 22:21:38 +02:00
|
|
|
|
public static partial class GeoImage
|
2022-01-18 18:07:19 +01:00
|
|
|
|
{
|
2024-09-14 13:26:57 +02:00
|
|
|
|
private class GeoBitmap
|
2024-09-13 22:21:38 +02:00
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
public GeoBitmap(BitmapSource bitmap, Matrix transform, MapProjection projection)
|
2024-09-13 22:21:38 +02:00
|
|
|
|
{
|
|
|
|
|
|
var p1 = transform.Transform(new Point());
|
2024-09-14 13:26:57 +02:00
|
|
|
|
var p2 = transform.Transform(new Point(
|
|
|
|
|
|
#if AVALONIA
|
2025-01-01 19:45:40 +01:00
|
|
|
|
bitmap.PixelSize.Width, bitmap.PixelSize.Height));
|
2024-09-14 13:26:57 +02:00
|
|
|
|
#else
|
2025-01-01 19:45:40 +01:00
|
|
|
|
bitmap.PixelWidth, bitmap.PixelHeight));
|
2024-09-14 13:26:57 +02:00
|
|
|
|
#endif
|
2025-01-01 19:45:40 +01:00
|
|
|
|
BitmapSource = bitmap;
|
2024-09-13 22:21:38 +02:00
|
|
|
|
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; }
|
2024-09-13 22:21:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-09 19:57:52 +01:00
|
|
|
|
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
|
|
|
|
|
2024-05-23 18:08:14 +02:00
|
|
|
|
public static readonly DependencyProperty SourcePathProperty =
|
2024-09-13 22:21:38 +02:00
|
|
|
|
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
|
|
|
|
|
2024-09-13 23:47:17 +02:00
|
|
|
|
public static async Task<Image> CreateAsync(string sourcePath)
|
2022-12-09 19:57:52 +01:00
|
|
|
|
{
|
2024-09-13 22:21:38 +02:00
|
|
|
|
var image = new Image();
|
2024-09-02 15:49:53 +02:00
|
|
|
|
|
2024-09-13 23:47:17 +02:00
|
|
|
|
await LoadGeoImageAsync(image, sourcePath);
|
2024-09-02 15:49:53 +02:00
|
|
|
|
|
|
|
|
|
|
return image;
|
2022-12-09 19:57:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-13 22:21:38 +02:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2024-09-13 22:21:38 +02:00
|
|
|
|
image.Stretch = Stretch.Fill;
|
2024-09-14 13:26:57 +02:00
|
|
|
|
image.Source = geoBitmap.BitmapSource;
|
2024-09-06 23:50:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
else if (element is Shape shape)
|
|
|
|
|
|
{
|
2024-09-13 22:21:38 +02:00
|
|
|
|
shape.Stretch = Stretch.Fill;
|
2024-09-14 13:26:57 +02:00
|
|
|
|
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
|
|
|
|
|
2024-09-13 22:21:38 +02:00
|
|
|
|
MapPanel.SetBoundingBox(element, geoBitmap.LatLonBox);
|
2024-08-31 13:00:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-08-31 16:39:49 +02:00
|
|
|
|
Debug.WriteLine($"{nameof(GeoImage)}: {sourcePath}: {ex.Message}");
|
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
|
|
|
|
{
|
2024-08-31 15:56:58 +02:00
|
|
|
|
var ext = 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
|
|
|
|
{
|
2024-09-13 22:21:38 +02: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
|
|
|
|
|
2024-09-13 22:21:38 +02:00
|
|
|
|
while (index < 6 &&
|
|
|
|
|
|
(line = await streamReader.ReadLineAsync()) != null &&
|
|
|
|
|
|
double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double parameter))
|
|
|
|
|
|
{
|
|
|
|
|
|
parameters[index++] = parameter;
|
|
|
|
|
|
}
|
2022-12-09 19:57:52 +01:00
|
|
|
|
|
2024-09-13 22:21:38 +02:00
|
|
|
|
if (index != 6)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}.");
|
|
|
|
|
|
}
|
2024-08-31 13:00:16 +02:00
|
|
|
|
|
2024-09-13 22:21:38 +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
|
|
|
|
|
2024-09-13 22:21:38 +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];
|
|
|
|
|
|
|
2024-09-13 22:21:38 +02:00
|
|
|
|
return MapProjectionFactory.Instance.GetProjection($"EPSG:{epsgCode}");
|
2024-09-01 23:57:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-13 22:21:38 +02:00
|
|
|
|
|
|
|
|
|
|
return null;
|
2024-09-01 23:57:47 +02:00
|
|
|
|
}
|
2022-01-18 18:07:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|