mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-03 20:05:35 +02:00
GeoImage implementation
This commit is contained in:
parent
aa6018cf0e
commit
1f5e82518a
|
|
@ -29,11 +29,34 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
public partial class GeoImage : Grid
|
public partial class GeoImage : Grid
|
||||||
{
|
{
|
||||||
|
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; }
|
public MapProjection Projection { get; set; } = new DefaultProjection();
|
||||||
|
|
||||||
|
public void SetProjection(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];
|
||||||
|
|
||||||
|
var projection = MapProjectionFactory.Instance.GetProjection(epsgCode) ??
|
||||||
|
throw new ArgumentException($"Can not create projection EPSG:{epsgCode}.");
|
||||||
|
|
||||||
|
Projection = projection;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private const ushort ProjectedCRSGeoKey = 3072;
|
private const ushort ProjectedCRSGeoKey = 3072;
|
||||||
|
|
@ -87,15 +110,20 @@ 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)
|
||||||
{
|
{
|
||||||
#if AVALONIA
|
|
||||||
return;
|
|
||||||
#else
|
|
||||||
geoBitmap = await ReadGeoTiffAsync(sourcePath);
|
geoBitmap = await ReadGeoTiffAsync(sourcePath);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var width = geoBitmap.Bitmap.PixelWidth;
|
||||||
|
var height = geoBitmap.Bitmap.PixelHeight;
|
||||||
|
#endif
|
||||||
var image = new Image
|
var image = new Image
|
||||||
{
|
{
|
||||||
Source = geoBitmap.Bitmap,
|
Source = geoBitmap.Bitmap,
|
||||||
|
|
@ -105,32 +133,11 @@ namespace MapControl
|
||||||
};
|
};
|
||||||
|
|
||||||
var transform = geoBitmap.Transform;
|
var transform = geoBitmap.Transform;
|
||||||
|
var p1 = transform.Transform(new Point());
|
||||||
|
var p2 = transform.Transform(new Point(width, height));
|
||||||
|
var mapRect = new Rect(p1, p2); ;
|
||||||
|
|
||||||
if (transform.M12 != 0 && transform.M21 != 0)
|
MapPanel.SetBoundingBox(this, geoBitmap.Projection.MapToBoundingBox(mapRect));
|
||||||
{
|
|
||||||
var rotation = (Math.Atan2(transform.M12, transform.M11) + Math.Atan2(transform.M21, -transform.M22)) * 90d / Math.PI;
|
|
||||||
|
|
||||||
image.RenderTransform = new RotateTransform { Angle = -rotation };
|
|
||||||
|
|
||||||
// Calculate effective unrotated transform.
|
|
||||||
//
|
|
||||||
geoBitmap.Transform = new Matrix(
|
|
||||||
Math.Sqrt(transform.M11 * transform.M11 + transform.M12 * transform.M12), 0d, 0d,
|
|
||||||
-Math.Sqrt(transform.M22 * transform.M22 + transform.M21 * transform.M21), 0d, 0d);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if AVALONIA
|
|
||||||
var size = new Point(geoBitmap.Bitmap.PixelSize.Width, geoBitmap.Bitmap.PixelSize.Height);
|
|
||||||
#else
|
|
||||||
var size = new Point(geoBitmap.Bitmap.PixelWidth, geoBitmap.Bitmap.PixelHeight);
|
|
||||||
#endif
|
|
||||||
var rect = new Rect(transform.Transform(new Point()), transform.Transform(size));
|
|
||||||
|
|
||||||
var boundingBox = geoBitmap.Projection != null
|
|
||||||
? geoBitmap.Projection.MapToBoundingBox(rect)
|
|
||||||
: new BoundingBox(rect.Y, rect.X, rect.Y + rect.Height, rect.X + rect.Width);
|
|
||||||
|
|
||||||
MapPanel.SetBoundingBox(this, boundingBox);
|
|
||||||
|
|
||||||
Children.Clear();
|
Children.Clear();
|
||||||
Children.Add(image);
|
Children.Add(image);
|
||||||
|
|
@ -167,25 +174,5 @@ 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)
|
|
||||||
{
|
|
||||||
MapProjection projection = null;
|
|
||||||
|
|
||||||
for (int i = 4; i < geoKeyDirectory.Length - 3; i += 4)
|
|
||||||
{
|
|
||||||
if (geoKeyDirectory[i] == ProjectedCRSGeoKey && geoKeyDirectory[i + 1] == 0)
|
|
||||||
{
|
|
||||||
int epsgCode = geoKeyDirectory[i + 3];
|
|
||||||
|
|
||||||
projection = MapProjectionFactory.Instance.GetProjection(epsgCode) ??
|
|
||||||
throw new ArgumentException($"Can not create projection EPSG:{epsgCode}.");
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return projection;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ namespace MapControl
|
||||||
|
|
||||||
if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory)
|
if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory)
|
||||||
{
|
{
|
||||||
geoBitmap.Projection = GetProjection(geoKeyDirectory);
|
geoBitmap.SetProjection(geoKeyDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata.GetQuery(QueryString(NoDataTag)) is string noData &&
|
if (metadata.GetQuery(QueryString(NoDataTag)) is string noData &&
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,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.Projection = GetProjection(geoKeyDirectory);
|
geoBitmap.SetProjection(geoKeyDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
return geoBitmap;
|
return geoBitmap;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue