GeoImage implementation

This commit is contained in:
ClemensFischer 2024-09-01 23:57:47 +02:00
parent 1f5e82518a
commit 07849eb1d8
4 changed files with 86 additions and 54 deletions

View file

@ -0,0 +1,23 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Threading.Tasks;
namespace MapControl
{
public partial class GeoImage : Image
{
private void SetImage(ImageSource image)
{
Source = image;
Stretch = Stretch.Fill;
}
private static Task<GeoBitmap> ReadGeoTiffAsync(string sourcePath)
{
throw new InvalidOperationException("GeoTIFF is not supported.");
}
}
}

View file

@ -10,51 +10,39 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
#if WPF #if WPF
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
#elif UWP #elif UWP
using Windows.UI.Xaml; using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Media.Imaging;
#elif WINUI #elif WINUI
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging; using Microsoft.UI.Xaml.Media.Imaging;
#endif #endif
namespace MapControl namespace MapControl
{ {
public partial class GeoImage : Grid public partial class GeoImage
{ {
private class DefaultProjection : MapProjection
{
public override Point? LocationToMap(Location location) => new Point(location.Longitude, location.Latitude);
public override Location MapToLocation(Point point) => new Location(point.Y, point.X);
}
private class GeoBitmap private class GeoBitmap
{ {
public BitmapSource Bitmap { get; set; } public BitmapSource Bitmap { get; set; }
public Matrix Transform { get; set; } public Matrix Transform { get; set; }
public MapProjection Projection { get; set; } = new DefaultProjection(); public MapProjection Projection { get; set; }
public void SetProjection(short[] geoKeyDirectory) public BoundingBox BoundingBox
{ {
for (var i = 4; i < geoKeyDirectory.Length - 3; i += 4) get
{ {
if (geoKeyDirectory[i] == ProjectedCRSGeoKey && geoKeyDirectory[i + 1] == 0) var p1 = Transform.Transform(new Point());
{ #if AVALONIA
var epsgCode = geoKeyDirectory[i + 3]; var p2 = Transform.Transform(new Point(Bitmap.PixelSize.Width, Bitmap.PixelSize.Height));
#else
var projection = MapProjectionFactory.Instance.GetProjection(epsgCode) ?? var p2 = Transform.Transform(new Point(Bitmap.PixelWidth, Bitmap.PixelHeight));
throw new ArgumentException($"Can not create projection EPSG:{epsgCode}."); #endif
return Projection != null
Projection = projection; ? Projection.MapToBoundingBox(new Rect(p1, p2))
break; : new BoundingBox(p1.Y, p1.X, p2.Y, p2.X);
}
} }
} }
} }
@ -110,37 +98,14 @@ namespace MapControl
} }
} }
#if AVALONIA
if (geoBitmap == null) return;
var width = geoBitmap.Bitmap.PixelSize.Width;
var height = geoBitmap.Bitmap.PixelSize.Height;
#else
if (geoBitmap == null) if (geoBitmap == null)
{ {
geoBitmap = await ReadGeoTiffAsync(sourcePath); geoBitmap = await ReadGeoTiffAsync(sourcePath);
} }
var width = geoBitmap.Bitmap.PixelWidth; MapPanel.SetBoundingBox(this, geoBitmap.BoundingBox);
var height = geoBitmap.Bitmap.PixelHeight;
#endif
var image = new Image
{
Source = geoBitmap.Bitmap,
Stretch = Stretch.Fill,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
};
var transform = geoBitmap.Transform; SetImage(geoBitmap.Bitmap);
var p1 = transform.Transform(new Point());
var p2 = transform.Transform(new Point(width, height));
var mapRect = new Rect(p1, p2); ;
MapPanel.SetBoundingBox(this, geoBitmap.Projection.MapToBoundingBox(mapRect));
Children.Clear();
Children.Add(image);
} }
private static async Task<GeoBitmap> ReadWorldFileImageAsync(string sourcePath, string worldFilePath) private static async Task<GeoBitmap> ReadWorldFileImageAsync(string sourcePath, string worldFilePath)
@ -174,5 +139,21 @@ 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 static MapProjection GetProjection(short[] geoKeyDirectory)
{
for (var i = 4; i < geoKeyDirectory.Length - 3; i += 4)
{
if (geoKeyDirectory[i] == ProjectedCRSGeoKey && geoKeyDirectory[i + 1] == 0)
{
var epsgCode = geoKeyDirectory[i + 3];
return MapProjectionFactory.Instance.GetProjection(epsgCode) ??
throw new ArgumentException($"Can not create projection EPSG:{epsgCode}.");
}
}
return null;
}
} }
} }

View file

@ -6,13 +6,20 @@ using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
namespace MapControl namespace MapControl
{ {
public partial class GeoImage public partial class GeoImage : Image
{ {
private void SetImage(ImageSource image)
{
Source = image;
Stretch = Stretch.Fill;
}
private static Task<GeoBitmap> ReadGeoTiffAsync(string sourcePath) private static Task<GeoBitmap> ReadGeoTiffAsync(string sourcePath)
{ {
return Task.Run(() => return Task.Run(() =>
@ -47,7 +54,7 @@ namespace MapControl
if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory) if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory)
{ {
geoBitmap.SetProjection(geoKeyDirectory); geoBitmap.Projection = GetProjection(geoKeyDirectory);
} }
if (metadata.GetQuery(QueryString(NoDataTag)) is string noData && if (metadata.GetQuery(QueryString(NoDataTag)) is string noData &&

View file

@ -6,11 +6,32 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Windows.Graphics.Imaging; using Windows.Graphics.Imaging;
using Windows.Storage; using Windows.Storage;
#if UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#else
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
#endif
namespace MapControl namespace MapControl
{ {
public partial class GeoImage public partial class GeoImage : Grid
{ {
private void SetImage(ImageSource image)
{
Children.Clear();
Children.Add(new Image
{
Source = image,
Stretch = Stretch.Fill,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch
});
}
private static async Task<GeoBitmap> ReadGeoTiffAsync(string sourcePath) private static async Task<GeoBitmap> ReadGeoTiffAsync(string sourcePath)
{ {
var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath)); var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath));
@ -60,7 +81,7 @@ 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)
{ {
geoBitmap.SetProjection(geoKeyDirectory); geoBitmap.Projection = GetProjection(geoKeyDirectory);
} }
return geoBitmap; return geoBitmap;