Simplified MapProjection

This commit is contained in:
ClemensFischer 2024-09-08 14:03:55 +02:00
parent eb046d97ad
commit 03ac94a002
6 changed files with 76 additions and 82 deletions

View file

@ -19,14 +19,36 @@ namespace MapControl
Type = MapProjectionType.Azimuthal; Type = MapProjectionType.Azimuthal;
} }
public override BoundingBox MapToBoundingBox(Rect rect) public override Rect? BoundingBoxToMap(BoundingBox boundingBox)
{ {
var rectCenter = new Point(rect.X + rect.Width / 2d, rect.Y + rect.Height / 2d); Rect? mapRect = null;
var center = LocationToMap(boundingBox.Center);
if (center.HasValue)
{
var width = boundingBox.Width * Wgs84MeterPerDegree;
var height = boundingBox.Height * Wgs84MeterPerDegree;
var x = center.Value.X - width / 2d;
var y = center.Value.Y - height / 2d;
mapRect = new Rect(x, y, width, height);
}
return mapRect;
}
public override BoundingBox MapToBoundingBox(Rect mapRect)
{
BoundingBox boundingBox = null;
var rectCenter = new Point(mapRect.X + mapRect.Width / 2d, mapRect.Y + mapRect.Height / 2d);
var center = MapToLocation(rectCenter); var center = MapToLocation(rectCenter);
return center != null if (center != null)
? new CenteredBoundingBox(center, rect.Width / Wgs84MeterPerDegree, rect.Height / Wgs84MeterPerDegree) {
: null; boundingBox = new CenteredBoundingBox(center, mapRect.Width / Wgs84MeterPerDegree, mapRect.Height / Wgs84MeterPerDegree);
}
return boundingBox;
} }
/// <summary> /// <summary>

View file

@ -3,7 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.Globalization;
#if WPF #if WPF
using System.Windows; using System.Windows;
#endif #endif
@ -51,20 +50,5 @@ namespace MapControl
point.Y / Wgs84MeterPerDegree, point.Y / Wgs84MeterPerDegree,
point.X / Wgs84MeterPerDegree); point.X / Wgs84MeterPerDegree);
} }
public override string GetBboxValue(Rect rect)
{
if (CrsId == DefaultCrsId || CrsId == "CRS:84")
{
return string.Format(CultureInfo.InvariantCulture,
CrsId == DefaultCrsId ? "{1:F8},{0:F8},{3:F8},{2:F8}" : "{0:F8},{1:F8},{2:F8},{3:F8}",
rect.X / Wgs84MeterPerDegree,
rect.Y / Wgs84MeterPerDegree,
(rect.X + rect.Width) / Wgs84MeterPerDegree,
(rect.Y + rect.Height) / Wgs84MeterPerDegree);
}
return base.GetBboxValue(rect);
}
} }
} }

View file

