mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 14:37:01 +00:00
Added GeoImage
This commit is contained in:
parent
6741e01af6
commit
9d368d7ee8
5 changed files with 323 additions and 18 deletions
155
MapControl/Shared/GeoImage.cs
Normal file
155
MapControl/Shared/GeoImage.cs
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
// 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.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
#if WINUI
|
||||
using Windows.Foundation;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
#elif UWP
|
||||
using Windows.Foundation;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Media;
|
||||
using Windows.UI.Xaml.Media.Imaging;
|
||||
#else
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public partial class GeoImage : ContentControl
|
||||
{
|
||||
private const string PixelScaleQuery = "/ifd/{ushort=33550}";
|
||||
private const string TiePointQuery = "/ifd/{ushort=33922}";
|
||||
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 Uri SourceUri
|
||||
{
|
||||
get { return (Uri)GetValue(SourceUriProperty); }
|
||||
set { SetValue(SourceUriProperty, value); }
|
||||
}
|
||||
|
||||
public GeoImage()
|
||||
{
|
||||
HorizontalContentAlignment = HorizontalAlignment.Stretch;
|
||||
VerticalContentAlignment = VerticalAlignment.Stretch;
|
||||
}
|
||||
|
||||
private async Task SourceUriPropertyChanged(Uri sourceUri)
|
||||
{
|
||||
Image image = null;
|
||||
BoundingBox boundingBox = null;
|
||||
|
||||
if (sourceUri != null)
|
||||
{
|
||||
Tuple<BitmapSource, Matrix> geoBitmap = null;
|
||||
|
||||
if (!sourceUri.IsAbsoluteUri || sourceUri.IsFile)
|
||||
{
|
||||
var imageFilePath = sourceUri.IsAbsoluteUri ? sourceUri.LocalPath : sourceUri.OriginalString;
|
||||
var ext = Path.GetExtension(imageFilePath);
|
||||
|
||||
if (ext.Length >= 4)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (geoBitmap == null)
|
||||
{
|
||||
geoBitmap = await ReadGeoTiff(sourceUri);
|
||||
}
|
||||
|
||||
var bitmap = geoBitmap.Item1;
|
||||
var transform = geoBitmap.Item2;
|
||||
|
||||
image = new Image
|
||||
{
|
||||
Source = bitmap,
|
||||
Stretch = Stretch.Fill
|
||||
};
|
||||
|
||||
if (transform.M12 != 0 || transform.M21 != 0)
|
||||
{
|
||||
var rotation = (Math.Atan2(transform.M12, transform.M11) + Math.Atan2(transform.M21, -transform.M22)) * 90d / Math.PI;
|
||||
|
||||
image.RenderTransform = new RotateTransform { Angle = -rotation };
|
||||
|
||||
// effective unrotated transform
|
||||
transform.M11 = Math.Sqrt(transform.M11 * transform.M11 + transform.M12 * transform.M12);
|
||||
transform.M22 = -Math.Sqrt(transform.M22 * transform.M22 + transform.M21 * transform.M21);
|
||||
transform.M12 = 0;
|
||||
transform.M21 = 0;
|
||||
}
|
||||
|
||||
var rect = new Rect(
|
||||
transform.Transform(new Point()),
|
||||
transform.Transform(new Point(bitmap.PixelWidth, bitmap.PixelHeight)));
|
||||
|
||||
boundingBox = new BoundingBox(rect.Y, rect.X, rect.Y + rect.Height, rect.X + rect.Width);
|
||||
}
|
||||
|
||||
Content = image;
|
||||
|
||||
MapPanel.SetBoundingBox(this, boundingBox);
|
||||
}
|
||||
|
||||
private static async Task<Tuple<BitmapSource, Matrix>> ReadWorldFileImage(string imageFilePath, string worldFilePath)
|
||||
{
|
||||
var bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(imageFilePath);
|
||||
|
||||
var transform = await Task.Run(() =>
|
||||
{
|
||||
var parameters = File.ReadLines(worldFilePath)
|
||||
.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();
|
||||
|
||||
if (parameters.Count != 6)
|
||||
{
|
||||
throw new ArgumentException("Insufficient number of parameters in world file \"" + worldFilePath + "\".");
|
||||
}
|
||||
|
||||
return new Matrix(
|
||||
parameters[0], // line 1: A or M11
|
||||
parameters[1], // line 2: D or M12
|
||||
parameters[2], // line 3: B or M21
|
||||
parameters[3], // line 4: E or M22
|
||||
parameters[4], // line 5: C or OffsetX
|
||||
parameters[5]); // line 6: F or OffsetY
|
||||
});
|
||||
|
||||
return new Tuple<BitmapSource, Matrix>(bitmap, transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -35,7 +35,7 @@ namespace MapControl
|
|||
|
||||
try
|
||||
{
|
||||
if (!uri.IsAbsoluteUri || uri.Scheme == "file")
|
||||
if (!uri.IsAbsoluteUri || uri.IsFile)
|
||||
{
|
||||
image = await LoadImageAsync(uri.IsAbsoluteUri ? uri.LocalPath : uri.OriginalString);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue