Updated projection center update behavior

This commit is contained in:
ClemensFischer 2026-01-20 22:21:26 +01:00
parent 94e8edf18b
commit c245a8f722
6 changed files with 38 additions and 11 deletions

View file

@ -8,6 +8,7 @@ namespace MapControl
public abstract class AzimuthalProjection : MapProjection public abstract class AzimuthalProjection : MapProjection
{ {
protected AzimuthalProjection() protected AzimuthalProjection()
: base(true)
{ {
Type = MapProjectionType.Azimuthal; Type = MapProjectionType.Azimuthal;
} }

View file

@ -87,8 +87,9 @@ namespace MapControl
} }
/// <summary> /// <summary>
/// Gets or sets an optional center (reference point) for azimuthal projections. /// Gets or sets a projection center e.g. for azimuthal projections.
/// If ProjectionCenter is null, the Center property value will be used instead. /// If ProjectionCenter is null, the value of the Center property is used as projection center.
/// This behavior is overridden when the Center property of a MapProjection is set explicitly.
/// </summary> /// </summary>
public Location ProjectionCenter public Location ProjectionCenter
{ {
@ -456,7 +457,7 @@ namespace MapControl
var viewScale = ZoomLevelToScale(ZoomLevel); var viewScale = ZoomLevelToScale(ZoomLevel);
var projection = MapProjection; var projection = MapProjection;
projection.Center = ProjectionCenter ?? Center; projection.SetCenter(ProjectionCenter ?? Center);
var mapCenter = projection.LocationToMap(transformCenter ?? Center); var mapCenter = projection.LocationToMap(transformCenter ?? Center);
@ -496,7 +497,7 @@ namespace MapControl
ResetTransformCenter(); ResetTransformCenter();
projection.Center = ProjectionCenter ?? Center; projection.SetCenter(ProjectionCenter ?? Center);
mapCenter = projection.LocationToMap(center); mapCenter = projection.LocationToMap(center);

View file

@ -25,7 +25,7 @@ namespace MapControl
#else #else
[System.ComponentModel.TypeConverter(typeof(MapProjectionConverter))] [System.ComponentModel.TypeConverter(typeof(MapProjectionConverter))]
#endif #endif
public abstract class MapProjection public abstract class MapProjection(bool hasCenter = false)
{ {
public const double Wgs84EquatorialRadius = 6378137d; public const double Wgs84EquatorialRadius = 6378137d;
public const double Wgs84Flattening = 1d / 298.257223563; public const double Wgs84Flattening = 1d / 298.257223563;
@ -60,19 +60,41 @@ namespace MapControl
/// </summary> /// </summary>
public string CrsId { get; protected set; } = ""; public string CrsId { get; protected set; } = "";
private Location center;
private bool updateCenter = hasCenter;
/// <summary> /// <summary>
/// Gets or sets an optional projection center. /// Gets or sets an optional projection center. If the property is set to a non-null value,
/// it overrides the projection center set by MapBase.Center or MapBase.ProjectionCenter.
/// </summary> /// </summary>
public Location Center public Location Center
{ {
get => field ??= new Location(); get => center ??= new Location();
protected internal set set
{ {
updateCenter = true;
if (value != null)
{
SetCenter(value);
updateCenter = false;
}
}
}
/// <summary>
/// Called by MapBase.UpdateTransform().
/// </summary>
internal void SetCenter(Location value)
{
if (updateCenter)
{
var latitude = value.Latitude;
var longitude = Location.NormalizeLongitude(value.Longitude); var longitude = Location.NormalizeLongitude(value.Longitude);
if (field == null || !field.Equals(value.Latitude, longitude)) if (center == null || !center.Equals(latitude, longitude))
{ {
field = new Location(value.Latitude, longitude); center = new Location(latitude, longitude);
CenterChanged(); CenterChanged();
} }
} }

View file

@ -14,7 +14,8 @@ namespace MapControl.Projections
/// </summary> /// </summary>
public class ProjNetMapProjection : MapProjection public class ProjNetMapProjection : MapProjection
{ {
protected ProjNetMapProjection() protected ProjNetMapProjection(bool hasCenter = false)
: base(hasCenter)
{ {
} }

View file

@ -13,6 +13,7 @@ namespace MapControl.Projections
public class Wgs84OrthographicProjection : ProjNetMapProjection public class Wgs84OrthographicProjection : ProjNetMapProjection
{ {
public Wgs84OrthographicProjection() public Wgs84OrthographicProjection()
: base(true)
{ {
CenterChanged(); CenterChanged();
} }

View file

@ -12,6 +12,7 @@ namespace MapControl.Projections
public class Wgs84StereographicProjection : ProjNetMapProjection public class Wgs84StereographicProjection : ProjNetMapProjection
{ {
public Wgs84StereographicProjection() public Wgs84StereographicProjection()
: base(true)
{ {
CenterChanged(); CenterChanged();
} }