@ -360,16 +360,16 @@ namespace MapControl
/// </summary> /// </summary>
public void ZoomToBounds(BoundingBox boundingBox) public void ZoomToBounds(BoundingBox boundingBox)
{ {
var rect = MapProjection.BoundingBoxToMap(boundingBox); var mapRect = MapProjection.BoundingBoxToMap(boundingBox);
if (rect.HasValue) if (mapRect.HasValue)
{ {
var rectCenter = new Point(rect.Value.X + rect.Value.Width / 2d, rect.Value.Y + rect.Value.Height / 2d); var rectCenter = new Point(mapRect.Value.X + mapRect.Value.Width / 2d, mapRect.Value.Y + mapRect.Value.Height / 2d);
var targetCenter = MapProjection.MapToLocation(rectCenter); var targetCenter = MapProjection.MapToLocation(rectCenter);
if (targetCenter != null) if (targetCenter != null)
{ {
var scale = Math.Min(ActualWidth / rect.Value.Width, ActualHeight / rect.Value.Height); var scale = Math.Min(ActualWidth / mapRect.Value.Width, ActualHeight / mapRect.Value.Height);
TargetZoomLevel = ScaleToZoomLevel(scale); TargetZoomLevel = ScaleToZoomLevel(scale);
TargetCenter = targetCenter; TargetCenter = targetCenter;

View file

@ -3,7 +3,6 @@
// Licensed under the Microsoft Public License (Ms-PL) // Licensed under the Microsoft Public License (Ms-PL)
using System; using System;
using System.Globalization;
#if WPF #if WPF
using System.Windows; using System.Windows;
#endif #endif
@ -67,70 +66,34 @@ namespace MapControl
/// </summary> /// </summary>
public virtual Rect? BoundingBoxToMap(BoundingBox boundingBox) public virtual Rect? BoundingBoxToMap(BoundingBox boundingBox)
{ {
Rect? rect = null; Rect? mapRect = null;
var southWest = LocationToMap(new Location(boundingBox.South, boundingBox.West));
var northEast = LocationToMap(new Location(boundingBox.North, boundingBox.East));
if (boundingBox.South < boundingBox.North && boundingBox.West < boundingBox.East) if (southWest.HasValue && northEast.HasValue)
{ {
var p1 = LocationToMap(new Location(boundingBox.South, boundingBox.West)); mapRect = new Rect(southWest.Value, northEast.Value);
var p2 = LocationToMap(new Location(boundingBox.North, boundingBox.East));
if (p1.HasValue && p2.HasValue)
{
rect = new Rect(p1.Value, p2.Value);
}
}
else if (boundingBox.Center != null)
{
// boundingBox is a CenteredBoundingBox
//
var center = LocationToMap(boundingBox.Center);
if (center.HasValue)
{
var width = boundingBox.Width * Wgs84MeterPerDegree;
var height = boundingBox.Height * Wgs84MeterPerDegree;
var x = center.Value.X - width / 2d;
var y = center.Value.Y - height / 2d;
rect = new Rect(x, y, width, height);
}
} }
return rect; return mapRect;
} }
/// <summary> /// <summary>
/// Transforms a Rect in projected map coordinates to a BoundingBox in geographic coordinates. /// Transforms a Rect in projected map coordinates to a BoundingBox in geographic coordinates.
/// Returns null when the MapRect can not be transformed. /// Returns null when the MapRect can not be transformed.
/// </summary> /// </summary>
public virtual BoundingBox MapToBoundingBox(Rect rect) public virtual BoundingBox MapToBoundingBox(Rect mapRect)
{ {
var southWest = MapToLocation(new Point(rect.X, rect.Y)); BoundingBox boundingBox = null;
var northEast = MapToLocation(new Point(rect.X + rect.Width, rect.Y + rect.Height)); var southWest = MapToLocation(new Point(mapRect.X, mapRect.Y));
var northEast = MapToLocation(new Point(mapRect.X + mapRect.Width, mapRect.Y + mapRect.Height));
return southWest != null && northEast != null if (southWest != null && northEast != null)
? new BoundingBox(southWest, northEast) {
: null; boundingBox = new BoundingBox(southWest, northEast);
} }
/// <summary> return boundingBox;
/// Gets the CRS parameter value for a WMS GetMap request.
/// </summary>
public virtual string GetCrsValue()
{
return CrsId.StartsWith("AUTO2:") || CrsId.StartsWith("AUTO:")
? string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", CrsId, Center.Longitude, Center.Latitude)
: CrsId;
}
/// <summary>
/// Gets the BBOX parameter value for a WMS GetMap request.
/// </summary>
public virtual string GetBboxValue(Rect rect)
{
return string.Format(CultureInfo.InvariantCulture,
"{0:F2},{1:F2},{2:F2},{3:F2}",
rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height);
} }
} }
} }

View file

@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml.Linq; using System.Xml.Linq;
@ -297,12 +298,36 @@ namespace MapControl
protected virtual string GetCrsValue() protected virtual string GetCrsValue()
{ {
return ParentMap.MapProjection.GetCrsValue(); var projection = ParentMap.MapProjection;
var crsId = projection.CrsId;
if (crsId.StartsWith("AUTO2:") || crsId.StartsWith("AUTO:"))
{
crsId = string.Format(CultureInfo.InvariantCulture, "{0},1,{1},{2}", crsId, projection.Center.Longitude, projection.Center.Latitude);
}
return crsId;
} }
protected virtual string GetBboxValue(Rect rect) protected virtual string GetBboxValue(Rect rect)
{ {
return ParentMap.MapProjection.GetBboxValue(rect); var crsId = ParentMap.MapProjection.CrsId;
var format = "{0:F2},{1:F2},{2:F2},{3:F2}";
var x1 = rect.X;
var y1 = rect.Y;
var x2 = rect.X + rect.Width;
var y2 = rect.Y + rect.Height;
if (crsId == "CRS:84" || crsId == "EPSG:4326")
{
format = crsId == "CRS:84" ? "{0:F8},{1:F8},{2:F8},{3:F8}" : "{1:F8},{0:F8},{3:F8},{2:F8}";
x1 /= MapProjection.Wgs84MeterPerDegree;
y1 /= MapProjection.Wgs84MeterPerDegree;
x2 /= MapProjection.Wgs84MeterPerDegree;
y2 /= MapProjection.Wgs84MeterPerDegree;
}
return string.Format(CultureInfo.InvariantCulture, format, x1, y1, x2, y2);
} }
protected string GetRequestUri(IDictionary<string, string> queryParameters) protected string GetRequestUri(IDictionary<string, string> queryParameters)

View file

@ -37,11 +37,11 @@ namespace MapControl
if (projection != null && items != null) if (projection != null && items != null)
{ {
var rect = projection.BoundingBoxToMap(boundingBox); var mapRect = projection.BoundingBoxToMap(boundingBox);
if (rect.HasValue) if (mapRect.HasValue)
{ {
image = await Task.Run(() => GetImage(projection, rect.Value, items)); image = await Task.Run(() => GetImage(projection, mapRect.Value, items));
} }
} }