Changed SourceUri to SourcePath

This commit is contained in:
Clemens 2022-01-18 23:38:25 +01:00
parent b04ed4eebe
commit 9cb37ba921
3 changed files with 33 additions and 41 deletions

View file

@ -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<BitmapSource, Matrix> 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<Tuple<BitmapSource, Matrix>> ReadWorldFileImage(string imageFilePath, string worldFilePath)
private static async Task<Tuple<BitmapSource, Matrix>> ReadWorldFileImage(string sourcePath, string worldFilePath)
{
var bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(imageFilePath);
var bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath);
var transform = await Task.Run(() =>
{

View file

@ -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<Tuple<BitmapSource, Matrix>> ReadGeoTiff(Uri sourceUri)
private static async Task<Tuple<BitmapSource, Matrix>> 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))

View file

@ -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<Tuple<BitmapSource, Matrix>> ReadGeoTiff(Uri sourceUri)
public static async Task<Tuple<BitmapSource, Matrix>> 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<BitmapSource, Matrix>(bitmap, transform);