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

72 lines
3 KiB
C#
Raw Normal View History

2022-01-18 18:07:19 +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.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage;
namespace MapControl
{
public partial class GeoImage
{
private static async Task<GeoBitmap> ReadGeoTiffAsync(string sourcePath)
2022-01-18 18:07:19 +01:00
{
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())
{
Matrix transform;
MapProjection projection = null;
2022-01-18 18:07:19 +01:00
var decoder = await BitmapDecoder.CreateAsync(stream);
var bitmap = await ImageLoader.LoadImageAsync(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
{
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
}
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
{
transform = new Matrix(transformValues[0], transformValues[1],
transformValues[4], transformValues[5],
transformValues[3], transformValues[7]);
2022-01-18 18:07:19 +01:00
}
else
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No coordinate transformation found in {sourcePath}.");
2022-01-18 18:07:19 +01:00
}
if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) &&
geoKeyDirValue.Value is short[] geoKeyDirectory)
{
projection = GetProjection(sourcePath, geoKeyDirectory);
}
return new GeoBitmap(bitmap, transform, projection);
2022-01-18 18:07:19 +01:00
}
}
}
}