mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2026-04-05 06:26:41 +00:00
Version 4.4.1: MapPolygon for UWP in progress
This commit is contained in:
parent
66a97b3fa2
commit
54c924f9e7
15 changed files with 497 additions and 125 deletions
105
MapControl/Shared/Intersections.cs
Normal file
105
MapControl/Shared/Intersections.cs
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
||||
// © 2018 Clemens Fischer
|
||||
// Licensed under the Microsoft Public License (Ms-PL)
|
||||
|
||||
using System;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.Foundation;
|
||||
#else
|
||||
using System.Windows;
|
||||
#endif
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public static class Intersections
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the intersection point of two line segments given by (p1,p2) and (p3,p4),
|
||||
/// or null if no intersection exists. See https://stackoverflow.com/a/1968345.
|
||||
/// </summary>
|
||||
public static Point? GetIntersection(Point p1, Point p2, Point p3, Point p4)
|
||||
{
|
||||
var x12 = p2.X - p1.X;
|
||||
var y12 = p2.Y - p1.Y;
|
||||
var x34 = p4.X - p3.X;
|
||||
var y34 = p4.Y - p3.Y;
|
||||
var x13 = p3.X - p1.X;
|
||||
var y13 = p3.Y - p1.Y;
|
||||
|
||||
var d = x12 * y34 - x34 * y12;
|
||||
var s = (x13 * y12 - y13 * x12) / d;
|
||||
var t = (x13 * y34 - y13 * x34) / d;
|
||||
|
||||
if (s >= 0d && s <= 1d && t >= 0d && t <= 1d)
|
||||
{
|
||||
return new Point(p1.X + t * x12, p1.Y + t * y12);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the potential intersections of a line segment given by (p1,p2) with a rectangle.
|
||||
/// Updates either p1, p2, or both with any found intersection and returns a value that indicates
|
||||
/// whether the segment intersects or lies inside the rectangle.
|
||||
/// </summary>
|
||||
public static bool GetIntersections(ref Point p1, ref Point p2, Rect rect)
|
||||
{
|
||||
if (rect.Contains(p1) && rect.Contains(p2))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var topLeft = new Point(rect.Left, rect.Top);
|
||||
var topRight = new Point(rect.Right, rect.Top);
|
||||
var bottomLeft = new Point(rect.Left, rect.Bottom);
|
||||
var bottomRight = new Point(rect.Right, rect.Bottom);
|
||||
var numIntersections = 0;
|
||||
|
||||
if (GetIntersection(ref p1, ref p2, topLeft, bottomLeft, p => p.X <= rect.Left)) // left edge
|
||||
{
|
||||
numIntersections++;
|
||||
}
|
||||
|
||||
if (GetIntersection(ref p1, ref p2, topLeft, topRight, p => p.Y <= rect.Top)) // top edge
|
||||
{
|
||||
numIntersections++;
|
||||
}
|
||||
|
||||
if (numIntersections < 2 &&
|
||||
GetIntersection(ref p1, ref p2, topRight, bottomRight, p => p.X >= rect.Right)) // right edge
|
||||
{
|
||||
numIntersections++;
|
||||
}
|
||||
|
||||
if (numIntersections < 2 &&
|
||||
GetIntersection(ref p1, ref p2, bottomLeft, bottomRight, p => p.Y >= rect.Bottom)) // bottom edge
|
||||
{
|
||||
numIntersections++;
|
||||
}
|
||||
|
||||
return numIntersections > 0;
|
||||
}
|
||||
|
||||
private static bool GetIntersection(ref Point p1, ref Point p2, Point p3, Point p4, Func<Point, bool> condition)
|
||||
{
|
||||
var intersection = GetIntersection(p1, p2, p3, p4);
|
||||
|
||||
if (!intersection.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (condition(p1))
|
||||
{
|
||||
p1 = intersection.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
p2 = intersection.Value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
172
MapControl/Shared/LocationEx.cs
Normal file
172
MapControl/Shared/LocationEx.cs
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
// 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.Linq;
|
||||
|
||||
namespace MapControl
|
||||
{
|
||||
public static class LocationEx
|
||||
{
|
||||
/// <summary>
|
||||
/// see https://en.wikipedia.org/wiki/Great-circle_navigation
|
||||
/// </summary>
|
||||
public static double GreatCircleDistance(this Location location1, Location location2, double earthRadius = MapProjection.Wgs84EquatorialRadius)
|
||||
{
|
||||
var lat1 = location1.Latitude * Math.PI / 180d;
|
||||
var lon1 = location1.Longitude * Math.PI / 180d;
|
||||
var lat2 = location2.Latitude * Math.PI / 180d;
|
||||
var lon2 = location2.Longitude * Math.PI / 180d;
|
||||
var cosS12 = Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1);
|
||||
|
||||
return earthRadius * Math.Acos(Math.Min(Math.Max(cosS12, -1d), 1d));
|
||||
}
|
||||
|
||||
public static LocationCollection CalculateMeridianLocations(this Location location, double latitude2, double resolution = 1d)
|
||||
{
|
||||
if (resolution <= 0d)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("resolution");
|
||||
}
|
||||
|
||||
var locations = new LocationCollection();
|
||||
var s = latitude2 - location.Latitude;
|
||||
var n = (int)Math.Ceiling(Math.Abs(s) / resolution);
|
||||
|
||||
for (int i = 0; i <= n; i++)
|
||||
{
|
||||
locations.Add(new Location(location.Latitude + i * s / n, location.Longitude));
|
||||
}
|
||||
|
||||
return locations;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// see https://en.wikipedia.org/wiki/Great-circle_navigation
|
||||
/// </summary>
|
||||
public static LocationCollection CalculateGreatCircleLocations(this Location location1, Location location2, double resolution = 1d)
|
||||
{
|
||||
if (resolution <= 0d)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("resolution");
|
||||
}
|
||||
|
||||
if (location1.Longitude == location2.Longitude ||
|
||||
location1.Latitude <= -90d || location1.Latitude >= 90d ||
|
||||
location2.Latitude <= -90d || location2.Latitude >= 90d)
|
||||
{
|
||||
return CalculateMeridianLocations(location1, location2.Latitude);
|
||||
}
|
||||
|
||||
var locations = new LocationCollection(new Location(location1.Latitude, location1.Longitude));
|
||||
|
||||
var lat1 = location1.Latitude * Math.PI / 180d;
|
||||
var lon1 = location1.Longitude * Math.PI / 180d;
|
||||
var lat2 = location2.Latitude * Math.PI / 180d;
|
||||
var lon2 = location2.Longitude * Math.PI / 180d;
|
||||
var cosLat1 = Math.Cos(lat1);
|
||||
var sinLat1 = Math.Sin(lat1);
|
||||
var cosLat2 = Math.Cos(lat2);
|
||||
var sinLat2 = Math.Sin(lat2);
|
||||
var cosLon12 = Math.Cos(lon2 - lon1);
|
||||
var sinLon12 = Math.Sin(lon2 - lon1);
|
||||
|
||||
var cosS12 = sinLat1 * sinLat2 + cosLat1 * cosLat2 * cosLon12;
|
||||
var s12 = Math.Acos(Math.Min(Math.Max(cosS12, -1d), 1d));
|
||||
var n = (int)Math.Ceiling(s12 / resolution * 180d / Math.PI);
|
||||
|
||||
if (n > 1)
|
||||
{
|
||||
var az1 = Math.Atan2(sinLon12, cosLat1 * sinLat2 / cosLat2 - sinLat1 * cosLon12);
|
||||
var cosAz1 = Math.Cos(az1);
|
||||
var sinAz1 = Math.Sin(az1);
|
||||
|
||||
var az0 = Math.Atan2(sinAz1 * cosLat1, Math.Sqrt(cosAz1 * cosAz1 + sinAz1 * sinAz1 * sinLat1 * sinLat1));
|
||||
var sinAz0 = Math.Sin(az0);
|
||||
var cosAz0 = Math.Cos(az0);
|
||||
|
||||
var s01 = Math.Atan2(sinLat1, cosLat1 * cosAz1);
|
||||
var lon0 = lon1 - Math.Atan2(sinAz0 * Math.Sin(s01), Math.Cos(s01));
|
||||
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
double s = s01 + i * s12 / n;
|
||||
double sinS = Math.Sin(s);
|
||||
double cosS = Math.Cos(s);
|
||||
double lat = Math.Atan2(cosAz0 * sinS, Math.Sqrt(cosS * cosS + sinAz0 * sinAz0 * sinS * sinS));
|
||||
double lon = Math.Atan2(sinAz0 * sinS, cosS) + lon0;
|
||||
|
||||
locations.Add(lat * 180d / Math.PI, lon * 180d / Math.PI);
|
||||
}
|
||||
}
|
||||
|
||||
locations.Add(location2.Latitude, location2.Longitude);
|
||||
return locations;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// see https://en.wikipedia.org/wiki/Rhumb_line
|
||||
/// </summary>
|
||||
public static LocationCollection CalculateRhumbLineLocations(this Location location1, Location location2, double resolution = 1d)
|
||||
{
|
||||
if (resolution <= 0d)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("resolution");
|
||||
}
|
||||
|
||||
var y1 = WebMercatorProjection.LatitudeToY(location1.Latitude);
|
||||
|
||||
if (double.IsInfinity(y1))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("location1");
|
||||
}
|
||||
|
||||
var y2 = WebMercatorProjection.LatitudeToY(location2.Latitude);
|
||||
|
||||
if (double.IsInfinity(y2))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("location2");
|
||||
}
|
||||
|
||||
var x1 = location1.Longitude;
|
||||
var x2 = location2.Longitude;
|
||||
var dx = x2 - x1;
|
||||
var dy = y2 - y1;
|
||||
var s = Math.Sqrt(dx * dx + dy * dy);
|
||||
var n = (int)Math.Ceiling(s / resolution);
|
||||
|
||||
var locations = new LocationCollection(new Location(location1.Latitude, location1.Longitude));
|
||||
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
double x = x1 + i * dx / n;
|
||||
double y = y1 + i * dy / n;
|
||||
|
||||
locations.Add(WebMercatorProjection.YToLatitude(y), x);
|
||||
}
|
||||
|
||||
locations.Add(location2.Latitude, location2.Longitude);
|
||||
return locations;
|
||||
}
|
||||
|
||||
public static void Add(this LocationCollection locations, double latitude, double longitude)
|
||||
{
|
||||
if (locations.Count > 0)
|
||||
{
|
||||
var deltaLon = longitude - locations.Last().Longitude;
|
||||
|
||||
if (deltaLon < -180d)
|
||||
{
|
||||
longitude += 360d;
|
||||
}
|
||||
else if (deltaLon > 180)
|
||||
{
|
||||
longitude -= 360;
|
||||
}
|
||||
}
|
||||
|
||||
locations.Add(new Location(latitude, longitude));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -32,6 +32,14 @@ namespace MapControl
|
|||
set { SetValue(LocationProperty, value); }
|
||||
}
|
||||
|
||||
private void LocationPropertyChanged()
|
||||
{
|
||||
if (parentMap != null)
|
||||
{
|
||||
OnViewportChanged(parentMap, new ViewportChangedEventArgs());
|
||||
}
|
||||
}
|
||||
|
||||
private MapBase parentMap;
|
||||
|
||||
public MapBase ParentMap
|
||||
|
|
@ -99,13 +107,5 @@ namespace MapControl
|
|||
|
||||
return longitudeOffset;
|
||||
}
|
||||
|
||||
private void LocationPropertyChanged()
|
||||
{
|
||||
if (parentMap != null)
|
||||
{
|
||||
OnViewportChanged(parentMap, new ViewportChangedEventArgs());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue