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

68 lines
2.5 KiB
C#
Raw Normal View History

2022-01-08 21:22:45 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// <20> 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
2022-01-09 22:40:52 +01:00
using System.IO;
2022-01-08 21:22:45 +01:00
using System.Threading.Tasks;
2022-01-09 22:40:52 +01:00
using Windows.Storage;
using System.Collections.Generic;
#if WINUI
using Windows.Graphics.Imaging;
using Microsoft.UI.Xaml.Media.Imaging;
#else
using Windows.Graphics.Imaging;
using Windows.UI.Xaml.Media.Imaging;
#endif
2022-01-08 21:22:45 +01:00
namespace MapControl.Images
{
2022-01-16 18:39:50 +01:00
public partial class GeoImage
2022-01-08 21:22:45 +01:00
{
2022-01-16 18:39:50 +01:00
public static async Task<GeoImage> ReadGeoTiff(string imageFilePath)
2022-01-08 21:22:45 +01:00
{
2022-01-09 22:40:52 +01:00
var file = await StorageFile.GetFileFromPathAsync(Path.GetFullPath(imageFilePath));
using (var stream = await file.OpenReadAsync())
{
WriteableBitmap bitmap;
Matrix transform;
var decoder = await BitmapDecoder.CreateAsync(stream);
using (var swbmp = await decoder.GetSoftwareBitmapAsync())
{
bitmap = new WriteableBitmap(swbmp.PixelWidth, swbmp.PixelHeight);
swbmp.CopyToBuffer(bitmap.PixelBuffer);
}
var query = new List<string>
{
PixelScaleQuery, TiePointQuery, TransformQuery //, NoDataQuery
};
var metadata = await decoder.BitmapProperties.GetPropertiesAsync(query);
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(TransformQuery, out BitmapTypedValue tformValue) &&
tformValue.Value is double[] tform && tform.Length == 16)
{
transform = new Matrix(tform[0], tform[1], tform[4], tform[5], tform[3], tform[7]);
}
else
{
throw new ArgumentException("No coordinate transformation found in \"" + imageFilePath + "\".");
}
2022-01-16 18:39:50 +01:00
return new GeoImage(bitmap, transform, null);
2022-01-09 22:40:52 +01:00
}
2022-01-08 21:22:45 +01:00
}
}
}