GeoImage and DependencyPropertyHelper implementation

This commit is contained in:
ClemensFischer 2024-09-13 22:21:38 +02:00
parent 0344db4b9b
commit 1b0e73dc35
12 changed files with 190 additions and 154 deletions

View file

@ -10,6 +10,23 @@ namespace MapControl
{ {
public static class DependencyPropertyHelper public static class DependencyPropertyHelper
{ {
public static AttachedProperty<TValue> RegisterAttached<TValue>(
string name,
Type ownerType,
TValue defaultValue = default,
Action<Control, TValue, TValue> changed = null,
bool inherits = false)
{
var property = AvaloniaProperty.RegisterAttached<Control, TValue>(name, ownerType, defaultValue, inherits);
if (changed != null)
{
property.Changed.AddClassHandler<Control, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
}
return property;
}
public static StyledProperty<TValue> Register<TOwner, TValue>( public static StyledProperty<TValue> Register<TOwner, TValue>(
string name, string name,
TValue defaultValue = default, TValue defaultValue = default,
@ -38,22 +55,6 @@ namespace MapControl
return property; return property;
} }
public static AttachedProperty<TValue> RegisterAttached<TOwner, TValue>(
string name,
TValue defaultValue = default,
Action<Control, TValue, TValue> changed = null,
bool inherits = false)
{
var property = AvaloniaProperty.RegisterAttached<TOwner, Control, TValue>(name, defaultValue, inherits);
if (changed != null)
{
property.Changed.AddClassHandler<Control, TValue>((o, e) => changed(o, e.OldValue.Value, e.NewValue.Value));
}
return property;
}
public static StyledProperty<TValue> AddOwner<TOwner, TValue>( public static StyledProperty<TValue> AddOwner<TOwner, TValue>(
StyledProperty<TValue> property, StyledProperty<TValue> property,
TValue defaultValue = default, TValue defaultValue = default,

View file

@ -7,13 +7,16 @@ using System.Threading.Tasks;
namespace MapControl namespace MapControl
{ {
public partial class GeoImage public static partial class GeoImage
{ {
private Point BitmapSize => new(bitmapSource.PixelSize.Width, bitmapSource.PixelSize.Height); private partial class GeoBitmap
{
public Point BitmapSize => new(BitmapSource.PixelSize.Width, BitmapSource.PixelSize.Height);
private ImageBrush ImageBrush => new(bitmapSource); public ImageBrush ImageBrush => new(BitmapSource);
}
private Task LoadGeoTiffAsync(string sourcePath) private static Task<GeoBitmap> LoadGeoTiffAsync(string sourcePath)
{ {
throw new InvalidOperationException("GeoTIFF is not supported."); throw new InvalidOperationException("GeoTIFF is not supported.");
} }

View file

@ -9,13 +9,13 @@ namespace MapControl
public partial class MapPanel public partial class MapPanel
{ {
public static readonly AttachedProperty<bool> AutoCollapseProperty = public static readonly AttachedProperty<bool> AutoCollapseProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, bool>("AutoCollapse"); DependencyPropertyHelper.RegisterAttached<bool>("AutoCollapse", typeof(MapPanel));
public static readonly AttachedProperty<Location> LocationProperty = public static readonly AttachedProperty<Location> LocationProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, Location>("Location"); DependencyPropertyHelper.RegisterAttached<Location>("Location", typeof(MapPanel));
public static readonly AttachedProperty<BoundingBox> BoundingBoxProperty = public static readonly AttachedProperty<BoundingBox> BoundingBoxProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, BoundingBox>("BoundingBox"); DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel));
protected IEnumerable<Control> ChildElements => Children; protected IEnumerable<Control> ChildElements => Children;

View file

@ -6,7 +6,6 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WPF #if WPF
using System.Windows; using System.Windows;
@ -32,8 +31,26 @@ using Shape = Avalonia.Controls.Shapes.Shape;
namespace MapControl namespace MapControl
{ {
public partial class GeoImage public static partial class GeoImage
{ {
private partial class GeoBitmap
{
public BitmapSource BitmapSource { get; }
public LatLonBox LatLonBox { get; }
public GeoBitmap(BitmapSource bitmapSource, Matrix transform, MapProjection projection)
{
BitmapSource = bitmapSource;
var p1 = transform.Transform(new Point());
var p2 = transform.Transform(BitmapSize);
LatLonBox = projection != null
? new LatLonBox(projection.MapToBoundingBox(new Rect(p1, p2)))
: new LatLonBox(p1.Y, p1.X, p2.Y, p2.X);
}
}
private const ushort ProjectedCRSGeoKey = 3072; private const ushort ProjectedCRSGeoKey = 3072;
private const ushort GeoKeyDirectoryTag = 34735; private const ushort GeoKeyDirectoryTag = 34735;
private const ushort ModelPixelScaleTag = 33550; private const ushort ModelPixelScaleTag = 33550;
@ -43,13 +60,9 @@ namespace MapControl
private static string QueryString(ushort tag) => $"/ifd/{{ushort={tag}}}"; private static string QueryString(ushort tag) => $"/ifd/{{ushort={tag}}}";
private BitmapSource bitmapSource;
private Matrix transformMatrix;
private MapProjection mapProjection;
public static readonly DependencyProperty SourcePathProperty = public static readonly DependencyProperty SourcePathProperty =
DependencyPropertyHelper.RegisterAttached<GeoImage, string>("SourcePath", null, DependencyPropertyHelper.RegisterAttached<string>("SourcePath", typeof(GeoImage), null,
async (image, oldValue, newValue) => await LoadGeoImageAsync(image, newValue)); async (element, oldValue, newValue) => await LoadGeoImageAsync(element, newValue));
public static string GetSourcePath(FrameworkElement image) public static string GetSourcePath(FrameworkElement image)
{ {
@ -63,40 +76,33 @@ namespace MapControl
public static Image LoadGeoImage(string sourcePath) public static Image LoadGeoImage(string sourcePath)
{ {
var image = new Image { Stretch = Stretch.Fill }; var image = new Image();
SetSourcePath(image, sourcePath); SetSourcePath(image, sourcePath);
return image; return image;
} }
private static async Task LoadGeoImageAsync(FrameworkElement element, string sourcePath) public static async Task LoadGeoImageAsync(this FrameworkElement element, string sourcePath)
{ {
if (!string.IsNullOrEmpty(sourcePath)) if (!string.IsNullOrEmpty(sourcePath))
{ {
try try
{ {
var geoImage = new GeoImage(); var geoBitmap = await LoadGeoBitmapAsync(sourcePath);
await geoImage.LoadGeoImageAsync(sourcePath);
var p1 = geoImage.transformMatrix.Transform(new Point());
var p2 = geoImage.transformMatrix.Transform(geoImage.BitmapSize);
var latLonBox = geoImage.mapProjection != null
? new LatLonBox(geoImage.mapProjection.MapToBoundingBox(new Rect(p1, p2)))
: new LatLonBox(p1.Y, p1.X, p2.Y, p2.X);
if (element is Image image) if (element is Image image)
{ {
image.Source = geoImage.bitmapSource; image.Source = geoBitmap.BitmapSource;
image.Stretch = Stretch.Fill;
} }
else if (element is Shape shape) else if (element is Shape shape)
{ {
shape.Fill = geoImage.ImageBrush; shape.Fill = geoBitmap.ImageBrush;
shape.Stretch = Stretch.Fill;
} }
MapPanel.SetBoundingBox(element, latLonBox); MapPanel.SetBoundingBox(element, geoBitmap.LatLonBox);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -105,7 +111,7 @@ namespace MapControl
} }
} }
private async Task LoadGeoImageAsync(string sourcePath) private static async Task<GeoBitmap> LoadGeoBitmapAsync(string sourcePath)
{ {
var ext = Path.GetExtension(sourcePath); var ext = Path.GetExtension(sourcePath);
@ -117,33 +123,33 @@ namespace MapControl
if (File.Exists(worldFilePath)) if (File.Exists(worldFilePath))
{ {
await LoadWorldFileImageAsync(sourcePath, worldFilePath); return new GeoBitmap(
(BitmapSource)await ImageLoader.LoadImageAsync(sourcePath),
await ReadWorldFileMatrix(worldFilePath),
null);
} }
} }
if (bitmapSource == null) return await LoadGeoTiffAsync(sourcePath);
}
private static async Task<Matrix> ReadWorldFileMatrix(string worldFilePath)
{ {
await LoadGeoTiffAsync(sourcePath); using (var fileStream = File.OpenRead(worldFilePath))
} using (var streamReader = new StreamReader(fileStream))
}
private async Task LoadWorldFileImageAsync(string sourcePath, string worldFilePath)
{ {
transformMatrix = await Task.Run(() => ReadWorldFileMatrix(worldFilePath)); var parameters = new double[6];
var index = 0;
string line;
bitmapSource = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath); while (index < 6 &&
(line = await streamReader.ReadLineAsync()) != null &&
double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double parameter))
{
parameters[index++] = parameter;
} }
private static Matrix ReadWorldFileMatrix(string worldFilePath) if (index != 6)
{
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)
.ToList();
if (parameters.Count != 6)
{ {
throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}."); throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}.");
} }
@ -156,8 +162,9 @@ namespace MapControl
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
} }
}
private void SetProjection(short[] geoKeyDirectory) private static MapProjection GetProjection(short[] geoKeyDirectory)
{ {
for (var i = 4; i < geoKeyDirectory.Length - 3; i += 4) for (var i = 4; i < geoKeyDirectory.Length - 3; i += 4)
{ {
@ -165,9 +172,11 @@ namespace MapControl
{ {
var epsgCode = geoKeyDirectory[i + 3]; var epsgCode = geoKeyDirectory[i + 3];
mapProjection = MapProjectionFactory.Instance.GetProjection($"EPSG:{epsgCode}"); return MapProjectionFactory.Instance.GetProjection($"EPSG:{epsgCode}");
} }
} }
return null;
} }
} }
} }

View file

@ -24,7 +24,7 @@ namespace MapControl
DependencyPropertyHelper.Register<MapBorderPanel, double>(nameof(BorderWidth)); DependencyPropertyHelper.Register<MapBorderPanel, double>(nameof(BorderWidth));
public static readonly DependencyProperty OnBorderProperty = public static readonly DependencyProperty OnBorderProperty =
DependencyPropertyHelper.RegisterAttached<MapBorderPanel, bool>("OnBorder"); DependencyPropertyHelper.RegisterAttached<bool>("OnBorder", typeof(MapBorderPanel));
public double BorderWidth public double BorderWidth
{ {

View file

@ -37,10 +37,10 @@ namespace MapControl
public partial class MapPanel : Panel, IMapElement public partial class MapPanel : Panel, IMapElement
{ {
private static readonly DependencyProperty ViewPositionProperty = private static readonly DependencyProperty ViewPositionProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, Point?>("ViewPosition"); DependencyPropertyHelper.RegisterAttached<Point?>("ViewPosition", typeof(MapPanel));
private static readonly DependencyProperty ParentMapProperty = private static readonly DependencyProperty ParentMapProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, MapBase>("ParentMap", null, DependencyPropertyHelper.RegisterAttached<MapBase>("ParentMap", typeof(MapPanel), null,
(element, oldValue, newValue) => (element, oldValue, newValue) =>
{ {
if (element is IMapElement mapElement) if (element is IMapElement mapElement)

View file

@ -9,6 +9,44 @@ namespace MapControl
{ {
public static class DependencyPropertyHelper public static class DependencyPropertyHelper
{ {
public static DependencyProperty RegisterAttached<TValue>(
string name,
Type ownerType,
TValue defaultValue = default,
Action<FrameworkElement, TValue, TValue> changed = null,
bool inherits = false)
{
var metadata = new FrameworkPropertyMetadata
{
DefaultValue = defaultValue,
Inherits = inherits
};
if (changed != null)
{
metadata.PropertyChangedCallback = (o, e) =>
{
if (o is FrameworkElement element)
{
changed(element, (TValue)e.OldValue, (TValue)e.NewValue);
}
};
}
return DependencyProperty.RegisterAttached(name, typeof(TValue), ownerType, metadata);
}
public static DependencyProperty RegisterAttached<TValue>(
string name,
Type ownerType,
TValue defaultValue,
FrameworkPropertyMetadataOptions options)
{
var metadata = new FrameworkPropertyMetadata(defaultValue, options);
return DependencyProperty.RegisterAttached(name, typeof(TValue), ownerType, metadata);
}
public static DependencyProperty Register<TOwner, TValue>( public static DependencyProperty Register<TOwner, TValue>(
string name, string name,
TValue defaultValue, TValue defaultValue,
@ -47,42 +85,6 @@ namespace MapControl
return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata); return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata);
} }
public static DependencyProperty RegisterAttached<TOwner, TValue>(
string name,
TValue defaultValue,
FrameworkPropertyMetadataOptions options)
{
var metadata = new FrameworkPropertyMetadata(defaultValue, options);
return DependencyProperty.RegisterAttached(name, typeof(TValue), typeof(TOwner), metadata);
}
public static DependencyProperty RegisterAttached<TOwner, TValue>(
string name,
TValue defaultValue = default,
Action<FrameworkElement, TValue, TValue> changed = null,
bool inherits = false)
{
var metadata = new FrameworkPropertyMetadata
{
DefaultValue = defaultValue,
Inherits = inherits
};
if (changed != null)
{
metadata.PropertyChangedCallback = (o, e) =>
{
if (o is FrameworkElement element)
{
changed(element, (TValue)e.OldValue, (TValue)e.NewValue);
}
};
}
return DependencyProperty.RegisterAttached(name, typeof(TValue), typeof(TOwner), metadata);
}
public static DependencyPropertyKey RegisterReadOnly<TOwner, TValue>( public static DependencyPropertyKey RegisterReadOnly<TOwner, TValue>(
string name, string name,
TValue defaultValue = default) TValue defaultValue = default)

View file

@ -12,16 +12,23 @@ using System.Windows.Media.Imaging;
namespace MapControl namespace MapControl
{ {
public partial class GeoImage public static partial class GeoImage
{ {
private Point BitmapSize => new Point(bitmapSource.PixelWidth, bitmapSource.PixelHeight); private partial class GeoBitmap
{
public Point BitmapSize => new Point(BitmapSource.PixelWidth, BitmapSource.PixelHeight);
private ImageBrush ImageBrush => new ImageBrush(bitmapSource); public ImageBrush ImageBrush => new ImageBrush(BitmapSource);
}
private Task LoadGeoTiffAsync(string sourcePath) private static Task<GeoBitmap> LoadGeoTiffAsync(string sourcePath)
{ {
return Task.Run(() => return Task.Run(() =>
{ {
BitmapSource bitmapSource;
Matrix transformMatrix;
MapProjection mapProjection = null;
using (var stream = File.OpenRead(sourcePath)) using (var stream = File.OpenRead(sourcePath))
{ {
bitmapSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); bitmapSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
@ -50,7 +57,7 @@ namespace MapControl
if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory) if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory)
{ {
SetProjection(geoKeyDirectory); mapProjection = GetProjection(geoKeyDirectory);
} }
if (metadata.GetQuery(QueryString(NoDataTag)) is string noData && if (metadata.GetQuery(QueryString(NoDataTag)) is string noData &&
@ -58,6 +65,8 @@ namespace MapControl
{ {
bitmapSource = ConvertTransparentPixel(bitmapSource, noDataValue); bitmapSource = ConvertTransparentPixel(bitmapSource, noDataValue);
} }
return new GeoBitmap(bitmapSource, transformMatrix, mapProjection);
}); });
} }

View file

@ -12,14 +12,14 @@ namespace MapControl
public partial class MapPanel public partial class MapPanel
{ {
public static readonly DependencyProperty AutoCollapseProperty = public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, bool>("AutoCollapse"); DependencyPropertyHelper.RegisterAttached< bool>("AutoCollapse", typeof(MapPanel));
public static readonly DependencyProperty LocationProperty = public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, Location>("Location", null, DependencyPropertyHelper.RegisterAttached<Location>("Location", typeof(MapPanel), null,
FrameworkPropertyMetadataOptions.AffectsParentArrange); FrameworkPropertyMetadataOptions.AffectsParentArrange);
public static readonly DependencyProperty BoundingBoxProperty = public static readonly DependencyProperty BoundingBoxProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, BoundingBox>("BoundingBox", null, DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel), null,
FrameworkPropertyMetadataOptions.AffectsParentArrange); FrameworkPropertyMetadataOptions.AffectsParentArrange);
protected IEnumerable<FrameworkElement> ChildElements => InternalChildren.OfType<FrameworkElement>(); protected IEnumerable<FrameworkElement> ChildElements => InternalChildren.OfType<FrameworkElement>();

View file

@ -15,21 +15,9 @@ namespace MapControl
{ {
public static class DependencyPropertyHelper public static class DependencyPropertyHelper
{ {
public static DependencyProperty Register<TOwner, TValue>( public static DependencyProperty RegisterAttached<TValue>(
string name,
TValue defaultValue = default,
Action<TOwner, TValue, TValue> changed = null)
where TOwner : DependencyObject
{
var metadata = changed != null
? new PropertyMetadata(defaultValue, (o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue))
: new PropertyMetadata(defaultValue);
return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata);
}
public static DependencyProperty RegisterAttached<TOwner, TValue>(
string name, string name,
Type ownerType,
TValue defaultValue = default, TValue defaultValue = default,
Action<FrameworkElement, TValue, TValue> changed = null, Action<FrameworkElement, TValue, TValue> changed = null,
bool inherits = false) // unused in WinUI/UWP bool inherits = false) // unused in WinUI/UWP
@ -44,7 +32,20 @@ namespace MapControl
} }
}); });
return DependencyProperty.RegisterAttached(name, typeof(TValue), typeof(TOwner), metadata); return DependencyProperty.RegisterAttached(name, typeof(TValue), ownerType, metadata);
}
public static DependencyProperty Register<TOwner, TValue>(
string name,
TValue defaultValue = default,
Action<TOwner, TValue, TValue> changed = null)
where TOwner : DependencyObject
{
var metadata = changed != null
? new PropertyMetadata(defaultValue, (o, e) => changed((TOwner)o, (TValue)e.OldValue, (TValue)e.NewValue))
: new PropertyMetadata(defaultValue);
return DependencyProperty.Register(name, typeof(TValue), typeof(TOwner), metadata);
} }
} }
} }

View file

@ -8,20 +8,29 @@ using Windows.Graphics.Imaging;
using Windows.Storage; using Windows.Storage;
#if UWP #if UWP
using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
#else #else
using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
#endif #endif
namespace MapControl namespace MapControl
{ {
public partial class GeoImage public static partial class GeoImage
{ {
private Point BitmapSize => new Point(bitmapSource.PixelWidth, bitmapSource.PixelHeight); private partial class GeoBitmap
private ImageBrush ImageBrush => new ImageBrush { ImageSource = bitmapSource };
private async Task LoadGeoTiffAsync(string sourcePath)
{ {
public Point BitmapSize => new Point(BitmapSource.PixelWidth, BitmapSource.PixelHeight);
public ImageBrush ImageBrush => new ImageBrush { ImageSource = BitmapSource };
}
private static async Task<GeoBitmap> LoadGeoTiffAsync(string sourcePath)
{
BitmapSource bitmapSource;
Matrix transformMatrix;
MapProjection mapProjection = null;
var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath)); var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath));
using (var stream = await file.OpenReadAsync()) using (var stream = await file.OpenReadAsync())
@ -68,9 +77,11 @@ namespace MapControl
if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) && if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) &&
geoKeyDirValue.Value is short[] geoKeyDirectory) geoKeyDirValue.Value is short[] geoKeyDirectory)
{ {
SetProjection(geoKeyDirectory); mapProjection = GetProjection(geoKeyDirectory);
} }
} }
return new GeoBitmap(bitmapSource, transformMatrix, mapProjection);
} }
} }
} }

View file

@ -17,14 +17,14 @@ namespace MapControl
public partial class MapPanel public partial class MapPanel
{ {
public static readonly DependencyProperty AutoCollapseProperty = public static readonly DependencyProperty AutoCollapseProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, bool>("AutoCollapse"); DependencyPropertyHelper.RegisterAttached<bool>("AutoCollapse", typeof(MapPanel));
public static readonly DependencyProperty LocationProperty = public static readonly DependencyProperty LocationProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, Location>("Location", null, DependencyPropertyHelper.RegisterAttached<Location>("Location", typeof(MapPanel), null,
(element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange()); (element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange());
public static readonly DependencyProperty BoundingBoxProperty = public static readonly DependencyProperty BoundingBoxProperty =
DependencyPropertyHelper.RegisterAttached<MapPanel, BoundingBox>("BoundingBox", null, DependencyPropertyHelper.RegisterAttached<BoundingBox>("BoundingBox", typeof(MapPanel), null,
(element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange()); (element, oldValue, newValue) => (element.Parent as MapPanel)?.InvalidateArrange());
protected IEnumerable<FrameworkElement> ChildElements => Children.OfType<FrameworkElement>(); protected IEnumerable<FrameworkElement> ChildElements => Children.OfType<FrameworkElement>();