mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 22:46:58 +00:00
Added GeoImage
This commit is contained in:
parent
6741e01af6
commit
9d368d7ee8
5 changed files with 323 additions and 18 deletions
76
MapControl/WinUI/GeoImage.WinUI.cs
Normal file
76
MapControl/WinUI/GeoImage.WinUI.cs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2022 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Graphics.Imaging;
|
||||
using Windows.Storage;
|
||||
#if WINUI
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
#else
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public partial class GeoImage
|
||||
{
|
||||
public static async Task<Tuple<BitmapSource, Matrix>> ReadGeoTiff(Uri sourceUri)
|
||||
{
|
||||
StorageFile file;
|
||||
|
||||
if (!sourceUri.IsAbsoluteUri || sourceUri.IsFile)
|
||||
{
|
||||
file = await StorageFile.GetFileFromPathAsync(
|
||||
sourceUri.IsAbsoluteUri ? sourceUri.LocalPath : Path.GetFullPath(sourceUri.OriginalString));
|
||||
}
|
||||
else
|
||||
{
|
||||
file = await StorageFile.GetFileFromApplicationUriAsync(sourceUri);
|
||||
}
|
||||
|
||||
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 \"" + sourceUri + "\".");
|
||||
}
|
||||
|
||||
return new Tuple<BitmapSource, Matrix>(bitmap, transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue