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

74 lines
2.8 KiB
C#
Raw Normal View History

2025-02-27 18:46:32 +01:00
using System;
2022-01-18 18:07:19 +01:00
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage;
2024-09-06 23:50:59 +02:00
#if UWP
using Windows.UI.Xaml.Media.Imaging;
2024-09-06 23:50:59 +02:00
#else
using Microsoft.UI.Xaml.Media.Imaging;
2024-09-06 23:50:59 +02:00
#endif
2022-01-18 18:07:19 +01:00
namespace MapControl
{
public static partial class GeoImage
2022-01-18 18:07:19 +01:00
{
2025-01-26 22:40:33 +01:00
private static async Task<GeoBitmap> LoadGeoTiff(string sourcePath)
2022-01-18 18:07:19 +01:00
{
2025-01-01 19:45:40 +01:00
BitmapSource bitmap;
Matrix transform;
MapProjection projection = null;
2022-12-06 18:07:38 +01:00
var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath));
2022-01-18 18:07:19 +01:00
2025-09-14 21:02:21 +02:00
using var stream = await file.OpenReadAsync();
2024-05-31 19:51:37 +02:00
2025-09-14 21:02:21 +02:00
var decoder = await BitmapDecoder.CreateAsync(stream);
2022-01-18 18:07:19 +01:00
2025-09-14 21:02:21 +02:00
bitmap = await ImageLoader.LoadWriteableBitmapAsync(decoder);
2022-01-18 18:07:19 +01:00
2025-09-14 21:02:21 +02:00
var geoKeyDirectoryQuery = QueryString(GeoKeyDirectoryTag);
var pixelScaleQuery = QueryString(ModelPixelScaleTag);
var tiePointQuery = QueryString(ModelTiePointTag);
var transformationQuery = QueryString(ModelTransformationTag);
var metadata = await decoder.BitmapProperties.GetPropertiesAsync(
new string[]
2022-01-18 18:07:19 +01:00
{
2025-09-14 21:02:21 +02:00
pixelScaleQuery,
tiePointQuery,
transformationQuery,
geoKeyDirectoryQuery
});
2022-01-18 18:07:19 +01:00
2025-09-14 21:02:21 +02:00
if (metadata.TryGetValue(pixelScaleQuery, out BitmapTypedValue pixelScaleValue) &&
pixelScaleValue.Value is double[] pixelScale &&
pixelScale.Length == 3 &&
metadata.TryGetValue(tiePointQuery, out BitmapTypedValue tiePointValue) &&
tiePointValue.Value is double[] tiePoint &&
tiePoint.Length >= 6)
{
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
}
else if (metadata.TryGetValue(transformationQuery, out BitmapTypedValue transformValue) &&
transformValue.Value is double[] transformValues &&
transformValues.Length == 16)
{
transform = new Matrix(transformValues[0], transformValues[1],
transformValues[4], transformValues[5],
transformValues[3], transformValues[7]);
}
else
{
throw new ArgumentException("No coordinate transformation found.");
}
if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) &&
geoKeyDirValue.Value is short[] geoKeyDirectory)
{
projection = GetProjection(geoKeyDirectory);
2022-01-18 18:07:19 +01:00
}
2025-01-01 19:45:40 +01:00
return new GeoBitmap(bitmap, transform, projection);
2022-01-18 18:07:19 +01:00
}
}
}