Update GeoImage.cs

This commit is contained in:
ClemensFischer 2024-08-31 13:00:16 +02:00
parent 71c435c323
commit 4c48f823bd

View file

@ -3,6 +3,7 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -57,6 +58,19 @@ namespace MapControl
private async Task SourcePathPropertyChanged(string sourcePath) private async Task SourcePathPropertyChanged(string sourcePath)
{ {
if (sourcePath != null) if (sourcePath != null)
{
try
{
await ReadGeoImageAsync(sourcePath);
}
catch (Exception ex)
{
Debug.WriteLine($"GeoImage: {ex.Message}");
}
}
}
private async Task ReadGeoImageAsync(string sourcePath)
{ {
GeoBitmap geoBitmap = null; GeoBitmap geoBitmap = null;
var ext = Path.GetExtension(sourcePath); var ext = Path.GetExtension(sourcePath);
@ -121,7 +135,6 @@ namespace MapControl
Children.Clear(); Children.Clear();
Children.Add(image); Children.Add(image);
} }
}
private static async Task<GeoBitmap> ReadWorldFileImageAsync(string sourcePath, string worldFilePath) private static async Task<GeoBitmap> ReadWorldFileImageAsync(string sourcePath, string worldFilePath)
{ {
@ -129,18 +142,18 @@ namespace MapControl
geoBitmap.Bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath); geoBitmap.Bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath);
geoBitmap.Transform = await Task.Run(() => geoBitmap.Transform = await Task.Run(() => ReadWorldFileMatrix(worldFilePath));
return geoBitmap;
}
private static Matrix ReadWorldFileMatrix(string worldFilePath)
{ {
var parameters = File.ReadLines(worldFilePath) var parameters = File.ReadLines(worldFilePath)
.Select(line => double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double p) ? (double?)p : null)
.Where(p => p.HasValue)
.Select(p => p.Value)
.Take(6) .Take(6)
.Select((line, i) =>
{
if (!double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double parameter))
{
throw new ArgumentException($"Failed parsing line {i + 1} in world file {worldFilePath}.");
}
return parameter;
})
.ToList(); .ToList();
if (parameters.Count != 6) if (parameters.Count != 6)
@ -155,9 +168,6 @@ namespace MapControl
parameters[3], // line 4: E or M22 parameters[3], // line 4: E or M22
parameters[4], // line 5: C or OffsetX parameters[4], // line 5: C or OffsetX
parameters[5]); // line 6: F or OffsetY parameters[5]); // line 6: F or OffsetY
});
return geoBitmap;
} }
private static MapProjection GetProjection(string sourcePath, short[] geoKeyDirectory) private static MapProjection GetProjection(string sourcePath, short[] geoKeyDirectory)