From 9cb37ba9218a882c139108fec8c0eaa0ead97e26 Mon Sep 17 00:00:00 2001 From: Clemens Date: Tue, 18 Jan 2022 23:38:25 +0100 Subject: [PATCH] Changed SourceUri to SourcePath --- MapControl/Shared/GeoImage.cs | 42 ++++++++++++++---------------- MapControl/WPF/GeoImage.WPF.cs | 15 ++++++++--- MapControl/WinUI/GeoImage.WinUI.cs | 17 +++--------- 3 files changed, 33 insertions(+), 41 deletions(-) diff --git a/MapControl/Shared/GeoImage.cs b/MapControl/Shared/GeoImage.cs index fd656f9d..c74f7e4c 100644 --- a/MapControl/Shared/GeoImage.cs +++ b/MapControl/Shared/GeoImage.cs @@ -35,14 +35,14 @@ namespace MapControl private const string TransformQuery = "/ifd/{ushort=34264}"; private const string NoDataQuery = "/ifd/{ushort=42113}"; - public static readonly DependencyProperty SourceUriProperty = DependencyProperty.Register( - nameof(SourceUri), typeof(Uri), typeof(GeoImage), - new PropertyMetadata(null, async (o, e) => await ((GeoImage)o).SourceUriPropertyChanged((Uri)e.NewValue))); + public static readonly DependencyProperty SourcePathProperty = DependencyProperty.Register( + nameof(SourcePath), typeof(string), typeof(GeoImage), + new PropertyMetadata(null, async (o, e) => await ((GeoImage)o).SourcePathPropertyChanged((string)e.NewValue))); - public Uri SourceUri + public string SourcePath { - get { return (Uri)GetValue(SourceUriProperty); } - set { SetValue(SourceUriProperty, value); } + get { return (string)GetValue(SourcePathProperty); } + set { SetValue(SourcePathProperty, value); } } public GeoImage() @@ -51,36 +51,32 @@ namespace MapControl VerticalContentAlignment = VerticalAlignment.Stretch; } - private async Task SourceUriPropertyChanged(Uri sourceUri) + private async Task SourcePathPropertyChanged(string sourcePath) { Image image = null; BoundingBox boundingBox = null; - if (sourceUri != null) + if (sourcePath != null) { Tuple geoBitmap = null; - if (!sourceUri.IsAbsoluteUri || sourceUri.IsFile) + var ext = Path.GetExtension(sourcePath); + + if (ext.Length >= 4) { - var imageFilePath = sourceUri.IsAbsoluteUri ? sourceUri.LocalPath : sourceUri.OriginalString; - var ext = Path.GetExtension(imageFilePath); + var dir = Path.GetDirectoryName(sourcePath); + var file = Path.GetFileNameWithoutExtension(sourcePath); + var worldFilePath = Path.Combine(dir, file + ext.Remove(2, 1) + "w"); - if (ext.Length >= 4) + if (File.Exists(worldFilePath)) { - var dir = Path.GetDirectoryName(imageFilePath); - var file = Path.GetFileNameWithoutExtension(imageFilePath); - var worldFilePath = Path.Combine(dir, file + ext.Remove(2, 1) + "w"); - - if (File.Exists(worldFilePath)) - { - geoBitmap = await ReadWorldFileImage(imageFilePath, worldFilePath); - } + geoBitmap = await ReadWorldFileImage(sourcePath, worldFilePath); } } if (geoBitmap == null) { - geoBitmap = await ReadGeoTiff(sourceUri); + geoBitmap = await ReadGeoTiff(sourcePath); } var bitmap = geoBitmap.Item1; @@ -117,9 +113,9 @@ namespace MapControl MapPanel.SetBoundingBox(this, boundingBox); } - private static async Task> ReadWorldFileImage(string imageFilePath, string worldFilePath) + private static async Task> ReadWorldFileImage(string sourcePath, string worldFilePath) { - var bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(imageFilePath); + var bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath); var transform = await Task.Run(() => { diff --git a/MapControl/WPF/GeoImage.WPF.cs b/MapControl/WPF/GeoImage.WPF.cs index 74502f67..e956a60c 100644 --- a/MapControl/WPF/GeoImage.WPF.cs +++ b/MapControl/WPF/GeoImage.WPF.cs @@ -3,6 +3,7 @@ // Licensed under the Microsoft Public License (Ms-PL) using System; +using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows.Media; @@ -12,14 +13,20 @@ namespace MapControl { public partial class GeoImage { - private static async Task> ReadGeoTiff(Uri sourceUri) + private static async Task> ReadGeoTiff(string sourcePath) { return await Task.Run(() => { - BitmapSource bitmap = BitmapFrame.Create(sourceUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); - var metadata = (BitmapMetadata)bitmap.Metadata; + BitmapSource bitmap; Matrix transform; + using (var stream = File.OpenRead(sourcePath)) + { + bitmap = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + } + + var metadata = (BitmapMetadata)bitmap.Metadata; + if (metadata.GetQuery(PixelScaleQuery) is double[] pixelScale && pixelScale.Length == 3 && metadata.GetQuery(TiePointQuery) is double[] tiePoint && tiePoint.Length >= 6) { @@ -31,7 +38,7 @@ namespace MapControl } else { - throw new ArgumentException("No coordinate transformation found in \"" + sourceUri + "\"."); + throw new ArgumentException("No coordinate transformation found in \"" + sourcePath + "\"."); } if (metadata.GetQuery(NoDataQuery) is string noData && int.TryParse(noData, out int noDataValue)) diff --git a/MapControl/WinUI/GeoImage.WinUI.cs b/MapControl/WinUI/GeoImage.WinUI.cs index 5b36bf86..b2d2825b 100644 --- a/MapControl/WinUI/GeoImage.WinUI.cs +++ b/MapControl/WinUI/GeoImage.WinUI.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Threading.Tasks; using Windows.Graphics.Imaging; using Windows.Storage; @@ -18,19 +17,9 @@ namespace MapControl { public partial class GeoImage { - public static async Task> ReadGeoTiff(Uri sourceUri) + public static async Task> ReadGeoTiff(string sourcePath) { - 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); - } + var file = await StorageFile.GetFileFromPathAsync(sourcePath); using (var stream = await file.OpenReadAsync()) { @@ -66,7 +55,7 @@ namespace MapControl } else { - throw new ArgumentException("No coordinate transformation found in \"" + sourceUri + "\"."); + throw new ArgumentException("No coordinate transformation found in \"" + sourcePath + "\"."); } return new Tuple(bitmap, transform);