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

187 lines
6.5 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;
using System.Globalization;
using System.IO;
2024-05-31 19:51:37 +02:00
using Path = System.IO.Path;
2022-01-18 18:07:19 +01:00
using System.Linq;
using System.Threading.Tasks;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
2022-01-18 18:07:19 +01:00
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
2022-01-18 18:07:19 +01:00
#endif
namespace MapControl
{
2024-05-31 19:51:37 +02:00
public partial class GeoImage : Grid
2022-01-18 18:07:19 +01:00
{
private class GeoBitmap
{
2024-05-31 19:51:37 +02:00
public BitmapSource Bitmap { get; set; }
public Matrix Transform { get; set; }
public MapProjection Projection { get; set; }
}
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-05-23 18:22:52 +02:00
DependencyPropertyHelper.Register<GeoImage, string>(nameof(SourcePath), null,
2024-05-23 18:08:14 +02:00
async (image, oldValue, newValue) => await image.SourcePathPropertyChanged(newValue));
2022-01-18 18:07:19 +01:00
public string SourcePath
{
get => (string)GetValue(SourcePathProperty);
set => SetValue(SourcePathProperty, value);
}
2022-01-18 23:38:25 +01:00
private async Task SourcePathPropertyChanged(string sourcePath)
2022-01-18 18:07:19 +01:00
{
2024-05-31 19:51:37 +02:00
if (sourcePath == null)
2022-01-18 18:07:19 +01:00
{
2024-05-31 19:51:37 +02:00
return;
}
2022-01-18 23:38:25 +01:00
2024-05-31 19:51:37 +02:00
GeoBitmap geoBitmap = null;
var ext = Path.GetExtension(sourcePath);
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +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");
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
if (File.Exists(worldFilePath))
2022-01-18 18:07:19 +01:00
{
2024-05-31 19:51:37 +02:00
geoBitmap = await ReadWorldFileImageAsync(sourcePath, worldFilePath);
2022-01-18 18:07:19 +01:00
}
2024-05-31 19:51:37 +02:00
}
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
if (geoBitmap == null)
{
#if AVALONIA
return;
#else
geoBitmap = await ReadGeoTiffAsync(sourcePath);
#endif
}
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
var image = new Image
{
Source = geoBitmap.Bitmap,
Stretch = Stretch.Fill,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
};
2024-05-31 19:51:37 +02:00
var transform = geoBitmap.Transform;
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
if (transform.M12 != 0 && transform.M21 != 0)
{
var rotation = (Math.Atan2(transform.M12, transform.M11) + Math.Atan2(transform.M21, -transform.M22)) * 90d / Math.PI;
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
image.RenderTransform = new RotateTransform { Angle = -rotation };
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
// Calculate effective unrotated transform.
//
geoBitmap.Transform = new Matrix(
Math.Sqrt(transform.M11 * transform.M11 + transform.M12 * transform.M12), 0d, 0d,
-Math.Sqrt(transform.M22 * transform.M22 + transform.M21 * transform.M21), 0d, 0d);
}
2024-05-31 20:11:23 +02:00
2024-05-31 19:51:37 +02:00
#if AVALONIA
2024-05-31 20:11:23 +02:00
var size = new Point(geoBitmap.Bitmap.PixelSize.Width, geoBitmap.Bitmap.PixelSize.Height);
2024-05-31 19:51:37 +02:00
#else
2024-05-31 20:11:23 +02:00
var size = new Point(geoBitmap.Bitmap.PixelWidth, geoBitmap.Bitmap.PixelHeight);
2024-05-31 19:51:37 +02:00
#endif
2024-05-31 20:11:23 +02:00
var rect = new Rect(transform.Transform(new Point()), transform.Transform(size));
2022-01-18 18:07:19 +01:00
2024-05-31 20:11:23 +02:00
var boundingBox = geoBitmap.Projection != null
? geoBitmap.Projection.MapToBoundingBox(rect)
: new BoundingBox(rect.Y, rect.X, rect.Y + rect.Height, rect.X + rect.Width);
2024-05-31 19:51:37 +02:00
2024-05-31 20:11:23 +02:00
MapPanel.SetBoundingBox(this, boundingBox);
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
Children.Clear();
Children.Add(image);
2022-01-18 18:07:19 +01:00
}
private static async Task<GeoBitmap> ReadWorldFileImageAsync(string sourcePath, string worldFilePath)
2022-01-18 18:07:19 +01:00
{
2024-05-31 19:51:37 +02:00
var geoBitmap = new GeoBitmap();
geoBitmap.Bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath);
2022-01-18 18:07:19 +01:00
2024-05-31 19:51:37 +02:00
geoBitmap.Transform = await Task.Run(() =>
2022-01-18 18:07:19 +01:00
{
var parameters = File.ReadLines(worldFilePath)
.Take(6)
.Select((line, i) =>
{
if (!double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double parameter))
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"Failed parsing line {i + 1} in world file {worldFilePath}.");
2022-01-18 18:07:19 +01:00
}
return parameter;
})
.ToList();
if (parameters.Count != 6)
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}.");
2022-01-18 18:07:19 +01: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-05-31 19:51:37 +02:00
return geoBitmap;
}
private static MapProjection GetProjection(string sourcePath, short[] geoKeyDirectory)
{
MapProjection projection = null;
for (int i = 4; i < geoKeyDirectory.Length - 3; i += 4)
{
if (geoKeyDirectory[i] == ProjectedCRSGeoKey && geoKeyDirectory[i + 1] == 0)
{
2022-12-13 22:12:49 +01:00
int epsgCode = geoKeyDirectory[i + 3];
projection = MapProjectionFactory.Instance.GetProjection(epsgCode) ??
2022-12-13 22:12:49 +01:00
throw new ArgumentException($"Can not create projection EPSG:{epsgCode} in {sourcePath}.");
break;
}
}
return projection;
2022-01-18 18:07:19 +01:00
}
}
}