mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
Query GeoTIFF GeoKeyDirectoryTag in GeoImage
This commit is contained in:
parent
5645f8f0e8
commit
17745dc6fb
|
|
@ -28,27 +28,45 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
public partial class GeoImage : ContentControl
|
public partial class GeoImage : ContentControl
|
||||||
{
|
{
|
||||||
private const string PixelScaleQuery = "/ifd/{ushort=33550}";
|
private class GeoBitmap
|
||||||
private const string TiePointQuery = "/ifd/{ushort=33922}";
|
{
|
||||||
private const string TransformQuery = "/ifd/{ushort=34264}";
|
public GeoBitmap(BitmapSource bitmap, Matrix transform, MapProjection projection = null)
|
||||||
private const string NoDataQuery = "/ifd/{ushort=42113}";
|
{
|
||||||
|
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(
|
public static readonly DependencyProperty SourcePathProperty = DependencyProperty.Register(
|
||||||
nameof(SourcePath), typeof(string), typeof(GeoImage),
|
nameof(SourcePath), typeof(string), typeof(GeoImage),
|
||||||
new PropertyMetadata(null, async (o, e) => await ((GeoImage)o).SourcePathPropertyChanged((string)e.NewValue)));
|
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()
|
public GeoImage()
|
||||||
{
|
{
|
||||||
HorizontalContentAlignment = HorizontalAlignment.Stretch;
|
HorizontalContentAlignment = HorizontalAlignment.Stretch;
|
||||||
VerticalContentAlignment = VerticalAlignment.Stretch;
|
VerticalContentAlignment = VerticalAlignment.Stretch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public string SourcePath
|
||||||
|
{
|
||||||
|
get => (string)GetValue(SourcePathProperty);
|
||||||
|
set => SetValue(SourcePathProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
private async Task SourcePathPropertyChanged(string sourcePath)
|
private async Task SourcePathPropertyChanged(string sourcePath)
|
||||||
{
|
{
|
||||||
Image image = null;
|
Image image = null;
|
||||||
|
|
@ -56,7 +74,7 @@ namespace MapControl
|
||||||
|
|
||||||
if (sourcePath != null)
|
if (sourcePath != null)
|
||||||
{
|
{
|
||||||
Tuple<BitmapSource, Matrix> geoBitmap = null;
|
GeoBitmap geoBitmap = null;
|
||||||
|
|
||||||
var ext = Path.GetExtension(sourcePath);
|
var ext = Path.GetExtension(sourcePath);
|
||||||
|
|
||||||
|
|
@ -77,15 +95,14 @@ namespace MapControl
|
||||||
geoBitmap = await ReadGeoTiff(sourcePath);
|
geoBitmap = await ReadGeoTiff(sourcePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
var bitmap = geoBitmap.Item1;
|
|
||||||
var transform = geoBitmap.Item2;
|
|
||||||
|
|
||||||
image = new Image
|
image = new Image
|
||||||
{
|
{
|
||||||
Source = bitmap,
|
Source = geoBitmap.Bitmap,
|
||||||
Stretch = Stretch.Fill
|
Stretch = Stretch.Fill
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var transform = geoBitmap.Transform;
|
||||||
|
|
||||||
if (transform.M12 != 0 || transform.M21 != 0)
|
if (transform.M12 != 0 || transform.M21 != 0)
|
||||||
{
|
{
|
||||||
var rotation = (Math.Atan2(transform.M12, transform.M11) + Math.Atan2(transform.M21, -transform.M22)) * 90d / Math.PI;
|
var rotation = (Math.Atan2(transform.M12, transform.M11) + Math.Atan2(transform.M21, -transform.M22)) * 90d / Math.PI;
|
||||||
|
|
@ -101,20 +118,25 @@ namespace MapControl
|
||||||
}
|
}
|
||||||
|
|
||||||
var p1 = transform.Transform(new Point());
|
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);
|
var mapRect = new MapRect(p1, p2);
|
||||||
|
|
||||||
// TODO: boundingBox = MapProjection.MapRectToBoundingBox(mapRect);
|
if (geoBitmap.Projection != null)
|
||||||
|
{
|
||||||
|
boundingBox = geoBitmap.Projection.MapRectToBoundingBox(mapRect);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
boundingBox = new BoundingBox(mapRect.YMin, mapRect.XMin, mapRect.YMax, mapRect.XMax);
|
boundingBox = new BoundingBox(mapRect.YMin, mapRect.XMin, mapRect.YMax, mapRect.XMax);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Content = image;
|
Content = image;
|
||||||
|
|
||||||
MapPanel.SetBoundingBox(this, boundingBox);
|
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);
|
var bitmap = (BitmapSource)await ImageLoader.LoadImageAsync(sourcePath);
|
||||||
|
|
||||||
|
|
@ -146,7 +168,27 @@ namespace MapControl
|
||||||
parameters[5]); // line 6: F or OffsetY
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,12 +13,13 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
public partial class GeoImage
|
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(() =>
|
return await Task.Run(() =>
|
||||||
{
|
{
|
||||||
BitmapSource bitmap;
|
BitmapSource bitmap;
|
||||||
Matrix transform;
|
Matrix transform;
|
||||||
|
MapProjection projection = null;
|
||||||
|
|
||||||
using (var stream = File.OpenRead(sourcePath))
|
using (var stream = File.OpenRead(sourcePath))
|
||||||
{
|
{
|
||||||
|
|
@ -27,12 +28,12 @@ namespace MapControl
|
||||||
|
|
||||||
var metadata = (BitmapMetadata)bitmap.Metadata;
|
var metadata = (BitmapMetadata)bitmap.Metadata;
|
||||||
|
|
||||||
if (metadata.GetQuery(PixelScaleQuery) is double[] pixelScale && pixelScale.Length == 3 &&
|
if (metadata.GetQuery(QueryString(ModelPixelScaleTag)) is double[] pixelScale && pixelScale.Length == 3 &&
|
||||||
metadata.GetQuery(TiePointQuery) is double[] tiePoint && tiePoint.Length >= 6)
|
metadata.GetQuery(QueryString(ModelTiePointTag)) is double[] tiePoint && tiePoint.Length >= 6)
|
||||||
{
|
{
|
||||||
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
|
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]);
|
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}.");
|
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);
|
bitmap = ConvertTransparentPixel(bitmap, noDataValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Tuple<BitmapSource, Matrix>(bitmap, transform);
|
return new GeoBitmap(bitmap, transform, projection);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@
|
||||||
// Licensed under the Microsoft Public License (Ms-PL)
|
// Licensed under the Microsoft Public License (Ms-PL)
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Windows.Graphics.Imaging;
|
using Windows.Graphics.Imaging;
|
||||||
using Windows.Storage;
|
using Windows.Storage;
|
||||||
|
|
@ -18,7 +16,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
public partial class GeoImage
|
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));
|
var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath));
|
||||||
|
|
||||||
|
|
@ -26,6 +24,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
WriteableBitmap bitmap;
|
WriteableBitmap bitmap;
|
||||||
Matrix transform;
|
Matrix transform;
|
||||||
|
MapProjection projection = null;
|
||||||
|
|
||||||
var decoder = await BitmapDecoder.CreateAsync(stream);
|
var decoder = await BitmapDecoder.CreateAsync(stream);
|
||||||
|
|
||||||
|
|
@ -35,31 +34,45 @@ namespace MapControl
|
||||||
swbmp.CopyToBuffer(bitmap.PixelBuffer);
|
swbmp.CopyToBuffer(bitmap.PixelBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
var query = new List<string>
|
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, TransformQuery, NoDataQuery
|
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 &&
|
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)
|
tiePointValue.Value is double[] tiePoint && tiePoint.Length >= 6)
|
||||||
{
|
{
|
||||||
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
|
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
|
||||||
}
|
}
|
||||||
else if (metadata.TryGetValue(TransformQuery, out BitmapTypedValue tformValue) &&
|
else if (metadata.TryGetValue(transformationQuery, out BitmapTypedValue transformValue) &&
|
||||||
tformValue.Value is double[] tform && tform.Length == 16)
|
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
|
else
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"No coordinate transformation found in {sourcePath}.");
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue