Query GeoTIFF GeoKeyDirectoryTag in GeoImage

This commit is contained in:
ClemensFischer 2022-12-09 19:57:52 +01:00
parent 5645f8f0e8
commit 17745dc6fb
3 changed files with 103 additions and 42 deletions

View file

@ -28,27 +28,45 @@ 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}";
private class GeoBitmap
{
public GeoBitmap(BitmapSource bitmap, Matrix transform, MapProjection projection = null)
{
Bitmap = bitmap;
Transform = transform;
Projection = projection;
}
public BitmapSource Bitmap { get; }
public Matrix Transform { get; }
public MapProjection Projection { get; }
}
private const ushort ProjectedCRSGeoKey = 3072;
private const ushort GeoKeyDirectoryTag = 34735;
private const ushort ModelPixelScaleTag = 33550;
private const ushort ModelTiePointTag = 33922;
private const ushort ModelTransformationTag = 34264;
private const ushort NoDataTag = 42113;
private static string QueryString(ushort tag) => $"/ifd/{{ushort={tag}}}";
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 string SourcePath
{
get => (string)GetValue(SourcePathProperty);
set => SetValue(SourcePathProperty, value);
}
public GeoImage()
{
HorizontalContentAlignment = HorizontalAlignment.Stretch;
VerticalContentAlignment = VerticalAlignment.Stretch;
}
public string SourcePath
{
get => (string)GetValue(SourcePathProperty);
set => SetValue(SourcePathProperty, value);
}
private async Task SourcePathPropertyChanged(string sourcePath)
{
Image image = null;
@ -56,7 +74,7 @@ namespace MapControl
if (sourcePath != null)
{
Tuple<BitmapSource, Matrix> geoBitmap = null;
GeoBitmap geoBitmap = null;
var ext = Path.GetExtension(sourcePath);
@ -77,15 +95,14 @@ namespace MapControl
geoBitmap = await ReadGeoTiff(sourcePath);
}
var bitmap = geoBitmap.Item1;
var transform = geoBitmap.Item2;
image = new Image
{
Source = bitmap,
Source = geoBitmap.Bitmap,
Stretch = Stretch.Fill
};
var transform = geoBitmap.Transform;
if (transform.M12 != 0 || transform.M21 != 0)
{
var rotation = (Math.Atan2(transform.M12, transform.M11) + Math.Atan2(transform.M21, -transform.M22)) * 90d / Math.PI;
@ -101,12 +118,17 @@ namespace MapControl
}
var p1 = transform.Transform(new Point());
var p2 = transform.Transform(new Point(bitmap.PixelWidth, bitmap.PixelHeight));
var p2 = transform.Transform(new Point(geoBitmap.Bitmap.PixelWidth, geoBitmap.Bitmap.PixelHeight));
var mapRect = new MapRect(p1, p2);
// TODO: boundingBox = MapProjection.MapRectToBoundingBox(mapRect);
boundingBox = new BoundingBox(mapRect.YMin, mapRect.XMin, mapRect.YMax, mapRect.XMax);
if (geoBitmap.Projection != null)
{
boundingBox = geoBitmap.Projection.MapRectToBoundingBox(mapRect);
}
else
{
boundingBox = new BoundingBox(mapRect.YMin, mapRect.XMin, mapRect.YMax, mapRect.XMax);
}
}
Content = image;
@ -114,7 +136,7 @@ namespace MapControl
MapPanel.SetBoundingBox(this, boundingBox);
}
private static async Task<Tuple<BitmapSource, Matrix>> ReadWorldFileImage(string sourcePath, string worldFilePath)
private static async Task<GeoBitmap> ReadWorldFileImage(string sourcePath, string worldFilePath)
{
var bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath);
@ -146,7 +168,27 @@ namespace MapControl
parameters[5]); // line 6: F or OffsetY
});
return new Tuple<BitmapSource, Matrix>(bitmap, transform);
return new GeoBitmap(bitmap, transform);
}
private static MapProjection GetProjection(string sourcePath, short[] geoKeyDirectory)
{
MapProjection projection = null;
for (int i = 4; i < geoKeyDirectory.Length - 3; i += 4)
{
if (geoKeyDirectory[i] == ProjectedCRSGeoKey && geoKeyDirectory[i + 1] == 0)
{
var crsId = $"EPSG:{geoKeyDirectory[i + 3]}";
projection = MapProjection.Factory.GetProjection(crsId) ??
throw new ArgumentException($"Can not create projection {crsId} in {sourcePath}.");
break;
}
}
return projection;
}
}
}

View file

