2018-12-09 17:14:32 +01:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
|
|
|
|
|
// © 2018 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2018-12-13 18:09:34 +01:00
|
|
|
|
using System.Globalization;
|
2018-12-09 17:14:32 +01:00
|
|
|
|
using System.IO;
|
2018-12-13 18:09:34 +01:00
|
|
|
|
using System.Linq;
|
2018-12-09 17:14:32 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
#if WINDOWS_UWP
|
2018-12-13 18:09:34 +01:00
|
|
|
|
using Windows.Foundation;
|
2018-12-09 17:14:32 +01:00
|
|
|
|
using Windows.UI.Xaml;
|
|
|
|
|
|
using Windows.UI.Xaml.Controls;
|
|
|
|
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
|
|
using Windows.UI.Xaml.Media.Imaging;
|
|
|
|
|
|
#else
|
|
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
#endif
|
2018-12-13 18:09:34 +01:00
|
|
|
|
using MapControl.Projections;
|
2018-12-09 17:14:32 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MapControl.Images
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class WorldFile
|
|
|
|
|
|
{
|
|
|
|
|
|
public static readonly DependencyProperty ImagePathProperty = DependencyProperty.RegisterAttached(
|
|
|
|
|
|
"ImagePath", typeof(string), typeof(WorldFile),
|
|
|
|
|
|
new PropertyMetadata(null, async (o, e) => await SetWorldImageAsync((Image)o, (string)e.NewValue)));
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetImagePath(this Image image)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (string)image.GetValue(ImagePathProperty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void SetImagePath(this Image image, string imagePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
image.SetValue(ImagePathProperty, imagePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-13 18:09:34 +01:00
|
|
|
|
public static void SetWorldImage(this Image image, BitmapSource bitmapSource, Matrix transform, MapProjection projection = null)
|
2018-12-09 17:14:32 +01:00
|
|
|
|
{
|
|
|
|
|
|
image.Source = bitmapSource;
|
|
|
|
|
|
image.Stretch = Stretch.Fill;
|
|
|
|
|
|
|
2018-12-13 18:09:34 +01:00
|
|
|
|
var boundingBox = GetBoundingBox(bitmapSource.PixelWidth, bitmapSource.PixelHeight, transform, projection);
|
|
|
|
|
|
|
|
|
|
|
|
MapPanel.SetBoundingBox(image, boundingBox);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static BoundingBox GetBoundingBox(double imageWidth, double imageHeight, Matrix transform, MapProjection projection = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (transform.M12 != 0d || transform.M21 != 0d)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Invalid Matrix, M12 and M21 must be zero.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var rect = new Rect(
|
|
|
|
|
|
transform.Transform(new Point()),
|
|
|
|
|
|
transform.Transform(new Point(imageWidth, imageHeight)));
|
|
|
|
|
|
|
|
|
|
|
|
if (projection != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return projection.RectToBoundingBox(rect);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new BoundingBox
|
|
|
|
|
|
{
|
|
|
|
|
|
West = rect.X,
|
|
|
|
|
|
East = rect.X + rect.Width,
|
|
|
|
|
|
South = rect.Y,
|
|
|
|
|
|
North = rect.Y + rect.Height
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Matrix ReadWorldFile(string path)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("World file \"" + path + "\"not found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var parameters = File.ReadLines(path).Take(6).Select((line, i) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
double p;
|
|
|
|
|
|
if (!double.TryParse(line, NumberStyles.Float, CultureInfo.InvariantCulture, out p))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Failed parsing line " + (i + 1) + " in world file \"" + path + "\".");
|
|
|
|
|
|
}
|
|
|
|
|
|
return p;
|
|
|
|
|
|
})
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
if (parameters.Count != 6)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Insufficient number of parameters in world file \"" + path + "\".");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new Matrix(
|
|
|
|
|
|
parameters[0], // line 1: A or M11
|
|
|
|
|
|
parameters[1], // line 2: D or M12
|
|
|
|
|
|
parameters[2], // line 3: B or M21
|
|
|
|
|
|
parameters[3], // line 4: E or M22
|
|
|
|
|
|
parameters[4], // line 5: C or OffsetX
|
|
|
|
|
|
parameters[5]); // line 6: F or OffsetY
|
2018-12-09 17:14:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task SetWorldImageAsync(Image image, string imagePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
var ext = Path.GetExtension(imagePath);
|
|
|
|
|
|
if (ext.Length < 4)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("Invalid image file path extension, must have at least three characters.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BitmapSource bitmap;
|
|
|
|
|
|
using (var stream = File.OpenRead(imagePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
#if WINDOWS_UWP
|
|
|
|
|
|
bitmap = await ImageLoader.LoadImageAsync(stream.AsRandomAccessStream());
|
|
|
|
|
|
#else
|
|
|
|
|
|
bitmap = await ImageLoader.LoadImageAsync(stream);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var dir = Path.GetDirectoryName(imagePath);
|
|
|
|
|
|
var file = Path.GetFileNameWithoutExtension(imagePath);
|
|
|
|
|
|
var worldFilePath = Path.Combine(dir, file + ext.Remove(2, 1) + "w");
|
|
|
|
|
|
var projFilePath = Path.Combine(dir, file + ".prj");
|
|
|
|
|
|
|
2018-12-13 18:09:34 +01:00
|
|
|
|
var transform = ReadWorldFile(worldFilePath);
|
2018-12-09 17:14:32 +01:00
|
|
|
|
MapProjection projection = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (File.Exists(projFilePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
projection = new GeoApiProjection { WKT = File.ReadAllText(projFilePath) };
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-13 18:09:34 +01:00
|
|
|
|
SetWorldImage(image, bitmap, transform, projection);
|
2018-12-09 17:14:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|