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

167 lines
5.3 KiB
C#
Raw Normal View History

// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2024-02-03 21:01:53 +01:00
// Copyright © 2024 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
2023-01-25 19:55:42 +01:00
using System.Collections.Generic;
using System.Linq;
2024-05-22 11:25:32 +02:00
#if WPF
using System.Windows;
using System.Windows.Media;
2021-11-17 23:17:11 +01:00
#elif UWP
using Windows.UI.Xaml;
2023-01-25 19:55:42 +01:00
using Windows.UI.Xaml.Media;
2024-05-22 11:25:32 +02:00
#elif WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#endif
namespace MapControl
{
/// <summary>
/// A path element with a Data property that holds a Geometry in view coordinates or
2022-11-05 17:32:29 +01:00
/// projected map coordinates that are relative to an origin Location.
/// </summary>
public partial class MapPath : IMapElement
{
public static readonly DependencyProperty LocationProperty = DependencyProperty.Register(
nameof(Location), typeof(Location), typeof(MapPath),
2020-05-13 18:17:28 +02:00
new PropertyMetadata(null, (o, e) => ((MapPath)o).UpdateData()));
private MapBase parentMap;
/// <summary>
/// Gets or sets a Location that is used as
2022-11-05 17:32:29 +01:00
/// - either the origin point of a geometry specified in projected map coordinates (meters)
2023-01-23 18:42:10 +01:00
/// - or as an optional anchor point to constrain the view position of MapPaths with multiple
/// Locations (like MapPolyline or MapPolygon) to the visible map viewport, as done
/// for elements where the MapPanel.Location property is set.
/// </summary>
public Location Location
{
2022-08-06 10:40:59 +02:00
get => (Location)GetValue(LocationProperty);
set => SetValue(LocationProperty, value);
}
/// <summary>
/// Implements IMapElement.ParentMap.
/// </summary>
public MapBase ParentMap
{
2022-08-06 10:40:59 +02:00
get => parentMap;
set
{
if (parentMap != null)
{
parentMap.ViewportChanged -= OnViewportChanged;
}
parentMap = value;
if (parentMap != null)
{
parentMap.ViewportChanged += OnViewportChanged;
}
2020-05-13 18:17:28 +02:00
UpdateData();
}
}
private void OnViewportChanged(object sender, ViewportChangedEventArgs e)
{
2020-05-13 18:17:28 +02:00
UpdateData();
}
2020-05-13 18:17:28 +02:00
protected virtual void UpdateData()
{
2022-11-02 23:51:15 +01:00
if (parentMap != null && Location != null && Data != null)
{
2023-01-23 18:42:10 +01:00
SetMapTransform(parentMap.GetMapTransform(Location));
2020-05-13 18:17:28 +02:00
}
2023-01-22 16:05:16 +01:00
MapPanel.SetLocation(this, Location);
}
#region Methods used only by derived classes MapPolyline, MapPolygon and MapMultiPolygon
2020-05-13 18:17:28 +02:00
protected Point? LocationToMap(Location location, double longitudeOffset)
{
if (longitudeOffset != 0d)
{
location = new Location(location.Latitude, location.Longitude + longitudeOffset);
}
var point = parentMap.MapProjection.LocationToMap(location);
2022-12-06 18:07:43 +01:00
if (point.HasValue)
{
2022-12-06 18:07:43 +01:00
if (point.Value.Y == double.PositiveInfinity)
{
point = new Point(point.Value.X, 1e9);
}
else if (point.Value.Y == double.NegativeInfinity)
{
point = new Point(point.Value.X, -1e9);
}
}
return point;
}
protected Point? LocationToView(Location location, double longitudeOffset)
{
var point = LocationToMap(location, longitudeOffset);
if (!point.HasValue)
{
return null;
}
return parentMap.ViewTransform.MapToView(point.Value);
}
2020-05-13 18:17:28 +02:00
2023-01-25 19:55:42 +01:00
protected double GetLongitudeOffset(Location location)
{
var longitudeOffset = 0d;
if (location != null && parentMap.MapProjection.Type <= MapProjectionType.NormalCylindrical)
{
var point = parentMap.LocationToView(location);
if (point.HasValue &&
(point.Value.X < 0d || point.Value.X > parentMap.RenderSize.Width ||
point.Value.Y < 0d || point.Value.Y > parentMap.RenderSize.Height))
{
longitudeOffset = parentMap.CoerceLongitude(location.Longitude) - location.Longitude;
2023-01-25 19:55:42 +01:00
}
}
return longitudeOffset;
}
2023-12-13 14:55:49 +01:00
protected void AddPolylinePoints(PathFigureCollection pathFigures, IEnumerable<Location> locations, bool closed)
2023-01-25 19:55:42 +01:00
{
if (parentMap != null && locations != null)
{
var longitudeOffset = GetLongitudeOffset(Location ?? locations.FirstOrDefault());
2023-12-13 14:55:49 +01:00
AddPolylinePoints(pathFigures, locations, longitudeOffset, closed);
2023-01-25 19:55:42 +01:00
}
}
2023-12-13 14:55:49 +01:00
protected void AddMultiPolygonPoints(PathFigureCollection pathFigures, IEnumerable<IEnumerable<Location>> polygons)
2023-01-25 19:55:42 +01:00
{
if (parentMap != null && polygons != null)
{
var longitudeOffset = GetLongitudeOffset(Location);
foreach (var polygon in polygons)
{
2023-12-13 14:55:49 +01:00
AddPolylinePoints(pathFigures, polygon, longitudeOffset, true);
2023-01-25 19:55:42 +01:00
}
}
}
2020-05-13 18:17:28 +02:00
#endregion
}
}