XAML-Map-Control/MapControl/Shared/GroundOverlay.cs

284 lines
9.8 KiB
C#
Raw Normal View History

2022-01-18 21:30:22 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// © 2022 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.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
#if WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
#elif 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
{
2022-01-19 16:43:00 +01:00
public class GroundOverlay : MapPanel
2022-01-18 21:30:22 +01:00
{
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; }
}
2022-01-18 23:44:08 +01:00
public static readonly DependencyProperty SourcePathProperty = DependencyProperty.Register(
2022-01-19 16:43:00 +01:00
nameof(SourcePath), typeof(string), typeof(GroundOverlay),
new PropertyMetadata(null, async (o, e) => await ((GroundOverlay)o).SourcePathPropertyChanged((string)e.NewValue)));
2022-01-18 21:30:22 +01:00
2022-01-18 23:44:08 +01:00
public string SourcePath
2022-01-18 21:30:22 +01:00
{
2022-08-06 10:40:59 +02:00
get => (string)GetValue(SourcePathProperty);
set => SetValue(SourcePathProperty, value);
2022-01-18 21:30:22 +01:00
}
2022-01-18 23:44:08 +01:00
private async Task SourcePathPropertyChanged(string sourcePath)
2022-01-18 21:30:22 +01:00
{
IEnumerable<ImageOverlay> imageOverlays = null;
2022-01-18 23:44:08 +01:00
if (!string.IsNullOrEmpty(sourcePath))
2022-01-18 21:30:22 +01:00
{
try
{
2022-01-18 23:44:08 +01:00
var ext = Path.GetExtension(sourcePath).ToLower();
2022-01-18 21:30:22 +01:00
if (ext == ".kmz")
{
2022-01-18 23:44:08 +01:00
imageOverlays = await ReadGroundOverlaysFromArchiveAsync(sourcePath);
2022-01-18 21:30:22 +01:00
}
else if (ext == ".kml")
{
2022-01-18 23:44:08 +01:00
imageOverlays = await ReadGroundOverlaysFromFileAsync(sourcePath);
2022-01-18 21:30:22 +01:00
}
}
catch (Exception ex)
{
2022-01-18 23:44:08 +01:00
Debug.WriteLine($"GroundOverlayPanel: {sourcePath}: {ex.Message}");
2022-01-18 21:30:22 +01:00
}
}
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,
UseLayoutRounding = false
};
if (imageOverlay.LatLonBox.Rotation != 0d)
{
overlay.RenderTransform = new RotateTransform { Angle = -imageOverlay.LatLonBox.Rotation };
overlay.RenderTransformOrigin = new Point(0.5, 0.5);
2022-11-30 22:18:45 +01:00
// Additional Panel for map rotation, see MapPanel.ArrangeElementWithBoundingBox.
//
2022-01-18 21:30:22 +01:00
var panel = new Grid { UseLayoutRounding = false };
panel.Children.Add(overlay);
overlay = panel;
}
SetBoundingBox(overlay, imageOverlay.LatLonBox);
Canvas.SetZIndex(overlay, imageOverlay.ZIndex);
Children.Add(overlay);
}
}
2022-01-18 23:44:08 +01:00
private static async Task<IEnumerable<ImageOverlay>> ReadGroundOverlaysFromArchiveAsync(string archiveFilePath)
2022-01-18 21:30:22 +01:00
{
2022-01-18 23:44:08 +01:00
using (var archive = await Task.Run(() => ZipFile.OpenRead(archiveFilePath)))
2022-01-18 21:30:22 +01:00
{
2022-12-13 22:12:49 +01:00
var docEntry = await Task.Run(() => archive.GetEntry("doc.kml") ??
archive.Entries.FirstOrDefault(e => e.Name.EndsWith(".kml")));
2022-01-18 21:30:22 +01:00
if (docEntry == null)
{
2022-01-20 22:15:43 +01:00
throw new ArgumentException($"No KML entry found in {archiveFilePath}.");
2022-01-18 21:30:22 +01:00
}
var imageOverlays = await Task.Run(() =>
{
var kmlDocument = new XmlDocument();
using (var docStream = docEntry.Open())
{
kmlDocument.Load(docStream);
}
return ReadGroundOverlays(kmlDocument).ToList();
});
foreach (var imageOverlay in imageOverlays)
{
var imageEntry = await Task.Run(() => archive.GetEntry(imageOverlay.ImagePath));
if (imageEntry != null)
{
using (var zipStream = imageEntry.Open())
using (var memoryStream = new MemoryStream())
{
await zipStream.CopyToAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
imageOverlay.ImageSource = await ImageLoader.LoadImageAsync(memoryStream);
}
}
}
return imageOverlays;
}
}
2022-01-18 23:44:08 +01:00
private static async Task<IEnumerable<ImageOverlay>> ReadGroundOverlaysFromFileAsync(string docFilePath)
2022-01-18 21:30:22 +01:00
{
2022-12-06 18:07:38 +01:00
docFilePath = FilePath.GetFullPath(docFilePath);
2022-12-05 23:34:03 +01:00
2022-01-18 23:44:08 +01:00
var docUri = new Uri(docFilePath);
2022-01-18 21:30:22 +01:00
var imageOverlays = await Task.Run(() =>
{
var kmlDocument = new XmlDocument();
2022-01-18 23:44:08 +01:00
kmlDocument.Load(docFilePath);
2022-01-18 21:30:22 +01:00
return ReadGroundOverlays(kmlDocument).ToList();
});
foreach (var imageOverlay in imageOverlays)
{
imageOverlay.ImageSource = await ImageLoader.LoadImageAsync(new Uri(docUri, imageOverlay.ImagePath));
}
return imageOverlays;
}
private static 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":
2022-12-08 15:05:23 +01:00
zIndex = int.Parse(childElement.InnerText.Trim());
2022-01-18 21:30:22 +01:00
break;
}
}
if (latLonBox != null && imagePath != null)
{
yield return new ImageOverlay(latLonBox, imagePath, zIndex);
}
}
}
private static 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 static LatLonBox ReadLatLonBox(XmlElement element)
{
2022-12-01 22:48:08 +01:00
var north = double.NaN;
var south = double.NaN;
var east = double.NaN;
var west = double.NaN;
var rotation = 0d;
2022-01-18 21:30:22 +01:00
foreach (var childElement in element.ChildNodes.OfType<XmlElement>())
{
switch (childElement.LocalName)
{
case "north":
2022-12-08 15:05:23 +01:00
north = double.Parse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
2022-01-18 21:30:22 +01:00
break;
case "south":
2022-12-08 15:05:23 +01:00
south = double.Parse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
2022-01-18 21:30:22 +01:00
break;
case "east":
2022-12-08 15:05:23 +01:00
east = double.Parse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
2022-01-18 21:30:22 +01:00
break;
case "west":
2022-12-08 15:05:23 +01:00
west = double.Parse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
2022-01-18 21:30:22 +01:00
break;
case "rotation":
2022-12-08 15:05:23 +01:00
rotation = double.Parse(childElement.InnerText.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture);
2022-01-18 21:30:22 +01:00
break;
}
}
2022-12-08 15:05:23 +01:00
if (north <= south || east <= west)
{
throw new FormatException("Invalid LatLonBox");
}
return new LatLonBox(south, west, north, east, rotation);
2022-01-18 21:30:22 +01:00
}
}
}