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

246 lines
8.4 KiB
C#
Raw Normal View History

2022-01-18 21:30:22 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
2022-01-18 21:30:22 +01:00
// 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;
2024-06-22 08:10:45 +02:00
using System.Xml.Linq;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
2022-01-18 21:30:22 +01:00
#elif UWP
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
2022-01-18 21:30:22 +01:00
#endif
namespace MapControl
{
2022-01-19 16:43:00 +01:00
public class GroundOverlay : MapPanel
2022-01-18 21:30:22 +01:00
{
class ImageOverlay
{
2024-09-05 21:27:04 +02:00
public ImageOverlay(BoundingBox boundingBox, string imagePath, int zIndex)
2022-01-18 21:30:22 +01:00
{
2024-09-05 21:27:04 +02:00
BoundingBox = boundingBox;
2022-01-18 21:30:22 +01:00
ImagePath = imagePath;
ZIndex = zIndex;
}
2024-09-05 21:27:04 +02:00
public BoundingBox BoundingBox { get; }
2022-01-18 21:30:22 +01:00
public string ImagePath { get; }
public int ZIndex { get; }
public ImageSource ImageSource { get; set; }
}
2024-05-22 17:04:31 +02:00
public static readonly DependencyProperty SourcePathProperty =
2024-05-23 18:22:52 +02:00
DependencyPropertyHelper.Register<GroundOverlay, string>(nameof(SourcePath), null,
2024-05-22 17:04:31 +02:00
async (overlay, oldValue, newValue) => await overlay.SourcePathPropertyChanged(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
}
2024-05-22 17:04:31 +02: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)
{
2024-08-31 16:39:49 +02:00
Debug.WriteLine($"{nameof(GroundOverlay)}: {sourcePath}: {ex.Message}");
2022-01-18 21:30:22 +01:00
}
}
2024-05-22 17:04:31 +02:00
Children.Clear();
2022-01-18 21:30:22 +01:00
if (imageOverlays != null)
{
2024-05-22 17:04:31 +02:00
AddImageOverlays(imageOverlays);
2022-01-18 21:30:22 +01:00
}
}
private void AddImageOverlays(IEnumerable<ImageOverlay> imageOverlays)
{
foreach (var imageOverlay in imageOverlays.Where(i => i.ImageSource != null))
{
2024-09-05 21:27:04 +02:00
var image = new Image
2022-01-18 21:30:22 +01:00
{
Source = imageOverlay.ImageSource,
Stretch = Stretch.Fill
2022-01-18 21:30:22 +01:00
};
2024-09-05 21:27:04 +02:00
image.SetValue(Canvas.ZIndexProperty, imageOverlay.ZIndex);
SetBoundingBox(image, imageOverlay.BoundingBox);
Children.Add(image);
2022-01-18 21:30:22 +01:00
}
}
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
{
2023-01-11 17:51:00 +01:00
using (var archive = ZipFile.OpenRead(archiveFilePath))
2022-01-18 21:30:22 +01:00
{
2023-01-11 17:51:00 +01:00
var docEntry = 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(() =>
{
using (var docStream = docEntry.Open())
{
2024-06-22 08:10:45 +02:00
var kmlDocument = XDocument.Load(docStream);
2022-01-18 21:30:22 +01:00
2024-06-22 08:10:45 +02:00
return ReadGroundOverlays(kmlDocument.Root).ToList();
}
2022-01-18 21:30:22 +01:00
});
foreach (var imageOverlay in imageOverlays)
{
2023-01-11 17:51:00 +01:00
var imageEntry = archive.GetEntry(imageOverlay.ImagePath);
2022-01-18 21:30:22 +01:00
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(() =>
{
2024-06-22 08:10:45 +02:00
var kmlDocument = XDocument.Load(docFilePath);
2022-01-18 21:30:22 +01:00
2024-06-22 08:10:45 +02:00
return ReadGroundOverlays(kmlDocument.Root).ToList();
2022-01-18 21:30:22 +01:00
});
foreach (var imageOverlay in imageOverlays)
{
imageOverlay.ImageSource = await ImageLoader.LoadImageAsync(new Uri(docUri, imageOverlay.ImagePath));
}
return imageOverlays;
}
2024-06-22 08:10:45 +02:00
private static IEnumerable<ImageOverlay> ReadGroundOverlays(XElement kmlElement)
2022-01-18 21:30:22 +01:00
{
2024-06-22 08:10:45 +02:00
var ns = kmlElement.Name.Namespace;
var docElement = kmlElement.Element(ns + "Document") ?? kmlElement;
2022-01-18 21:30:22 +01:00
2024-06-22 08:10:45 +02:00
foreach (var folderElement in docElement.Elements(ns + "Folder"))
{
foreach (var groundOverlayElement in folderElement.Elements(ns + "GroundOverlay"))
2022-01-18 21:30:22 +01:00
{
2024-09-05 21:27:04 +02:00
var boundingBoxElement = groundOverlayElement.Element(ns + "LatLonBox");
var boundingBox = boundingBoxElement != null ? ReadBoundingBox(boundingBoxElement) : null;
2022-01-18 21:30:22 +01:00
2024-06-22 08:10:45 +02:00
var imagePathElement = groundOverlayElement.Element(ns + "Icon");
var imagePath = imagePathElement?.Element(ns + "href")?.Value;
2022-01-18 21:30:22 +01:00
2024-06-22 08:10:45 +02:00
var drawOrder = groundOverlayElement.Element(ns + "drawOrder")?.Value;
var zIndex = drawOrder != null ? int.Parse(drawOrder) : 0;
2022-01-18 21:30:22 +01:00
2024-09-05 21:27:04 +02:00
if (boundingBox != null && imagePath != null)
2024-06-22 08:10:45 +02:00
{
2024-09-05 21:27:04 +02:00
yield return new ImageOverlay(boundingBox, imagePath, zIndex);
2024-06-22 08:10:45 +02:00
}
2022-01-18 21:30:22 +01:00
}
}
}
2024-09-05 21:27:04 +02:00
private static BoundingBox ReadBoundingBox(XElement latLonBoxElement)
2022-01-18 21:30:22 +01:00
{
2024-06-22 08:10:45 +02:00
var ns = latLonBoxElement.Name.Namespace;
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
2024-06-22 08:10:45 +02:00
var value = latLonBoxElement.Element(ns + "north")?.Value;
if (value != null)
2022-01-18 21:30:22 +01:00
{
2024-06-22 08:10:45 +02:00
north = double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture);
}
value = latLonBoxElement.Element(ns + "south")?.Value;
if (value != null)
{
south = double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture);
}
value = latLonBoxElement.Element(ns + "east")?.Value;
if (value != null)
{
east = double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture);
}
value = latLonBoxElement.Element(ns + "west")?.Value;
if (value != null)
{
west = double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture);
}
value = latLonBoxElement.Element(ns + "rotation")?.Value;
if (value != null)
{
rotation = double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture);
2022-01-18 21:30:22 +01:00
}
2024-06-22 08:10:45 +02:00
if (double.IsNaN(north) || double.IsNaN(south) ||
double.IsNaN(east) || double.IsNaN(west) ||
north <= south || east <= west)
2022-12-08 15:05:23 +01:00
{
throw new FormatException("Invalid LatLonBox");
}
2024-09-05 21:27:04 +02:00
return new BoundingBox(south, west, north, east, rotation);
2022-01-18 21:30:22 +01:00
}
}
}