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

165 lines
5.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;
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;
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-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;
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;
private BoundingBox boundingBox;
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,
async (image, oldValue, newValue) => await LoadGeoImageAsync((Image)image, newValue));
public static string GetSourcePath(Image image)
{
return (string)image.GetValue(SourcePathProperty);
}
public static void SetSourcePath(Image image, string value)
{
image.SetValue(SourcePathProperty, value);
}
2022-01-18 18:07:19 +01:00
2024-09-02 15:49:53 +02:00
public static Image LoadGeoImage(string sourcePath)
{
2024-09-02 15:49:53 +02:00
var image = new Image();
SetSourcePath(image, sourcePath);
return image;
}
2024-09-02 15:49:53 +02:00
private static async Task LoadGeoImageAsync(Image image, 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);
image.Source = geoImage.bitmapSource;
image.Stretch = Stretch.Fill;
MapPanel.SetBoundingBox(image, geoImage.boundingBox);
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
var p1 = transformMatrix.Transform(new Point());
var p2 = transformMatrix.Transform(BitmapSize);
2022-01-18 18:07:19 +01:00
2024-09-02 15:49:53 +02:00
boundingBox = mapProjection != null
? mapProjection.MapToBoundingBox(new Rect(p1, p2))
: new BoundingBox(p1.Y, p1.X, p2.Y, p2.X);
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-02 15:49:53 +02:00
mapProjection = MapProjectionFactory.Instance.GetProjection(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
}
}