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

175 lines
6 KiB
C#
Raw Normal View History

2023-01-03 15:12:53 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
2022-01-18 18:07:19 +01:00
// Licensed under the Microsoft Public License (Ms-PL)
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.Linq;
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-01 23:57:47 +02:00
public partial class GeoImage
2022-01-18 18:07:19 +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-09-02 15:49:53 +02:00
private BitmapSource bitmapSource;
private Matrix transformMatrix;
private MapProjection mapProjection;
2024-05-23 18:08:14 +02:00
public static readonly DependencyProperty SourcePathProperty =
2024-09-02 15:49:53 +02:00
DependencyPropertyHelper.RegisterAttached<GeoImage, string>("SourcePath", null,
2024-09-06 23:50:59 +02:00
async (image, oldValue, newValue) => await LoadGeoImageAsync(image, 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-07 06:58:43 +02:00
public static Image LoadGeoImage(string sourcePath)
{
2024-09-06 23:50:59 +02:00
var image = new Image { Stretch = Stretch.Fill };
2024-09-02 15:49:53 +02:00
SetSourcePath(image, sourcePath);
return image;
}
2024-09-06 23:50:59 +02:00
private static async Task LoadGeoImageAsync(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
{
2024-09-02 15:49:53 +02:00
var geoImage = new GeoImage();
await geoImage.LoadGeoImageAsync(sourcePath);
2024-09-03 08:29:51 +02:00
var p1 = geoImage.transformMatrix.Transform(new Point());
var p2 = geoImage.transformMatrix.Transform(geoImage.BitmapSize);
2024-09-11 11:53:09 +02:00
var latLonBox = geoImage.mapProjection != null
? new LatLonBox(geoImage.mapProjection.MapToBoundingBox(new Rect(p1, p2)))
: new LatLonBox(p1.Y, p1.X, p2.Y, p2.X);
2024-09-03 08:29:51 +02:00
2024-09-06 23:50:59 +02:00
if (element is Image image)
{
image.Source = geoImage.bitmapSource;
}
else if (element is Shape shape)
{
shape.Fill = geoImage.ImageBrush;
}
2024-09-02 15:49:53 +02:00
2024-09-11 11:53:09 +02:00
MapPanel.SetBoundingBox(element, 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
}
}
}
2024-09-02 15:49:53 +02:00
private async Task LoadGeoImageAsync(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");
if (File.Exists(worldFilePath))
2022-01-18 18:07:19 +01:00
{
2024-09-02 15:49:53 +02:00
await LoadWorldFileImageAsync(sourcePath, worldFilePath);
2022-01-18 18:07:19 +01:00
}
2024-08-31 15:56:58 +02:00
}
2022-01-18 18:07:19 +01:00
2024-09-02 15:49:53 +02:00
if (bitmapSource == null)
2024-09-01 14:33:34 +02:00
{
2024-09-02 15:49:53 +02:00
await LoadGeoTiffAsync(sourcePath);
2024-08-31 15:56:58 +02:00
}
2022-01-18 18:07:19 +01:00
}
2024-09-02 15:49:53 +02:00
private async Task LoadWorldFileImageAsync(string sourcePath, string worldFilePath)
2022-01-18 18:07:19 +01:00
{
2024-09-02 15:49:53 +02:00
transformMatrix = await Task.Run(() => ReadWorldFileMatrix(worldFilePath));
bitmapSource = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath);
}
2024-08-31 13:00:16 +02:00
private static Matrix ReadWorldFileMatrix(string worldFilePath)
{
var parameters = File.ReadLines(worldFilePath)
.Select(line => double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double p) ? (double?)p : null)
.Where(p => p.HasValue)
.Select(p => p.Value)
.Take(6)
.ToList();
if (parameters.Count != 6)
{
throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}.");
}
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-09-01 23:57:47 +02:00
2024-09-02 15:49:53 +02:00
private void SetProjection(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-09 17:22:33 +02:00
mapProjection = MapProjectionFactory.Instance.GetProjection($"EPSG:{epsgCode}") ??
2024-09-01 23:57:47 +02:00
throw new ArgumentException($"Can not create projection EPSG:{epsgCode}.");
}
}
}
2022-01-18 18:07:19 +01:00
}
}