@ -13,12 +13,13 @@ namespace MapControl
{
public partial class GeoImage
{
private static async Task<Tuple<BitmapSource, Matrix>> ReadGeoTiff(string sourcePath)
private static async Task<GeoBitmap> ReadGeoTiff(string sourcePath)
{
return await Task.Run(() =>
{
BitmapSource bitmap;
Matrix transform;
MapProjection projection = null;
using (var stream = File.OpenRead(sourcePath))
{
@ -27,12 +28,12 @@ namespace MapControl
var metadata = (BitmapMetadata)bitmap.Metadata;
if (metadata.GetQuery(PixelScaleQuery) is double[] pixelScale && pixelScale.Length == 3 &&
metadata.GetQuery(TiePointQuery) is double[] tiePoint && tiePoint.Length >= 6)
if (metadata.GetQuery(QueryString(ModelPixelScaleTag)) is double[] pixelScale && pixelScale.Length == 3 &&
metadata.GetQuery(QueryString(ModelTiePointTag)) is double[] tiePoint && tiePoint.Length >= 6)
{
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
}
else if (metadata.GetQuery(TransformQuery) is double[] tform && tform.Length == 16)
else if (metadata.GetQuery(QueryString(ModelTransformationTag)) is double[] tform && tform.Length == 16)
{
transform = new Matrix(tform[0], tform[1], tform[4], tform[5], tform[3], tform[7]);
}
@ -41,12 +42,17 @@ namespace MapControl
throw new ArgumentException($"No coordinate transformation found in {sourcePath}.");
}
if (metadata.GetQuery(NoDataQuery) is string noData && int.TryParse(noData, out int noDataValue))
if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory)
{
projection = GetProjection(sourcePath, geoKeyDirectory);
}
if (metadata.GetQuery(QueryString(NoDataTag)) is string noData && int.TryParse(noData, out int noDataValue))
{
bitmap = ConvertTransparentPixel(bitmap, noDataValue);
}
return new Tuple<BitmapSource, Matrix>(bitmap, transform);
return new GeoBitmap(bitmap, transform, projection);
});
}

View file

@ -3,8 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Graphics.Imaging;
using Windows.Storage;
@ -18,7 +16,7 @@ namespace MapControl
{
public partial class GeoImage
{
public static async Task<Tuple<BitmapSource, Matrix>> ReadGeoTiff(string sourcePath)
private static async Task<GeoBitmap> ReadGeoTiff(string sourcePath)
{
var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath));
@ -26,6 +24,7 @@ namespace MapControl
{
WriteableBitmap bitmap;
Matrix transform;
MapProjection projection = null;
var decoder = await BitmapDecoder.CreateAsync(stream);
@ -35,31 +34,45 @@ namespace MapControl
swbmp.CopyToBuffer(bitmap.PixelBuffer);
}
var query = new List<string>
{
PixelScaleQuery, TiePointQuery, TransformQuery, NoDataQuery
};
var geoKeyDirectoryQuery = QueryString(GeoKeyDirectoryTag);
var pixelScaleQuery = QueryString(ModelPixelScaleTag);
var tiePointQuery = QueryString(ModelTiePointTag);
var transformationQuery = QueryString(ModelTransformationTag);
var metadata = await decoder.BitmapProperties.GetPropertiesAsync(
new string[]
{
pixelScaleQuery,
tiePointQuery,
transformationQuery,
geoKeyDirectoryQuery
});
var metadata = await decoder.BitmapProperties.GetPropertiesAsync(query);
if (metadata.TryGetValue(PixelScaleQuery, out BitmapTypedValue pixelScaleValue) &&
if (metadata.TryGetValue(pixelScaleQuery, out BitmapTypedValue pixelScaleValue) &&
pixelScaleValue.Value is double[] pixelScale && pixelScale.Length == 3 &&
metadata.TryGetValue(TiePointQuery, out BitmapTypedValue tiePointValue) &&
metadata.TryGetValue(tiePointQuery, out BitmapTypedValue tiePointValue) &&
tiePointValue.Value is double[] tiePoint && tiePoint.Length >= 6)
{
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
}
else if (metadata.TryGetValue(TransformQuery, out BitmapTypedValue tformValue) &&
tformValue.Value is double[] tform && tform.Length == 16)
else if (metadata.TryGetValue(transformationQuery, out BitmapTypedValue transformValue) &&
transformValue.Value is double[] transformValues && transformValues.Length == 16)
{
transform = new Matrix(tform[0], tform[1], tform[4], tform[5], tform[3], tform[7]);
transform = new Matrix(transformValues[0], transformValues[1],
transformValues[4], transformValues[5],
transformValues[3], transformValues[7]);
}
else
{
throw new ArgumentException($"No coordinate transformation found in {sourcePath}.");
}
return new Tuple<BitmapSource, Matrix>(bitmap, transform);
if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) &&
geoKeyDirValue.Value is short[] geoKeyDirectory)
{
projection = GetProjection(sourcePath, geoKeyDirectory);
}
return new GeoBitmap(bitmap, transform, projection);
}
}
}