Version 4.12. Added MapControl.Images project, fixed assembly signing

This commit is contained in:
ClemensF 2018-12-09 17:14:32 +01:00
parent 3310f58912
commit ac26c57a81
22 changed files with 1015 additions and 40 deletions

View file

@ -0,0 +1,205 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
#if WINDOWS_UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
#else
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
#endif
namespace MapControl.Images
{
public partial class GroundOverlayPanel : MapPanel
{
class LatLonBox : BoundingBox
{
public LatLonBox(double south, double west, double north, double east, double rotation)
: base(south, west, north, east)
{
Rotation = rotation;
}
public double Rotation { get; }
}
class ImageOverlay
{
public ImageOverlay(LatLonBox latLonBox, string imagePath, int zIndex)
{
LatLonBox = latLonBox;
ImagePath = imagePath;
ZIndex = zIndex;
}
public LatLonBox LatLonBox { get; }
public string ImagePath { get; }
public int ZIndex { get; }
public ImageSource ImageSource { get; set; }
}
public static readonly DependencyProperty KmlFileProperty = DependencyProperty.Register(
nameof(KmlFile), typeof(string), typeof(GroundOverlayPanel),
new PropertyMetadata(null, async (o, e) => await ((GroundOverlayPanel)o).KmlFilePropertyChanged((string)e.NewValue)));
public string KmlFile
{
get { return (string)GetValue(KmlFileProperty); }
set { SetValue(KmlFileProperty, value); }
}
private async Task KmlFilePropertyChanged(string path)
{
IEnumerable<ImageOverlay> imageOverlays = null;
if (!string.IsNullOrEmpty(path))
{
try
{
var ext = Path.GetExtension(path).ToLower();
if (ext == ".kmz")
{
imageOverlays = await ReadGroundOverlaysFromArchive(path);
}
else if (ext == ".kml")
{
imageOverlays = await ReadGroundOverlaysFromFile(path);
}
}
catch (Exception ex)
{
Debug.WriteLine("GroundOverlayPanel: {0}: {1}", path, ex.Message);
}
}
Children.Clear();
if (imageOverlays != null)
{
AddImageOverlays(imageOverlays);
}
}
private void AddImageOverlays(IEnumerable<ImageOverlay> imageOverlays)
{
foreach (var imageOverlay in imageOverlays.Where(i => i.ImageSource != null))
{
FrameworkElement overlay = new Image
{
Source = imageOverlay.ImageSource,
Stretch = Stretch.Fill
};
if (imageOverlay.LatLonBox.Rotation != 0d)
{
overlay.RenderTransform = new RotateTransform { Angle = -imageOverlay.LatLonBox.Rotation };
overlay.RenderTransformOrigin = new Point(0.5, 0.5);
// additional Panel for map rotation, see MapPanel.ArrangeElementWithBoundingBox
var panel = new Grid { Background = null };
panel.Children.Add(overlay);
overlay = panel;
}
SetBoundingBox(overlay, imageOverlay.LatLonBox);
Canvas.SetZIndex(overlay, imageOverlay.ZIndex);
Children.Add(overlay);
}
}
private IEnumerable<ImageOverlay> ReadGroundOverlays(XmlDocument kmlDocument)
{
foreach (XmlElement groundOverlayElement in kmlDocument.GetElementsByTagName("GroundOverlay"))
{
LatLonBox latLonBox = null;
string imagePath = null;
int zIndex = 0;
foreach (var childElement in groundOverlayElement.ChildNodes.OfType<XmlElement>())
{
switch (childElement.LocalName)
{
case "LatLonBox":
latLonBox = ReadLatLonBox(childElement);
break;
case "Icon":
imagePath = ReadImagePath(childElement);
break;
case "drawOrder":
int.TryParse(childElement.InnerText.Trim(), out zIndex);
break;
}
}
if (latLonBox != null && imagePath != null)
{
yield return new ImageOverlay(latLonBox, imagePath, zIndex);
}
}
}
private string ReadImagePath(XmlElement element)
{
string href = null;
foreach (var childElement in element.ChildNodes.OfType<XmlElement>())
{
switch (childElement.LocalName)
{
case "href":
href = childElement.InnerText.Trim();
break;
}
}
return href;
}
private LatLonBox ReadLatLonBox(XmlElement element)
{
double north = double.NaN;
double south = double.NaN;
double east = double.NaN;
double west = double.NaN;
double rotation = 0d;
foreach (var childElement in element.ChildNodes.OfType<XmlElement>())
{
switch (childElement.LocalName)
{
case "north":
double.TryParse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out north);
break;
case "south":
double.TryParse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out south);
break;
case "east":
double.TryParse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out east);
break;
case "west":
double.TryParse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out west);
break;
case "rotation":
double.TryParse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture, out rotation);
break;
}
}
return !double.IsNaN(north) && !double.IsNaN(south) && !double.IsNaN(east) && !double.IsNaN(west)
? new LatLonBox(south, west, north, east, rotation)
: null;
}
}
}

View file

