mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-05-07 13:37:47 +00:00
File scoped namespaces
This commit is contained in:
parent
c14377f976
commit
65aba44af6
152 changed files with 11962 additions and 12115 deletions
|
|
@ -29,183 +29,182 @@ using Shape = Avalonia.Controls.Shapes.Shape;
|
|||
using BitmapSource = Avalonia.Media.Imaging.Bitmap;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
namespace MapControl;
|
||||
|
||||
public static partial class GeoImage
|
||||
{
|
||||
public static partial class GeoImage
|
||||
private class GeoBitmap
|
||||
{
|
||||
private class GeoBitmap
|
||||
public GeoBitmap(BitmapSource bitmap, Matrix transform, MapProjection projection)
|
||||
{
|
||||
public GeoBitmap(BitmapSource bitmap, Matrix transform, MapProjection projection)
|
||||
{
|
||||
var p1 = transform.Transform(new Point());
|
||||
var p1 = transform.Transform(new Point());
|
||||
#if AVALONIA
|
||||
var p2 = transform.Transform(new Point(bitmap.PixelSize.Width, bitmap.PixelSize.Height));
|
||||
var p2 = transform.Transform(new Point(bitmap.PixelSize.Width, bitmap.PixelSize.Height));
|
||||
#else
|
||||
var p2 = transform.Transform(new Point(bitmap.PixelWidth, bitmap.PixelHeight));
|
||||
var p2 = transform.Transform(new Point(bitmap.PixelWidth, bitmap.PixelHeight));
|
||||
#endif
|
||||
BitmapSource = bitmap;
|
||||
BitmapSource = bitmap;
|
||||
|
||||
if (projection != null)
|
||||
{
|
||||
var sw = projection.MapToLocation(p1);
|
||||
var ne = projection.MapToLocation(p2);
|
||||
BoundingBox = new BoundingBox(sw.Latitude, sw.Longitude, ne.Latitude, ne.Longitude);
|
||||
}
|
||||
else
|
||||
{
|
||||
BoundingBox = new BoundingBox(p1.Y, p1.X, p2.Y, p2.X);
|
||||
}
|
||||
}
|
||||
|
||||
public BitmapSource BitmapSource { get; }
|
||||
public BoundingBox BoundingBox { 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}}}";
|
||||
|
||||
private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(GeoImage));
|
||||
|
||||
public static readonly DependencyProperty SourcePathProperty =
|
||||
DependencyPropertyHelper.RegisterAttached<string>("SourcePath", typeof(GeoImage), null,
|
||||
async (element, oldValue, newValue) => await LoadGeoImage(element, newValue));
|
||||
|
||||
public static string GetSourcePath(FrameworkElement image)
|
||||
{
|
||||
return (string)image.GetValue(SourcePathProperty);
|
||||
}
|
||||
|
||||
public static void SetSourcePath(FrameworkElement image, string value)
|
||||
{
|
||||
image.SetValue(SourcePathProperty, value);
|
||||
}
|
||||
|
||||
public static async Task<Image> CreateAsync(string sourcePath)
|
||||
{
|
||||
var image = new Image();
|
||||
|
||||
await LoadGeoImage(image, sourcePath);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
public static Task LoadGeoImageAsync(this Image image, string sourcePath)
|
||||
{
|
||||
return LoadGeoImage(image, sourcePath);
|
||||
}
|
||||
|
||||
public static Task LoadGeoImageAsync(this Shape shape, string sourcePath)
|
||||
{
|
||||
return LoadGeoImage(shape, sourcePath);
|
||||
}
|
||||
|
||||
private static async Task LoadGeoImage(FrameworkElement element, string sourcePath)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(sourcePath))
|
||||
if (projection != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var geoBitmap = await LoadGeoBitmap(sourcePath);
|
||||
var sw = projection.MapToLocation(p1);
|
||||
var ne = projection.MapToLocation(p2);
|
||||
BoundingBox = new BoundingBox(sw.Latitude, sw.Longitude, ne.Latitude, ne.Longitude);
|
||||
}
|
||||
else
|
||||
{
|
||||
BoundingBox = new BoundingBox(p1.Y, p1.X, p2.Y, p2.X);
|
||||
}
|
||||
}
|
||||
|
||||
if (element is Image image)
|
||||
public BitmapSource BitmapSource { get; }
|
||||
public BoundingBox BoundingBox { 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}}}";
|
||||
|
||||
private static ILogger Logger => field ??= ImageLoader.LoggerFactory?.CreateLogger(typeof(GeoImage));
|
||||
|
||||
public static readonly DependencyProperty SourcePathProperty =
|
||||
DependencyPropertyHelper.RegisterAttached<string>("SourcePath", typeof(GeoImage), null,
|
||||
async (element, oldValue, newValue) => await LoadGeoImage(element, newValue));
|
||||
|
||||
public static string GetSourcePath(FrameworkElement image)
|
||||
{
|
||||
return (string)image.GetValue(SourcePathProperty);
|
||||
}
|
||||
|
||||
public static void SetSourcePath(FrameworkElement image, string value)
|
||||
{
|
||||
image.SetValue(SourcePathProperty, value);
|
||||
}
|
||||
|
||||
public static async Task<Image> CreateAsync(string sourcePath)
|
||||
{
|
||||
var image = new Image();
|
||||
|
||||
await LoadGeoImage(image, sourcePath);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
public static Task LoadGeoImageAsync(this Image image, string sourcePath)
|
||||
{
|
||||
return LoadGeoImage(image, sourcePath);
|
||||
}
|
||||
|
||||
public static Task LoadGeoImageAsync(this Shape shape, string sourcePath)
|
||||
{
|
||||
return LoadGeoImage(shape, sourcePath);
|
||||
}
|
||||
|
||||
private static async Task LoadGeoImage(FrameworkElement element, string sourcePath)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(sourcePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
var geoBitmap = await LoadGeoBitmap(sourcePath);
|
||||
|
||||
if (element is Image image)
|
||||
{
|
||||
image.Stretch = Stretch.Fill;
|
||||
image.Source = geoBitmap.BitmapSource;
|
||||
}
|
||||
else if (element is Shape shape)
|
||||
{
|
||||
shape.Stretch = Stretch.Fill;
|
||||
shape.Fill = new ImageBrush
|
||||
{
|
||||
image.Stretch = Stretch.Fill;
|
||||
image.Source = geoBitmap.BitmapSource;
|
||||
}
|
||||
else if (element is Shape shape)
|
||||
{
|
||||
shape.Stretch = Stretch.Fill;
|
||||
shape.Fill = new ImageBrush
|
||||
{
|
||||
Stretch = Stretch.Fill,
|
||||
Stretch = Stretch.Fill,
|
||||
#if AVALONIA
|
||||
Source = geoBitmap.BitmapSource
|
||||
Source = geoBitmap.BitmapSource
|
||||
#else
|
||||
ImageSource = geoBitmap.BitmapSource
|
||||
ImageSource = geoBitmap.BitmapSource
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
MapPanel.SetBoundingBox(element, geoBitmap.BoundingBox);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.LogError(ex, "Failed loading from {path}", sourcePath);
|
||||
};
|
||||
}
|
||||
|
||||
MapPanel.SetBoundingBox(element, geoBitmap.BoundingBox);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<GeoBitmap> LoadGeoBitmap(string sourcePath)
|
||||
{
|
||||
var ext = System.IO.Path.GetExtension(sourcePath);
|
||||
|
||||
if (ext.Length >= 4)
|
||||
catch (Exception ex)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(sourcePath);
|
||||
var file = Path.GetFileNameWithoutExtension(sourcePath);
|
||||
var worldFilePath = Path.Combine(dir, file + ext.Remove(2, 1) + "w");
|
||||
|
||||
if (File.Exists(worldFilePath))
|
||||
{
|
||||
return new GeoBitmap(
|
||||
(BitmapSource)await ImageLoader.LoadImageAsync(sourcePath),
|
||||
await ReadWorldFileMatrix(worldFilePath),
|
||||
null);
|
||||
}
|
||||
Logger?.LogError(ex, "Failed loading from {path}", sourcePath);
|
||||
}
|
||||
|
||||
return await LoadGeoTiff(sourcePath);
|
||||
}
|
||||
|
||||
private static async Task<Matrix> ReadWorldFileMatrix(string worldFilePath)
|
||||
{
|
||||
using var fileStream = File.OpenRead(worldFilePath);
|
||||
using var streamReader = new StreamReader(fileStream);
|
||||
|
||||
var parameters = new double[6];
|
||||
var index = 0;
|
||||
string line;
|
||||
|
||||
while (index < 6 &&
|
||||
(line = await streamReader.ReadLineAsync()) != null &&
|
||||
double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double parameter))
|
||||
{
|
||||
parameters[index++] = parameter;
|
||||
}
|
||||
|
||||
if (index != 6)
|
||||
{
|
||||
throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}.");
|
||||
}
|
||||
|
||||
return new Matrix( // https://en.wikipedia.org/wiki/World_file
|
||||
parameters[0], // line 1: A
|
||||
parameters[1], // line 2: D
|
||||
parameters[2], // line 3: B
|
||||
parameters[3], // line 4: E
|
||||
parameters[4], // line 5: C
|
||||
parameters[5]); // line 6: F
|
||||
}
|
||||
|
||||
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 MapProjection.Parse($"EPSG:{epsgCode}");
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<GeoBitmap> LoadGeoBitmap(string sourcePath)
|
||||
{
|
||||
var ext = System.IO.Path.GetExtension(sourcePath);
|
||||
|
||||
if (ext.Length >= 4)
|
||||
{
|
||||
var dir = Path.GetDirectoryName(sourcePath);
|
||||
var file = Path.GetFileNameWithoutExtension(sourcePath);
|
||||
var worldFilePath = Path.Combine(dir, file + ext.Remove(2, 1) + "w");
|
||||
|
||||
if (File.Exists(worldFilePath))
|
||||
{
|
||||
return new GeoBitmap(
|
||||
(BitmapSource)await ImageLoader.LoadImageAsync(sourcePath),
|
||||
await ReadWorldFileMatrix(worldFilePath),
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
return await LoadGeoTiff(sourcePath);
|
||||
}
|
||||
|
||||
private static async Task<Matrix> ReadWorldFileMatrix(string worldFilePath)
|
||||
{
|
||||
using var fileStream = File.OpenRead(worldFilePath);
|
||||
using var streamReader = new StreamReader(fileStream);
|
||||
|
||||
var parameters = new double[6];
|
||||
var index = 0;
|
||||
string line;
|
||||
|
||||
while (index < 6 &&
|
||||
(line = await streamReader.ReadLineAsync()) != null &&
|
||||
double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out double parameter))
|
||||
{
|
||||
parameters[index++] = parameter;
|
||||
}
|
||||
|
||||
if (index != 6)
|
||||
{
|
||||
throw new ArgumentException($"Insufficient number of parameters in world file {worldFilePath}.");
|
||||
}
|
||||
|
||||
return new Matrix( // https://en.wikipedia.org/wiki/World_file
|
||||
parameters[0], // line 1: A
|
||||
parameters[1], // line 2: D
|
||||
parameters[2], // line 3: B
|
||||
parameters[3], // line 4: E
|
||||
parameters[4], // line 5: C
|
||||
parameters[5]); // line 6: F
|
||||
}
|
||||
|
||||
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 MapProjection.Parse($"EPSG:{epsgCode}");
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue