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

79 lines
3.2 KiB
C#
Raw Normal View History

2022-01-18 18:07:19 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2025-01-01 18:57:55 +01:00
// Copyright © Clemens Fischer
2022-01-18 18:07:19 +01:00
// Licensed under the Microsoft Public License (Ms-PL)
using System;
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
{
private static async Task<GeoBitmap> LoadGeoTiffAsync(string sourcePath)
2022-01-18 18:07:19 +01:00
{
BitmapSource bitmapSource;
Matrix transformMatrix;
MapProjection mapProjection = 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
using (var stream = await file.OpenReadAsync())
{
var decoder = await BitmapDecoder.CreateAsync(stream);
2024-05-31 19:51:37 +02:00
2025-01-01 18:20:33 +01:00
bitmapSource = await ImageLoader.LoadWriteableBitmapAsync(decoder);
2022-01-18 18:07:19 +01: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[]
{
pixelScaleQuery,
tiePointQuery,
transformationQuery,
geoKeyDirectoryQuery
});
2022-01-18 18:07:19 +01:00
if (metadata.TryGetValue(pixelScaleQuery, out BitmapTypedValue pixelScaleValue) &&
2022-12-09 21:08:24 +01:00
pixelScaleValue.Value is double[] pixelScale &&
pixelScale.Length == 3 &&
metadata.TryGetValue(tiePointQuery, out BitmapTypedValue tiePointValue) &&
2022-12-09 21:08:24 +01:00
tiePointValue.Value is double[] tiePoint &&
tiePoint.Length >= 6)
2022-01-18 18:07:19 +01:00
{
2024-09-02 15:49:53 +02:00
transformMatrix = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
2022-01-18 18:07:19 +01:00
}
else if (metadata.TryGetValue(transformationQuery, out BitmapTypedValue transformValue) &&
2022-12-09 21:08:24 +01:00
transformValue.Value is double[] transformValues &&
transformValues.Length == 16)
2022-01-18 18:07:19 +01:00
{
2024-09-02 15:49:53 +02:00
transformMatrix = new Matrix(transformValues[0], transformValues[1],
transformValues[4], transformValues[5],
transformValues[3], transformValues[7]);
2022-01-18 18:07:19 +01:00
}
else
{
2024-08-31 16:39:49 +02:00
throw new ArgumentException("No coordinate transformation found.");
2022-01-18 18:07:19 +01:00
}
if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) &&
geoKeyDirValue.Value is short[] geoKeyDirectory)
{
mapProjection = GetProjection(geoKeyDirectory);
}
2022-01-18 18:07:19 +01:00
}
return new GeoBitmap(bitmapSource, transformMatrix, mapProjection);
2022-01-18 18:07:19 +01:00
}
}
}