@ -0,0 +1,133 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.IO;
using System.Threading.Tasks;
using MapControl.Projections;
#if WINDOWS_UWP
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
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);
}
public static void SetWorldImage(this Image image, BitmapSource bitmapSource, WorldFileParameters parameters, MapProjection projection = null)
{
image.Source = bitmapSource;
image.Stretch = Stretch.Fill;
MapPanel.SetBoundingBox(image, parameters.GetBoundingBox(bitmapSource.PixelWidth, bitmapSource.PixelHeight, projection));
}
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");
var parameters = new WorldFileParameters(worldFilePath);
MapProjection projection = null;
if (File.Exists(projFilePath))
{
projection = new GeoApiProjection { WKT = File.ReadAllText(projFilePath) };
}
SetWorldImage(image, bitmap, parameters, projection);
}
public static FrameworkElement CreateWorldImage(BitmapSource bitmap, WorldFileParameters parameters)
{
if (parameters.XScale == 0d || parameters.YScale == 0d)
{
throw new ArgumentException("Invalid WorldFileParameters, XScale and YScale must be non-zero.");
}
var pixelWidth = parameters.XScale;
var pixelHeight = parameters.YScale;
var rotation = 0d;
if (parameters.YSkew != 0 || parameters.XSkew != 0)
{
pixelWidth = Math.Sqrt(parameters.XScale * parameters.XScale + parameters.YSkew * parameters.YSkew);
pixelHeight = Math.Sqrt(parameters.YScale * parameters.YScale + parameters.XSkew * parameters.XSkew);
var xAxisRotation = Math.Atan2(parameters.YSkew, parameters.XScale) / Math.PI * 180d;
var yAxisRotation = Math.Atan2(parameters.XSkew, -parameters.YScale) / Math.PI * 180d;
rotation = 0.5 * (xAxisRotation + yAxisRotation);
}
var x1 = parameters.XOrigin;
var x2 = parameters.XOrigin + pixelWidth * bitmap.PixelWidth;
var y1 = parameters.YOrigin;
var y2 = parameters.YOrigin + pixelHeight * bitmap.PixelHeight;
var bbox = new BoundingBox
{
West = Math.Min(x1, x2),
East = Math.Max(x1, x2),
South = Math.Min(y1, y2),
North = Math.Max(y1, y2)
};
FrameworkElement image = new Image
{
Source = bitmap,
Stretch = Stretch.Fill
};
if (rotation != 0d)
{
image.RenderTransform = new RotateTransform { Angle = rotation };
var panel = new Grid();
panel.Children.Add(image);
image = panel;
}
MapPanel.SetBoundingBox(image, bbox);
return image;
}
}
}

View file

@ -0,0 +1,94 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
using System.IO;
using System.Linq;
#if WINDOWS_UWP
using Windows.Foundation;
#else
using System.Windows;
#endif
namespace MapControl.Images
{
public class WorldFileParameters
{
public WorldFileParameters()
{
}
public WorldFileParameters(string path)
{
if (!File.Exists(path))
{
throw new ArgumentException("World file \"" + path + "\"not found.");
}
var lines = File.ReadLines(path).Take(6).ToList();
if (lines.Count != 6)
{
throw new ArgumentException("Invalid number of parameters in world file \"" + path + "\".");
}
double xscale, yskew, xskew, yscale, xorigin, yorigin;
if (!double.TryParse(lines[0], NumberStyles.Float, CultureInfo.InvariantCulture, out xscale) ||
!double.TryParse(lines[1], NumberStyles.Float, CultureInfo.InvariantCulture, out yskew) ||
!double.TryParse(lines[2], NumberStyles.Float, CultureInfo.InvariantCulture, out xskew) ||
!double.TryParse(lines[3], NumberStyles.Float, CultureInfo.InvariantCulture, out yscale) ||
!double.TryParse(lines[4], NumberStyles.Float, CultureInfo.InvariantCulture, out xorigin) ||
!double.TryParse(lines[5], NumberStyles.Float, CultureInfo.InvariantCulture, out yorigin))
{
throw new ArgumentException("Failed parsing parameters in world file \"" + path + "\".");
}
XScale = xscale;
YSkew = yskew;
XSkew = xskew;
YScale = yscale;
XOrigin = xorigin;
YOrigin = yorigin;
}
public double XScale { get; set; }
public double YSkew { get; set; }
public double XSkew { get; set; }
public double YScale { get; set; }
public double XOrigin { get; set; }
public double YOrigin { get; set; }
public BoundingBox GetBoundingBox(double imageWidth, double imageHeight, MapProjection projection = null)
{
if (XScale == 0d || YScale == 0d)
{
throw new ArgumentException("Invalid WorldFileParameters, XScale and YScale must be non-zero.");
}
if (YSkew != 0d || XSkew != 0d)
{
throw new ArgumentException("Invalid WorldFileParameters, YSkew and XSkew must be zero.");
}
var p1 = new Point(XOrigin, YOrigin);
var p2 = new Point(XOrigin + XScale * imageWidth, YOrigin + YScale * imageHeight);
var rect = new Rect(p1, p2);
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
};
}
}
}

View file

@ -0,0 +1,62 @@
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2018 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.Globalization;
#if WINDOWS_UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
#else
using System.Windows;
using System.Windows.Data;
#endif
namespace MapControl.Images
{
public class ZoomLevelToOpacityConverter : IValueConverter
{
public double MinZoomLevel { get; set; } = 0d;
public double MaxZoomLevel { get; set; } = 22d;
public double FadeZoomRange { get; set; } = 1d;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is double))
{
return DependencyProperty.UnsetValue;
}
var zoomLevel = (double)value;
var opacity = 0d;
if (zoomLevel > MinZoomLevel && zoomLevel < MaxZoomLevel)
{
opacity = 1d;
if (FadeZoomRange > 0d)
{
opacity = Math.Min(opacity, (zoomLevel - MinZoomLevel) / FadeZoomRange);
opacity = Math.Min(opacity, (MaxZoomLevel - zoomLevel) / FadeZoomRange);
}
}
return opacity;
}
public object Convert(object value, Type targetType, object parameter, string language)
{
return Convert(value, targetType, parameter, CultureInfo.InvariantCulture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotSupportedException();
}
}
}